codeparrot/xlcost-text-to-code huggingface dataset:
Deepest left leaf node in a binary tree | A binary tree node ; Constructor to create a new node ; A utility function to find deepest leaf node . lvl : level of current node . maxlvl : pointer to the deepest left leaf node found so far isLeft : A bool indicate that this node is left child of its parent resPtr : Pointer to the result ; Base CAse ; Update result if this node is left leaf and its level is more than the max level of the current result ; Recur for left and right subtrees ; A wrapper for left and right subtree ; Driver program to test above function
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 deepestLeftLeafUtil ( root , lvl , maxlvl , isLeft ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return NEW_LINE DEDENT if ( isLeft is True ) : NEW_LINE INDENT if ( root . left == None and root . right == None ) : NEW_LINE INDENT if lvl > maxlvl [ 0 ] : NEW_LINE INDENT deepestLeftLeafUtil . resPtr = root NEW_LINE maxlvl [ 0 ] = lvl NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT deepestLeftLeafUtil ( root . left , lvl + 1 , maxlvl , True ) NEW_LINE deepestLeftLeafUtil ( root . right , lvl + 1 , maxlvl , False ) NEW_LINE DEDENT def deepestLeftLeaf ( root ) : NEW_LINE INDENT maxlvl = [ 0 ] NEW_LINE deepestLeftLeafUtil . resPtr = None NEW_LINE deepestLeftLeafUtil ( root , 0 , maxlvl , False ) NEW_LINE return deepestLeftLeafUtil . resPtr 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 . right . left = Node ( 5 ) NEW_LINE root . right . right = Node ( 6 ) NEW_LINE root . right . left . right = Node ( 7 ) NEW_LINE root . right . right . right = Node ( 8 ) NEW_LINE root . right . left . right . left = Node ( 9 ) NEW_LINE root . right . right . right . right = Node ( 10 ) NEW_LINE result = deepestLeftLeaf ( root ) NEW_LINE if result is None : NEW_LINE INDENT print " There ▁ is ▁ not ▁ left ▁ leaf ▁ in ▁ the ▁ given ▁ tree " NEW_LINE DEDENT else : NEW_LINE INDENT print " The ▁ deepst ▁ left ▁ child ▁ is " , result . val NEW_LINE DEDENT
Check if an array contains all elements of a given range | Function to check the array for elements in given range ; Range is the no . of elements that are to be checked ; Traversing the array ; If an element is in range ; Checking whether elements in range 0 - range are negative ; Element from range is missing from array ; All range elements are present ; Defining Array and size ; A is lower limit and B is the upper limit of range ; True denotes all elements were present ; False denotes any element was not present
def check_elements ( arr , n , A , B ) : NEW_LINE INDENT rangeV = B - A NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( abs ( arr [ i ] ) >= A and abs ( arr [ i ] ) <= B ) : NEW_LINE INDENT z = abs ( arr [ i ] ) - A NEW_LINE if ( arr [ z ] > 0 ) : NEW_LINE INDENT arr [ z ] = arr [ z ] * - 1 NEW_LINE DEDENT DEDENT DEDENT count = 0 NEW_LINE for i in range ( 0 , rangeV + 1 ) : NEW_LINE INDENT if i >= n : NEW_LINE INDENT break NEW_LINE DEDENT if ( arr [ i ] > 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT DEDENT if ( count != ( rangeV + 1 ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT arr = [ 1 , 4 , 5 , 2 , 7 , 8 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE A = 2 NEW_LINE B = 5 NEW_LINE if ( check_elements ( arr , n , A , B ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Recursive Programs to find Minimum and Maximum elements of array | function to print Minimum element using recursion ; if size = 0 means whole array has been traversed ; Driver Code
def findMinRec ( A , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return A [ 0 ] NEW_LINE DEDENT return min ( A [ n - 1 ] , findMinRec ( A , n - 1 ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 4 , 45 , 6 , - 50 , 10 , 2 ] NEW_LINE n = len ( A ) NEW_LINE print ( findMinRec ( A , n ) ) NEW_LINE DEDENT
Allocate minimum number of pages | Utility function to check if current minimum value is feasible or not . ; iterate over all books ; check if current number of pages are greater than curr_min that means we will get the result after mid no . of pages ; count how many students are required to distribute curr_min pages ; increment student count ; update curr_sum ; if students required becomes greater than given no . of students , return False ; else update curr_sum ; function to find minimum pages ; return - 1 if no . of books is less than no . of students ; Count total number of pages ; initialize start as 0 pages and end as total pages ; traverse until start <= end ; check if it is possible to distribute books by using mid as current minimum ; update result to current distribution as it 's the best we have found till now. ; as we are finding minimum and books are sorted so reduce end = mid - 1 that means ; if not possible means pages should be increased so update start = mid + 1 ; at - last return minimum no . of pages ; Number of pages in books ; m = 2 No . of students
def isPossible ( arr , n , m , curr_min ) : NEW_LINE INDENT studentsRequired = 1 NEW_LINE curr_sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] > curr_min ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( curr_sum + arr [ i ] > curr_min ) : NEW_LINE INDENT studentsRequired += 1 NEW_LINE curr_sum = arr [ i ] NEW_LINE if ( studentsRequired > m ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT curr_sum += arr [ i ] NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def findPages ( arr , n , m ) : NEW_LINE INDENT sum = 0 NEW_LINE if ( n < m ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT start , end = 0 , sum NEW_LINE result = 10 ** 9 NEW_LINE while ( start <= end ) : NEW_LINE INDENT mid = ( start + end ) // 2 NEW_LINE if ( isPossible ( arr , n , m , mid ) ) : NEW_LINE INDENT result = mid NEW_LINE end = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT start = mid + 1 NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT arr = [ 12 , 34 , 67 , 90 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Minimum ▁ number ▁ of ▁ pages ▁ = ▁ " , findPages ( arr , n , m ) ) NEW_LINE
Find Minimum Depth of a Binary Tree | Tree node ; Function to calculate the minimum depth of the tree ; Corner Case . Should never be hit unless the code is called on root = NULL ; Base Case : Leaf node . This acoounts for height = 1 ; If left subtree is Null , recur for right subtree ; If right subtree is Null , recur for left subtree ; Let us construct the Tree shown in the above figure
class Node : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . data = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def minDepth ( root ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return 0 NEW_LINE DEDENT if root . left is None and root . right is None : NEW_LINE INDENT return 1 NEW_LINE DEDENT if root . left is None : NEW_LINE INDENT return minDepth ( root . right ) + 1 NEW_LINE DEDENT if root . right is None : NEW_LINE INDENT return minDepth ( root . left ) + 1 NEW_LINE DEDENT return min ( minDepth ( root . left ) , minDepth ( root . right ) ) + 1 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 print minDepth ( root ) NEW_LINE
Remove an occurrence of most frequent array element exactly K times | Function to print the most frequent array element exactly K times ; Stores frequency array element ; Count frequency of array element ; Maximum array element ; Traverse the Map ; Find the element with maximum frequency ; If the frequency is maximum , store that number in element ; Print element as it contains the element having highest frequency ; Decrease the frequency of the maximum array element ; Reduce the number of operations ; Given array ; Size of the array ; Given K
def maxFreqElements ( arr , N , K ) : NEW_LINE INDENT mp = { } NEW_LINE for i in range ( N ) : NEW_LINE INDENT if arr [ i ] in mp : NEW_LINE INDENT mp [ arr [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ arr [ i ] ] = 1 NEW_LINE DEDENT DEDENT while ( K > 0 ) : NEW_LINE INDENT Max = 0 NEW_LINE for i in mp : NEW_LINE INDENT if ( mp [ i ] > Max ) : NEW_LINE INDENT Max = mp [ i ] NEW_LINE element = i NEW_LINE DEDENT DEDENT print ( element , end = " ▁ " ) NEW_LINE if element in mp : NEW_LINE INDENT mp [ element ] -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ element ] = - 1 NEW_LINE DEDENT K -= 1 NEW_LINE DEDENT DEDENT arr = [ 1 , 3 , 2 , 1 , 4 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE K = 2 NEW_LINE maxFreqElements ( arr , N , K ) NEW_LINE
Maximize difference between maximum and minimum array elements after K operations | Function to find the maximum difference between the maximum and minimum in the array after K operations ; Stores maximum difference between largest and smallest array element ; Sort the array in descending order ; Traverse the array arr [ ] ; Update maxDiff ; Driver Code
def maxDiffLargSmallOper ( arr , N , K ) : NEW_LINE INDENT maxDiff = 0 ; NEW_LINE arr . sort ( reverse = True ) ; NEW_LINE for i in range ( min ( K + 1 , N ) ) : NEW_LINE INDENT maxDiff += arr [ i ] ; NEW_LINE DEDENT return maxDiff ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 7 , 7 , 7 , 7 ] ; NEW_LINE N = len ( arr ) NEW_LINE K = 1 ; NEW_LINE print ( maxDiffLargSmallOper ( arr , N , K ) ) ; NEW_LINE DEDENT
Minimize cost to convert all characters of a binary string to 0 s | Function to get the minimum Cost to convert all characters of given string to 0 s ; Stores the range of indexes of characters that need to be flipped ; Stores the number of times current character is flipped ; Stores minimum cost to get the required string ; Traverse the given string ; Remove all value from pq whose value is less than i ; Update flip ; Get the current number ; If current character is flipped odd times ; If current character contains non - zero value ; Update flip ; Update cost ; Append R [ i ] into pq ; Driver code ; Function call
def minCost ( s , R , C , N ) : NEW_LINE INDENT ch = list ( s ) NEW_LINE pq = [ ] NEW_LINE flip = 0 NEW_LINE cost = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT while ( len ( pq ) > 0 and pq [ 0 ] < i ) : NEW_LINE INDENT pq . pop ( 0 ) ; NEW_LINE flip -= 1 NEW_LINE DEDENT cn = ord ( ch [ i ] ) - ord ( '0' ) NEW_LINE if ( flip % 2 == 1 ) : NEW_LINE INDENT cn = 1 - cn NEW_LINE DEDENT if ( cn == 1 ) : NEW_LINE INDENT flip += 1 NEW_LINE cost += C [ i ] NEW_LINE pq . append ( R [ i ] ) NEW_LINE DEDENT DEDENT return cost NEW_LINE DEDENT N = 4 NEW_LINE s = "1010" NEW_LINE R = [ 1 , 2 , 2 , 3 ] NEW_LINE C = [ 3 , 1 , 2 , 3 ] NEW_LINE print ( minCost ( s , R , C , N ) ) NEW_LINE
Find Minimum Depth of a Binary Tree | A Binary Tree node ; Iterative method to find minimum depth of Binary Tree ; Corner Case ; Create an empty queue for level order traversal ; Enqueue root and initialize depth as 1 ; Do level order traversal ; Remove the front queue item ; Get details of the removed item ; If this is the first leaf node seen so far then return its depth as answer ; If left subtree is not None , add it to queue ; if right subtree is not None , add it to queue ; Lets construct a binary tree shown in above diagram
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 minDepth ( root ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return 0 NEW_LINE DEDENT q = [ ] NEW_LINE q . append ( { ' node ' : root , ' depth ' : 1 } ) NEW_LINE while ( len ( q ) > 0 ) : NEW_LINE INDENT queueItem = q . pop ( 0 ) NEW_LINE node = queueItem [ ' node ' ] NEW_LINE depth = queueItem [ ' depth ' ] NEW_LINE if node . left is None and node . right is None : NEW_LINE INDENT return depth NEW_LINE DEDENT if node . left is not None : NEW_LINE INDENT q . append ( { ' node ' : node . left , ' depth ' : depth + 1 } ) NEW_LINE DEDENT if node . right is not None : NEW_LINE INDENT q . append ( { ' node ' : node . right , ' depth ' : depth + 1 } ) NEW_LINE DEDENT DEDENT 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 print minDepth ( root ) NEW_LINE
Maximum Manhattan distance between a distinct pair from N coordinates | Function to calculate the maximum Manhattan distance ; List to store maximum and minimum of all the four forms ; Sorting both the vectors ; Driver code ; Given Co - ordinates ; Function call
def MaxDist ( A , N ) : NEW_LINE INDENT V = [ 0 for i in range ( N ) ] NEW_LINE V1 = [ 0 for i in range ( N ) ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT V [ i ] = A [ i ] [ 0 ] + A [ i ] [ 1 ] NEW_LINE V1 [ i ] = A [ i ] [ 0 ] - A [ i ] [ 1 ] NEW_LINE DEDENT V . sort ( ) NEW_LINE V1 . sort ( ) NEW_LINE maximum = max ( V [ - 1 ] - V [ 0 ] , V1 [ - 1 ] - V1 [ 0 ] ) NEW_LINE print ( maximum ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 3 NEW_LINE A = [ [ 1 , 2 ] , [ 2 , 3 ] , [ 3 , 4 ] ] NEW_LINE MaxDist ( A , N ) NEW_LINE DEDENT
Maximum distance between two points in coordinate plane using Rotating Caliper 's Method |  ; Function calculates distance between two points ; Function to find the maximum distance between any two points ; Iterate over all possible pairs ; Update maxm ; Return actual distance ; Driver Code ; Number of points ; Given points ; Function Call
from math import sqrt NEW_LINE def dist ( p1 , p2 ) : NEW_LINE INDENT x0 = p1 [ 0 ] - p2 [ 0 ] NEW_LINE y0 = p1 [ 1 ] - p2 [ 1 ] NEW_LINE return x0 * x0 + y0 * y0 NEW_LINE DEDENT def maxDist ( p ) : NEW_LINE INDENT n = len ( p ) NEW_LINE maxm = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT maxm = max ( maxm , dist ( p [ i ] , p [ j ] ) ) NEW_LINE DEDENT DEDENT return sqrt ( maxm ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE p = [ ] NEW_LINE p . append ( [ 4 , 0 ] ) NEW_LINE p . append ( [ 0 , 2 ] ) NEW_LINE p . append ( [ - 1 , - 7 ] ) NEW_LINE p . append ( [ 1 , 10 ] ) NEW_LINE p . append ( [ 2 , - 3 ] ) NEW_LINE print ( maxDist ( p ) ) NEW_LINE DEDENT
Reorder an array such that sum of left half is not equal to sum of right half | Function to print the required reordering of the array if possible ; Sort the array in increasing order ; If all elements are equal , then it is not possible ; Else print the sorted array arr [ ] ; Driver Code ; Given array ; Function Call
def printArr ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE if ( arr [ 0 ] == arr [ n - 1 ] ) : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Yes " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 2 , 1 , 3 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE printArr ( arr , N ) NEW_LINE DEDENT
Maximize Array sum by swapping at most K elements with another array | Function to find the maximum sum ; If element of array a is smaller than that of array b , swap them . ; Find sum of resultant array ; Driver code
def maximumSum ( a , b , k , n ) : NEW_LINE INDENT a . sort ( ) NEW_LINE b . sort ( ) NEW_LINE i = 0 NEW_LINE j = n - 1 NEW_LINE while i < k : NEW_LINE INDENT if ( a [ i ] < b [ j ] ) : NEW_LINE INDENT a [ i ] , b [ j ] = b [ j ] , a [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT i += 1 NEW_LINE j -= 1 NEW_LINE DEDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += a [ i ] NEW_LINE DEDENT print ( sum ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT K = 1 NEW_LINE A = [ 2 , 3 , 4 ] NEW_LINE B = [ 6 , 8 , 5 ] NEW_LINE N = len ( A ) NEW_LINE maximumSum ( A , B , K , N ) NEW_LINE DEDENT
Maximize distinct elements by incrementing / decrementing an element or keeping it same | Function that Maximize the count of distinct element ; Sort thr list ; Keeping track of previous change ; Decrement is possible ; Remain as it is ; Increment is possible ; Driver Code
def max_dist_ele ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE ans = 0 NEW_LINE prev = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if prev < ( arr [ i ] - 1 ) : NEW_LINE INDENT ans += 1 ; NEW_LINE prev = arr [ i ] - 1 NEW_LINE DEDENT elif prev < ( arr [ i ] ) : NEW_LINE INDENT ans += 1 NEW_LINE prev = arr [ i ] NEW_LINE DEDENT elif prev < ( arr [ i ] + 1 ) : NEW_LINE INDENT ans += 1 NEW_LINE prev = arr [ i ] + 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 1 , 1 , 1 , 8 , 8 , 8 , 9 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE print ( max_dist_ele ( arr , n ) ) NEW_LINE
Find if array can be sorted by swaps limited to multiples of k | CheckSort function To check if array can be sorted ; sortarr is sorted array of arr ; if k = 1 then ( always possible to sort ) swapping can easily give sorted array ; comparing sortarray with array ; element at index j must be in j = i + l * k form where i = 0 , 1 , 2 , 3. . . where l = 0 , 1 , 2 , 3 , . . n - 1 ; if element is present then swapped ; if element of sorted array does not found in its sequence then flag remain zero that means arr can not be sort after swapping ; if flag is 0 Not possible else Possible ; Driver code ; size of step ; array initialized ; length of arr ; calling function
def CheckSort ( arr , k , n ) : NEW_LINE INDENT sortarr = sorted ( arr ) NEW_LINE if ( k == 1 ) : NEW_LINE INDENT print ( " yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT flag = 0 NEW_LINE for j in range ( i , n , k ) : NEW_LINE INDENT if ( sortarr [ i ] == arr [ j ] ) : NEW_LINE INDENT arr [ i ] , arr [ j ] = arr [ j ] , arr [ i ] NEW_LINE flag = 1 NEW_LINE break NEW_LINE DEDENT if ( j + k >= n ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT print ( " Not ▁ possible ▁ to ▁ sort " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Possible ▁ to ▁ sort " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT k = 3 NEW_LINE arr = [ 1 , 5 , 6 , 9 , 2 , 3 , 5 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE CheckSort ( arr , k , n ) NEW_LINE DEDENT
Replace node with depth in a binary tree | A tree node structure ; Helper function replaces the data with depth Note : Default value of level is 0 for root . ; Base Case ; Replace data with current depth ; A utility function to prinorder traversal of a Binary Tree ; Driver Code ; Constructing tree given in the above figure
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 replaceNode ( node , level = 0 ) : NEW_LINE INDENT if ( node == None ) : NEW_LINE INDENT return NEW_LINE DEDENT node . data = level NEW_LINE replaceNode ( node . left , level + 1 ) NEW_LINE replaceNode ( node . right , level + 1 ) NEW_LINE DEDENT def printInorder ( node ) : NEW_LINE INDENT if ( node == None ) : NEW_LINE INDENT return NEW_LINE DEDENT printInorder ( node . left ) NEW_LINE print ( node . data , end = " ▁ " ) NEW_LINE printInorder ( node . right ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 3 ) NEW_LINE root . left = newNode ( 2 ) NEW_LINE root . right = newNode ( 5 ) NEW_LINE root . left . left = newNode ( 1 ) NEW_LINE root . left . right = newNode ( 4 ) NEW_LINE print ( " Before ▁ Replacing ▁ Nodes " ) NEW_LINE printInorder ( root ) NEW_LINE replaceNode ( root ) NEW_LINE print ( ) NEW_LINE print ( " After ▁ Replacing ▁ Nodes " ) NEW_LINE printInorder ( root ) NEW_LINE DEDENT
Product of minimum edge weight between all pairs of a Tree | Python3 implementation of the approach ; Function to return ( x ^ y ) mod p ; Declaring size array globally ; Initializing DSU data structure ; Function to find the root of ith node in the disjoint set ; Weighted union using Path Compression ; size of set A is small than size of set B ; size of set B is small than size of set A ; Function to add an edge in the tree ; Build the tree ; Function to return the required product ; Sorting the edges with respect to its weight ; Start iterating in decreasing order of weight ; Determine Current edge values ; Calculate root of each node and size of each set ; Using the formula ; Calculating final result ; Weighted union using Path Compression ; Driver Code
mod = 1000000007 NEW_LINE def power ( x : int , y : int , p : int ) -> int : NEW_LINE INDENT res = 1 NEW_LINE x %= p NEW_LINE while y > 0 : NEW_LINE INDENT if y & 1 : NEW_LINE INDENT res = ( res * x ) % p NEW_LINE DEDENT y = y // 2 NEW_LINE x = ( x * x ) % p NEW_LINE DEDENT return res NEW_LINE DEDENT size = [ 0 ] * 300005 NEW_LINE freq = [ 0 ] * 300004 NEW_LINE edges = [ ] NEW_LINE def initialize ( arr : list , N : int ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT arr [ i ] = i NEW_LINE size [ i ] = 1 NEW_LINE DEDENT DEDENT def root ( arr : list , i : int ) -> int : NEW_LINE INDENT while arr [ i ] != i : NEW_LINE INDENT i = arr [ i ] NEW_LINE DEDENT return i NEW_LINE DEDENT def weighted_union ( arr : list , size : list , A : int , B : int ) : NEW_LINE INDENT root_A = root ( arr , A ) NEW_LINE root_B = root ( arr , B ) NEW_LINE if size [ root_A ] < size [ root_B ] : NEW_LINE INDENT arr [ root_A ] = arr [ root_B ] NEW_LINE size [ root_B ] += size [ root_A ] NEW_LINE DEDENT else : NEW_LINE INDENT arr [ root_B ] = arr [ root_A ] NEW_LINE size [ root_A ] += size [ root_B ] NEW_LINE DEDENT DEDENT def AddEdge ( a : int , b : int , w : int ) : NEW_LINE INDENT edges . append ( ( w , ( a , b ) ) ) NEW_LINE DEDENT def makeTree ( ) : NEW_LINE INDENT AddEdge ( 1 , 2 , 1 ) NEW_LINE AddEdge ( 1 , 3 , 3 ) NEW_LINE AddEdge ( 3 , 4 , 2 ) NEW_LINE DEDENT def minProduct ( ) -> int : NEW_LINE INDENT result = 1 NEW_LINE edges . sort ( key = lambda a : a [ 0 ] ) NEW_LINE for i in range ( len ( edges ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT curr_weight = edges [ i ] [ 0 ] NEW_LINE node1 = edges [ i ] [ 1 ] [ 0 ] NEW_LINE node2 = edges [ i ] [ 1 ] [ 1 ] NEW_LINE root1 = root ( freq , node1 ) NEW_LINE set1_size = size [ root1 ] NEW_LINE root2 = root ( freq , node2 ) NEW_LINE set2_size = size [ root2 ] NEW_LINE prod = set1_size * set2_size NEW_LINE product = power ( curr_weight , prod , mod ) NEW_LINE result = ( ( result % mod ) * ( product % mod ) ) % mod NEW_LINE weighted_union ( freq , size , node1 , node2 ) NEW_LINE DEDENT return result % mod NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 NEW_LINE initialize ( freq , n ) NEW_LINE makeTree ( ) NEW_LINE print ( minProduct ( ) ) NEW_LINE DEDENT