codeparrot/xlcost-text-to-code huggingface dataset:
Maximum width of a binary tree | A binary tree node ; Function to get the maximum width of a binary tree ; Create an array that will store count of nodes at each level ; Fill the count array using preorder traversal ; Return the maximum value from count array ; A function that fills count array with count of nodes at every level of given binary tree ; Compute the " height " of a tree -- the number of nodes along the longest path from the root node down to the farthest leaf node . ; compute the height of each subtree ; use the larger one ; Return the maximum value from count array ; Constructed bunary tree is : 1 / \ 2 3 / \ \ 4 5 8 / \ 6 7
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def getMaxWidth ( root ) : NEW_LINE INDENT h = height ( root ) NEW_LINE count = [ 0 ] * h NEW_LINE level = 0 NEW_LINE getMaxWidthRecur ( root , count , level ) NEW_LINE return getMax ( count , h ) NEW_LINE DEDENT def getMaxWidthRecur ( root , count , level ) : NEW_LINE INDENT if root is not None : NEW_LINE INDENT count [ level ] += 1 NEW_LINE getMaxWidthRecur ( root . left , count , level + 1 ) NEW_LINE getMaxWidthRecur ( root . right , count , level + 1 ) NEW_LINE DEDENT DEDENT def height ( node ) : NEW_LINE INDENT if node is None : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT lHeight = height ( node . left ) NEW_LINE rHeight = height ( node . right ) NEW_LINE return ( lHeight + 1 ) if ( lHeight > rHeight ) else ( rHeight + 1 ) NEW_LINE DEDENT DEDENT def getMax ( count , n ) : NEW_LINE INDENT max = count [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( count [ i ] > max ) : NEW_LINE INDENT max = count [ i ] NEW_LINE DEDENT DEDENT return max NEW_LINE DEDENT root = Node ( 1 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 3 ) NEW_LINE root . left . left = Node ( 4 ) NEW_LINE root . left . right = Node ( 5 ) NEW_LINE root . right . right = Node ( 8 ) NEW_LINE root . right . right . left = Node ( 6 ) NEW_LINE root . right . right . right = Node ( 7 ) NEW_LINE print " Maximum ▁ width ▁ is ▁ % d " % ( getMaxWidth ( root ) ) NEW_LINE
Minimize the sum of product of two arrays with permutations allowed | Returns minimum sum of product of two arrays with permutations allowed ; Sort A and B so that minimum and maximum value can easily be fetched . ; Multiplying minimum value of A and maximum value of B ; Driven Program
def minValue ( A , B , n ) : NEW_LINE INDENT A . sort ( ) NEW_LINE B . sort ( ) NEW_LINE result = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT result += ( A [ i ] * B [ n - i - 1 ] ) NEW_LINE DEDENT return result NEW_LINE DEDENT A = [ 3 , 1 , 1 ] NEW_LINE B = [ 6 , 5 , 4 ] NEW_LINE n = len ( A ) NEW_LINE print minValue ( A , B , n ) NEW_LINE
Count of numbers in range [ L , R ] which can be represented as sum of two perfect powers | Function to find the number of numbers that can be expressed in the form of the sum of two perfect powers ; Stores all possible powers ; Push 1 and 0 in it ; Iterate over all the exponents ; Iterate over all possible numbers ; This loop will run for a maximum of sqrt ( R ) times ; Push this power in the array pows [ ] ; Increase the number ; Stores if i can be expressed as the sum of perfect power or not ; int ok [ R + 1 ] ; memset ( ok , 0 , sizeof ( ok ) ) ; Iterate over all possible pairs of the array pows [ ] ; The number is valid ; Find the prefix sum of the array ok [ ] ; Return the count of required number ; Driver Code
def TotalPerfectPowerSum ( L , R ) : NEW_LINE INDENT pows = [ ] NEW_LINE pows . append ( 0 ) NEW_LINE pows . append ( 1 ) NEW_LINE for p in range ( 2 , 25 ) : NEW_LINE INDENT num = 2 NEW_LINE while ( ( int ) ( pow ( num , p ) + 0.5 ) <= R ) : NEW_LINE INDENT pows . append ( ( int ) ( pow ( num , p ) + 0.5 ) ) NEW_LINE num = num + 1 NEW_LINE DEDENT DEDENT ok = [ 0 for _ in range ( R + 1 ) ] NEW_LINE for i in range ( 0 , int ( len ( pows ) ) ) : NEW_LINE INDENT for j in range ( 0 , len ( pows ) ) : NEW_LINE INDENT if ( pows [ i ] + pows [ j ] <= R and pows [ i ] + pows [ j ] >= L ) : NEW_LINE INDENT ok [ pows [ i ] + pows [ j ] ] = 1 NEW_LINE DEDENT DEDENT DEDENT for i in range ( 0 , R + 1 ) : NEW_LINE INDENT ok [ i ] += ok [ i - 1 ] NEW_LINE DEDENT return ok [ R ] - ok [ L - 1 ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT L = 5 NEW_LINE R = 8 NEW_LINE print ( TotalPerfectPowerSum ( L , R ) ) NEW_LINE DEDENT
Minimize absolute difference between sum of subtrees formed after splitting Binary tree into two | Python3 implementation for the above approach ; Structure of Node ; Function to calculate minimum absolute difference of subtrees after splitting the tree into two parts ; Reference variable to store the answer ; Function to store sum of values of current node , left subtree and right subtree in place of current node 's value ; Function to perform every possible split and calculate absolute difference of subtrees ; Absolute difference in subtrees if left edge is broken ; Absolute difference in subtrees if right edge is broken ; Update minDiff if a difference lesser than it is found ; Construct the tree ; Print the output
import sys NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , val ) : NEW_LINE INDENT self . val = val NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def minAbsDiff ( root ) : NEW_LINE INDENT minDiff = [ 0 ] * ( 1 ) NEW_LINE minDiff [ 0 ] = sys . maxsize NEW_LINE postOrder ( root ) NEW_LINE preOrder ( root , minDiff ) NEW_LINE return minDiff [ 0 ] NEW_LINE DEDENT def postOrder ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT root . val += postOrder ( root . left ) + postOrder ( root . right ) NEW_LINE return root . val NEW_LINE DEDENT def preOrder ( root , minDiff ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT leftDiff = abs ( root . left . val - ( root . val - root . left . val ) ) NEW_LINE rightDiff = abs ( root . right . val - ( root . val - root . right . val ) ) NEW_LINE minDiff [ 0 ] = min ( minDiff [ 0 ] , min ( leftDiff , rightDiff ) ) NEW_LINE return NEW_LINE DEDENT root = Node ( 1 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 3 ) NEW_LINE root . left . left = Node ( 4 ) NEW_LINE root . left . right = Node ( 5 ) NEW_LINE root . right . left = Node ( 6 ) NEW_LINE root . right . right = Node ( 7 ) NEW_LINE print ( minAbsDiff ( root ) ) NEW_LINE
Vertical width of Binary tree | Set 1 | class to create a new tree node ; get vertical width ; traverse left ; if curr is decrease then get value in minimum ; if curr is increase then get value in maximum ; traverse right ; 1 is added to include root in the width ; Driver Code
class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT def lengthUtil ( root , maximum , minimum , curr = 0 ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT lengthUtil ( root . left , maximum , minimum , curr - 1 ) NEW_LINE if ( minimum [ 0 ] > curr ) : NEW_LINE INDENT minimum [ 0 ] = curr NEW_LINE DEDENT if ( maximum [ 0 ] < curr ) : NEW_LINE INDENT maximum [ 0 ] = curr NEW_LINE DEDENT lengthUtil ( root . right , maximum , minimum , curr + 1 ) NEW_LINE DEDENT def getLength ( root ) : NEW_LINE INDENT maximum = [ 0 ] NEW_LINE minimum = [ 0 ] NEW_LINE lengthUtil ( root , maximum , minimum , 0 ) NEW_LINE return ( abs ( minimum [ 0 ] ) + maximum [ 0 ] ) + 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 7 ) NEW_LINE root . left = newNode ( 6 ) NEW_LINE root . right = newNode ( 5 ) NEW_LINE root . left . left = newNode ( 4 ) NEW_LINE root . left . right = newNode ( 3 ) NEW_LINE root . right . left = newNode ( 2 ) NEW_LINE root . right . right = newNode ( 1 ) NEW_LINE print ( getLength ( root ) ) NEW_LINE DEDENT
Maximize Array sum after changing sign of any elements for exactly M times | Function to find the maximum sum with M flips ; Declare a priority queue i . e . min heap ; Declare the sum as zero ; Push all elements of the array in it ; Iterate for M times ; Get the top element ; Flip the sign of the top element ; Remove the top element ; Update the sum ; Push the temp into the queue ; Driver program ; Given input
def findMaximumSumWithMflips ( arr , N , M ) : NEW_LINE INDENT pq = [ ] NEW_LINE sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT pq . append ( arr [ i ] ) NEW_LINE sum += arr [ i ] NEW_LINE pq . sort ( ) NEW_LINE DEDENT while ( M > 0 ) : NEW_LINE INDENT sum -= pq [ 0 ] NEW_LINE temp = - 1 * pq [ 0 ] NEW_LINE pq = pq [ 1 : ] NEW_LINE sum += temp NEW_LINE pq . append ( temp ) NEW_LINE pq . sort ( ) NEW_LINE M -= 1 NEW_LINE DEDENT print ( sum ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ - 3 , 7 , - 1 , - 5 , - 3 ] NEW_LINE N = len ( arr ) NEW_LINE M = 4 NEW_LINE findMaximumSumWithMflips ( arr , N , M ) NEW_LINE DEDENT
Minimum operations to make Array equal by repeatedly adding K from an element and subtracting K from other | Function to find the minimum number of operations to make all the elements of the array equal ; Store the sum of the array arr [ ] ; Traverse through the array ; If it is not possible to make all array element equal ; Store the minimum number of operations needed ; Traverse through the array ; Finally , print the minimum number operation to make array elements equal ; Driver Code ; Given Input ; Function Call
def miniOperToMakeAllEleEqual ( arr , n , k ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT if ( sum % n ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT valueAfterDivision = sum // n NEW_LINE count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( abs ( valueAfterDivision - arr [ i ] ) % k != 0 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT count += abs ( valueAfterDivision - arr [ i ] ) // k NEW_LINE DEDENT print ( count // 2 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE k = 3 NEW_LINE arr = [ 5 , 8 , 11 ] NEW_LINE miniOperToMakeAllEleEqual ( arr , n , k ) NEW_LINE DEDENT
Minimum operations to make product of adjacent element pair of prefix sum negative | Function to find minimum operations needed to make the product of any two adjacent elements in prefix sum array negative ; Stores the minimum operations ; Stores the prefix sum and number of operations ; Traverse the array ; Update the value of sum ; Check if i + r is odd ; Check if prefix sum is not positive ; Update the value of ans and sum ; Check if prefix sum is not negative ; Update the value of ans and sum ; Update the value of res ; Print the value of res ; Driver code
def minOperations ( a ) : NEW_LINE INDENT res = 100000000000 NEW_LINE N = len ( a ) NEW_LINE for r in range ( 0 , 2 ) : NEW_LINE INDENT sum = 0 NEW_LINE ans = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT sum += a [ i ] NEW_LINE if ( ( i + r ) % 2 ) : NEW_LINE INDENT if ( sum <= 0 ) : NEW_LINE INDENT ans += - sum + 1 NEW_LINE sum = 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( sum >= 0 ) : NEW_LINE INDENT ans += sum + 1 ; NEW_LINE sum = - 1 ; NEW_LINE DEDENT DEDENT DEDENT res = min ( res , ans ) NEW_LINE DEDENT print ( res ) NEW_LINE DEDENT a = [ 1 , - 3 , 1 , 0 ] NEW_LINE minOperations ( a ) ; NEW_LINE
Minimize sum of product of same | Function to print the arrays ; Function to reverse the subarray ; Function to calculate the minimum product of same - indexed elements of two given arrays ; Calculate initial product ; Traverse all odd length subarrays ; Remove the previous product ; Add the current product ; Check if current product is minimum or not ; Traverse all even length subarrays ; Remove the previous product ; Add to the current product ; Check if current product is minimum or not ; Update the pointers ; Reverse the subarray ; Print the subarray ; Driver Code
def Print ( a , b ) : NEW_LINE INDENT minProd = 0 NEW_LINE for i in range ( len ( a ) ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) ; NEW_LINE for i in range ( len ( b ) ) : NEW_LINE INDENT print ( b [ i ] , end = " ▁ " ) NEW_LINE minProd += a [ i ] * b [ i ] NEW_LINE DEDENT print ( ) NEW_LINE print ( minProd ) NEW_LINE DEDENT def reverse ( left , right , arr ) : NEW_LINE INDENT while ( left < right ) : NEW_LINE INDENT temp = arr [ left ] NEW_LINE arr [ left ] = arr [ right ] NEW_LINE arr [ right ] = temp NEW_LINE left += 1 NEW_LINE right -= 1 NEW_LINE DEDENT DEDENT def minimumProdArray ( a , b , l ) : NEW_LINE INDENT total = 0 NEW_LINE for i in range ( len ( a ) ) : NEW_LINE INDENT total += a [ i ] * b [ i ] NEW_LINE DEDENT Min = 2147483647 NEW_LINE first = 0 ; NEW_LINE second = 0 ; NEW_LINE for i in range ( len ( a ) ) : NEW_LINE INDENT left = i - 1 ; NEW_LINE right = i + 1 ; NEW_LINE total1 = total ; NEW_LINE while ( left >= 0 and right < len ( a ) ) : NEW_LINE INDENT total1 -= a [ left ] * b [ left ] + a [ right ] * b [ right ] ; NEW_LINE total1 += a [ left ] * b [ right ] + a [ right ] * b [ left ] ; NEW_LINE if ( Min >= total1 ) : NEW_LINE INDENT Min = total1 NEW_LINE first = left NEW_LINE second = right NEW_LINE DEDENT left -= 1 NEW_LINE right += 1 NEW_LINE DEDENT DEDENT for i in range ( len ( a ) ) : NEW_LINE INDENT left = i NEW_LINE right = i + 1 NEW_LINE total1 = total NEW_LINE while ( left >= 0 and right < len ( a ) ) : NEW_LINE INDENT total1 -= a [ left ] * b [ left ] + a [ right ] * b [ right ] NEW_LINE total1 += a [ left ] * b [ right ] + a [ right ] * b [ left ] NEW_LINE if ( Min >= total1 ) : NEW_LINE INDENT Min = total1 NEW_LINE first = left NEW_LINE second = right NEW_LINE DEDENT left -= 1 NEW_LINE right += 1 NEW_LINE DEDENT DEDENT if ( Min < total ) : NEW_LINE INDENT reverse ( first , second , a ) NEW_LINE Print ( a , b ) NEW_LINE DEDENT else : NEW_LINE INDENT Print ( a , b ) NEW_LINE DEDENT DEDENT n = 4 ; NEW_LINE a = [ 2 , 3 , 1 , 5 ] NEW_LINE b = [ 8 , 2 , 4 , 3 ] NEW_LINE minimumProdArray ( a , b , n ) NEW_LINE
Maximize cost of removing all occurrences of substrings " ab " and " ba " | Function to find the maximum cost of removing substrings " ab " and " ba " from S ; MaxStr is the substring char array with larger cost ; MinStr is the substring char array with smaller cost ; ; Denotes larger point ; Denotes smaller point ; Stores cost scored ; Stack to keep track of characters ; Traverse the string ; If the substring is maxstr ; Pop from the stack ; Add maxp to cost ; Push the character to the stack ; Remaining string after removing maxstr ; Find remaining string ; Reversing the string retrieved from the stack ; Removing all occurences of minstr ; If the substring is minstr ; Pop from the stack ; Add minp to the cost ; Otherwise ; Return the maximum cost ; Driver Code ; Input String ; Costs
def MaxCollection ( S , P , Q ) : NEW_LINE INDENT maxstr = [ i for i in ( " ab " if P >= Q else " ba " ) ] NEW_LINE minstr = [ i for i in ( " ba " if P >= Q else " ab " ) ] NEW_LINE maxp = max ( P , Q ) NEW_LINE minp = min ( P , Q ) NEW_LINE cost = 0 NEW_LINE stack1 = [ ] NEW_LINE s = [ i for i in S ] NEW_LINE for ch in s : NEW_LINE INDENT if ( len ( stack1 ) > 0 and ( stack1 [ - 1 ] == maxstr [ 0 ] and ch == maxstr [ 1 ] ) ) : NEW_LINE INDENT del stack1 [ - 1 ] NEW_LINE cost += maxp NEW_LINE DEDENT else : NEW_LINE INDENT stack1 . append ( ch ) NEW_LINE DEDENT DEDENT sb = " " NEW_LINE while ( len ( stack1 ) > 0 ) : NEW_LINE INDENT sb += stack1 [ - 1 ] NEW_LINE del stack1 [ - 1 ] NEW_LINE DEDENT sb = sb [ : : - 1 ] NEW_LINE remstr = sb NEW_LINE for ch in remstr : NEW_LINE INDENT if ( len ( stack1 ) > 0 and ( stack1 [ - 1 ] == minstr [ 0 ] and ch == minstr [ 1 ] ) ) : NEW_LINE INDENT del stack1 [ - 1 ] NEW_LINE cost += minp NEW_LINE DEDENT else : NEW_LINE INDENT stack1 . append ( ch ) NEW_LINE DEDENT DEDENT return cost NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " cbbaabbaab " NEW_LINE P = 6 ; NEW_LINE Q = 4 ; NEW_LINE print ( MaxCollection ( S , P , Q ) ) ; NEW_LINE DEDENT
Vertical width of Binary tree | Set 2 | A binary tree node has data , pointer to left child and a pointer to right child ; Function to fill hd in set . ; Third parameter is horizontal distance ; Driver Code ; Creating the above tree
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT def fillSet ( root , s , hd ) : NEW_LINE INDENT if ( not root ) : NEW_LINE INDENT return NEW_LINE DEDENT fillSet ( root . left , s , hd - 1 ) NEW_LINE s . add ( hd ) NEW_LINE fillSet ( root . right , s , hd + 1 ) NEW_LINE DEDENT def verticalWidth ( root ) : NEW_LINE INDENT s = set ( ) NEW_LINE fillSet ( root , s , 0 ) NEW_LINE return len ( s ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = Node ( 1 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 3 ) NEW_LINE root . left . left = Node ( 4 ) NEW_LINE root . left . right = Node ( 5 ) NEW_LINE root . right . left = Node ( 6 ) NEW_LINE root . right . right = Node ( 7 ) NEW_LINE root . right . left . right = Node ( 8 ) NEW_LINE root . right . right . right = Node ( 9 ) NEW_LINE print ( verticalWidth ( root ) ) NEW_LINE DEDENT
Maximum value of ( arr [ i ] * arr [ j ] ) + ( arr [ j ] | Function to find the value of the expression a * b + ( b - a ) ; Function to find the maximum value of the expression a * b + ( b - a ) possible for any pair ( a , b ) ; Sort the vector in ascending order ; Stores the maximum value ; Update ans by choosing the pair of the minimum and 2 nd minimum ; Update ans by choosing the pair of maximum and 2 nd maximum ; Return the value of ans ; Driver Code ; Given inputs
def calc ( a , b ) : NEW_LINE INDENT return a * b + ( b - a ) NEW_LINE DEDENT def findMaximum ( arr , N ) : NEW_LINE INDENT arr = sorted ( arr ) NEW_LINE ans = - 10 ** 9 NEW_LINE ans = max ( ans , calc ( arr [ 0 ] , arr [ 1 ] ) ) NEW_LINE ans = max ( ans , calc ( arr [ N - 2 ] , arr [ N - 1 ] ) ) NEW_LINE return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 0 , - 47 , 12 ] NEW_LINE N = len ( arr ) NEW_LINE print ( findMaximum ( arr , N ) ) NEW_LINE arr = [ 0 , - 47 , 12 ] NEW_LINE N = len ( arr ) NEW_LINE print ( findMaximum ( arr , N ) ) NEW_LINE DEDENT
Minimize flips on K |  ; Store previous flip events ; Remove an item which is out range of window . ; In a window , if A [ i ] is a even number with even times fliped , it need to be fliped again . On other hand , if A [ i ] is a odd number with odd times fliped , it need to be fliped again . ; Insert ; Driver code
def minKBitFlips ( A , K , N ) : NEW_LINE INDENT flip = [ ] NEW_LINE count = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( len ( flip ) > 0 and ( i - flip [ 0 ] >= K ) ) : NEW_LINE INDENT flip . pop ( 0 ) NEW_LINE DEDENT if ( A [ i ] % 2 == len ( flip ) % 2 ) : NEW_LINE INDENT if ( i + K - 1 >= N ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT flip . append ( i ) NEW_LINE count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT A = [ 0 , 1 , 0 ] NEW_LINE N = len ( A ) NEW_LINE K = 1 NEW_LINE ans = minKBitFlips ( A , K , 3 ) NEW_LINE print ( ans ) NEW_LINE
Minimize flips on K |  ; We 're flipping the subarray from  A[i] to A[i+K-1] ; ' ' ▁ If ▁ we ▁ can ' t flip the entire subarray , its impossible ; Driver Code
def minKBitFlips ( A , K ) : NEW_LINE INDENT temp = [ 0 ] * len ( A ) NEW_LINE count = 0 NEW_LINE flip = 0 NEW_LINE for i in range ( len ( A ) ) : NEW_LINE INDENT flip ^= temp [ i ] NEW_LINE if ( A [ i ] == flip ) : NEW_LINE INDENT count += 1 NEW_LINE if ( i + K > len ( A ) ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT flip ^= 1 NEW_LINE if ( i + K < len ( A ) ) : NEW_LINE INDENT temp [ i + K ] ^= 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT A = [ 0 , 1 , 0 ] NEW_LINE K = 1 NEW_LINE ans = minKBitFlips ( A , K ) NEW_LINE print ( ans ) NEW_LINE
Generate an N | Function to calculate GCD of given array ; Function to calculate GCD of two integers ; Utility function to check for all the combinations ; If a sequence of size N is obtained ; If gcd of current combination is K ; If current element from first array is divisible by K ; Recursively proceed further ; If current combination satisfies given condition ; Remove the element from the combination ; If current element from second array is divisible by K ; Recursively proceed further ; If current combination satisfies given condition ; Remove the element from the combination ; Function to check for all possible combinations ; Stores the subsequence ; If GCD of any sequence is not equal to K ; Given arrays ; Given value of K ; Function call to generate a subsequence whose GCD is K
def GCDArr ( a ) : NEW_LINE INDENT ans = a [ 0 ] NEW_LINE for i in a : NEW_LINE INDENT ans = GCD ( ans , i ) NEW_LINE DEDENT return ans NEW_LINE DEDENT def GCD ( a , b ) : NEW_LINE INDENT if not b : NEW_LINE INDENT return a NEW_LINE DEDENT return GCD ( b , a % b ) NEW_LINE DEDENT def findSubseqUtil ( a , b , ans , k , i ) : NEW_LINE INDENT if len ( ans ) == len ( a ) : NEW_LINE INDENT if GCDArr ( ans ) == k : NEW_LINE INDENT print ( ans ) NEW_LINE return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if not a [ i ] % K : NEW_LINE INDENT ans . append ( a [ i ] ) NEW_LINE temp = findSubseqUtil ( a , b , ans , k , i + 1 ) NEW_LINE if temp == True : NEW_LINE INDENT return True NEW_LINE DEDENT ans . pop ( ) NEW_LINE DEDENT if not b [ i ] % k : NEW_LINE INDENT ans . append ( b [ i ] ) NEW_LINE temp = findSubseqUtil ( a , b , ans , k , i + 1 ) NEW_LINE if temp == True : NEW_LINE INDENT return True NEW_LINE DEDENT ans . pop ( ) NEW_LINE DEDENT return False NEW_LINE DEDENT def findSubseq ( A , B , K , i ) : NEW_LINE INDENT ans = [ ] NEW_LINE findSubseqUtil ( A , B , ans , K , i ) NEW_LINE if not ans : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT A = [ 5 , 3 , 6 , 2 , 9 ] NEW_LINE B = [ 21 , 7 , 14 , 12 , 28 ] NEW_LINE K = 3 NEW_LINE ans = findSubseq ( A , B , K , 0 ) NEW_LINE
Kth element in permutation of first N natural numbers having all even numbers placed before odd numbers in increasing order | Function to find the K - th element in the required permutation ; Stores the required permutation ; Insert all the even numbers less than or equal to N ; Now , insert all odd numbers less than or equal to N ; Print the Kth element ; Driver Code ; functions call
def findKthElement ( N , K ) : NEW_LINE INDENT v = [ ] NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT v . append ( i ) NEW_LINE DEDENT DEDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( i % 2 != 0 ) : NEW_LINE INDENT v . append ( i ) NEW_LINE DEDENT DEDENT print ( v [ K - 1 ] ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 10 NEW_LINE K = 3 NEW_LINE findKthElement ( N , K ) NEW_LINE DEDENT