text
stringlengths 1
636
| code
stringlengths 8
1.89k
|
---|---|
Sum of array elements | for i in range ( 0 , n ) : NEW_LINE INDENT sum1 += a [ i ] ; NEW_LINE DEDENT |
Return the answer | return sum1 - n ; NEW_LINE |
Driver Code | a = [ 3 , 4 , 6 ] ; NEW_LINE n = len ( a ) ; NEW_LINE print ( maxModulosum ( a , n ) ) ; NEW_LINE |
Function to get the minimum jump value | def getMinJumps ( s ) : NEW_LINE |
Store all indices of ones | ones = [ ] NEW_LINE jumps , median , ind = 0 , 0 , 0 NEW_LINE |
Populating one 's indices | for i in range ( len ( s ) ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT ones . append ( i ) NEW_LINE DEDENT DEDENT if ( len ( ones ) == 0 ) : NEW_LINE INDENT return jumps NEW_LINE DEDENT |
Calculate median | median = ones [ len ( ones ) // 2 ] NEW_LINE ind = median NEW_LINE |
Jumps required for 1 's to the left of median | for i in range ( ind , - 1 , - 1 ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT jumps += ind - i NEW_LINE ind -= 1 NEW_LINE DEDENT DEDENT ind = median NEW_LINE |
Jumps required for 1 's to the right of median | for i in range ( ind , len ( s ) ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT jumps += i - ind NEW_LINE ind += 1 NEW_LINE DEDENT DEDENT |
Return the final answer | return jumps NEW_LINE |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = "00100000010011" NEW_LINE print ( getMinJumps ( s ) ) NEW_LINE DEDENT |
Maximum number of nodes | N = 10 ** 5 + 5 NEW_LINE |
Tree represented as adjacency list | v = [ [ ] for i in range ( N ) ] NEW_LINE |
For storing value associates with node | val = [ 0 ] * ( N ) NEW_LINE |
For storing GCD of every subarray | answer = [ 0 ] * ( N ) NEW_LINE |
Number of nodes | n = 0 NEW_LINE |
Function to find GCD of two numbers . Using Euclidean algo | def gcd ( a , b ) : NEW_LINE |
If b == 0 then simply return a | if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT return gcd ( b , a % b ) NEW_LINE |
DFS function to traverse the tree | def DFS ( node , parent ) : NEW_LINE |
Initializing answer with GCD of this node . | answer [ node ] = val [ node ] NEW_LINE |
Iterate over each child of current node | for child in v [ node ] : NEW_LINE |
Skipping the parent | if ( child == parent ) : NEW_LINE INDENT continue NEW_LINE DEDENT |
Call DFS for each child | DFS ( child , node ) NEW_LINE |
Taking GCD of the answer of the child to find node 's GCD | answer [ node ] = gcd ( answer [ node ] , answer [ child ] ) NEW_LINE |
Calling DFS from the root ( 1 ) for precomputing answers | def preprocess ( ) : NEW_LINE INDENT DFS ( 1 , - 1 ) NEW_LINE DEDENT |
Function to find and prGCD for Q queries | def findGCD ( queries , q ) : NEW_LINE |
Doing preprocessing | preprocess ( ) NEW_LINE |
Iterate over each given query | for i in range ( q ) : NEW_LINE INDENT GCD = answer [ queries [ i ] ] NEW_LINE print ( " For β subtree β of β " , queries [ i ] , " , β GCD β = β " , GCD ) NEW_LINE DEDENT |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE |
Tree : 1 ( 2 ) / \ 2 ( 3 ) 3 ( 4 ) / \ 4 ( 8 ) 5 ( 16 ) | n = 5 NEW_LINE |
Making a undirected tree | v [ 1 ] . append ( 2 ) NEW_LINE v [ 2 ] . append ( 1 ) NEW_LINE v [ 1 ] . append ( 3 ) NEW_LINE v [ 3 ] . append ( 1 ) NEW_LINE v [ 3 ] . append ( 4 ) NEW_LINE v [ 4 ] . append ( 3 ) NEW_LINE v [ 3 ] . append ( 5 ) NEW_LINE v [ 5 ] . append ( 3 ) NEW_LINE |
Values associated with nodes | val [ 1 ] = 2 NEW_LINE val [ 2 ] = 3 NEW_LINE val [ 3 ] = 4 NEW_LINE val [ 4 ] = 8 NEW_LINE val [ 5 ] = 16 NEW_LINE queries = [ 2 , 3 , 1 ] NEW_LINE q = len ( queries ) NEW_LINE |
Function call | findGCD ( queries , q ) NEW_LINE |
Function to find out the minimum cost to make two number X and Y equal to zero | def makeZero ( x , y , a , b ) : NEW_LINE |
If x is greater than y then swap | if ( x > y ) : NEW_LINE INDENT x , y = y , x NEW_LINE DEDENT |
Cost of making y equal to x | tot_cost = ( y - x ) * a NEW_LINE |
Cost if we choose 1 st operation | cost1 = 2 * x * a NEW_LINE |
Cost if we choose 2 nd operation | cost2 = x * b NEW_LINE |
Total cost | tot_cost += min ( cost1 , cost2 ) NEW_LINE print ( tot_cost ) NEW_LINE |
Function to split the fraction into the N parts | def splitFraction ( n , d ) : NEW_LINE INDENT ar = [ ] NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT ar . append ( 0 ) NEW_LINE DEDENT first = d + n - 1 NEW_LINE ar [ 0 ] = first NEW_LINE DEDENT |
Loop to find the N - 1 fraction | for i in range ( 1 , n ) : NEW_LINE INDENT temp = first - 1 NEW_LINE ar [ i ] = first * temp NEW_LINE first -= 1 NEW_LINE DEDENT |
Loop to print the Fractions | for i in range ( 0 , n ) : NEW_LINE INDENT if ar [ i ] % n == 0 : NEW_LINE INDENT print ( "1 / " , int ( ar [ i ] / n ) , " , " , end = " β " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( n , " / " , ar [ i ] , " , " , end = " β " ) NEW_LINE DEDENT DEDENT |
Driver Code | N = 4 NEW_LINE D = 2 NEW_LINE |
Function Call | splitFraction ( N , D ) NEW_LINE |
Function to return the count of Pairs | def numPairs ( arr , n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if arr [ j ] % arr [ i ] == 0 : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT |
Driver code | arr = [ 1 , 1 , 2 , 2 , 3 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE print ( numPairs ( arr , n ) ) NEW_LINE |
Function to check that array can be converted into a strictly decreasing sequence | def check ( arr , n ) : NEW_LINE INDENT flag = True ; NEW_LINE DEDENT |
Loop to check that each element is greater than the ( N - index ) | for i in range ( n ) : NEW_LINE |
If element is less than ( N - index ) | if ( arr [ i ] < n - i ) : NEW_LINE INDENT flag = False ; NEW_LINE DEDENT |
If array can be converted | if ( flag ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT else : NEW_LINE INDENT return False ; NEW_LINE DEDENT |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr1 = [ 11 , 11 , 11 , 11 ] ; NEW_LINE n1 = len ( arr1 ) ; NEW_LINE DEDENT |
Function calling | if ( check ( arr1 , n1 ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT |
Function to find a maximum number X , such that a given string can be partitioned into X substrings that are each balanced | def BalancedPartition ( str1 , n ) : NEW_LINE |
If the size of the string is 0 , then answer is zero | if ( n == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT |
Variable that represents the number of ' R ' s and ' L ' s | r = 0 NEW_LINE l = 0 NEW_LINE |
To store maximum number of possible partitions | ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE |
Increment the variable r if the character in the string is 'R | ' NEW_LINE INDENT if ( str1 [ i ] == ' R ' ) : NEW_LINE INDENT r += 1 NEW_LINE DEDENT DEDENT |
Increment the variable l if the character in the string is 'L | ' NEW_LINE INDENT elif ( str1 [ i ] == ' L ' ) : NEW_LINE INDENT l += 1 NEW_LINE DEDENT DEDENT |
If r and l are equal , then increment ans | if ( r == l ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT |
Return the required answer | return ans NEW_LINE |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " LLRRRLLRRL " NEW_LINE n = len ( str1 ) NEW_LINE DEDENT |
Function call | print ( BalancedPartition ( str1 , n ) ) NEW_LINE |
Function to minimize the non - zero elements in the given array | def minOccupiedPosition ( A , n ) : NEW_LINE |
To store the min pos needed | minPos = 0 NEW_LINE |
Loop to iterate over the elements of the given array | i = 0 NEW_LINE while i < n : NEW_LINE |
If current position A [ i ] is occupied the we can place A [ i ] , A [ i + 1 ] and A [ i + 2 ] elements together at A [ i + 1 ] if exists . | if ( A [ i ] > 0 ) : NEW_LINE INDENT minPos += 1 NEW_LINE i += 2 NEW_LINE DEDENT i += 1 NEW_LINE return minPos NEW_LINE |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 8 , 0 , 7 , 0 , 0 , 6 ] NEW_LINE n = len ( A ) NEW_LINE DEDENT |
Function Call | print ( minOccupiedPosition ( A , n ) ) NEW_LINE |
Python3 implementation of the approach | import math NEW_LINE |
Function to return the minimum value of k that satisfies the given condition | def findMinimumK ( a , n , S ) : NEW_LINE |
store sum of array elements | sum = 0 NEW_LINE |
Calculate the sum after | for i in range ( 0 , n ) : NEW_LINE INDENT sum += a [ i ] NEW_LINE DEDENT |
return minimum possible K | return math . ceil ( ( ( S + 1 ) * 1.0 ) / ( sum * 1.0 ) ) NEW_LINE |
Driver code | a = [ 10 , 7 , 8 , 10 , 12 , 19 ] NEW_LINE n = len ( a ) NEW_LINE s = 200 NEW_LINE print ( findMinimumK ( a , n , s ) ) NEW_LINE |
Function to return the largest number less than N | def largestNum ( n ) : NEW_LINE INDENT num = 0 ; NEW_LINE DEDENT |
Iterate through all possible values | for i in range ( 32 ) : NEW_LINE |
Multiply the number by 2 i times | x = ( 1 << i ) ; NEW_LINE if ( ( x - 1 ) <= n ) : NEW_LINE INDENT num = ( 1 << i ) - 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT break ; NEW_LINE DEDENT |
Return the final result | return num ; NEW_LINE |
Driver code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 345 ; NEW_LINE print ( largestNum ( N ) ) ; NEW_LINE DEDENT |
Function to find the required output string | def findString ( N , K ) : NEW_LINE |
Each element at index i is modulus of K | for i in range ( N ) : NEW_LINE INDENT print ( chr ( ord ( ' A ' ) + i % K ) , end = " " ) ; NEW_LINE DEDENT |
Driver code | if __name__ == " _ _ main _ _ " : NEW_LINE |
initialise integers N and K | N = 10 ; NEW_LINE K = 3 ; NEW_LINE findString ( N , K ) ; NEW_LINE |
Function to count no of collision | def count ( s ) : NEW_LINE INDENT cnt , ans = 0 , 0 NEW_LINE DEDENT |
Length of the string | N = len ( s ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( s [ i ] == ' R ' ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT if ( s [ i ] == ' L ' ) : NEW_LINE INDENT ans += cnt NEW_LINE DEDENT DEDENT return ans NEW_LINE |
Driver code | s = " RRLL " NEW_LINE print ( count ( s ) ) NEW_LINE |
Function to find the maximum number that can be displayed using the N segments | def segments ( n ) : NEW_LINE |
Condition to check base case | if ( n == 1 or n == 0 ) : NEW_LINE INDENT return ; NEW_LINE DEDENT |
Condition to check if the number is even | if ( n % 2 == 0 ) : NEW_LINE INDENT print ( "1" , end = " " ) ; NEW_LINE segments ( n - 2 ) ; NEW_LINE DEDENT |
Condition to check if the number is odd | elif ( n % 2 == 1 ) : NEW_LINE INDENT print ( "7" , end = " " ) ; NEW_LINE segments ( n - 3 ) ; NEW_LINE DEDENT |
Driver Code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 11 ; NEW_LINE segments ( n ) ; NEW_LINE DEDENT |
Function to find the minimum number of subsequences required to convert one to another S2 == A and S1 == B | def findMinimumSubsequences ( A , B ) : NEW_LINE |
At least 1 subsequence is required Even in best case , when A is same as B | numberOfSubsequences = 1 NEW_LINE |
size of B | sizeOfB = len ( B ) NEW_LINE |
size of A | sizeOfA = len ( A ) NEW_LINE inf = 1000000 NEW_LINE |
Create an 2D array next [ ] [ ] of size 26 * sizeOfB to store the next occurrence of a character ( ' a ' to ' z ' ) as an index [ 0 , sizeOfA - 1 ] | next = [ [ inf for i in range ( sizeOfB ) ] for i in range ( 26 ) ] NEW_LINE |
Loop to Store the values of index | for i in range ( sizeOfB ) : NEW_LINE INDENT next [ ord ( B [ i ] ) - ord ( ' a ' ) ] [ i ] = i NEW_LINE DEDENT |
If the value of next [ i ] [ j ] is infinite then update it with next [ i ] [ j + 1 ] | for i in range ( 26 ) : NEW_LINE INDENT for j in range ( sizeOfB - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( next [ i ] [ j ] == inf ) : NEW_LINE INDENT next [ i ] [ j ] = next [ i ] [ j + 1 ] NEW_LINE DEDENT DEDENT DEDENT |
Greedy algorithm to obtain the maximum possible subsequence of B to cover the remaining of A using next subsequence | pos = 0 NEW_LINE i = 0 NEW_LINE |
Loop to iterate over the A | while ( i < sizeOfA ) : NEW_LINE |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.