text
stringlengths 1
636
| code
stringlengths 8
1.89k
|
---|---|
Select the maximum sum from all the possible calls | maximum = max ( ch , max ( ca , max ( co , no ) ) ) NEW_LINE dp [ n ] [ X ] [ Y ] [ Z ] = maximum NEW_LINE return dp [ n ] [ X ] [ Y ] [ Z ] NEW_LINE |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE |
Given X , Y and Z | X = 1 NEW_LINE Y = 1 NEW_LINE Z = 1 NEW_LINE |
Given A [ ] | A = [ 10 , 0 , 5 ] NEW_LINE |
Given B [ ] | B = [ 5 , 10 , 0 ] NEW_LINE |
Given C [ ] | C = [ 0 , 5 , 10 ] NEW_LINE |
Given Size | n = len ( B ) NEW_LINE |
Function Call | print ( FindMaxS ( X , Y , Z , n - 1 , A , B , C ) ) NEW_LINE |
Python3 program for the above approach | MOD = 10 ** 9 + 7 NEW_LINE |
Precalculate the values of power of 2 | MEM = [ 1 , 2 , 4 , 8 , 16 , 32 , 64 , 128 , 256 , 512 , 1024 , 2048 , 4096 ] NEW_LINE |
Function to calculate 2 ^ N % mod | def mod_pow2 ( n ) : NEW_LINE INDENT while n >= len ( MEM ) : NEW_LINE INDENT MEM . append ( ( MEM [ - 1 ] * 2 ) % MOD ) NEW_LINE DEDENT return MEM [ n ] NEW_LINE DEDENT |
Function to find sum of inversions | def inversions ( bstr ) : NEW_LINE |
Initialise a list of 0 s and ? s | total , zeros , questions = ( 0 , ) * 3 NEW_LINE |
Traverse the string in the reversed manner | for x in reversed ( bstr ) : NEW_LINE |
If the current character is 1 | if x == '1' : NEW_LINE |
Effectively calculate a * b ^ ( b - 1 ) | z = zeros * mod_pow2 ( questions ) NEW_LINE if questions == 0 : NEW_LINE INDENT q = 0 NEW_LINE DEDENT else : NEW_LINE INDENT q = questions * mod_pow2 ( questions - 1 ) NEW_LINE DEDENT total = ( total + z + q ) % MOD NEW_LINE |
If the current character is 0 | elif x == '0' : NEW_LINE |
Increment count of zeroes | zeros += 1 NEW_LINE else : NEW_LINE |
Double count the zeroes | total *= 2 NEW_LINE |
Find b * 2 ^ ( b - 1 ) | z = zeros * mod_pow2 ( questions ) NEW_LINE if questions == 0 : NEW_LINE INDENT q = 0 NEW_LINE DEDENT else : NEW_LINE INDENT q = questions * mod_pow2 ( questions - 1 ) NEW_LINE DEDENT total = ( total + z + q ) % MOD NEW_LINE |
Increment count of questions | questions += 1 NEW_LINE |
Return the final count | return total NEW_LINE |
Driver Code | def main ( ) : NEW_LINE |
Given string S | S = " ? 0 ? " NEW_LINE |
Function Call | print ( inversions ( S ) ) NEW_LINE if __name__ == " _ _ main _ _ " : NEW_LINE main ( ) NEW_LINE |
Utility function to count array elements required to be removed to make array a mountain array | def minRemovalsUtil ( arr ) : NEW_LINE INDENT result = 0 NEW_LINE if ( len ( arr ) < 3 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT |
Stores length of increasing subsequence from [ 0 , i - 1 ] | leftIncreasing = [ 0 ] * len ( arr ) NEW_LINE |
Stores length of increasing subsequence from [ i + 1 , n - 1 ] | rightIncreasing = [ 0 ] * len ( arr ) NEW_LINE |
Iterate for each position up to N - 1 to find the length of subsequence | for i in range ( 1 , len ( arr ) ) : NEW_LINE INDENT for j in range ( i ) : NEW_LINE DEDENT |
If j is less than i , then i - th position has leftIncreasing [ j ] + 1 lesser elements including itself | if ( arr [ j ] < arr [ i ] ) : NEW_LINE |
Check if it is the maximum obtained so far | leftIncreasing [ i ] = max ( leftIncreasing [ i ] , leftIncreasing [ j ] + 1 ) ; NEW_LINE |
Search for increasing subsequence from right | for i in range ( len ( arr ) - 2 , - 1 , - 1 ) : NEW_LINE INDENT j = len ( arr ) - 1 NEW_LINE while j > i : NEW_LINE INDENT if ( arr [ j ] < arr [ i ] ) : NEW_LINE INDENT rightIncreasing [ i ] = max ( rightIncreasing [ i ] , rightIncreasing [ j ] + 1 ) NEW_LINE DEDENT j -= 1 NEW_LINE DEDENT DEDENT |
Find the position following the peak condition and have maximum leftIncreasing [ i ] + rightIncreasing [ i ] | for i in range ( len ( arr ) ) : NEW_LINE INDENT if ( leftIncreasing [ i ] != 0 and rightIncreasing [ i ] != 0 ) : NEW_LINE INDENT result = max ( result , leftIncreasing [ i ] + rightIncreasing [ i ] ) ; NEW_LINE DEDENT DEDENT return len ( arr ) - ( result + 1 ) NEW_LINE |
Function to count elements to be removed to make array a mountain array | def minRemovals ( arr ) : NEW_LINE INDENT ans = minRemovalsUtil ( arr ) NEW_LINE DEDENT |
Print the answer | print ( ans ) NEW_LINE |
Driver Code | if __name__ == " _ _ main _ _ " : NEW_LINE |
Given array | arr = [ 2 , 1 , 1 , 5 , 6 , 2 , 3 , 1 ] NEW_LINE |
Function Call | minRemovals ( arr ) NEW_LINE |
Bfs for odd numbers are source | def bfs ( n , a , invGr , ans , parity ) : NEW_LINE |
Initialize queue | q = [ ] NEW_LINE |
Stores for each node , the nodes visited and their distances | vis = [ 0 for i in range ( n + 1 ) ] NEW_LINE dist = [ 0 for i in range ( n + 1 ) ] NEW_LINE |
If parity is 0 -> odd Otherwise -> even | for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( ( a [ i ] + parity ) & 1 ) : NEW_LINE INDENT q . append ( i ) NEW_LINE vis [ i ] = 1 NEW_LINE DEDENT DEDENT |
Perform multi - source bfs | while ( len ( q ) != 0 ) : NEW_LINE |
Extract the front element of the queue | v = q [ 0 ] NEW_LINE q . pop ( 0 ) NEW_LINE |
Traverse nodes connected to the current node | for u in invGr [ v ] : NEW_LINE |
If u is not visited | if ( not vis [ u ] ) : NEW_LINE INDENT dist [ u ] = dist [ v ] + 1 NEW_LINE vis [ u ] = 1 NEW_LINE DEDENT |
If element with opposite parity is obtained | if ( ( a [ u ] + parity ) % 2 == 0 ) : NEW_LINE INDENT if ( ans [ u ] == - 1 ) : NEW_LINE DEDENT |
Store its distance from source in ans [ ] | ans [ u ] = dist [ u ] NEW_LINE |
Push the current neighbour to the queue | q . append ( u ) NEW_LINE |
Function to find the minimum jumps required by each index to reach element of opposite parity | def minJumps ( a , jump , n ) : NEW_LINE |
Initialise Inverse Graph | invGr = [ [ ] for i in range ( n + 1 ) ] NEW_LINE |
Stores the result for each index | ans = [ - 1 for i in range ( n + 1 ) ] NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE |
For the jumped index | for ind in [ i + jump [ i ] , i - jump [ i ] ] : NEW_LINE |
If the ind is valid then add reverse directed edge | if ( ind >= 1 and ind <= n ) : NEW_LINE INDENT invGr [ ind ] . append ( i ) NEW_LINE DEDENT |
Multi - source bfs with odd numbers as source by passing 0 | bfs ( n , a , invGr , ans , 0 ) NEW_LINE |
Multi - source bfs with even numbers as source by passing 1 | bfs ( n , a , invGr , ans , 1 ) NEW_LINE |
Print the answer | for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( str ( ans [ i ] ) , end = ' ▁ ' ) NEW_LINE DEDENT |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 0 , 4 , 2 , 5 , 2 , 1 ] NEW_LINE jump = [ 0 , 1 , 2 , 3 , 1 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE minJumps ( arr , jump , N - 1 ) NEW_LINE DEDENT |
Python3 program for the above approach | import math NEW_LINE |
Function to find GCD of pair with maximum GCD | def findMaxGCD ( arr , N ) : NEW_LINE |
Stores maximum element of arr [ ] | high = 0 NEW_LINE |
Find the maximum element | for i in range ( 0 , N ) : NEW_LINE INDENT high = max ( high , arr [ i ] ) NEW_LINE DEDENT |
Maintain a count array | count = [ 0 ] * ( high + 1 ) NEW_LINE |
Store the frequency of arr [ ] | for i in range ( 0 , N ) : NEW_LINE INDENT count [ arr [ i ] ] += 1 NEW_LINE DEDENT |
Stores the multiples of a number | counter = 0 NEW_LINE |
Iterate over the range [ MAX , 1 ] | for i in range ( high , 0 , - 1 ) : NEW_LINE INDENT j = i NEW_LINE DEDENT |
Iterate from current potential GCD till it is less than MAX | while ( j <= high ) : NEW_LINE |
A multiple found | if ( count [ j ] > 0 ) : NEW_LINE INDENT counter += count [ j ] NEW_LINE DEDENT |
Increment potential GCD by itself io check i , 2 i , 3 i ... | j += i NEW_LINE |
If 2 multiples found max GCD found | if ( counter == 2 ) : NEW_LINE INDENT return i NEW_LINE DEDENT counter = 0 NEW_LINE |
Function to find longest subsequence such that GCD of any two distinct integers is maximum | def maxlen ( i , j ) : NEW_LINE INDENT a = 0 NEW_LINE DEDENT |
Base Cases | if i >= N or j >= N : NEW_LINE INDENT return 0 NEW_LINE DEDENT |
Compare current GCD to the maximum GCD | if math . gcd ( arr [ i ] , arr1 [ j ] ) == maxgcd and arr [ i ] != arr1 [ j ] : NEW_LINE |
If true increment and move the pointer | a = max ( a , 1 + maxlen ( i , j + 1 ) ) NEW_LINE return a NEW_LINE |
Return max of either subsequences | return max ( maxlen ( i + 1 , j ) , maxlen ( i , j + 1 ) ) NEW_LINE |
Drivers Code | arr = [ 1 , 2 , 8 , 5 , 6 ] NEW_LINE |
Sorted array | N = len ( arr ) NEW_LINE arr1 = sorted ( arr ) NEW_LINE |
Function call to calculate GCD of pair with maximum GCD | maxgcd = findMaxGCD ( arr , N ) NEW_LINE |
Print the result | print ( maxlen ( 0 , 0 ) ) NEW_LINE |
Python3 program for the above approach | from math import sqrt , floor NEW_LINE |
Stores the results | dp = [ [ [ ] for j in range ( 105 ) ] for k in range ( 105 ) ] NEW_LINE |
Count of unique product paths | countPaths = 0 NEW_LINE |
Function to check whether number is perfect square or not | def isPerfectSquare ( n ) : NEW_LINE INDENT sr = sqrt ( n ) NEW_LINE DEDENT |
If square root is an integer | return ( ( sr - floor ( sr ) ) == 0 ) NEW_LINE |
Function to calculate and store all the paths product in vector | def countUniquePaths ( a , m , n , ans ) : NEW_LINE INDENT global dp NEW_LINE global countPaths NEW_LINE DEDENT |
Store the value a [ 0 ] [ 0 ] | dp [ 0 ] [ 0 ] . append ( a [ 0 ] [ 0 ] ) NEW_LINE |
Initialize first row of dp | for i in range ( 1 , m ) : NEW_LINE |
Find prefix product | a [ i ] [ 0 ] *= a [ i - 1 ] [ 0 ] NEW_LINE dp [ i ] [ 0 ] . append ( a [ i ] [ 0 ] ) NEW_LINE |
Initialize first column of dp | for i in range ( 1 , n ) : NEW_LINE |
Find the prefix product | a [ 0 ] [ i ] *= a [ 0 ] [ i - 1 ] NEW_LINE dp [ 0 ] [ i ] . append ( a [ 0 ] [ i ] ) NEW_LINE |
Iterate over range ( 1 , 1 ) to ( N , M ) | for i in range ( 1 , m ) : NEW_LINE INDENT for j in range ( 1 , n ) : NEW_LINE DEDENT |
Copy dp [ i - 1 ] [ j ] in top [ ] | top = dp [ i - 1 ] [ j ] NEW_LINE |
Copy dp [ i ] [ j - 1 ] into left [ ] | left = dp [ i ] [ j - 1 ] NEW_LINE |
Compute the values of current state and store it in curr [ ] | curr = [ ] NEW_LINE |
Find the product of a [ i ] [ j ] with elements at top [ ] | for k in range ( len ( top ) ) : NEW_LINE INDENT curr . append ( top [ k ] * a [ i ] [ j ] ) NEW_LINE DEDENT |
Find the product of a [ i ] [ j ] with elements at left [ ] | for k in range ( len ( left ) ) : NEW_LINE INDENT curr . append ( left [ k ] * a [ i ] [ j ] ) NEW_LINE DEDENT |
Update the current state | dp [ i ] [ j ] = curr NEW_LINE |
Traverse dp [ m - 1 ] [ n - 1 ] | for i in dp [ m - 1 ] [ n - 1 ] : NEW_LINE |
Check if perfect square | if ( isPerfectSquare ( i ) ) : NEW_LINE INDENT countPaths += 1 NEW_LINE DEDENT |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT M = 3 NEW_LINE N = 4 NEW_LINE DEDENT |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.