codeparrot/xlcost-text-to-code huggingface dataset:
ZigZag Level Order Traversal of an N | Structure of a tree node ; Function to create a new node ; Function to perform zig zag traversal of the given tree ; Stores the vectors containing nodes in each level of tree respectively ; Create a queue for BFS ; Enqueue Root of the tree ; Standard Level Order Traversal code using queue ; Stores the element in the current level ; Iterate over all nodes of the current level ; Insert all children of the current node into the queue ; Insert curLevel into result ; Loop to Print the ZigZag Level order Traversal of the given tree ; If i + 1 is even reverse the order of nodes in the current level ; Print the node of ith level ; Function Call
class Node : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . val = key NEW_LINE self . child = [ ] NEW_LINE DEDENT DEDENT def newNode ( key ) : NEW_LINE INDENT temp = Node ( key ) NEW_LINE return temp NEW_LINE DEDENT def zigzagLevelOrder ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT result = [ ] NEW_LINE q = [ ] NEW_LINE q . append ( root ) NEW_LINE while len ( q ) > 0 : NEW_LINE INDENT size = len ( q ) NEW_LINE curLevel = [ ] NEW_LINE for i in range ( size ) : NEW_LINE INDENT node = q [ 0 ] NEW_LINE q . pop ( 0 ) NEW_LINE curLevel . append ( node . val ) NEW_LINE for j in range ( len ( node . child ) ) : NEW_LINE INDENT q . append ( node . child [ j ] ) NEW_LINE DEDENT DEDENT result . append ( curLevel ) NEW_LINE DEDENT for i in range ( len ( result ) ) : NEW_LINE INDENT if ( ( i + 1 ) % 2 == 0 ) : NEW_LINE INDENT result [ i ] . reverse ( ) NEW_LINE DEDENT for j in range ( len ( result [ i ] ) ) : NEW_LINE INDENT print ( result [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT root = newNode ( 1 ) NEW_LINE ( root . child ) . append ( newNode ( 2 ) ) NEW_LINE ( root . child ) . append ( newNode ( 3 ) ) NEW_LINE ( root . child [ 0 ] . child ) . append ( newNode ( 4 ) ) NEW_LINE ( root . child [ 0 ] . child ) . append ( newNode ( 5 ) ) NEW_LINE ( root . child [ 1 ] . child ) . append ( newNode ( 6 ) ) NEW_LINE ( root . child [ 1 ] ) . child . append ( newNode ( 7 ) ) NEW_LINE ( root . child [ 1 ] . child ) . append ( newNode ( 8 ) ) NEW_LINE zigzagLevelOrder ( root ) NEW_LINE
Minimum length paths between 1 to N including each node | Function to calculate the distances from node 1 to N ; Vector to store our edges ; Storing the edgees in the Vector ; Initialize queue ; BFS from first node using queue ; Pop from queue ; Traversing its adjacency list ; Initialize queue ; BFS from last node using queue ; Pop from queue ; Traversing its adjacency list ; Printing the minimum distance including node i ; If not reachable ; Path exists ; Driver Code ; Given Input ; Function Call
def minDisIncludingNode ( n , m , edges ) : NEW_LINE INDENT g = [ [ ] for i in range ( 10005 ) ] NEW_LINE for i in range ( m ) : NEW_LINE INDENT a = edges [ i ] [ 0 ] - 1 NEW_LINE b = edges [ i ] [ 1 ] - 1 NEW_LINE g [ a ] . append ( b ) NEW_LINE g [ b ] . append ( a ) NEW_LINE DEDENT q = [ ] NEW_LINE q . append ( [ 0 , 0 ] ) NEW_LINE dist = [ 1e9 for i in range ( n ) ] NEW_LINE dist [ 0 ] = 0 NEW_LINE while ( len ( q ) > 0 ) : NEW_LINE INDENT up = q [ 0 ] NEW_LINE q = q [ 1 : ] NEW_LINE x = up [ 0 ] NEW_LINE lev = up [ 1 ] NEW_LINE if ( lev > dist [ x ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( x == n - 1 ) : NEW_LINE INDENT continue NEW_LINE DEDENT for y in g [ x ] : NEW_LINE INDENT if ( dist [ y ] > lev + 1 ) : NEW_LINE INDENT dist [ y ] = lev + 1 NEW_LINE q . append ( [ y , lev + 1 ] ) NEW_LINE DEDENT DEDENT DEDENT q1 = [ ] NEW_LINE q1 . append ( [ n - 1 , 0 ] ) NEW_LINE dist1 = [ 1e9 for i in range ( n ) ] NEW_LINE dist1 [ n - 1 ] = 0 NEW_LINE while ( len ( q1 ) > 0 ) : NEW_LINE INDENT up = q1 [ 0 ] NEW_LINE q1 = q1 [ 1 : ] NEW_LINE x = up [ 0 ] NEW_LINE lev = up [ 1 ] NEW_LINE if ( lev > dist1 [ x ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( x == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT for y in g [ x ] : NEW_LINE INDENT if ( dist1 [ y ] > lev + 1 ) : NEW_LINE INDENT dist1 [ y ] = lev + 1 NEW_LINE q1 . append ( [ y , lev + 1 ] ) NEW_LINE DEDENT DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if ( dist [ i ] + dist1 [ i ] > 1e9 ) : NEW_LINE INDENT print ( - 1 , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( dist [ i ] + dist1 [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE m = 7 NEW_LINE edges = [ [ 1 , 2 ] , [ 1 , 4 ] , [ 2 , 3 ] , [ 2 , 5 ] , [ 4 , 3 ] , [ 4 , 5 ] , [ 1 , 5 ] ] NEW_LINE minDisIncludingNode ( n , m , edges ) NEW_LINE DEDENT
Check if possible to make Array sum equal to Array product by replacing exactly one element | Function to check if it is possible to form an array whose sum and the product is the same or not ; Find the sum of the array ;  ; Find the product of the array ; Check a complete integer y for every x ; If got such y ; If no such y exist ; Driver Code
def canPossibleReplacement ( N , arr ) : NEW_LINE INDENT S = sum ( arr ) NEW_LINE DEDENT / * Iterate through all elements and NEW_LINE INDENT add them to sum * / NEW_LINE P = 1 NEW_LINE for i in arr : NEW_LINE INDENT P *= i NEW_LINE DEDENT for x in arr : NEW_LINE INDENT y = ( S - x ) // ( P / x - 1 ) NEW_LINE if ( S - x + y ) == ( P * y ) / x : NEW_LINE INDENT return ' Yes ' NEW_LINE DEDENT DEDENT return ' No ' NEW_LINE DEDENT N , arr = 3 , [ 1 , 3 , 4 ] NEW_LINE print ( canPossibleReplacement ( N , arr ) ) NEW_LINE
Check if all the digits of the given number are same | Python3 program for the above approach ; Function to check if all the digits in the number N is the same or not ; Get the length of N ; Form the number M of the type K * 111. . . where K is the rightmost digit of N ; Check if the numbers are equal ; Otherwise ; Driver Code
import math NEW_LINE def checkSameDigits ( N ) : NEW_LINE INDENT length = int ( math . log10 ( N ) ) + 1 ; NEW_LINE M = ( int ( math . pow ( 10 , length ) ) - 1 ) // ( 10 - 1 ) ; NEW_LINE M *= N % 10 ; NEW_LINE if ( M == N ) : NEW_LINE INDENT return " Yes " ; NEW_LINE DEDENT return " No " ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 222 ; NEW_LINE print ( checkSameDigits ( N ) ) ; NEW_LINE DEDENT
Maximize sum of path from the Root to a Leaf node in N | Stores the maximum sum of a path ; Structure of a node in the tree ; Utility function to create a new node in the tree ; Recursive function to calculate the maximum sum in a path using DFS ; If current node is a leaf node ; Traversing all children of the current node ; Recursive call for all the children nodes ; Given Generic Tree ; Function Call
maxSumPath = 0 NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . val = key NEW_LINE self . child = [ ] NEW_LINE DEDENT DEDENT def newNode ( key ) : NEW_LINE INDENT temp = Node ( key ) NEW_LINE return temp NEW_LINE DEDENT def DFS ( root , Sum ) : NEW_LINE INDENT global maxSumPath NEW_LINE if ( len ( root . child ) == 0 ) : NEW_LINE INDENT maxSumPath = max ( maxSumPath , Sum ) NEW_LINE return NEW_LINE DEDENT for i in range ( len ( root . child ) ) : NEW_LINE INDENT DFS ( root . child [ i ] , Sum + root . child [ i ] . val ) NEW_LINE DEDENT DEDENT root = newNode ( 1 ) NEW_LINE ( root . child ) . append ( newNode ( 2 ) ) NEW_LINE ( root . child ) . append ( newNode ( 3 ) ) NEW_LINE ( root . child [ 0 ] . child ) . append ( newNode ( 4 ) ) NEW_LINE ( root . child [ 1 ] . child ) . append ( newNode ( 6 ) ) NEW_LINE ( root . child [ 0 ] . child ) . append ( newNode ( 5 ) ) NEW_LINE ( root . child [ 1 ] ) . child . append ( newNode ( 7 ) ) NEW_LINE ( root . child [ 1 ] . child ) . append ( newNode ( 8 ) ) NEW_LINE DFS ( root , root . val ) NEW_LINE print ( maxSumPath ) NEW_LINE
Count of even sum triplets in the array for Q range queries | Function to count number of triplets with even sum in range l , r for each query ; Initialization of array ; Initialization of variables ; Traversing array ; If element is odd ; If element is even ; Storing count of even and odd till each i ; Traversing each query ; Count of odd numbers in l to r ; Count of even numbers in l to r ; Finding the ans ; Printing the ans ; Driver Code ; Given Input ; Function Call
def countTriplets ( size , queries , arr , Q ) : NEW_LINE INDENT arr_even = [ 0 for i in range ( size + 1 ) ] NEW_LINE arr_odd = [ 0 for i in range ( size + 1 ) ] NEW_LINE even = 0 NEW_LINE odd = 0 NEW_LINE arr_even [ 0 ] = 0 NEW_LINE arr_odd [ 0 ] = 0 NEW_LINE for i in range ( size ) : NEW_LINE INDENT if ( arr [ i ] % 2 ) : NEW_LINE INDENT odd += 1 NEW_LINE DEDENT else : NEW_LINE INDENT even += 1 NEW_LINE DEDENT arr_even [ i + 1 ] = even NEW_LINE arr_odd [ i + 1 ] = odd NEW_LINE DEDENT for i in range ( queries ) : NEW_LINE INDENT l = Q [ i ] [ 0 ] NEW_LINE r = Q [ i ] [ 1 ] NEW_LINE odd = arr_odd [ r ] - arr_odd [ l - 1 ] NEW_LINE even = arr_even [ r ] - arr_even [ l - 1 ] NEW_LINE ans = ( even * ( even - 1 ) * ( even - 2 ) ) // 6 + ( odd * ( odd - 1 ) // 2 ) * even NEW_LINE print ( ans , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 6 NEW_LINE Q = 2 NEW_LINE arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] NEW_LINE queries = [ [ 1 , 3 ] , [ 2 , 5 ] ] NEW_LINE countTriplets ( N , Q , arr , queries ) NEW_LINE DEDENT
Maximize the rightmost element of an array in k operations in Linear Time | Function to calculate maximum value of Rightmost element ; Calculating maximum value of Rightmost element ; Checking if arr [ i ] is operationable ; Performing operation of i - th element ; Decreasing the value of k by 1 ; Printing rightmost element ; Given Input ; Function Call
def maxRightmostElement ( N , k , p , arr ) : NEW_LINE INDENT while ( k ) : NEW_LINE INDENT for i in range ( N - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( arr [ i ] >= p ) : NEW_LINE INDENT arr [ i ] = arr [ i ] - p NEW_LINE arr [ i + 1 ] = arr [ i + 1 ] + p NEW_LINE break NEW_LINE DEDENT DEDENT k = k - 1 NEW_LINE DEDENT print ( arr [ N - 1 ] ) NEW_LINE DEDENT N = 4 NEW_LINE p = 2 NEW_LINE k = 5 NEW_LINE arr = [ 3 , 8 , 1 , 4 ] NEW_LINE maxRightmostElement ( N , k , p , arr ) NEW_LINE
Mean of minimum of all possible K | Function to find the value of nCr ; Base Case ; Find nCr recursively ; Function to find the expected minimum values of all the subsets of size K ; Find the factorials that will be used later ; Find the factorials ; Total number of subsets ; Stores the sum of minimum over all possible subsets ; Iterate over all possible minimum ; Find the mean over all subsets ; Driver Code
def nCr ( n , r , f ) : NEW_LINE INDENT if ( n < r ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return f [ n ] / ( f [ r ] * f [ n - r ] ) NEW_LINE DEDENT def findMean ( N , X ) : NEW_LINE INDENT f = [ 0 for i in range ( N + 1 ) ] NEW_LINE f [ 0 ] = 1 NEW_LINE for i in range ( 1 , N + 1 , 1 ) : NEW_LINE INDENT f [ i ] = f [ i - 1 ] * i NEW_LINE DEDENT total = nCr ( N , X , f ) NEW_LINE count = 0 NEW_LINE for i in range ( 1 , N + 1 , 1 ) : NEW_LINE INDENT count += nCr ( N - i , X - 1 , f ) * i NEW_LINE DEDENT E_X = ( count ) / ( total ) NEW_LINE print ( " { 0 : . 9f } " . format ( E_X ) ) NEW_LINE return 0 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE X = 2 NEW_LINE findMean ( N , X ) NEW_LINE DEDENT
Maximize count of odd | To find maximum number of pairs in array with conversion of at most one element ; Initialize count of even elements ; Initialize count of odd elements ; If current number is even then increment x by 1 ; If current number is odd then increment y by 1 ; Initialize the answer by min ( x , y ) ; If difference in count of odd and even is more than 2 than increment answer ; Return final answer ; Driver code ; Given array
def maximumNumberofpairs ( n , arr ) : NEW_LINE INDENT x = 0 NEW_LINE y = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] % 2 == 0 ) : NEW_LINE INDENT x += 1 NEW_LINE DEDENT else : NEW_LINE INDENT y += 1 NEW_LINE DEDENT DEDENT answer = min ( x , y ) NEW_LINE if ( abs ( x - y ) >= 2 ) : NEW_LINE INDENT answer += 1 NEW_LINE DEDENT return answer NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 4 , 6 , 5 , 10 , 12 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maximumNumberofpairs ( n , arr ) ) NEW_LINE DEDENT
Sum of product of all unordered pairs in given range with update queries | Function to calculate the Pairwise Product Sum in range from L to R ; Loop to iterate over all possible pairs from L to R ; Print answer ; Function to update the Array element at index P to X ; Update the value at Pth index in the array ; Function to solve Q queries ; If Query is of type 1 ; If Query is of type 2 ; Driver Code
def pairwiseProductSum ( a , l , r ) : NEW_LINE INDENT sum = 0 NEW_LINE for j in range ( l - 1 , r , 1 ) : NEW_LINE INDENT for k in range ( j + 1 , r , 1 ) : NEW_LINE INDENT sum += ( a [ j ] * a [ k ] ) ; NEW_LINE DEDENT DEDENT print ( sum ) NEW_LINE DEDENT def updateArray ( a , p , x ) : NEW_LINE INDENT a [ p - 1 ] = x NEW_LINE DEDENT def solveQueries ( a , n , Q , query ) : NEW_LINE INDENT for i in range ( Q ) : NEW_LINE INDENT if ( query [ i ] [ 0 ] == 1 ) : NEW_LINE INDENT pairwiseProductSum ( a , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT updateArray ( a , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 5 , 7 , 2 , 3 , 1 ] NEW_LINE N = len ( A ) NEW_LINE Q = 3 NEW_LINE query = [ [ 1 , 1 , 3 ] , [ 2 , 2 , 5 ] , [ 1 , 2 , 5 ] ] NEW_LINE solveQueries ( A , N , Q , query ) NEW_LINE DEDENT
Check if final remainder is present in original Array by reducing it based on given conditions | Python program to implement above approach ; copying original array ; loop till length of array become 2. ; find middle element ; pop element from front and rear ; find remainder ; append remainder to a ; now since length of array is 2 take product and divide it by n ; if remainder is present is original array return 1 , else return 0 ; calling function Reduced ; if x = 1 print YES else NO
def Reduced ( a , n ) : NEW_LINE INDENT original_array = a [ : ] NEW_LINE while len ( a ) != 2 : NEW_LINE INDENT mid = len ( a ) // 2 NEW_LINE mid_ele = a [ mid ] NEW_LINE start = a . pop ( 0 ) NEW_LINE end = a . pop ( ) NEW_LINE rmd = ( start * end ) % mid_ele NEW_LINE a . append ( rmd ) NEW_LINE DEDENT remainder = ( a [ 0 ] * a [ 1 ] ) % n NEW_LINE if remainder in original_array : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT Arr = [ 2 , 3 , 4 , 8 , 5 , 7 ] NEW_LINE N = len ( Arr ) NEW_LINE x = Reduced ( Arr , N ) NEW_LINE if x : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT
Find two numbers from their sum and OR | Python 3 program for the above approach ; Function to find the two integers from the given sum and Bitwise OR value ; Check if Z is non negative ; Iterate through all the bits ; Find the kth bit of A & B ; Find the kth bit of A | B ; If bit1 = 1 and bit2 = 0 , then there will be no possible pairs ; Print the possible pairs ; Driver Code
MaxBit = 32 NEW_LINE def possiblePair ( X , Y ) : NEW_LINE INDENT Z = Y - X NEW_LINE if ( Z < 0 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return 0 NEW_LINE DEDENT for k in range ( MaxBit ) : NEW_LINE INDENT bit1 = ( Z >> k ) & 1 NEW_LINE bit2 = ( Z >> k ) & 1 NEW_LINE if ( bit1 == 1 and bit2 == 0 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return 0 NEW_LINE DEDENT DEDENT print ( Z , X ) NEW_LINE return 0 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = 7 NEW_LINE Y = 11 NEW_LINE possiblePair ( X , Y ) NEW_LINE DEDENT
Maximum frequencies in each M | Function to find the frequency of the most common element in each M length subarrays ; Stores frequency of array element ; Stores the maximum frequency ; Iterate for the first sub - array and store the maximum ; Print the maximum frequency for the first subarray ; Iterate over the range [ M , N ] ; Subtract the A [ i - M ] and add the A [ i ] in the map ; Find the maximum frequency ; Print the maximum frequency for the current subarray ; Driver Code
def maxFrequencySubarrayUtil ( A , N , M ) : NEW_LINE INDENT i = 0 NEW_LINE m = { } NEW_LINE val = 0 NEW_LINE while ( i < M ) : NEW_LINE INDENT if A [ i ] in m : NEW_LINE INDENT m [ A [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT m [ A [ i ] ] = 1 NEW_LINE DEDENT val = max ( val , m [ A [ i ] ] ) NEW_LINE i += 1 NEW_LINE DEDENT print ( val , end = " ▁ " ) NEW_LINE for i in range ( M , N , 1 ) : NEW_LINE INDENT if A [ i - M ] in m : NEW_LINE INDENT m [ A [ i - M ] ] -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT m [ A [ i - M ] ] = 0 NEW_LINE DEDENT if A [ i ] in m : NEW_LINE INDENT m [ A [ i ] ] += 1 NEW_LINE DEDENT val = 0 NEW_LINE for key , value in m . items ( ) : NEW_LINE INDENT val = max ( val , value ) NEW_LINE DEDENT print ( val , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 1 , 2 , 2 , 3 , 5 ] NEW_LINE N = len ( A ) NEW_LINE M = 4 NEW_LINE maxFrequencySubarrayUtil ( A , N , M ) NEW_LINE DEDENT
Maximum number of pairs of distinct array elements possible by including each element in only one pair | Function to count the maximum number of pairs having different element from the given array ; Stores the frequency of array element ; Stores maximum count of pairs ; Increasing the frequency of every element ; Stores the frequencies of array element from highest to lowest ; Pushing the frequencies to the priority queue ; Iterate until size of PQ > 1 ; Stores the top two element ; Form the pair between the top two pairs ; Decrement the frequencies ; Insert updated frequencies if it is greater than 0 ; Return the total count of resultant pairs ; Driver Code
def maximumPairs ( a , n ) : NEW_LINE INDENT freq = { } NEW_LINE count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if a [ i ] in freq : NEW_LINE INDENT freq [ a [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT freq [ a [ i ] ] = 1 NEW_LINE DEDENT DEDENT pq = [ ] NEW_LINE for key , value in freq . items ( ) : NEW_LINE INDENT pq . append ( value ) NEW_LINE DEDENT pq . sort ( ) NEW_LINE while ( len ( pq ) > 1 ) : NEW_LINE INDENT freq1 = pq [ len ( pq ) - 1 ] NEW_LINE pq = pq [ : - 1 ] NEW_LINE freq2 = pq [ len ( pq ) - 1 ] NEW_LINE pq = pq [ : - 1 ] NEW_LINE count += 1 NEW_LINE freq1 -= 1 NEW_LINE freq2 -= 1 NEW_LINE if ( freq1 > 0 ) : NEW_LINE INDENT pq . append ( freq1 ) NEW_LINE DEDENT if ( freq2 > 0 ) : NEW_LINE INDENT pq . append ( freq2 ) NEW_LINE DEDENT pq . sort ( ) NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 2 , 4 , 1 , 4 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE print ( maximumPairs ( arr , N ) ) NEW_LINE DEDENT
Generate an N | Python3 program for above approach ; Function to print target array ; Sort the given array ; Seeking for index of elements with minimum diff . ; Seeking for index ; To store target array ; Copying element ; Copying remaining element ; Printing target array ; Driver Code ; Given Input ; Function Call
import sys NEW_LINE def printArr ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE minDifference = sys . maxsize NEW_LINE minIndex = - 1 NEW_LINE for i in range ( 1 , n , 1 ) : NEW_LINE INDENT if ( minDifference > abs ( arr [ i ] - arr [ i - 1 ] ) ) : NEW_LINE INDENT minDifference = abs ( arr [ i ] - arr [ i - 1 ] ) NEW_LINE minIndex = i - 1 NEW_LINE DEDENT DEDENT Arr = [ 0 for i in range ( n ) ] NEW_LINE Arr [ 0 ] = arr [ minIndex ] NEW_LINE Arr [ n - 1 ] = arr [ minIndex + 1 ] NEW_LINE pos = 1 NEW_LINE for i in range ( minIndex + 2 , n , 1 ) : NEW_LINE INDENT Arr [ pos ] = arr [ i ] NEW_LINE pos += 1 NEW_LINE DEDENT for i in range ( minIndex ) : NEW_LINE INDENT Arr [ pos ] = arr [ i ] NEW_LINE pos += 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT print ( Arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 8 NEW_LINE arr = [ 4 , 6 , 2 , 6 , 8 , 2 , 6 , 4 ] NEW_LINE printArr ( arr , N ) NEW_LINE DEDENT
Construct a Perfect Binary Tree from Preorder Traversal | Structure of the tree ; Function to create a new node with the value val ; Return the newly created node ; Function to create the Perfect Binary Tree ; If preStart > preEnd return NULL ; Initialize root as pre [ preStart ] ; If the only node is left , then return node ; Parameters for further recursion ; Recursive Call to build the subtree of root node ; Return the created root ; Function to build Perfect Binary Tree ; Function to print the Inorder of the given Tree ; Base Case ; Left Recursive Call ; Print the data ; Right Recursive Call ; Driver Code ; Function Call ; Print Inorder Traversal
class Node : NEW_LINE INDENT def __init__ ( self , val ) : NEW_LINE INDENT self . data = val NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def getNewNode ( val ) : NEW_LINE INDENT newNode = Node ( val ) NEW_LINE return newNode NEW_LINE DEDENT def buildPerfectBT_helper ( preStart , preEnd , pre ) : NEW_LINE INDENT if ( preStart > preEnd ) : NEW_LINE INDENT return None NEW_LINE DEDENT root = getNewNode ( pre [ preStart ] ) NEW_LINE if ( preStart == preEnd ) : NEW_LINE INDENT return root NEW_LINE DEDENT leftPreStart = preStart + 1 NEW_LINE rightPreStart = leftPreStart + int ( ( preEnd - leftPreStart + 1 ) / 2 ) NEW_LINE leftPreEnd = rightPreStart - 1 NEW_LINE rightPreEnd = preEnd NEW_LINE root . left = buildPerfectBT_helper ( leftPreStart , leftPreEnd , pre ) NEW_LINE root . right = buildPerfectBT_helper ( rightPreStart , rightPreEnd , pre ) NEW_LINE return root NEW_LINE DEDENT def buildPerfectBT ( pre , size ) : NEW_LINE INDENT return buildPerfectBT_helper ( 0 , size - 1 , pre ) NEW_LINE DEDENT def printInorder ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT printInorder ( root . left ) NEW_LINE print ( root . data , " " , end = " " ) NEW_LINE printInorder ( root . right ) NEW_LINE DEDENT pre = [ 1 , 2 , 4 , 5 , 3 , 6 , 7 ] NEW_LINE N = len ( pre ) NEW_LINE root = buildPerfectBT ( pre , N ) NEW_LINE print ( " Inorder ▁ traversal ▁ of ▁ the ▁ tree : ▁ " , end = " " ) NEW_LINE printInorder ( root ) NEW_LINE