codeparrot/xlcost-text-to-code huggingface dataset:
Find the maximum sum leaf to root path in a Binary Tree | A tree node structure ; A utility function that prints all nodes on the path from root to target_leaf ; base case ; return True if this node is the target_leaf or target leaf is present in one of its descendants ; This function Sets the target_leaf_ref to refer the leaf node of the maximum path sum . Also , returns the max_sum using max_sum_ref ; Update current sum to hold sum of nodes on path from root to this node ; If this is a leaf node and path to this node has maximum sum so far , then make this node target_leaf ; If this is not a leaf node , then recur down to find the target_leaf ; Returns the maximum sum and prints the nodes on max sum path ; base case ; find the target leaf and maximum sum ; print the path from root to the target leaf ; return maximum sum ; Utility function to create a new Binary Tree node ; Driver function to test above functions
class node : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . data = 0 NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def printPath ( root , target_leaf ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( root == target_leaf or printPath ( root . left , target_leaf ) or printPath ( root . right , target_leaf ) ) : NEW_LINE INDENT print ( root . data , end = " ▁ " ) NEW_LINE return True NEW_LINE DEDENT return False NEW_LINE DEDENT max_sum_ref = 0 NEW_LINE target_leaf_ref = None NEW_LINE def getTargetLeaf ( Node , curr_sum ) : NEW_LINE INDENT global max_sum_ref NEW_LINE global target_leaf_ref NEW_LINE if ( Node == None ) : NEW_LINE INDENT return NEW_LINE DEDENT curr_sum = curr_sum + Node . data NEW_LINE if ( Node . left == None and Node . right == None ) : NEW_LINE INDENT if ( curr_sum > max_sum_ref ) : NEW_LINE INDENT max_sum_ref = curr_sum NEW_LINE target_leaf_ref = Node NEW_LINE DEDENT DEDENT getTargetLeaf ( Node . left , curr_sum ) NEW_LINE getTargetLeaf ( Node . right , curr_sum ) NEW_LINE DEDENT def maxSumPath ( Node ) : NEW_LINE INDENT global max_sum_ref NEW_LINE global target_leaf_ref NEW_LINE if ( Node == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT target_leaf_ref = None NEW_LINE max_sum_ref = - 32676 NEW_LINE getTargetLeaf ( Node , 0 ) NEW_LINE printPath ( Node , target_leaf_ref ) ; NEW_LINE return max_sum_ref ; NEW_LINE DEDENT def newNode ( data ) : NEW_LINE INDENT temp = node ( ) ; NEW_LINE temp . data = data ; NEW_LINE temp . left = None ; NEW_LINE temp . right = None ; NEW_LINE return temp ; NEW_LINE DEDENT root = None ; NEW_LINE root = newNode ( 10 ) ; NEW_LINE root . left = newNode ( - 2 ) ; NEW_LINE root . right = newNode ( 7 ) ; NEW_LINE root . left . left = newNode ( 8 ) ; NEW_LINE root . left . right = newNode ( - 4 ) ; NEW_LINE print ( " Following ▁ are ▁ the ▁ nodes ▁ on ▁ the ▁ maximum ▁ sum ▁ path ▁ " ) ; NEW_LINE sum = maxSumPath ( root ) ; NEW_LINE print (   " Sum of the nodes is   " , sum ) ; NEW_LINE
Find Union and Intersection of two unsorted arrays | Function to find intersection ; when both are equal ; Driver Code ; sort ; function call
def intersection ( a , b , n , m ) : NEW_LINE INDENT i = 0 NEW_LINE j = 0 NEW_LINE while ( i < n and j < m ) : NEW_LINE INDENT if ( a [ i ] > b [ j ] ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( b [ j ] > a [ i ] ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE i += 1 NEW_LINE j += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 3 , 2 , 3 , 4 , 5 , 5 , 6 ] NEW_LINE b = [ 3 , 3 , 5 ] NEW_LINE n = len ( a ) NEW_LINE m = len ( b ) NEW_LINE a . sort ( ) NEW_LINE b . sort ( ) NEW_LINE intersection ( a , b , n , m ) NEW_LINE DEDENT
Count of 1 's in any path in a Binary Tree | A binary tree node ; A utility function to allocate a new node ; This function updates overall count of 1 in ' res ' And returns count 1 s going through root . ; Base Case ; l and r store count of 1 s going through left and right child of root respectively ; maxCount represents the count of 1 s when the Node under consideration is the root of the maxCount path and no ancestors of the root are there in maxCount path ; if the value at node is 1 then its count will be considered including the leftCount and the rightCount ; Store the Maximum Result . ; if the value at node is 1 then its count will be considered including the maximum of leftCount or the rightCount ; Returns maximum count of 1 in any path in tree with given root ; Initialize result ; Compute and return result ; Driver program
class Node : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . data = 0 NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def newNode ( data ) : NEW_LINE INDENT newNode = Node ( ) NEW_LINE newNode . data = data NEW_LINE newNode . left = newNode . right = None NEW_LINE return ( newNode ) NEW_LINE DEDENT res = 0 NEW_LINE def countUntil ( root ) : NEW_LINE INDENT global res NEW_LINE if ( root == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT l = countUntil ( root . left ) NEW_LINE r = countUntil ( root . right ) NEW_LINE maxCount = 0 NEW_LINE if ( root . data == 1 ) : NEW_LINE INDENT maxCount = l + r + 1 NEW_LINE DEDENT else : NEW_LINE INDENT maxCount = l + r NEW_LINE DEDENT res = max ( res , maxCount ) NEW_LINE if ( root . data == 1 ) : NEW_LINE INDENT return max ( l , r ) + 1 NEW_LINE DEDENT else : NEW_LINE INDENT return max ( l , r ) NEW_LINE DEDENT DEDENT def findMaxCount ( root ) : NEW_LINE INDENT global res NEW_LINE res = - 999999 NEW_LINE countUntil ( root ) NEW_LINE return res NEW_LINE DEDENT root = newNode ( 1 ) NEW_LINE root . left = newNode ( 0 ) NEW_LINE root . right = newNode ( 1 ) NEW_LINE root . left . left = newNode ( 1 ) NEW_LINE root . left . right = newNode ( 1 ) NEW_LINE root . left . right . left = newNode ( 1 ) NEW_LINE root . left . right . right = newNode ( 0 ) NEW_LINE print ( findMaxCount ( root ) ) NEW_LINE
Find Union and Intersection of two unsorted arrays | Prints union of arr1 [ 0. . n1 - 1 ] and arr2 [ 0. . n2 - 1 ] ; Prints intersection of arr1 [ 0. . n1 - 1 ] and arr2 [ 0. . n2 - 1 ] ; Driver Program ; Function call
def printUnion ( arr1 , arr2 , n1 , n2 ) : NEW_LINE INDENT hs = set ( ) NEW_LINE for i in range ( 0 , n1 ) : NEW_LINE INDENT hs . add ( arr1 [ i ] ) NEW_LINE DEDENT for i in range ( 0 , n2 ) : NEW_LINE INDENT hs . add ( arr2 [ i ] ) NEW_LINE DEDENT print ( " Union : " ) NEW_LINE for i in hs : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT print ( " " ) NEW_LINE DEDENT def printIntersection ( arr1 , arr2 , n1 , n2 ) : NEW_LINE INDENT hs = set ( ) NEW_LINE for i in range ( 0 , n1 ) : NEW_LINE INDENT hs . add ( arr1 [ i ] ) NEW_LINE DEDENT print ( " Intersection : " ) NEW_LINE for i in range ( 0 , n2 ) : NEW_LINE INDENT if arr2 [ i ] in hs : NEW_LINE INDENT print ( arr2 [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT arr1 = [ 7 , 1 , 5 , 2 , 3 , 6 ] NEW_LINE arr2 = [ 3 , 8 , 6 , 20 , 7 ] NEW_LINE n1 = len ( arr1 ) NEW_LINE n2 = len ( arr2 ) NEW_LINE printUnion ( arr1 , arr2 , n1 , n2 ) NEW_LINE printIntersection ( arr1 , arr2 , n1 , n2 ) NEW_LINE
Find Union and Intersection of two unsorted arrays | Prints intersection of arr1 [ 0. . n1 - 1 ] and arr2 [ 0. . n2 - 1 ] ; Iterate first array ; Iterate second array ; Prints union of arr1 [ 0. . n1 - 1 ] and arr2 [ 0. . n2 - 1 ] ; placeZeroes function ; Function to itreate array ; placeValue function ; Hashing collision happened increment position and do recursive call ; Driver code
def findPosition ( a , b ) : NEW_LINE INDENT v = len ( a ) + len ( b ) ; NEW_LINE ans = [ 0 ] * v ; NEW_LINE zero1 = zero2 = 0 ; NEW_LINE print ( " Intersection ▁ : " , end = " ▁ " ) ; NEW_LINE for i in range ( len ( a ) ) : NEW_LINE INDENT zero1 = iterateArray ( a , v , ans , i ) ; NEW_LINE DEDENT for j in range ( len ( b ) ) : NEW_LINE INDENT zero2 = iterateArray ( b , v , ans , j ) ; NEW_LINE DEDENT zero = zero1 + zero2 ; NEW_LINE placeZeros ( v , ans , zero ) ; NEW_LINE printUnion ( v , ans , zero ) ; NEW_LINE DEDENT def printUnion ( v , ans , zero ) : NEW_LINE INDENT zero1 = 0 ; NEW_LINE print ( " Union : " , end = "   " ) ; NEW_LINE for i in range ( v ) : NEW_LINE INDENT if ( ( zero == 0 and ans [ i ] == 0 ) or ( ans [ i ] == 0 and zero1 > 0 ) ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT if ( ans [ i ] == 0 ) : NEW_LINE INDENT zero1 += 1 ; NEW_LINE DEDENT print ( ans [ i ] , end = " , " ) ; NEW_LINE DEDENT DEDENT def placeZeros ( v , ans , zero ) : NEW_LINE INDENT if ( zero == 2 ) : NEW_LINE INDENT print ( "0" ) ; NEW_LINE d = [ 0 ] ; NEW_LINE placeValue ( d , ans , 0 , 0 , v ) ; NEW_LINE DEDENT if ( zero == 1 ) : NEW_LINE INDENT d = [ 0 ] ; NEW_LINE placeValue ( d , ans , 0 , 0 , v ) ; NEW_LINE DEDENT DEDENT def iterateArray ( a , v , ans , i ) : NEW_LINE INDENT if ( a [ i ] != 0 ) : NEW_LINE INDENT p = a [ i ] % v ; NEW_LINE placeValue ( a , ans , i , p , v ) ; NEW_LINE DEDENT else : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT return 0 ; NEW_LINE DEDENT def placeValue ( a , ans , i , p , v ) : NEW_LINE INDENT p = p % v ; NEW_LINE if ( ans [ p ] == 0 ) : NEW_LINE INDENT ans [ p ] = a [ i ] ; NEW_LINE DEDENT else : NEW_LINE INDENT if ( ans [ p ] == a [ i ] ) : NEW_LINE INDENT print ( a [ i ] , end = " , " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT p = p + 1 ; NEW_LINE placeValue ( a , ans , i , p , v ) ; NEW_LINE DEDENT DEDENT DEDENT a = [ 7 , 1 , 5 , 2 , 3 , 6 ] ; NEW_LINE b = [ 3 , 8 , 6 , 20 , 7 ] ; NEW_LINE findPosition ( a , b ) ; NEW_LINE
Minimum number of coins that can generate all the values in the given range | Function to return the count of minimum coins required ; To store the required sequence ; Creating list of the sum of all previous bit values including that bit value ; Driver code
def findCount ( N ) : NEW_LINE INDENT list = [ ] NEW_LINE sum = 0 NEW_LINE for i in range ( 0 , 20 ) : NEW_LINE INDENT sum += 2 ** i NEW_LINE list . append ( sum ) NEW_LINE DEDENT for value in list : NEW_LINE INDENT if ( value >= N ) : NEW_LINE INDENT return ( list . index ( value ) + 1 ) NEW_LINE DEDENT DEDENT DEDENT N = 10 NEW_LINE print ( findCount ( N ) ) NEW_LINE
Sort an array of 0 s , 1 s and 2 s | Function to sort array ; Function to print array ; Driver Program
def sort012 ( a , arr_size ) : NEW_LINE INDENT lo = 0 NEW_LINE hi = arr_size - 1 NEW_LINE mid = 0 NEW_LINE while mid <= hi : NEW_LINE INDENT if a [ mid ] == 0 : NEW_LINE INDENT a [ lo ] , a [ mid ] = a [ mid ] , a [ lo ] NEW_LINE lo = lo + 1 NEW_LINE mid = mid + 1 NEW_LINE DEDENT elif a [ mid ] == 1 : NEW_LINE INDENT mid = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT a [ mid ] , a [ hi ] = a [ hi ] , a [ mid ] NEW_LINE hi = hi - 1 NEW_LINE DEDENT DEDENT return a NEW_LINE DEDENT def printArray ( a ) : NEW_LINE INDENT for k in a : NEW_LINE INDENT print k , NEW_LINE DEDENT DEDENT arr = [ 0 , 1 , 1 , 0 , 1 , 2 , 1 , 2 , 0 , 0 , 0 , 1 ] NEW_LINE arr_size = len ( arr ) NEW_LINE arr = sort012 ( arr , arr_size ) NEW_LINE print   " Array after segregation : NEW_LINE " , NEW_LINE printArray ( arr ) NEW_LINE
Calculate the number of set bits for every number from 0 to N | Function to find the count of set bits in all the integers from 0 to n ; dp [ i ] will store the count of set bits in i Initialise the dp array ; Count of set bits in 0 is 0 ; For every number starting from 1 ; If current number is even ; Count of set bits in i is equal to the count of set bits in ( i / 2 ) ; If current element is odd ; Count of set bits in i is equal to the count of set bits in ( i / 2 ) + 1 ; Print the count of set bits in i ; Driver code
def findSetBits ( n ) : NEW_LINE INDENT dp = [ 0 ] * ( n + 1 ) ; NEW_LINE print ( dp [ 0 ] , end = " ▁ " ) ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT dp [ i ] = dp [ i // 2 ] ; NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] = dp [ i // 2 ] + 1 ; NEW_LINE DEDENT print ( dp [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 ; NEW_LINE findSetBits ( n ) ; NEW_LINE DEDENT
Sort an array of 0 s , 1 s and 2 s | Utility function to print contents of an array ; Function to sort the array of 0 s , 1 s and 2 s ; Count the number of 0 s , 1 s and 2 s in the array ; Update the array ; Store all the 0 s in the beginning ; Then all the 1 s ; Finally all the 2 s ; Prthe sorted array ; Driver code
def printArr ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT def sortArr ( arr , n ) : NEW_LINE INDENT cnt0 = 0 NEW_LINE cnt1 = 0 NEW_LINE cnt2 = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if arr [ i ] == 0 : NEW_LINE INDENT cnt0 += 1 NEW_LINE DEDENT elif arr [ i ] == 1 : NEW_LINE INDENT cnt1 += 1 NEW_LINE DEDENT elif arr [ i ] == 2 : NEW_LINE INDENT cnt2 += 1 NEW_LINE DEDENT DEDENT i = 0 NEW_LINE while ( cnt0 > 0 ) : NEW_LINE INDENT arr [ i ] = 0 NEW_LINE i += 1 NEW_LINE cnt0 -= 1 NEW_LINE DEDENT while ( cnt1 > 0 ) : NEW_LINE INDENT arr [ i ] = 1 NEW_LINE i += 1 NEW_LINE cnt1 -= 1 NEW_LINE DEDENT while ( cnt2 > 0 ) : NEW_LINE INDENT arr [ i ] = 2 NEW_LINE i += 1 NEW_LINE cnt2 -= 1 NEW_LINE DEDENT printArr ( arr , n ) NEW_LINE DEDENT arr = [ 0 , 1 , 1 , 0 , 1 , 2 , 1 , 2 , 0 , 0 , 0 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE sortArr ( arr , n ) NEW_LINE
Find the Minimum length Unsorted Subarray , sorting which makes the complete array sorted | Python3 program to find the Minimum length Unsorted Subarray , sorting which makes the complete array sorted ; step 1 ( a ) of above algo ; step 1 ( b ) of above algo ; step 2 ( a ) of above algo ; step 2 ( b ) of above algo ; step 2 ( c ) of above algo ; step 3 of above algo
def printUnsorted ( arr , n ) : NEW_LINE INDENT e = n - 1 NEW_LINE for s in range ( 0 , n - 1 ) : NEW_LINE INDENT if arr [ s ] > arr [ s + 1 ] : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if s == n - 1 : NEW_LINE INDENT print ( " The ▁ complete ▁ array ▁ is ▁ sorted " ) NEW_LINE exit ( ) NEW_LINE DEDENT e = n - 1 NEW_LINE while e > 0 : NEW_LINE INDENT if arr [ e ] < arr [ e - 1 ] : NEW_LINE INDENT break NEW_LINE DEDENT e -= 1 NEW_LINE DEDENT max = arr [ s ] NEW_LINE min = arr [ s ] NEW_LINE for i in range ( s + 1 , e + 1 ) : NEW_LINE INDENT if arr [ i ] > max : NEW_LINE INDENT max = arr [ i ] NEW_LINE DEDENT if arr [ i ] < min : NEW_LINE INDENT min = arr [ i ] NEW_LINE DEDENT DEDENT for i in range ( s ) : NEW_LINE INDENT if arr [ i ] > min : NEW_LINE INDENT s = i NEW_LINE break NEW_LINE DEDENT DEDENT i = n - 1 NEW_LINE while i >= e + 1 : NEW_LINE INDENT if arr [ i ] < max : NEW_LINE INDENT e = i NEW_LINE break NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT print ( " The ▁ unsorted ▁ subarray ▁ which ▁ makes ▁ the ▁ given ▁ array " ) NEW_LINE print ( " sorted ▁ lies ▁ between ▁ the ▁ indexes ▁ % d ▁ and ▁ % d " % ( s , e ) ) NEW_LINE DEDENT arr = [ 10 , 12 , 20 , 30 , 25 , 40 , 32 , 31 , 35 , 50 , 60 ] NEW_LINE arr_size = len ( arr ) NEW_LINE printUnsorted ( arr , arr_size ) NEW_LINE
Count the number of possible triangles | Function to count all possible triangles with arr [ ] elements ; Count of triangles ; The three loops select three different values from array ; The innermost loop checks for the triangle property ; Sum of two sides is greater than the third ; Driver code
def findNumberOfTriangles ( arr , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT for k in range ( j + 1 , n ) : NEW_LINE INDENT if ( arr [ i ] + arr [ j ] > arr [ k ] and arr [ i ] + arr [ k ] > arr [ j ] and arr [ k ] + arr [ j ] > arr [ i ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT return count NEW_LINE DEDENT arr = [ 10 , 21 , 22 , 100 , 101 , 200 , 300 ] NEW_LINE size = len ( arr ) NEW_LINE print ( " Total ▁ number ▁ of ▁ triangles ▁ possible ▁ is " , findNumberOfTriangles ( arr , size ) ) NEW_LINE
Count the number of possible triangles | Function to count all possible triangles with arr [ ] elements ; Sort array and initialize count as 0 ; Initialize count of triangles ; Fix the first element . We need to run till n - 3 as the other two elements are selected from arr [ i + 1. . . n - 1 ] ; Initialize index of the rightmost third element ; Fix the second element ; Find the rightmost element which is smaller than the sum of two fixed elements The important thing to note here is , we use the previous value of k . If value of arr [ i ] + arr [ j - 1 ] was greater than arr [ k ] , then arr [ i ] + arr [ j ] must be greater than k , because the array is sorted . ; Total number of possible triangles that can be formed with the two fixed elements is k - j - 1. The two fixed elements are arr [ i ] and arr [ j ] . All elements between arr [ j + 1 ] to arr [ k - 1 ] can form a triangle with arr [ i ] and arr [ j ] . One is subtracted from k because k is incremented one extra in above while loop . k will always be greater than j . If j becomes equal to k , then above loop will increment k , because arr [ k ] + arr [ i ] is always greater than arr [ k ] ; Driver function to test above function
def findnumberofTriangles ( arr ) : NEW_LINE INDENT n = len ( arr ) NEW_LINE arr . sort ( ) NEW_LINE count = 0 NEW_LINE for i in range ( 0 , n - 2 ) : NEW_LINE INDENT k = i + 2 NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT while ( k < n and arr [ i ] + arr [ j ] > arr [ k ] ) : NEW_LINE INDENT k += 1 NEW_LINE DEDENT if ( k > j ) : NEW_LINE INDENT count += k - j - 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT arr = [ 10 , 21 , 22 , 100 , 101 , 200 , 300 ] NEW_LINE print " Number ▁ of ▁ Triangles : " , findnumberofTriangles ( arr ) NEW_LINE
Count the number of possible triangles | CountTriangles function ; If it is possible with a [ l ] , a [ r ] and a [ i ] then it is also possible with a [ l + 1 ] . . a [ r - 1 ] , a [ r ] and a [ i ] ; checking for more possible solutions ; if not possible check for higher values of arr [ l ] ; Driver Code
def CountTriangles ( A ) : NEW_LINE INDENT n = len ( A ) ; NEW_LINE A . sort ( ) ; NEW_LINE count = 0 ; NEW_LINE for i in range ( n - 1 , 0 , - 1 ) : NEW_LINE INDENT l = 0 ; NEW_LINE r = i - 1 ; NEW_LINE while ( l < r ) : NEW_LINE INDENT if ( A [ l ] + A [ r ] > A [ i ] ) : NEW_LINE INDENT count += r - l ; NEW_LINE r -= 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT l += 1 ; NEW_LINE DEDENT DEDENT DEDENT print ( " No ▁ of ▁ possible ▁ solutions : ▁ " , count ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 4 , 3 , 5 , 7 , 6 ] ; NEW_LINE CountTriangles ( A ) ; NEW_LINE DEDENT
Maximum sum of nodes in Binary tree such that no two are adjacent | A binary tree node structure ; Utility function to create a new Binary Tree node ; method returns maximum sum possible from subtrees rooted at grandChildrens of node ; call for children of left child only if it is not NULL ; call for children of right child only if it is not NULL ; Utility method to return maximum sum rooted at node ; If node is already processed then return calculated value from map ; take current node value and call for all grand children ; don 't take current node  value and call for all children ; choose maximum from both above calls and store that in map ; Returns maximum sum from subset of nodes of binary tree under given constraints ; Driver code
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 newNode ( data ) : NEW_LINE INDENT temp = Node ( data ) NEW_LINE return temp ; NEW_LINE DEDENT def sumOfGrandChildren ( node , mp ) : NEW_LINE INDENT sum = 0 ; NEW_LINE if ( node . left ) : NEW_LINE INDENT sum += ( getMaxSumUtil ( node . left . left , mp ) + getMaxSumUtil ( node . left . right , mp ) ) ; NEW_LINE DEDENT if ( node . right ) : NEW_LINE INDENT sum += ( getMaxSumUtil ( node . right . left , mp ) + getMaxSumUtil ( node . right . right , mp ) ) ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT def getMaxSumUtil ( node , mp ) : NEW_LINE INDENT if ( node == None ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if node in mp : NEW_LINE INDENT return mp [ node ] ; NEW_LINE DEDENT incl = ( node . data + sumOfGrandChildren ( node , mp ) ) ; NEW_LINE excl = ( getMaxSumUtil ( node . left , mp ) + getMaxSumUtil ( node . right , mp ) ) ; NEW_LINE mp [ node ] = max ( incl , excl ) ; NEW_LINE return mp [ node ] ; NEW_LINE DEDENT def getMaxSum ( node ) : NEW_LINE INDENT if ( node == None ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT mp = dict ( ) NEW_LINE return getMaxSumUtil ( node , mp ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT root = newNode ( 1 ) ; NEW_LINE root . left = newNode ( 2 ) ; NEW_LINE root . right = newNode ( 3 ) ; NEW_LINE root . right . left = newNode ( 4 ) ; NEW_LINE root . right . right = newNode ( 5 ) ; NEW_LINE root . left . left = newNode ( 1 ) ; NEW_LINE print ( getMaxSum ( root ) ) NEW_LINE DEDENT
Flip minimum signs of array elements to get minimum sum of positive elements possible | Function to return the minimum number of elements whose sign must be flipped to get the positive sum of array elements as close to 0 as possible ; boolean variable used for toggling between maps ; Calculate the sum of all elements of the array ; Initializing first map ( dp [ 0 ] ) with INT_MAX because for i = 0 , there are no elements in the array to flip ; Base Case ; For toggling ; Required sum is minimum non - negative So , we iterate from i = 0 to sum and find the first i where dp [ flag ^ 1 ] [ i ] != INT_MAX ; In worst case we will flip max n - 1 elements ; Driver code
def solve ( A , n ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( 2000 ) ] for i in range ( 2000 ) ] NEW_LINE flag = 1 NEW_LINE sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += A [ i ] NEW_LINE DEDENT for i in range ( - sum , sum + 1 ) : NEW_LINE INDENT dp [ 0 ] [ i ] = 10 ** 9 NEW_LINE DEDENT dp [ 0 ] [ 0 ] = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( - sum , sum + 1 ) : NEW_LINE INDENT dp [ flag ] [ j ] = 10 ** 9 NEW_LINE if ( j - A [ i - 1 ] <= sum and j - A [ i - 1 ] >= - sum ) : NEW_LINE INDENT dp [ flag ] [ j ] = dp [ flag ^ 1 ] [ j - A [ i - 1 ] ] NEW_LINE DEDENT if ( j + A [ i - 1 ] <= sum and j + A [ i - 1 ] >= - sum and dp [ flag ^ 1 ] [ j + A [ i - 1 ] ] != 10 ** 9 ) : NEW_LINE INDENT dp [ flag ] [ j ] = min ( dp [ flag ] [ j ] , dp [ flag ^ 1 ] [ j + A [ i - 1 ] ] + 1 ) NEW_LINE DEDENT DEDENT flag = flag ^ 1 NEW_LINE DEDENT for i in range ( sum + 1 ) : NEW_LINE INDENT if ( dp [ flag ^ 1 ] [ i ] != 10 ** 9 ) : NEW_LINE INDENT return dp [ flag ^ 1 ] [ i ] NEW_LINE DEDENT DEDENT return n - 1 NEW_LINE DEDENT arr = [ 10 , 22 , 9 , 33 , 21 , 50 , 41 , 60 ] NEW_LINE n = len ( arr ) NEW_LINE print ( solve ( arr , n ) ) NEW_LINE
Find number of pairs ( x , y ) in an array such that x ^ y > y ^ x |
def countPairsBruteForce ( X , Y , m , n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( m ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if ( pow ( X [ i ] , Y [ j ] ) > pow ( Y [ j ] , X [ i ] ) ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT