codeparrot/xlcost-text-to-code huggingface dataset:
Count of replacements required to make the sum of all Pairs of given type from the Array equal | Python3 program to implement the above approach ; Function to find the minimum replacements required ; Stores the maximum and minimum values for every pair of the form arr [ i ] , arr [ n - i - 1 ] ; Map for storing frequencies of every sum formed by pairs ; Minimum element in the pair ; Maximum element in the pair ; Incrementing the frequency of sum encountered ; Insert minimum and maximum values ; Sorting the vectors ; Iterate over all possible values of x ; Count of pairs for which x > x + k ; Count of pairs for which x < mn + 1 ; Count of pairs requiring 2 replacements ; Count of pairs requiring no replacements ; Count of pairs requiring 1 replacement ; Update the answer ; Return the answer ; Driver Code
inf = 10 ** 18 NEW_LINE def firstOccurance ( numbers , length , searchnum ) : NEW_LINE INDENT answer = - 1 NEW_LINE start = 0 NEW_LINE end = length - 1 NEW_LINE while start <= end : NEW_LINE INDENT middle = ( start + end ) // 2 NEW_LINE if numbers [ middle ] == searchnum : NEW_LINE INDENT answer = middle NEW_LINE end = middle - 1 NEW_LINE DEDENT elif numbers [ middle ] > searchnum : NEW_LINE INDENT end = middle - 1 NEW_LINE DEDENT else : NEW_LINE INDENT start = middle + 1 NEW_LINE DEDENT DEDENT return answer NEW_LINE DEDENT def minimumReplacement ( arr , N , K ) : NEW_LINE INDENT ans = inf NEW_LINE max_values = [ ] NEW_LINE min_values = [ ] NEW_LINE sum_equal_to_x = dict ( ) NEW_LINE for i in range ( N // 2 ) : NEW_LINE INDENT mn = min ( arr [ i ] , arr [ N - i - 1 ] ) NEW_LINE mx = max ( arr [ i ] , arr [ N - i - 1 ] ) NEW_LINE if ( arr [ i ] + arr [ N - i - 1 ] not in sum_equal_to_x ) : NEW_LINE INDENT sum_equal_to_x [ arr [ i ] + arr [ N - i - 1 ] ] = 0 NEW_LINE DEDENT sum_equal_to_x [ arr [ i ] + arr [ N - i - 1 ] ] += 1 NEW_LINE min_values . append ( mn ) NEW_LINE max_values . append ( mx ) NEW_LINE DEDENT max_values . sort ( ) NEW_LINE min_values . sort ( ) NEW_LINE for x in range ( 2 , 2 * K + 1 ) : NEW_LINE INDENT mp1 = firstOccurance ( max_values , len ( max_values ) , x - K ) NEW_LINE mp2 = firstOccurance ( min_values , len ( min_values ) , x ) NEW_LINE rep2 = mp1 + ( N // 2 - mp2 ) NEW_LINE if x in sum_equal_to_x : NEW_LINE INDENT rep0 = sum_equal_to_x [ x ] NEW_LINE DEDENT else : NEW_LINE INDENT rep0 = 0 NEW_LINE DEDENT rep1 = ( N // 2 - rep2 - rep0 ) NEW_LINE ans = min ( ans , rep2 * 2 + rep1 ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 NEW_LINE K = 3 NEW_LINE arr = [ 1 , 2 , 2 , 1 ] NEW_LINE print ( minimumReplacement ( arr , N , K ) ) NEW_LINE DEDENT
Print the Forests of a Binary Tree after removal of given Nodes | Stores the nodes to be deleted ; Structure of a Tree node ; Function to create a new node ; Function to check whether the node needs to be deleted or not ; Function to perform tree pruning by performing post order traversal ; If the node needs to be deleted ; Store the its subtree ; Perform Inorder Traversal ; Function to print the forests ; Stores the remaining nodes ; Print the inorder traversal of Forests ; Driver Code
mp = dict ( ) NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . key = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def newNode ( key ) : NEW_LINE INDENT temp = Node ( key ) NEW_LINE return temp NEW_LINE DEDENT def deleteNode ( nodeVal ) : NEW_LINE INDENT if nodeVal in mp : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def treePruning ( root , result ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return None ; NEW_LINE DEDENT root . left = treePruning ( root . left , result ) ; NEW_LINE root . right = treePruning ( root . right , result ) ; NEW_LINE if ( deleteNode ( root . key ) ) : NEW_LINE INDENT if ( root . left ) : NEW_LINE INDENT result . append ( root . left ) ; NEW_LINE DEDENT if ( root . right ) : NEW_LINE INDENT result . append ( root . right ) ; NEW_LINE DEDENT return None ; NEW_LINE DEDENT return root ; NEW_LINE DEDENT def printInorderTree ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return ; NEW_LINE DEDENT printInorderTree ( root . left ) ; NEW_LINE print ( root . key , end = ' ▁ ' ) NEW_LINE printInorderTree ( root . right ) ; NEW_LINE DEDENT def printForests ( root , arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT mp [ arr [ i ] ] = True ; NEW_LINE DEDENT result = [ ] NEW_LINE if ( treePruning ( root , result ) ) : NEW_LINE INDENT result . append ( root ) ; NEW_LINE DEDENT for i in range ( len ( result ) ) : NEW_LINE INDENT printInorderTree ( result [ i ] ) ; NEW_LINE print ( ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 1 ) ; NEW_LINE root . left = newNode ( 12 ) ; NEW_LINE root . right = newNode ( 13 ) ; NEW_LINE root . right . left = newNode ( 14 ) ; NEW_LINE root . right . right = newNode ( 15 ) ; NEW_LINE root . right . left . left = newNode ( 21 ) ; NEW_LINE root . right . left . right = newNode ( 22 ) ; NEW_LINE root . right . right . left = newNode ( 23 ) ; NEW_LINE root . right . right . right = newNode ( 24 ) ; NEW_LINE arr = [ 14 , 23 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE printForests ( root , arr , n ) ; NEW_LINE DEDENT
Count of Ways to obtain given Sum from the given Array elements | Function to call dfs to calculate the number of ways ; If target + sum is odd or S exceeds sum ; No sultion exists ; Return the answer ; Driver Code
def knapSack ( nums , S ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( len ( nums ) ) : NEW_LINE INDENT sum += nums [ i ] ; NEW_LINE DEDENT if ( sum < S or - sum > - S or ( S + sum ) % 2 == 1 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT dp = [ 0 ] * ( ( ( S + sum ) // 2 ) + 1 ) ; NEW_LINE dp [ 0 ] = 1 ; NEW_LINE for j in range ( len ( nums ) ) : NEW_LINE INDENT for i in range ( len ( dp ) - 1 , nums [ j ] - 1 , - 1 ) : NEW_LINE INDENT dp [ i ] += dp [ i - nums [ j ] ] ; NEW_LINE DEDENT DEDENT return dp [ len ( dp ) - 1 ] ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = 3 ; NEW_LINE arr = [ 1 , 2 , 3 , 4 , 5 ] ; NEW_LINE answer = knapSack ( arr , S ) ; NEW_LINE print ( answer ) ; NEW_LINE DEDENT
Maximum distance between two elements whose absolute difference is K | Function that find maximum distance between two elements whose absolute difference is K ; To store the first index ; Traverse elements and find maximum distance between 2 elements whose absolute difference is K ; If this is first occurrence of element then insert its index in map ; If next element present in map then update max distance ; If previous element present in map then update max distance ; Return the answer ; Given array arr [ ] ; Given difference K ; Function call
def maxDistance ( arr , K ) : NEW_LINE INDENT map = { } NEW_LINE maxDist = 0 NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT if not arr [ i ] in map : NEW_LINE INDENT map [ arr [ i ] ] = i NEW_LINE DEDENT if arr [ i ] + K in map : NEW_LINE INDENT maxDist = max ( maxDist , i - map [ arr [ i ] + K ] ) NEW_LINE DEDENT if arr [ i ] - K in map : NEW_LINE INDENT maxDist = max ( maxDist , i - map [ arr [ i ] - K ] ) NEW_LINE DEDENT DEDENT if maxDist == 0 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return maxDist NEW_LINE DEDENT DEDENT arr = [ 11 , 2 , 3 , 8 , 5 , 2 ] NEW_LINE K = 2 NEW_LINE print ( maxDistance ( arr , K ) ) NEW_LINE
Count of Ordered Pairs ( X , Y ) satisfying the Equation 1 / X + 1 / Y = 1 / N | Function to find number of ordered positive integer pairs ( x , y ) such that they satisfy the equation ; Initialize answer variable ; Iterate over all possible values of y ; For valid x and y , ( n * n ) % ( y - n ) has to be 0 ; Increment count of ordered pairs ; Print the answer ; Driver Code ; Function call
def solve ( n ) : NEW_LINE INDENT ans = 0 NEW_LINE y = n + 1 NEW_LINE while ( y <= n * n + n ) : NEW_LINE INDENT if ( ( n * n ) % ( y - n ) == 0 ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT y += 1 NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT n = 5 NEW_LINE solve ( n ) NEW_LINE
Count of Root to Leaf Paths consisting of at most M consecutive Nodes having value K | Initialize the adjacency list and visited array ; Function to find the number of root to leaf paths that contain atmost m consecutive nodes with value k ; Mark the current node as visited ; If value at current node is k ; Increment counter ; If count is greater than m return from that path ; Path is allowed if size of present node becomes 0 i . e it has no child root and no more than m consecutive 1 's ; Driver code ; Desigining the tree ; Counter counts no . of consecutive nodes
adj = [ [ ] for i in range ( 100005 ) ] NEW_LINE visited = [ 0 for i in range ( 100005 ) ] NEW_LINE ans = 0 ; NEW_LINE def dfs ( node , count , m , arr , k ) : NEW_LINE INDENT global ans NEW_LINE visited [ node ] = 1 ; NEW_LINE if ( arr [ node - 1 ] == k ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT count = 0 ; NEW_LINE DEDENT if ( count > m ) : NEW_LINE INDENT return ; NEW_LINE DEDENT if ( len ( adj [ node ] ) == 1 and node != 1 ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT for x in adj [ node ] : NEW_LINE INDENT if ( not visited [ x ] ) : NEW_LINE INDENT dfs ( x , count , m , arr , k ) ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 1 , 3 , 2 , 1 , 2 , 1 ] NEW_LINE N = 7 NEW_LINE K = 2 NEW_LINE M = 2 NEW_LINE adj [ 1 ] . append ( 2 ) ; NEW_LINE adj [ 2 ] . append ( 1 ) ; NEW_LINE adj [ 1 ] . append ( 3 ) ; NEW_LINE adj [ 3 ] . append ( 1 ) ; NEW_LINE adj [ 2 ] . append ( 4 ) ; NEW_LINE adj [ 4 ] . append ( 2 ) ; NEW_LINE adj [ 2 ] . append ( 5 ) ; NEW_LINE adj [ 5 ] . append ( 2 ) ; NEW_LINE adj [ 3 ] . append ( 6 ) ; NEW_LINE adj [ 6 ] . append ( 3 ) ; NEW_LINE adj [ 3 ] . append ( 7 ) ; NEW_LINE adj [ 7 ] . append ( 3 ) ; NEW_LINE counter = 0 ; NEW_LINE dfs ( 1 , counter , M , arr , K ) ; NEW_LINE print ( ans ) NEW_LINE DEDENT
Count of N | Function to calculate and return the reverse of the number ; Function to calculate the total count of N - digit numbers satisfying the necessary conditions ; Initialize two variables ; Calculate the sum of odd and even positions ; Check for divisibility ; Return the answer ; Driver Code
def reverse ( num ) : NEW_LINE INDENT rev = 0 ; NEW_LINE while ( num > 0 ) : NEW_LINE INDENT r = int ( num % 10 ) ; NEW_LINE rev = rev * 10 + r ; NEW_LINE num = num // 10 ; NEW_LINE DEDENT return rev ; NEW_LINE DEDENT def count ( N , A , B ) : NEW_LINE INDENT l = int ( pow ( 10 , N - 1 ) ) ; NEW_LINE r = int ( pow ( 10 , N ) - 1 ) ; NEW_LINE if ( l == 1 ) : NEW_LINE INDENT l = 0 ; NEW_LINE DEDENT ans = 0 ; NEW_LINE for i in range ( l , r + 1 ) : NEW_LINE INDENT even_sum = 0 ; NEW_LINE odd_sum = 0 ; NEW_LINE itr = 0 ; NEW_LINE num = reverse ( i ) ; NEW_LINE while ( num > 0 ) : NEW_LINE INDENT if ( itr % 2 == 0 ) : NEW_LINE INDENT odd_sum += num % 10 ; NEW_LINE DEDENT else : NEW_LINE INDENT even_sum += num % 10 ; NEW_LINE DEDENT num = num // 10 ; NEW_LINE itr += 1 ; NEW_LINE DEDENT if ( even_sum % A == 0 and odd_sum % B == 0 ) : NEW_LINE INDENT ans += 1 ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 2 ; NEW_LINE A = 5 ; NEW_LINE B = 3 ; NEW_LINE print ( count ( N , A , B ) ) ; NEW_LINE DEDENT
Distance Traveled by Two Trains together in the same Direction | Function to find the distance traveled together ; Stores distance travelled by A ; Stpres distance travelled by B ; Stores the total distance travelled together ; Sum of distance travelled ; Condition for traveling together ; Driver Code
def calc_distance ( A , B , n ) : NEW_LINE INDENT distance_traveled_A = 0 NEW_LINE distance_traveled_B = 0 NEW_LINE answer = 0 NEW_LINE for i in range ( 5 ) : NEW_LINE INDENT distance_traveled_A += A [ i ] NEW_LINE distance_traveled_B += B [ i ] NEW_LINE if ( ( distance_traveled_A == distance_traveled_B ) and ( A [ i ] == B [ i ] ) ) : NEW_LINE INDENT answer += A [ i ] NEW_LINE DEDENT DEDENT return answer NEW_LINE DEDENT A = [ 1 , 2 , 3 , 2 , 4 ] NEW_LINE B = [ 2 , 1 , 3 , 1 , 4 ] NEW_LINE N = len ( A ) NEW_LINE print ( calc_distance ( A , B , N ) ) NEW_LINE
Sum of Nodes and respective Neighbors on the path from root to a vertex V | Creating Adjacency list ; Function to perform DFS traversal ; Initializing parent of each node to p ; Iterate over the children ; Function to add values of children to respective parent nodes ; Root node ; Function to return sum of values of each node in path from V to the root ; Path from node V to root node ; Driver Code ; Insert edges into the graph ; Values assigned to each vertex ; Constructing the tree using adjacency list ; Parent array ; store values in the parent array ; Add values of children to the parent ; Find sum of nodes lying in the path ; Add root node since its value is not included yet
def constructTree ( n , edges ) : NEW_LINE INDENT adjl = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT adjl . append ( [ ] ) NEW_LINE DEDENT for i in range ( len ( edges ) ) : NEW_LINE INDENT u = edges [ i ] [ 0 ] NEW_LINE v = edges [ i ] [ 1 ] NEW_LINE adjl [ u ] . append ( v ) NEW_LINE adjl [ v ] . append ( u ) NEW_LINE DEDENT return adjl NEW_LINE DEDENT def DFS ( adjl , parent , u , p ) : NEW_LINE INDENT parent [ u ] = p NEW_LINE for v in adjl [ u ] : NEW_LINE INDENT if ( v != p ) : NEW_LINE INDENT DFS ( adjl , parent , v , u ) NEW_LINE DEDENT DEDENT DEDENT def valuesFromChildren ( parent , values ) : NEW_LINE INDENT valuesChildren = [ 0 ] * ( len ( parent ) ) NEW_LINE for i in range ( len ( parent ) ) : NEW_LINE INDENT if ( parent [ i ] == - 1 ) : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT p = parent [ i ] NEW_LINE valuesChildren [ p ] += values [ i ] NEW_LINE DEDENT DEDENT return valuesChildren NEW_LINE DEDENT def findSumOfValues ( v , parent , valuesChildren ) : NEW_LINE INDENT cur_node = v NEW_LINE Sum = 0 NEW_LINE while ( cur_node != - 1 ) : NEW_LINE INDENT Sum += valuesChildren [ cur_node ] NEW_LINE cur_node = parent [ cur_node ] NEW_LINE DEDENT return Sum NEW_LINE DEDENT n = 8 NEW_LINE edges = [ [ 0 , 1 ] , [ 0 , 2 ] , [ 0 , 3 ] , [ 1 , 4 ] , [ 1 , 5 ] , [ 4 , 7 ] , [ 3 , 6 ] ] NEW_LINE v = 7 NEW_LINE values = [ 1 , 2 , 3 , 0 , 0 , 4 , 3 , 6 ] NEW_LINE adjl = constructTree ( n , edges ) NEW_LINE parent = [ 0 ] * ( n ) NEW_LINE DFS ( adjl , parent , 0 , - 1 ) NEW_LINE valuesChildren = valuesFromChildren ( parent , values ) NEW_LINE Sum = findSumOfValues ( v , parent , valuesChildren ) NEW_LINE Sum += values [ 0 ] NEW_LINE print ( Sum ) NEW_LINE
Find a pair in Array with second largest product | Function to find second largest product pair in arr [ 0. . n - 1 ] ; No pair exits ; Initialize max product pair ; Traverse through every possible pair and keep track of largest product ; If pair is largest ; Second largest ; If pair dose not largest but larger then second largest ; Print the pairs ; Driver Code ; Given array ; Function call
def maxProduct ( arr , N ) : NEW_LINE INDENT if ( N < 3 ) : NEW_LINE INDENT return ; NEW_LINE DEDENT a = arr [ 0 ] ; b = arr [ 1 ] ; NEW_LINE c = 0 ; d = 0 ; NEW_LINE for i in range ( 0 , N , 1 ) : NEW_LINE INDENT for j in range ( i + 1 , N - 1 , 1 ) : NEW_LINE INDENT if ( arr [ i ] * arr [ j ] > a * b ) : NEW_LINE INDENT c = a ; NEW_LINE d = b ; NEW_LINE a = arr [ i ] ; NEW_LINE b = arr [ j ] ; NEW_LINE DEDENT if ( arr [ i ] * arr [ j ] < a * b and arr [ i ] * arr [ j ] > c * d ) : NEW_LINE INDENT c = arr [ i ] ; NEW_LINE DEDENT d = arr [ j ] ; NEW_LINE DEDENT DEDENT print ( c , " ▁ " , d ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , 2 , 67 , 45 , 160 , 78 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE maxProduct ( arr , N ) ; NEW_LINE DEDENT
Count of substrings consisting of even number of vowels | Utility function to check if a character is a vowel ; Function to calculate and return the count of substrings with even number of vowels ; Stores the count of substrings ; If the current character is a vowel ; Increase count ; If substring contains even number of vowels ; Increase the answer ; Prthe final answer ; Driver Code
def isVowel ( c ) : NEW_LINE INDENT if ( c == ' a ' or c == ' e ' or c == ' i ' or c == ' o ' or c == ' u ' ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def countSubstrings ( s , n ) : NEW_LINE INDENT result = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT count = 0 NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT if ( isVowel ( s [ j ] ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT if ( count % 2 == 0 ) : NEW_LINE INDENT result += 1 NEW_LINE DEDENT DEDENT DEDENT print ( result ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE s = " abcde " NEW_LINE countSubstrings ( s , n ) NEW_LINE DEDENT
Count of substrings consisting of even number of vowels | Utility function to check if a character is a vowel ; Function to calculate and return the count of substrings with even number of vowels ; Stores the count of substrings with even and odd number of vowels respectively ; Update count of vowels modulo 2 in sum to obtain even or odd ; Increment even / odd count ; Count substrings with even number of vowels using Handshaking Lemma ; Driver Code
def isVowel ( c ) : NEW_LINE INDENT if ( c == ' a ' or c == ' e ' or c == ' i ' or c == ' o ' or c == ' u ' ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT def countSubstrings ( s , n ) : NEW_LINE INDENT temp = [ 1 , 0 ] ; NEW_LINE result = 0 ; NEW_LINE sum = 0 ; NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT sum += ( 1 if isVowel ( s [ i ] ) else 0 ) ; NEW_LINE sum %= 2 ; NEW_LINE temp [ sum ] += 1 ; NEW_LINE DEDENT result += ( ( temp [ 0 ] * ( temp [ 0 ] - 1 ) ) // 2 ) ; NEW_LINE result += ( ( temp [ 1 ] * ( temp [ 1 ] - 1 ) ) // 2 ) ; NEW_LINE print ( result ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 ; NEW_LINE s = " abcde " ; NEW_LINE countSubstrings ( s , n ) ; NEW_LINE DEDENT
Find the Level of a Binary Tree with Width K | Python3 program to implement the above approach ; Structure of a Tree node ; Function to create new node ; Function returns required level of width k , if found else - 1 ; To store the node and the label and perform traversal ; Taking the last label of each level of the tree ; Check width of current level ; If the width is equal to k then return that level ; Taking the first label of each level of the tree ; If any level does not has width equal to k , return - 1 ; Driver Code
from collections import deque NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . key = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def findLevel ( root : Node , k : int , level : int ) -> int : NEW_LINE INDENT qt = deque ( ) NEW_LINE qt . append ( [ root , 0 ] ) NEW_LINE count = 1 NEW_LINE b = 0 NEW_LINE a = 0 NEW_LINE while qt : NEW_LINE INDENT temp = qt . popleft ( ) NEW_LINE if ( count == 1 ) : NEW_LINE INDENT b = temp [ 1 ] NEW_LINE DEDENT if ( temp [ 0 ] . left ) : NEW_LINE INDENT qt . append ( [ temp [ 0 ] . left , 2 * temp [ 1 ] ] ) NEW_LINE DEDENT if ( temp [ 0 ] . right ) : NEW_LINE INDENT qt . append ( [ temp [ 0 ] . right , 2 * temp [ 1 ] + 1 ] ) NEW_LINE DEDENT count -= 1 NEW_LINE if ( count == 0 ) : NEW_LINE INDENT if ( b - a + 1 == k ) : NEW_LINE INDENT return level NEW_LINE DEDENT secondLabel = qt [ 0 ] NEW_LINE a = secondLabel [ 1 ] NEW_LINE level += 1 NEW_LINE count = len ( qt ) NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT root = Node ( 5 ) NEW_LINE root . left = Node ( 6 ) NEW_LINE root . right = Node ( 2 ) NEW_LINE root . right . right = Node ( 8 ) NEW_LINE root . left . left = Node ( 7 ) NEW_LINE root . left . left . left = Node ( 5 ) NEW_LINE root . left . right = Node ( 3 ) NEW_LINE root . left . right . right = Node ( 4 ) NEW_LINE k = 4 NEW_LINE print ( findLevel ( root , k , 1 ) ) NEW_LINE DEDENT
Maximize median after doing K addition operation on the Array | Function to check operation can be perform or not ; Number of operation to perform s . t . mid is median ; If mid is median of the array ; Function to find max median of the array ; Lowest possible median ; Highest possible median ; Checking for mid is possible for the median of array after doing at most k operation ; Return the max possible ans ; Driver Code ; Given array ; Given number of operation ; Size of array ; Sort the array ; Function call
def possible ( arr , N , mid , K ) : NEW_LINE INDENT add = 0 NEW_LINE for i in range ( N // 2 - ( N + 1 ) % 2 , N ) : NEW_LINE INDENT if ( mid - arr [ i ] > 0 ) : NEW_LINE INDENT add += ( mid - arr [ i ] ) NEW_LINE if ( add > K ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT if ( add <= K ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def findMaxMedian ( arr , N , K ) : NEW_LINE INDENT low = 1 NEW_LINE mx = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT mx = max ( mx , arr [ i ] ) NEW_LINE DEDENT high = K + mx NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = ( high + low ) // 2 NEW_LINE if ( possible ( arr , N , mid , K ) ) : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT DEDENT if ( N % 2 == 0 ) : NEW_LINE INDENT if ( low - 1 < arr [ N // 2 ] ) : NEW_LINE INDENT return ( arr [ N // 2 ] + low - 1 ) // 2 NEW_LINE DEDENT DEDENT return low - 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 3 , 6 ] NEW_LINE K = 10 NEW_LINE N = len ( arr ) NEW_LINE arr = sorted ( arr ) NEW_LINE print ( findMaxMedian ( arr , N , K ) ) NEW_LINE DEDENT
Kth Smallest Element of a Matrix of given dimensions filled with product of indices | Function that returns true if the count of elements is less than mid ; To store count of elements less than mid ; Loop through each row ; Count elements less than mid in the ith row ; Function that returns the Kth smallest element in the NxM Matrix after sorting in an array ; Initialize low and high ; Perform binary search ; Find the mid ; Check if the count of elements is less than mid ; Return Kth smallest element of the matrix ; Driver Code
def countLessThanMid ( mid , N , M , K ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 1 , min ( N , mid ) + 1 ) : NEW_LINE INDENT count = count + min ( mid // i , M ) NEW_LINE DEDENT if ( count >= K ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT def findKthElement ( N , M , K ) : NEW_LINE INDENT low = 1 NEW_LINE high = N * M NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = low + ( high - low ) // 2 NEW_LINE if ( countLessThanMid ( mid , N , M , K ) ) : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT DEDENT return high + 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 2 NEW_LINE M = 3 NEW_LINE K = 5 NEW_LINE print ( findKthElement ( N , M , K ) ) NEW_LINE DEDENT
Count of substrings containing only the given character | Function that finds the count of substrings containing only character C in the S ; To store total count of substrings ; To store count of consecutive C 's ; Loop through the string ; Increase the consecutive count of C 's ; Add count of sub - strings from consecutive strings ; Reset the consecutive count of C 's ; Add count of sub - strings from consecutive strings ; Print the count of sub - strings containing only C ; Driver Code
def countSubString ( S , C ) : NEW_LINE INDENT count = 0 NEW_LINE conCount = 0 NEW_LINE for ch in S : NEW_LINE INDENT if ( ch == C ) : NEW_LINE INDENT conCount += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count += ( ( conCount * ( conCount + 1 ) ) // 2 ) NEW_LINE conCount = 0 NEW_LINE DEDENT DEDENT count += ( ( conCount * ( conCount + 1 ) ) // 2 ) NEW_LINE print ( count ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " geeksforgeeks " NEW_LINE C = ' e ' NEW_LINE countSubString ( S , C ) NEW_LINE DEDENT