codeparrot/xlcost-text-to-code huggingface dataset:
Find postorder traversal of BST from preorder traversal | Python3 program for finding postorder traversal of BST from preorder traversal ; Function to find postorder traversal from preorder traversal . ; If entire preorder array is traversed then return as no more element is left to be added to post order array . ; If array element does not lie in range specified , then it is not part of current subtree . ; Store current value , to be printed later , after printing left and right subtrees . Increment preIndex to find left and right subtrees , and pass this updated value to recursive calls . ; All elements with value between minval and val lie in left subtree . ; All elements with value between val and maxval lie in right subtree . ; Function to find postorder traversal . ; To store index of element to be traversed next in preorder array . This is passed by reference to utility function . ; Driver Code ; Calling function
INT_MIN = - 2 ** 31 NEW_LINE INT_MAX = 2 ** 31 NEW_LINE def findPostOrderUtil ( pre , n , minval , maxval , preIndex ) : NEW_LINE INDENT if ( preIndex [ 0 ] == n ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( pre [ preIndex [ 0 ] ] < minval or pre [ preIndex [ 0 ] ] > maxval ) : NEW_LINE INDENT return NEW_LINE DEDENT val = pre [ preIndex [ 0 ] ] NEW_LINE preIndex [ 0 ] += 1 NEW_LINE findPostOrderUtil ( pre , n , minval , val , preIndex ) NEW_LINE findPostOrderUtil ( pre , n , val , maxval , preIndex ) NEW_LINE print ( val , end = " ▁ " ) NEW_LINE DEDENT def findPostOrder ( pre , n ) : NEW_LINE INDENT preIndex = [ 0 ] NEW_LINE findPostOrderUtil ( pre , n , INT_MIN , INT_MAX , preIndex ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT pre = [ 40 , 30 , 35 , 80 , 100 ] NEW_LINE n = len ( pre ) NEW_LINE findPostOrder ( pre , n ) NEW_LINE DEDENT
Height of binary tree considering even level leaves only | A binary tree node has data , pointer to left child and a pointer to right child ; Base Case ; left stores the result of left subtree , and right stores the result of right subtree ; If both left and right returns 0 , it means there is no valid path till leaf node ; Driver Code ; Let us create binary tree shown in above diagram
class newNode : 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 heightOfTreeUtil ( root , isEven ) : NEW_LINE INDENT if ( not root ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( not root . left and not root . right ) : NEW_LINE INDENT if ( isEven ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT left = heightOfTreeUtil ( root . left , not isEven ) NEW_LINE right = heightOfTreeUtil ( root . right , not isEven ) NEW_LINE if ( left == 0 and right == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return ( 1 + max ( left , right ) ) NEW_LINE DEDENT def heightOfTree ( root ) : NEW_LINE INDENT return heightOfTreeUtil ( root , False ) 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 . left . left = newNode ( 4 ) NEW_LINE root . left . right = newNode ( 5 ) NEW_LINE root . left . right . left = newNode ( 6 ) NEW_LINE print ( " Height ▁ of ▁ tree ▁ is " , heightOfTree ( root ) ) NEW_LINE DEDENT
Find Height of Binary Tree represented by Parent array | This functio fills depth of i 'th element in parent[] The depth is filled in depth[i] ; If depth [ i ] is already filled ; If node at index i is root ; If depth of parent is not evaluated before , then evaluate depth of parent first ; Depth of this node is depth of parent plus 1 ; This function reutns height of binary tree represented by parent array ; Create an array to store depth of all nodes and initialize depth of every node as 0 Depth of root is 1 ; fill depth of all nodes ; The height of binary tree is maximum of all depths . Find the maximum in depth [ ] and assign it to ht ; int parent [ ] = { 1 , 5 , 5 , 2 , 2 , - 1 , 3 } ;
def fillDepth ( parent , i , depth ) : NEW_LINE INDENT if depth [ i ] != 0 : NEW_LINE INDENT return NEW_LINE DEDENT if parent [ i ] == - 1 : NEW_LINE INDENT depth [ i ] = 1 NEW_LINE return NEW_LINE DEDENT if depth [ parent [ i ] ] == 0 : NEW_LINE INDENT fillDepth ( parent , parent [ i ] , depth ) NEW_LINE DEDENT depth [ i ] = depth [ parent [ i ] ] + 1 NEW_LINE DEDENT def findHeight ( parent ) : NEW_LINE INDENT n = len ( parent ) NEW_LINE depth = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT fillDepth ( parent , i , depth ) NEW_LINE DEDENT ht = depth [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT ht = max ( ht , depth [ i ] ) NEW_LINE DEDENT return ht NEW_LINE DEDENT parent = [ - 1 , 0 , 0 , 1 , 1 , 3 , 5 ] NEW_LINE print " Height ▁ is ▁ % d " % ( findHeight ( parent ) ) NEW_LINE
How to determine if a binary tree is height | A binary tree Node ; function to check if tree is height - balanced or not ; Base condition ; for left and right subtree height allowed values for ( lh - rh ) are 1 , - 1 , 0 ; if we reach here means tree is not height - balanced tree ; function to find height of binary tree ; base condition when binary tree is empty ; If tree is not empty then height = 1 + max of left height and right heights ; Driver function to test the above function
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 isBalanced ( root ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return True NEW_LINE DEDENT lh = height ( root . left ) NEW_LINE rh = height ( root . right ) NEW_LINE if ( abs ( lh - rh ) <= 1 ) and isBalanced ( root . left ) is True and isBalanced ( root . right ) is True : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def height ( root ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return 0 NEW_LINE DEDENT return max ( height ( root . left ) , height ( 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 root . left . left . left = Node ( 8 ) NEW_LINE if isBalanced ( root ) : NEW_LINE INDENT print ( " Tree ▁ is ▁ balanced " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Tree ▁ is ▁ not ▁ balanced " ) NEW_LINE DEDENT
Find height of a special binary tree whose leaf nodes are connected | Helper function that allocates a new node with the given data and None left and right poers . ; Construct to create a new node ; function to check if given node is a leaf node or node ; If given node ' s ▁ left ' s right is pointing to given node and its right ' s ▁ left ▁ is ▁ pointing ▁ to ▁ the ▁ node ▁ itself ▁ ▁ then ▁ it ' s a leaf ; Compute the height of a tree -- the number of Nodes along the longest path from the root node down to the farthest leaf node . ; if node is None , return 0 ; if node is a leaf node , return 1 ; compute the depth of each subtree and take maximum ; Driver Code ; Given tree contains 3 leaf nodes ; create circular doubly linked list out of leaf nodes of the tree set next pointer of linked list ; set prev pointer of linked list ; calculate height of the tree
class newNode : 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 isLeaf ( node ) : NEW_LINE INDENT return node . left and node . left . right == node and node . right and node . right . left == node NEW_LINE DEDENT def maxDepth ( node ) : NEW_LINE INDENT if ( node == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( isLeaf ( node ) ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 1 + max ( maxDepth ( node . left ) , maxDepth ( node . right ) ) 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 . left . left = newNode ( 4 ) NEW_LINE root . left . right = newNode ( 5 ) NEW_LINE root . left . left . left = newNode ( 6 ) NEW_LINE L1 = root . left . left . left NEW_LINE L2 = root . left . right NEW_LINE L3 = root . right NEW_LINE L1 . right = L2 NEW_LINE L2 . right = L3 NEW_LINE L3 . right = L1 NEW_LINE L3 . left = L2 NEW_LINE L2 . left = L1 NEW_LINE L1 . left = L3 NEW_LINE print ( " Height ▁ of ▁ tree ▁ is ▁ " , maxDepth ( root ) ) NEW_LINE DEDENT
Diameter of a Binary Tree | A binary tree node ; The function Compute the " height " of a tree . Height is the number of nodes along the longest path from the root node down to the farthest leaf node . ; Base Case : Tree is empty ; If tree is not empty then height = 1 + max of left height and right heights ; Function to get the diameter of a binary tree ; Base Case when tree is empty ; Get the height of left and right sub - trees ; Get the diameter of left and right sub - trees ; Return max of the following tree : 1 ) Diameter of left subtree 2 ) Diameter of right subtree 3 ) Height of left subtree + height of right subtree + 1 ; Constructed binary tree is 1 / \ 2 3 / \ 4 5 ; Function Call
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 height ( node ) : NEW_LINE INDENT if node is None : NEW_LINE INDENT return 0 NEW_LINE DEDENT return 1 + max ( height ( node . left ) , height ( node . right ) ) NEW_LINE DEDENT def diameter ( root ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return 0 NEW_LINE DEDENT lheight = height ( root . left ) NEW_LINE rheight = height ( root . right ) NEW_LINE ldiameter = diameter ( root . left ) NEW_LINE rdiameter = diameter ( root . right ) NEW_LINE return max ( lheight + rheight + 1 , max ( ldiameter , rdiameter ) ) 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 ( diameter ( root ) ) NEW_LINE
Find if possible to visit every nodes in given Graph exactly once based on given conditions | Function to find print path ; If a [ 0 ] is 1 ; Printing path ; Seeking for a [ i ] = 0 and a [ i + 1 ] = 1 ; Printing path ; If a [ N - 1 ] = 0 ; Driver Code ; Given Input ; Function Call
def findpath ( N , a ) : NEW_LINE INDENT if ( a [ 0 ] ) : NEW_LINE INDENT print ( N + 1 ) NEW_LINE for i in range ( 1 , N + 1 , 1 ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT return NEW_LINE DEDENT for i in range ( N - 1 ) : NEW_LINE INDENT if ( a [ i ] == 0 and a [ i + 1 ] ) : NEW_LINE INDENT for j in range ( 1 , i + 1 , 1 ) : NEW_LINE INDENT print ( j , end = " ▁ " ) NEW_LINE DEDENT print ( N + 1 , end = " ▁ " ) ; NEW_LINE for j in range ( i + 1 , N + 1 , 1 ) : NEW_LINE INDENT print ( j , end = " ▁ " ) NEW_LINE DEDENT return NEW_LINE DEDENT DEDENT for i in range ( 1 , N + 1 , 1 ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT print ( N + 1 , end = " ▁ " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE arr = [ 0 , 1 , 0 ] NEW_LINE findpath ( N , arr ) NEW_LINE DEDENT
Modify a numeric string to a balanced parentheses by replacements | Function to check if the given string can be converted to a balanced bracket sequence or not ; Check if the first and last characters are equal ; Initialize two variables to store the count of open and closed brackets ; If the current character is same as the first character ; If the current character is same as the last character ; If count of open brackets becomes less than 0 ; Print the new string ; If the current character is same as the first character ; If bracket sequence is not balanced ; Check for unbalanced bracket sequence ; Print the sequence ; Given Input ; Function Call
def balBracketSequence ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE if ( str [ 0 ] == str [ n - 1 ] ) : NEW_LINE INDENT print ( " No " , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT cntForOpen = 0 NEW_LINE cntForClose = 0 NEW_LINE check = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( str [ i ] == str [ 0 ] ) : NEW_LINE INDENT cntForOpen += 1 NEW_LINE DEDENT elif str [ i ] == str [ n - 1 ] : NEW_LINE INDENT cntForOpen -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT cntForOpen += 1 NEW_LINE DEDENT if ( cntForOpen < 0 ) : NEW_LINE INDENT check = 0 NEW_LINE break NEW_LINE DEDENT DEDENT if ( check and cntForOpen == 0 ) : NEW_LINE INDENT print ( " Yes , ▁ " , end = " " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( str [ i ] == str [ n - 1 ] ) : NEW_LINE INDENT print ( ' ) ' , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' ( ' , end = " " ) NEW_LINE DEDENT DEDENT return NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( str [ i ] == str [ 0 ] ) : NEW_LINE INDENT cntForClose += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cntForClose -= 1 NEW_LINE DEDENT if ( cntForClose < 0 ) : NEW_LINE INDENT check = 0 NEW_LINE break NEW_LINE DEDENT DEDENT if ( check and cntForClose == 0 ) : NEW_LINE INDENT print ( " Yes , ▁ " , end = " " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( str [ i ] == str [ 0 ] ) : NEW_LINE INDENT print ( ' ( ' , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' ) ' , end = " " ) NEW_LINE DEDENT DEDENT return NEW_LINE DEDENT DEDENT print ( " NO " , end = " " ) NEW_LINE DEDENT DEDENT str = "123122" NEW_LINE balBracketSequence ( str ) NEW_LINE
Diameter of a Binary Tree | A binary tree Node ; utility class to pass height object ; Optimised recursive function to find diameter of binary tree ; to store height of left and right subtree ; base condition - when binary tree is empty ; diameter is also 0 ; ldiameter -- > diameter of left subtree rdiamter -- > diameter of right subtree height of left subtree and right subtree is obtained from lh and rh and returned value of function is stored in ldiameter and rdiameter ; height of tree will be max of left subtree height and right subtree height plus1 ; function to calculate diameter of binary tree ; Constructed binary tree is 1 / \ 2 3 / \ 4 5 ; Function Call
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT class Height : NEW_LINE INDENT def __init ( self ) : NEW_LINE INDENT self . h = 0 NEW_LINE DEDENT DEDENT def diameterOpt ( root , height ) : NEW_LINE INDENT lh = Height ( ) NEW_LINE rh = Height ( ) NEW_LINE if root is None : NEW_LINE INDENT height . h = 0 NEW_LINE return 0 NEW_LINE DEDENT ldiameter = diameterOpt ( root . left , lh ) NEW_LINE rdiameter = diameterOpt ( root . right , rh ) NEW_LINE height . h = max ( lh . h , rh . h ) + 1 NEW_LINE return max ( lh . h + rh . h + 1 , max ( ldiameter , rdiameter ) ) NEW_LINE DEDENT def diameter ( root ) : NEW_LINE INDENT height = Height ( ) NEW_LINE return diameterOpt ( root , height ) 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 ( diameter ( root ) ) NEW_LINE
Diameter of a Binary Tree in O ( n ) [ A new method ] | Tree node structure used in the program ; Function to find height of a tree ; update the answer , because diameter of a tree is nothing but maximum value of ( left_height + right_height + 1 ) for each node ; Computes the diameter of binary tree with given root . ; This will storethe final answer ; Driver code
class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT def height ( root , ans ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT left_height = height ( root . left , ans ) NEW_LINE right_height = height ( root . right , ans ) NEW_LINE ans [ 0 ] = max ( ans [ 0 ] , 1 + left_height + right_height ) NEW_LINE return 1 + max ( left_height , right_height ) NEW_LINE DEDENT def diameter ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT ans = [ - 999999999999 ] NEW_LINE height_of_tree = height ( root , ans ) NEW_LINE return ans [ 0 ] 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 . left . left = newNode ( 4 ) NEW_LINE root . left . right = newNode ( 5 ) NEW_LINE print ( " Diameter ▁ is " , diameter ( root ) ) NEW_LINE DEDENT
Find postorder traversal of BST from preorder traversal | Run loop from 1 to length of pre ; Prfrom pivot length - 1 to zero ; Prfrom end to pivot length ; Driver Code
def getPostOrderBST ( pre , N ) : NEW_LINE INDENT pivotPoint = 0 NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT if ( pre [ 0 ] <= pre [ i ] ) : NEW_LINE INDENT pivotPoint = i NEW_LINE break NEW_LINE DEDENT DEDENT for i in range ( pivotPoint - 1 , 0 , - 1 ) : NEW_LINE INDENT print ( pre [ i ] , end = " ▁ " ) NEW_LINE DEDENT for i in range ( N - 1 , pivotPoint - 1 , - 1 ) : NEW_LINE INDENT print ( pre [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( pre [ 0 ] ) NEW_LINE DEDENT
Count substrings made up of a single distinct character | Function to count the number of substrings made up of a single distinct character ; Stores the required count ; Stores the count of substrings possible by using current character ; Stores the previous character ; Traverse the string ; If current character is same as the previous character ; Increase count of substrings possible with current character ; Reset count of substrings possible with current character ; Update count of substrings ; Update previous character ; Driver Code
def countSubstrings ( s ) : NEW_LINE INDENT ans = 0 NEW_LINE subs = 1 NEW_LINE pre = ' ' NEW_LINE for i in s : NEW_LINE INDENT if pre == i : NEW_LINE INDENT subs += 1 NEW_LINE DEDENT else : NEW_LINE INDENT subs = 1 NEW_LINE DEDENT ans += subs NEW_LINE pre = i NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT s = ' geeksforgeeks ' NEW_LINE countSubstrings ( s ) NEW_LINE
Generate an array from given pairs of adjacent elements | Utility function to find original array ; Map to store all neighbors for each element ; Vector to store original elements ; Stotrs which array elements are visited ; A djacency list to store neighbors of each array element ; Find the first corner element ; Stores first element of the original array ; Push it into the original array ; Mark as visited ; Traversing the neighbors and check if the elements are visited or not ; Traverse adjacent elements ; If element is not visited ; Push it into res ; Mark as visited ; Update the next adjacent ; Print original array ; Driver Code ; Given pairs of adjacent elements
def find_original_array ( A ) : NEW_LINE INDENT mp = [ [ ] for i in range ( 6 ) ] NEW_LINE res = [ ] NEW_LINE visited = { } NEW_LINE for it in A : NEW_LINE INDENT mp [ it [ 0 ] ] . append ( it [ 1 ] ) NEW_LINE mp [ it [ 1 ] ] . append ( it [ 0 ] ) NEW_LINE DEDENT start = 0 NEW_LINE for it in range ( 6 ) : NEW_LINE INDENT if ( len ( mp [ it ] ) == 1 ) : NEW_LINE INDENT start = it + 3 NEW_LINE break NEW_LINE DEDENT DEDENT adjacent = start NEW_LINE res . append ( start ) NEW_LINE visited [ start ] = True NEW_LINE while ( len ( res ) != len ( A ) + 1 ) : NEW_LINE INDENT for elements in mp [ adjacent ] : NEW_LINE INDENT if ( elements not in visited ) : NEW_LINE INDENT res . append ( elements ) NEW_LINE visited [ elements ] = True NEW_LINE adjacent = elements NEW_LINE DEDENT DEDENT DEDENT print ( * res ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ [ 5 , 1 ] , [ 3 , 4 ] , [ 3 , 5 ] ] NEW_LINE find_original_array ( A ) NEW_LINE DEDENT
Possible edges of a tree for given diameter , height and vertices | Function to construct the tree ; Special case when d == 2 , only one edge ; Tree is not possible ; Satisfy the height condition by add edges up to h ; Add d - h edges from 1 to satisfy diameter condition ; Remaining edges at vertex 1 or 2 ( d == h ) ; Driver Code
def constructTree ( n , d , h ) : NEW_LINE INDENT if d == 1 : NEW_LINE INDENT if n == 2 and h == 1 : NEW_LINE INDENT print ( "1 ▁ 2" ) NEW_LINE return 0 NEW_LINE DEDENT print ( " - 1" ) NEW_LINE return 0 NEW_LINE DEDENT if d > 2 * h : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return 0 NEW_LINE DEDENT for i in range ( 1 , h + 1 ) : NEW_LINE INDENT print ( i , " ▁ " , i + 1 ) NEW_LINE DEDENT if d > h : NEW_LINE INDENT print ( 1 , " ▁ " , h + 2 ) NEW_LINE for i in range ( h + 2 , d + 1 ) : NEW_LINE INDENT print ( i , " ▁ " , i + 1 ) NEW_LINE DEDENT DEDENT for i in range ( d + 1 , n ) : NEW_LINE INDENT k = 1 NEW_LINE if d == h : NEW_LINE INDENT k = 2 NEW_LINE DEDENT print ( k , " ▁ " , i + 1 ) NEW_LINE DEDENT DEDENT n = 5 NEW_LINE d = 3 NEW_LINE h = 2 NEW_LINE constructTree ( n , d , h ) NEW_LINE
Smallest element present in every subarray of all possible lengths | Function to add count of numbers in the map for a subarray of length k ; Set to store unique elements ; Add elements to the set ; Adding count in map ; Function to check if there is any number which repeats itself in every subarray of length K ; Check all number starting from 1 ; Check if i occurred n - k + 1 times ; Print the smallest number ; Print - 1 , if no such number found ; Function to count frequency of each number in each subarray of length K ; Traverse all subarrays of length K ; Check and print the smallest number present in every subarray and print it ; Function to generate the value of K ; Function call ; Driver Code ; Given array ; Size of array ; Function call
def uniqueElements ( arr , start , K , mp ) : NEW_LINE INDENT st = set ( ) ; NEW_LINE for i in range ( K ) : NEW_LINE INDENT st . add ( arr [ start + i ] ) ; NEW_LINE DEDENT for itr in st : NEW_LINE INDENT if itr in mp : NEW_LINE INDENT mp [ itr ] += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT mp [ itr ] = 1 ; NEW_LINE DEDENT DEDENT DEDENT def checkAnswer ( mp , N , K ) : NEW_LINE INDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if i in mp : NEW_LINE INDENT if ( mp [ i ] == ( N - K + 1 ) ) : NEW_LINE INDENT print ( i , end = " ▁ " ) ; NEW_LINE return ; NEW_LINE DEDENT DEDENT DEDENT print ( - 1 , end = " ▁ " ) ; NEW_LINE DEDENT def smallestPresentNumber ( arr , N , K ) : NEW_LINE INDENT mp = { } ; NEW_LINE for i in range ( N - K + 1 ) : NEW_LINE INDENT uniqueElements ( arr , i , K , mp ) ; NEW_LINE DEDENT checkAnswer ( mp , N , K ) ; NEW_LINE DEDENT def generateK ( arr , N ) : NEW_LINE INDENT for k in range ( 1 , N + 1 ) : NEW_LINE INDENT smallestPresentNumber ( arr , N , k ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 3 , 5 , 3 , 2 , 3 , 1 , 3 , 2 , 7 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE generateK ( arr , N ) ; NEW_LINE DEDENT