codeparrot/xlcost-text-to-code huggingface dataset:
Minimum time required to fill given N slots | Function to return the minimum time to fill all the slots ; Stores visited slots ; Checks if a slot is visited or not ; Insert all filled slots ; Iterate until queue is not empty ; Iterate through all slots in the queue ; Front index ; If previous slot is present and not visited ; If next slot is present and not visited ; Increment the time at each level ; Print the answer ; Driver Code ; Function Call
def minTime ( arr , N , K ) : NEW_LINE INDENT q = [ ] NEW_LINE vis = [ False ] * ( N + 1 ) NEW_LINE time = 0 NEW_LINE for i in range ( K ) : NEW_LINE INDENT q . append ( arr [ i ] ) NEW_LINE vis [ arr [ i ] ] = True NEW_LINE DEDENT while ( len ( q ) > 0 ) : NEW_LINE INDENT for i in range ( len ( q ) ) : NEW_LINE INDENT curr = q [ 0 ] NEW_LINE q . pop ( 0 ) NEW_LINE if ( curr - 1 >= 1 and vis [ curr - 1 ] == 0 ) : NEW_LINE INDENT vis [ curr - 1 ] = True NEW_LINE q . append ( curr - 1 ) NEW_LINE DEDENT if ( curr + 1 <= N and vis [ curr + 1 ] == 0 ) : NEW_LINE INDENT vis [ curr + 1 ] = True NEW_LINE q . append ( curr + 1 ) NEW_LINE DEDENT DEDENT time += 1 NEW_LINE DEDENT print ( time - 1 ) NEW_LINE DEDENT N = 6 NEW_LINE arr = [ 2 , 6 ] NEW_LINE K = len ( arr ) NEW_LINE minTime ( arr , N , K ) NEW_LINE
Count of integers having difference with its reverse equal to D | Maximum digits in N ; Utility function to find count of N such that N + D = reverse ( N ) ; If d is a multiple of 9 , no such value N found ; Divide D by 9 check reverse of number and its sum ; B [ i ] : Stores power of ( 10 , i ) ; Calculate power ( 10 , i ) ; Update B [ i ] ; Stores count of N such that N + D = reverse ( N ) ; Iterate over the range [ 1 , MAXL ] ; Update ans ; Function to find count of possible values of N such that N + D = reverse ( N ) ; Base Case ; If D is not qual to 0 ; Stores count of possible values of N such that N + D = reverse ( N ) ; Update ans ; If l is even ; Update ans ; Stores count of possible values of N such that N + D = reverse ( N ) ; Iterae over the range [ - 9 , 9 ] ; Update ans ; Driver Code ; Function call
MAXL = 17 ; NEW_LINE N = 0 ; NEW_LINE v = 0 ; NEW_LINE def findN ( D ) : NEW_LINE INDENT global N ; NEW_LINE global v ; NEW_LINE if ( D % 9 != 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT D //= 9 ; NEW_LINE B = [ 0 ] * MAXL ; NEW_LINE B [ 0 ] = 1 ; NEW_LINE for i in range ( 1 , MAXL ) : NEW_LINE INDENT B [ i ] = B [ i - 1 ] * 10 ; NEW_LINE DEDENT ans = 0 ; NEW_LINE for i in range ( 1 , MAXL + 1 ) : NEW_LINE INDENT N = ( i + 1 ) // 2 ; NEW_LINE v = [ 0 ] * N ; NEW_LINE for j in range ( N ) : NEW_LINE INDENT for k in range ( j , i - j ) : NEW_LINE INDENT v [ j ] += B [ k ] ; NEW_LINE DEDENT DEDENT temp = [ 0 ] * N ; NEW_LINE ans += count ( D , i , 0 , temp ) ; NEW_LINE return ans ; NEW_LINE DEDENT DEDENT def count ( D , l , t , x ) : NEW_LINE INDENT global N ; NEW_LINE global v ; NEW_LINE if ( t == N ) : NEW_LINE INDENT if ( D != 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT ans = 1 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT ans *= ( 9 if i == 0 else 10 ) - abs ( x [ i ] ) ; NEW_LINE DEDENT if ( l % 2 == 0 ) : NEW_LINE INDENT ans *= 10 ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT ans = 0 ; NEW_LINE for m in range ( - 9 , 10 ) : NEW_LINE INDENT if ( - v [ t ] < D + v [ t ] * m and D + v [ t ] * m < v [ t ] ) : NEW_LINE INDENT x [ t ] = m ; NEW_LINE ans += count ( D + v [ t ] * m , l , t + 1 , x ) ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT D = 63 ; NEW_LINE print ( findN ( D ) ) ; NEW_LINE DEDENT
Search for an element in a Mountain Array | Function to find the index of the peak element in the array ; Stores left most index in which the peak element can be found ; Stores right most index in which the peak element can be found ; Stores mid of left and right ; If element at mid is less than element at ( mid + 1 ) ; Update left ; Update right ; Function to perform binary search in an a subarray if elements of the subarray are in an ascending order ; Stores mid of left and right ; If X found at mid ; If X is greater than mid ; Update left ; Update right ; Function to perform binary search in an a subarray if elements of the subarray are in an ascending order ; Stores mid of left and right ; If X found at mid ; Update right ; Update left ; Function to find the smallest index of X ; Stores index of peak element in array ; Stores index of X in the array ; If X greater than or equal to first element of array and less than the peak element ; Update res ; If element not found on left side of peak element ; Update res ; Print res ; Driver Code ; Given X ; Given array ; Function Call
def findPeak ( arr ) : NEW_LINE INDENT left = 0 NEW_LINE right = len ( arr ) - 1 NEW_LINE while ( left < right ) : NEW_LINE INDENT mid = left + ( right - left ) // 2 NEW_LINE if ( arr [ mid ] < arr [ ( mid + 1 ) ] ) : NEW_LINE INDENT left = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT right = mid NEW_LINE DEDENT DEDENT return left NEW_LINE DEDENT def BS ( X , left , right , arr ) : NEW_LINE INDENT while ( left <= right ) : NEW_LINE INDENT mid = left + ( right - left ) // 2 NEW_LINE if ( arr [ mid ] == X ) : NEW_LINE INDENT return mid NEW_LINE DEDENT elif ( X > arr [ mid ] ) : NEW_LINE INDENT left = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT right = mid - 1 NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT def reverseBS ( X , left , right , arr ) : NEW_LINE INDENT while ( left <= right ) : NEW_LINE INDENT mid = left + ( right - left ) // 2 NEW_LINE if ( arr [ mid ] == X ) : NEW_LINE INDENT return mid NEW_LINE DEDENT elif ( X > arr [ mid ] ) : NEW_LINE INDENT right = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT left = mid + 1 NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT def findInMA ( X , mountainArr ) : NEW_LINE INDENT peakIndex = findPeak ( mountainArr ) NEW_LINE res = - 1 NEW_LINE if ( X >= mountainArr [ 0 ] and X <= mountainArr [ peakIndex ] ) : NEW_LINE INDENT res = BS ( X , 0 , peakIndex , mountainArr ) NEW_LINE DEDENT if ( res == - 1 ) : NEW_LINE INDENT res = reverseBS ( X , peakIndex + 1 , mountainArr . size ( ) - 1 , mountainArr ) NEW_LINE DEDENT print ( res ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT X = 3 NEW_LINE arr = [ 1 , 2 , 3 , 4 , 5 , 3 , 1 ] NEW_LINE findInMA ( X , arr ) NEW_LINE DEDENT
Rearrange given array to obtain positive prefix sums at exactly X indices | Function to rearrange the array according to the given condition ; Using 2 nd operation making all values positive ; Sort the array ; Assign K = N - K ; Count number of zeros ; If number of zeros if greater ; Using 2 nd operation convert it into one negative ; Using 2 nd operation convert it into one negative ; Print array ; Driver Code ; Function Call
def rearrange ( a , n , x ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT a [ i ] = abs ( a [ i ] ) NEW_LINE DEDENT a = sorted ( a ) NEW_LINE x = n - x ; NEW_LINE z = a . count ( 0 ) NEW_LINE if ( x > n - z ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT for i in range ( 0 , n , 2 ) : NEW_LINE INDENT if x <= 0 : NEW_LINE INDENT break NEW_LINE DEDENT a [ i ] = - a [ i ] NEW_LINE x -= 1 NEW_LINE DEDENT for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if x <= 0 : NEW_LINE INDENT break NEW_LINE DEDENT if ( a [ i ] > 0 ) : NEW_LINE INDENT a [ i ] = - a [ i ] NEW_LINE x -= 1 NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 0 , - 2 , 4 , 5 , - 3 ] NEW_LINE K = 3 NEW_LINE N = len ( arr ) NEW_LINE rearrange ( arr , N , K ) NEW_LINE DEDENT
Maximize product of a strictly increasing or decreasing subarray | Function to find the maximum product of subarray in the array , arr [ ] ; Maximum positive product ending at the i - th index ; Minimum negative product ending at the current index ; Maximum product up to i - th index ; Check if an array element is positive or not ; Traverse the array ; If current element is positive ; Update max_ending_here ; Update min_ending_here ; Update flag ; If current element is 0 , reset the start index of subarray ; Update max_ending_here ; Update min_ending_here ; If current element is negative ; Stores max_ending_here ; Update max_ending_here ; Update min_ending_here ; Update max_so_far , if needed ; If no array elements is positive and max_so_far is 0 ; Function to find the maximum product of either increasing subarray or the decreasing subarray ; Stores start index of either increasing subarray or the decreasing subarray ; Initially assume maxProd to be 1 ; Traverse the array ; Store the longest either increasing subarray or the decreasing subarray whose start index is i ; Check for increasing subarray ; Insert elements of increasing subarray ; Check for decreasing subarray ; Insert elements of decreasing subarray ; Stores maximum subarray product of current increasing or decreasing subarray ; Update maxProd ; Update i ; Finally prmaxProd ; Driver Code
def maxSubarrayProduct ( arr , n ) : NEW_LINE INDENT max_ending_here = 1 NEW_LINE min_ending_here = 1 NEW_LINE max_so_far = 0 NEW_LINE flag = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] > 0 ) : NEW_LINE INDENT max_ending_here = max_ending_here * arr [ i ] NEW_LINE min_ending_here = min ( min_ending_here * arr [ i ] , 1 ) NEW_LINE flag = 1 NEW_LINE DEDENT elif ( arr [ i ] == 0 ) : NEW_LINE INDENT max_ending_here = 1 NEW_LINE min_ending_here = 1 NEW_LINE DEDENT else : NEW_LINE INDENT temp = max_ending_here NEW_LINE max_ending_here = max ( min_ending_here * arr [ i ] , 1 ) NEW_LINE min_ending_here = temp * arr [ i ] NEW_LINE DEDENT if ( max_so_far < max_ending_here ) : NEW_LINE INDENT max_so_far = max_ending_here NEW_LINE DEDENT DEDENT if ( flag == 0 and max_so_far == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return max_so_far NEW_LINE DEDENT def findMaxProduct ( a , n ) : NEW_LINE INDENT i = 0 NEW_LINE maxProd = - 10 ** 9 NEW_LINE while ( i < n ) : NEW_LINE INDENT v = [ ] NEW_LINE v . append ( a [ i ] ) NEW_LINE if i < n - 1 and a [ i ] < a [ i + 1 ] : NEW_LINE INDENT while ( i < n - 1 and a [ i ] < a [ i + 1 ] ) : NEW_LINE INDENT v . append ( a [ i + 1 ] ) NEW_LINE i += 1 NEW_LINE DEDENT DEDENT elif ( i < n - 1 and a [ i ] > a [ i + 1 ] ) : NEW_LINE INDENT while ( i < n - 1 and a [ i ] > a [ i + 1 ] ) : NEW_LINE INDENT v . append ( a [ i + 1 ] ) NEW_LINE i += 1 NEW_LINE DEDENT DEDENT prod = maxSubarrayProduct ( v , len ( v ) ) NEW_LINE maxProd = max ( maxProd , prod ) NEW_LINE i += 1 NEW_LINE DEDENT return maxProd NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 10 , 8 , 1 , 100 , 101 ] NEW_LINE N = len ( arr ) NEW_LINE print ( findMaxProduct ( arr , N ) ) NEW_LINE DEDENT
Number of connected components of a graph ( using Disjoint Set Union ) | Stores the parent of each vertex ; Function to find the topmost parent of vertex a ; If current vertex is the topmost vertex ; Otherwise , set topmost vertex of its parent as its topmost vertex ; Function to connect the component having vertex a with the component having vertex b ; Connect edges ; Function to find unique top most parents ; Traverse all vertices ; Insert all topmost vertices obtained ; Print count of connected components ; Function to print answer ; Setting parent to itself ; Traverse all edges ; Print answer ; Given N ; Given edges ; Function call
parent = [ 0 ] * ( 1000000 ) NEW_LINE def root ( a ) : NEW_LINE INDENT if ( a == parent [ a ] ) : NEW_LINE INDENT return a NEW_LINE DEDENT parent [ a ] = root ( parent [ a ] ) NEW_LINE return parent [ a ] NEW_LINE DEDENT def connect ( a , b ) : NEW_LINE INDENT a = root ( a ) NEW_LINE b = root ( b ) NEW_LINE if ( a != b ) : NEW_LINE INDENT parent [ b ] = a NEW_LINE DEDENT DEDENT def connectedComponents ( n ) : NEW_LINE INDENT s = set ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT s . add ( root ( parent [ i ] ) ) NEW_LINE DEDENT print ( len ( s ) ) NEW_LINE DEDENT def printAnswer ( N , edges ) : NEW_LINE INDENT for i in range ( N + 1 ) : NEW_LINE INDENT parent [ i ] = i NEW_LINE DEDENT for i in range ( len ( edges ) ) : NEW_LINE INDENT connect ( edges [ i ] [ 0 ] , edges [ i ] [ 1 ] ) NEW_LINE DEDENT connectedComponents ( N ) NEW_LINE DEDENT N = 6 NEW_LINE edges = [ [ 1 , 0 ] , [ 2 , 3 ] , [ 1 , 2 ] , [ 4 , 5 ] ] NEW_LINE printAnswer ( N , edges ) NEW_LINE
Remove all zero | Function to remove the rows or columns from the matrix which contains all 0 s elements ; Stores count of rows ; col [ i ] : Stores count of 0 s in current column ; row [ i ] : Stores count of 0 s in current row ; Traverse the matrix ; Stores count of 0 s in current row ; Update col [ j ] ; Update count ; Update row [ i ] ; Traverse the matrix ; If all elements of current row is 0 ; If all elements of current column is 0 ; Driver Code ; Function Call
def removeZeroRowCol ( arr ) : NEW_LINE INDENT n = len ( arr ) NEW_LINE col = [ 0 ] * ( n + 1 ) NEW_LINE row = [ 0 ] * ( n + 1 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT count = 0 NEW_LINE for j in range ( n ) : NEW_LINE INDENT col [ j ] += ( arr [ i ] [ j ] == 1 ) NEW_LINE count += ( arr [ i ] [ j ] == 1 ) NEW_LINE DEDENT row [ i ] = count NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( row [ i ] == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT for j in range ( n ) : NEW_LINE INDENT if ( col [ j ] != 0 ) : NEW_LINE INDENT print ( arr [ i ] [ j ] , end = " " ) NEW_LINE DEDENT DEDENT print ( ) NEW_LINE DEDENT DEDENT arr = [ [ 1 , 1 , 0 , 1 ] , [ 0 , 0 , 0 , 0 ] , [ 1 , 1 , 0 , 1 ] , [ 0 , 1 , 0 , 1 ] ] NEW_LINE removeZeroRowCol ( arr ) NEW_LINE
Find a pair of overlapping intervals from a given Set | Function to find a pair ( i , j ) such that i - th interval lies within the j - th interval ; Store interval and index of the interval in the form of { { l , r } , index } ; Traverse the array , arr [ ] [ ] ; Stores l - value of the interval ; Stores r - value of the interval ; Push current interval and index into tup ; Sort the vector based on l - value of the intervals ; Stores r - value of current interval ; Stores index of current interval ; Traverse the vector , tup [ ] ; Stores l - value of previous interval ; Stores l - value of current interval ; If Q and R are equal ; Print the index of interval ; Stores r - value of current interval ; If T is less than or equal to curr ; Update curr ; Update currPos ; If such intervals found ; Given l - value of segments ; Given r - value of segments ; Given size ; Function Call
def findOverlapSegement ( N , a , b ) : NEW_LINE INDENT tup = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT x = a [ i ] NEW_LINE y = b [ i ] NEW_LINE tup . append ( ( ( x , y ) , i ) ) NEW_LINE DEDENT tup . sort ( ) NEW_LINE curr = tup [ 0 ] [ 0 ] [ 1 ] NEW_LINE currPos = tup [ 0 ] [ 1 ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT Q = tup [ i - 1 ] [ 0 ] [ 0 ] NEW_LINE R = tup [ i ] [ 0 ] [ 0 ] NEW_LINE if Q == R : NEW_LINE INDENT if tup [ i - 1 ] [ 0 ] [ 1 ] < tup [ i ] [ 0 ] [ 1 ] : NEW_LINE INDENT print ( tup [ i - 1 ] [ 1 ] , tup [ i ] [ 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( tup [ i ] [ 1 ] , tup [ i - 1 ] [ 1 ] ) NEW_LINE DEDENT return NEW_LINE DEDENT T = tup [ i ] [ 0 ] [ 1 ] NEW_LINE if ( T <= curr ) : NEW_LINE INDENT print ( tup [ i ] [ 1 ] , currPos ) NEW_LINE return NEW_LINE DEDENT else : NEW_LINE INDENT curr = T NEW_LINE currPos = tup [ i ] [ 1 ] NEW_LINE DEDENT DEDENT print ( " - 1" , " - 1" , end = " " ) NEW_LINE DEDENT a = [ 1 , 2 , 3 , 2 , 2 ] NEW_LINE b = [ 5 , 10 , 10 , 2 , 15 ] NEW_LINE N = len ( a ) NEW_LINE findOverlapSegement ( N , a , b ) NEW_LINE
Replace each node of a Binary Tree with the sum of all the nodes present in its diagonal | Structure of a tree node ; Function to replace each node with the sum of nodes at the same diagonal ; IF root is NULL ; Replace nodes ; Traverse the left subtree ; Traverse the right subtree ; Function to find the sum of all the nodes at each diagonal of the tree ; If root is not NULL ; If update sum of nodes at current diagonal ; Traverse the left subtree ; Traverse the right subtree ; Function to print the nodes of the tree using level order traversal ; Stores node at each level of the tree ; Stores count of nodes at current level ; Stores front element of the queue ; Insert left subtree ; Insert right subtree ; Update length ; Driver code ; Build tree ; Store sum of nodes at each diagonal of the tree ; Find sum of nodes at each diagonal of the tree ; Replace nodes with the sum of nodes at the same diagonal ; Print tree
class TreeNode : NEW_LINE INDENT def __init__ ( self , val = 0 , left = None , right = None ) : NEW_LINE INDENT self . val = val NEW_LINE self . left = left NEW_LINE self . right = right NEW_LINE DEDENT DEDENT def replaceDiag ( root , d , diagMap ) : NEW_LINE INDENT if not root : NEW_LINE INDENT return NEW_LINE DEDENT root . val = diagMap [ d ] NEW_LINE replaceDiag ( root . left , d + 1 , diagMap ) NEW_LINE replaceDiag ( root . right , d , diagMap ) NEW_LINE DEDENT def getDiagSum ( root , d , diagMap ) : NEW_LINE INDENT if not root : NEW_LINE INDENT return NEW_LINE DEDENT if d in diagMap : NEW_LINE INDENT diagMap [ d ] += root . val NEW_LINE DEDENT else : NEW_LINE INDENT diagMap [ d ] = root . val NEW_LINE DEDENT getDiagSum ( root . left , d + 1 , diagMap ) NEW_LINE getDiagSum ( root . right , d , diagMap ) NEW_LINE DEDENT def levelOrder ( root ) : NEW_LINE INDENT que = [ root ] NEW_LINE while True : NEW_LINE INDENT length = len ( que ) NEW_LINE if not length : NEW_LINE INDENT break NEW_LINE DEDENT while length : NEW_LINE INDENT temp = que . pop ( 0 ) NEW_LINE print ( temp . val , end = ' ▁ ' ) NEW_LINE if temp . left : NEW_LINE INDENT que . append ( temp . left ) NEW_LINE DEDENT if temp . right : NEW_LINE INDENT que . append ( temp . right ) NEW_LINE DEDENT length -= 1 NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = TreeNode ( 5 ) NEW_LINE root . left = TreeNode ( 6 ) NEW_LINE root . right = TreeNode ( 3 ) NEW_LINE root . left . left = TreeNode ( 4 ) NEW_LINE root . left . right = TreeNode ( 9 ) NEW_LINE root . right . right = TreeNode ( 2 ) NEW_LINE diagMap = { } NEW_LINE getDiagSum ( root , 0 , diagMap ) NEW_LINE replaceDiag ( root , 0 , diagMap ) NEW_LINE levelOrder ( root ) NEW_LINE DEDENT
Count maximum concatenation of pairs from given array that are divisible by 3 | Function to count pairs whose concatenation is divisible by 3 and each element can be present in at most one pair ; Stores count pairs whose concatenation is divisible by 3 and each element can be present in at most one pair ; Check if an element present in any pair or not ; Generate all possible pairs ; If the element already present in a pair ; If the element already present in a pair ; If concatenation of elements is divisible by 3 ; Update ans ; Mark i is True ; Mark j is True ; Driver Code ; To display the result
def countDivBy3InArray ( arr ) : NEW_LINE INDENT ans = 0 NEW_LINE taken = [ False ] * len ( arr ) NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT if taken [ i ] : NEW_LINE INDENT continue NEW_LINE DEDENT for j in range ( i + 1 , len ( arr ) ) : NEW_LINE INDENT if taken [ j ] : NEW_LINE INDENT continue NEW_LINE DEDENT if ( not int ( str ( arr [ i ] ) + str ( arr [ j ] ) ) % 3 or not int ( str ( arr [ j ] ) + str ( arr [ i ] ) ) % 3 ) : NEW_LINE INDENT ans += 1 NEW_LINE taken [ i ] = True NEW_LINE taken [ j ] = True NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 5 , 3 , 2 , 8 , 7 ] NEW_LINE print ( countDivBy3InArray ( arr ) ) NEW_LINE
Maximum average of subtree values in a given Binary Tree | Structure of the Tree node ; Stores the result ; Function for finding maximum subtree average ; Checks if current node is not None and doesn 't have any children ; Stores sum of its subtree in index 0 and count number of nodes in index 1 ; Traverse all children of the current node ; Recursively calculate max average of subtrees among its children ; Increment sum by sum of its child 's subtree ; Increment number of nodes by its child 's node ; Increment sum by current node 's value ; Increment number of nodes by one ; Take maximum of ans and current node 's average ; Finally return pair of { sum , count } ; Driver Code ; Given tree ; Function call ; Print answer
class TreeNode : NEW_LINE INDENT def __init__ ( self , val ) : NEW_LINE INDENT self . val = val NEW_LINE self . children = [ ] NEW_LINE DEDENT DEDENT ans = 0.0 NEW_LINE def MaxAverage ( root ) : NEW_LINE INDENT global ans NEW_LINE if ( root != None and len ( root . children ) == 0 ) : NEW_LINE INDENT ans = max ( ans , ( root . val ) ) NEW_LINE return [ root . val , 1 ] NEW_LINE DEDENT childResult = [ 0 for i in range ( 2 ) ] NEW_LINE for child in root . children : NEW_LINE INDENT childTotal = MaxAverage ( child ) NEW_LINE childResult [ 0 ] = childResult [ 0 ] + childTotal [ 0 ] NEW_LINE childResult [ 1 ] = childResult [ 1 ] + childTotal [ 1 ] NEW_LINE DEDENT sum = childResult [ 0 ] + root . val NEW_LINE count = childResult [ 1 ] + 1 NEW_LINE ans = max ( ans , sum / count ) NEW_LINE return [ sum , count ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = TreeNode ( 20 ) NEW_LINE left = TreeNode ( 12 ) NEW_LINE right = TreeNode ( 18 ) NEW_LINE root . children . append ( left ) NEW_LINE root . children . append ( right ) NEW_LINE left . children . append ( TreeNode ( 11 ) ) NEW_LINE left . children . append ( TreeNode ( 3 ) ) NEW_LINE right . children . append ( TreeNode ( 15 ) ) NEW_LINE right . children . append ( TreeNode ( 8 ) ) NEW_LINE MaxAverage ( root ) NEW_LINE print ( ans * 1.0 ) NEW_LINE DEDENT
Check if a Binary String can be converted to another by reversing substrings consisting of even number of 1 s | Function to check if string A can be transformed to string B by reversing substrings of A having even number of 1 s ; Store the size of string A ; Store the size of string B ; Store the count of 1 s in A and B ; Stores cntA for string A and cntB for string B ; Traverse the string A ; If current character is 1 ; Increment 1 s count ; Otherwise , update odd1A or even1A depending whether count1A is odd or even ; Traverse the string B ; If current character is 1 ; Increment 1 s count ; Otherwise , update odd1B or even1B depending whether count1B is odd or even ; If the condition is satisfied ; If True , prYes ; Otherwise , prNo ; Driver Code ; Function Call
def canTransformStrings ( A , B ) : NEW_LINE INDENT n1 = len ( A ) ; NEW_LINE n2 = len ( B ) ; NEW_LINE count1A = 0 ; NEW_LINE count1B = 0 ; NEW_LINE odd1A = 0 ; odd1B = 0 ; NEW_LINE even1A = 0 ; even1B = 0 ; NEW_LINE for i in range ( n1 ) : NEW_LINE INDENT if ( A [ i ] == '1' ) : NEW_LINE INDENT count1A += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT if ( ( count1A & 1 ) == 1 ) : NEW_LINE INDENT odd1A += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT even1A += 1 ; NEW_LINE DEDENT DEDENT DEDENT for i in range ( n2 ) : NEW_LINE INDENT if ( B [ i ] == '1' ) : NEW_LINE INDENT count1B += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT if ( ( count1B & 1 ) == 1 ) : NEW_LINE INDENT odd1B += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT even1B += 1 ; NEW_LINE DEDENT DEDENT DEDENT if ( count1A == count1B and odd1A == odd1B and even1A == even1B ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = "10011" ; NEW_LINE B = "11100" ; NEW_LINE canTransformStrings ( A , B ) ; NEW_LINE DEDENT