codeparrot/xlcost-text-to-code huggingface dataset:
Maximize length of subarray of equal elements by performing at most K increment operations | Python3 program for above approach ; Function to find the maximum length of subarray of equal elements after performing at most K increments ; Length of array ; Stores the size of required subarray ; Starting po of a window ; Stores the sum of window ; Iterate over array ; Current element ; Remove index of minimum elements from deque which are less than the current element ; Insert current index in deque ; Update current window sum ; Calculate required operation to make current window elements equal ; If cost is less than k ; Shift window start pointer towards right and update current window sum ; Return answer ; Driver Code ; Function call
from collections import deque NEW_LINE def maxSubarray ( a , k ) : NEW_LINE INDENT n = len ( a ) NEW_LINE answer = 0 NEW_LINE start = 0 NEW_LINE s = 0 NEW_LINE dq = deque ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT x = a [ i ] NEW_LINE while ( len ( dq ) > 0 and a [ dq [ - 1 ] ] <= x ) : NEW_LINE INDENT dq . popleft ( ) NEW_LINE DEDENT dq . append ( i ) NEW_LINE s += x NEW_LINE cost = a [ dq [ 0 ] ] * ( answer + 1 ) - s NEW_LINE if ( cost <= k ) : NEW_LINE INDENT answer += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( dq [ 0 ] == start ) : NEW_LINE INDENT dq . popleft ( ) NEW_LINE DEDENT s -= a [ start ] NEW_LINE start += 1 NEW_LINE DEDENT DEDENT return answer NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 2 , 2 , 4 ] NEW_LINE k = 10 NEW_LINE print ( maxSubarray ( a , k ) ) NEW_LINE DEDENT
Find depth of the deepest odd level leaf node | A Binary tree node ; A recursive function to find depth of the deepest odd level leaf node ; Base Case ; If this node is leaf and its level is odd , return its level ; If not leaf , return the maximum value from left and right subtrees ; Main function which calculates the depth of deepest odd level leaf . This function mainly uses depthOfOddLeafUtil ( ) ; Driver program to test 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 depthOfOddLeafUtil ( root , level ) : 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 and level & 1 : NEW_LINE INDENT return level NEW_LINE DEDENT return ( max ( depthOfOddLeafUtil ( root . left , level + 1 ) , depthOfOddLeafUtil ( root . right , level + 1 ) ) ) NEW_LINE DEDENT def depthOfOddLeaf ( root ) : NEW_LINE INDENT level = 1 NEW_LINE depth = 0 NEW_LINE return depthOfOddLeafUtil ( root , level ) 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 root . right . right . right . right . left = Node ( 11 ) NEW_LINE print " % d ▁ is ▁ the ▁ required ▁ depth " % ( depthOfOddLeaf ( root ) ) NEW_LINE
Check if an array can be split into subarrays with GCD exceeding K | Function to check if it is possible to split an array into subarrays having GCD at least K ; Iterate over the array arr [ ] ; If the current element is less than or equal to k ; If no array element is found to be less than or equal to k ; Driver Code ; Given array arr [ ] ; Length of the array ; Given K ; Function call
def canSplitArray ( arr , n , k ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] <= k ) : NEW_LINE INDENT return " No " NEW_LINE DEDENT DEDENT return " Yes " NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 4 , 6 , 1 , 8 , 16 ] NEW_LINE N = len ( arr ) NEW_LINE K = 3 NEW_LINE print ( canSplitArray ( arr , N , K ) ) NEW_LINE DEDENT
Smallest subarray from a given Array with sum greater than or equal to K | Set 2 | Function to find the smallest subarray sum greater than or equal to target ; Base Case ; If sum of the array is less than target ; If target is equal to the sum of the array ; Required condition ; Driver Code
def smallSumSubset ( data , target , maxVal ) : NEW_LINE INDENT if target <= 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT elif sum ( data ) < target : NEW_LINE INDENT return maxVal NEW_LINE DEDENT elif sum ( data ) == target : NEW_LINE INDENT return len ( data ) NEW_LINE DEDENT elif data [ 0 ] >= target : NEW_LINE INDENT return 1 NEW_LINE DEDENT elif data [ 0 ] < target : NEW_LINE INDENT return min ( smallSumSubset ( data [ 1 : ] ,   \ target , maxVal ) , 1 + smallSumSubset ( data [ 1 : ] ,   \ target - data [ 0 ] , maxVal ) ) NEW_LINE DEDENT DEDENT data = [ 3 , 1 , 7 , 1 , 2 ] NEW_LINE target = 11 NEW_LINE val = smallSumSubset ( data , target , len ( data ) + 1 ) NEW_LINE if val > len ( data ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( val ) NEW_LINE DEDENT
Maximize cost obtained by removal of substrings " pr " or " rp " from a given String | Function to maintain the case , X >= Y ; To maintain X >= Y ; Replace ' p ' to 'r ; Replace ' r ' to ' p ' . ; Function to return the maximum cost ; Stores the length of the string ; To maintain X >= Y . ; Stores the maximum cost ; Stores the count of ' p ' after removal of all " pr " substrings up to str [ i ] ; Stores the count of ' r ' after removal of all " pr " substrings up to str [ i ] ; Stack to maintain the order of characters after removal of substrings ; If substring " pr " is removed ; Increase cost by X ; If any substring " rp " left in the Stack ; If any substring " rp " left in the Stack ; Driver Code
def swapXandY ( str , X , Y ) : NEW_LINE INDENT N = len ( str ) NEW_LINE X , Y = Y , X NEW_LINE for i in range ( N ) : NEW_LINE DEDENT ' NEW_LINE INDENT if ( str [ i ] == ' p ' ) : NEW_LINE INDENT str [ i ] = ' r ' NEW_LINE DEDENT elif ( str [ i ] == ' r ' ) : NEW_LINE INDENT str [ i ] = ' p ' NEW_LINE DEDENT DEDENT def maxCost ( str , X , Y ) : NEW_LINE INDENT N = len ( str ) NEW_LINE if ( Y > X ) : NEW_LINE INDENT swapXandY ( str , X , Y ) NEW_LINE DEDENT res = 0 NEW_LINE countP = 0 NEW_LINE countR = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( str [ i ] == ' p ' ) : NEW_LINE INDENT countP += 1 NEW_LINE DEDENT elif ( str [ i ] == ' r ' ) : NEW_LINE INDENT if ( countP > 0 ) : NEW_LINE INDENT countP -= 1 NEW_LINE res += X NEW_LINE DEDENT else : NEW_LINE INDENT countR += 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT res += min ( countP , countR ) * Y NEW_LINE countP = 0 NEW_LINE countR = 0 NEW_LINE DEDENT DEDENT res += min ( countP , countR ) * Y NEW_LINE return res NEW_LINE DEDENT str = " abppprrr " NEW_LINE X = 5 NEW_LINE Y = 4 NEW_LINE print ( maxCost ( str , X , Y ) ) NEW_LINE
Check if an array contains only one distinct element | Function to find if the array contains only one distinct element ; Assume first element to be the unique element ; Traversing the array ; If current element is not equal to X then break the loop and print No ; Compare and print the result ; Driver Code ; Function call
def uniqueElement ( arr ) : NEW_LINE INDENT x = arr [ 0 ] NEW_LINE flag = 1 NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT if ( arr [ i ] != x ) : NEW_LINE INDENT flag = 0 NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag == 1 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT arr = [ 9 , 9 , 9 , 9 , 9 , 9 , 9 ] NEW_LINE uniqueElement ( arr ) NEW_LINE
Maximum Count of pairs having equal Sum based on the given conditions | Function to find the maximum count of pairs having equal sum ; Size of the array ; Iterate through evey sum of pairs possible from the given array ; Count of pairs with given sum ; Check for a possible pair ; Update count of possible pair ; Update the answer by taking the pair which is maximum for every possible sum ; Return the max possible pair ; Function to return the count of pairs ; Size of the array ; Stores the frequencies ; Count the frequency ; Driver Code
def maxCount ( freq , maxi , mini ) : NEW_LINE INDENT n = len ( freq ) - 1 NEW_LINE ans = 0 NEW_LINE sum = 2 * mini NEW_LINE while sum <= 2 * maxi : NEW_LINE INDENT possiblePair = 0 NEW_LINE for firElement in range ( 1 , ( sum + 1 ) // 2 ) : NEW_LINE INDENT if ( sum - firElement <= maxi ) : NEW_LINE INDENT possiblePair += min ( freq [ firElement ] , freq [ sum - firElement ] ) NEW_LINE DEDENT DEDENT sum += 1 NEW_LINE if ( sum % 2 == 0 ) : NEW_LINE INDENT possiblePair += freq [ sum // 2 ] // 2 NEW_LINE DEDENT ans = max ( ans , possiblePair ) NEW_LINE DEDENT return ans NEW_LINE DEDENT def countofPairs ( a ) : NEW_LINE INDENT n = len ( a ) NEW_LINE freq = [ 0 ] * ( n + 1 ) NEW_LINE maxi = - 1 NEW_LINE mini = n + 1 NEW_LINE for i in range ( len ( a ) ) : NEW_LINE INDENT maxi = max ( maxi , a [ i ] ) NEW_LINE mini = min ( mini , a [ i ] ) NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT freq [ a [ i ] ] += 1 NEW_LINE DEDENT return maxCount ( freq , maxi , mini ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 2 , 4 , 3 , 3 , 5 , 6 ] NEW_LINE print ( countofPairs ( a ) ) NEW_LINE DEDENT
Maximum Subarray Sum possible by replacing an Array element by its Square | Function to find the maximum subarray sum possible ; Stores sum without squaring ; Stores sum squaring ; Stores the maximum subarray sum ; Either extend the subarray or start a new subarray ; Either extend previous squared subarray or start a new subarray by squaring the current element ; Update maximum subarray sum ; Return answer ; Driver Code ; Function call
def getMaxSum ( a , n ) : NEW_LINE INDENT dp = [ [ 0 for x in range ( 2 ) ] for y in range ( n ) ] NEW_LINE dp [ 0 ] [ 0 ] = a [ 0 ] NEW_LINE dp [ 0 ] [ 1 ] = a [ 0 ] * a [ 0 ] NEW_LINE max_sum = max ( dp [ 0 ] [ 0 ] , dp [ 0 ] [ 1 ] ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT dp [ i ] [ 0 ] = max ( a [ i ] , dp [ i - 1 ] [ 0 ] + a [ i ] ) NEW_LINE dp [ i ] [ 1 ] = max ( dp [ i - 1 ] [ 1 ] + a [ i ] , a [ i ] * a [ i ] ) NEW_LINE dp [ i ] [ 1 ] = max ( dp [ i ] [ 1 ] , dp [ i - 1 ] [ 0 ] + a [ i ] * a [ i ] ) NEW_LINE max_sum = max ( max_sum , dp [ i ] [ 1 ] ) NEW_LINE max_sum = max ( max_sum , dp [ i ] [ 0 ] ) NEW_LINE DEDENT return max_sum NEW_LINE DEDENT n = 5 NEW_LINE a = [ 1 , - 5 , 8 , 12 , - 8 ] NEW_LINE print ( getMaxSum ( a , n ) ) NEW_LINE
Smallest Subarray with Sum K from an Array | Python3 program to implement the above approach ; Function to find the length of the smallest subarray with sum K ; Stores the frequency of prefix sums in the array ; Initialize ln ; If sum of array till i - th index is less than K ; No possible subarray exists till i - th index ; Find the exceeded value ; If exceeded value is zero ; Driver Code
from collections import defaultdict NEW_LINE import sys NEW_LINE def subArraylen ( arr , n , K ) : NEW_LINE INDENT mp = defaultdict ( lambda : 0 ) NEW_LINE mp [ arr [ 0 ] ] = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT arr [ i ] = arr [ i ] + arr [ i - 1 ] NEW_LINE mp [ arr [ i ] ] = i NEW_LINE DEDENT ln = sys . maxsize NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] < K ) : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT x = K - arr [ i ] NEW_LINE if ( x == 0 ) : NEW_LINE INDENT ln = min ( ln , i ) NEW_LINE DEDENT if ( x in mp . keys ( ) ) : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT ln = min ( ln , i - mp [ x ] ) NEW_LINE DEDENT DEDENT DEDENT return ln NEW_LINE DEDENT arr = [ 1 , 2 , 4 , 3 , 2 , 4 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE K = 7 NEW_LINE ln = subArraylen ( arr , n , K ) NEW_LINE if ( ln == sys . maxsize ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ln ) NEW_LINE DEDENT
Find depth of the deepest odd level leaf node | Python3 program to find depth of the deepest odd level leaf node of binary tree ; tree node returns a new tree Node ; return max odd number depth of leaf node ; create a queue for level order traversal ; traverse until the queue is empty ; traverse for complete level ; check if the node is leaf node and level is odd if level is odd , then update result ; check for left child ; check for right child ; Driver Code ; construct a tree
INT_MAX = 2 ** 31 NEW_LINE 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 maxOddLevelDepth ( root ) : NEW_LINE INDENT if ( not root ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT q = [ ] NEW_LINE q . append ( root ) NEW_LINE result = INT_MAX NEW_LINE level = 0 NEW_LINE while ( len ( q ) ) : NEW_LINE INDENT size = len ( q ) NEW_LINE level += 1 NEW_LINE while ( size > 0 ) : NEW_LINE INDENT temp = q [ 0 ] NEW_LINE q . pop ( 0 ) NEW_LINE if ( not temp . left and not temp . right and ( level % 2 != 0 ) ) : NEW_LINE INDENT result = level NEW_LINE DEDENT if ( temp . left ) : NEW_LINE INDENT q . append ( temp . left ) NEW_LINE DEDENT if ( temp . right ) : NEW_LINE INDENT q . append ( temp . right ) NEW_LINE DEDENT size -= 1 NEW_LINE DEDENT DEDENT return result 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 . right . left = newNode ( 5 ) NEW_LINE root . right . right = newNode ( 6 ) NEW_LINE root . right . left . right = newNode ( 7 ) NEW_LINE root . right . right . right = newNode ( 8 ) NEW_LINE root . right . left . right . left = newNode ( 9 ) NEW_LINE root . right . right . right . right = newNode ( 10 ) NEW_LINE root . right . right . right . right . left = newNode ( 11 ) NEW_LINE result = maxOddLevelDepth ( root ) NEW_LINE if ( result == INT_MAX ) : NEW_LINE INDENT print ( " No ▁ leaf ▁ node ▁ at ▁ odd ▁ level " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( result , end = " " ) NEW_LINE print ( " ▁ is ▁ the ▁ required ▁ depth ▁ " ) NEW_LINE DEDENT DEDENT
Nth Term of a Fibonacci Series of Primes formed by concatenating pairs of Primes in a given range | Stores at each index if it 's a prime or not ; Sieve of Eratosthenes to generate all possible primes ; If p is a prime ; Set all multiples of p as non - prime ; Function to generate the required Fibonacci Series ; Stores all primes between n1 and n2 ; Generate all primes between n1 and n2 ; Stores all concatenations of each pair of primes ; Generate all concatenations of each pair of primes ; Stores the primes out of the numbers generated above ; Store the unique primes ; Find the minimum ; Find the minimum ; Find N ; Print the N - th term of the Fibonacci Series ; Driver Code
prime = [ True for i in range ( 100001 ) ] NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT p = 2 NEW_LINE while ( p * p <= 100000 ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * p , 100001 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT DEDENT def fibonacciOfPrime ( n1 , n2 ) : NEW_LINE INDENT SieveOfEratosthenes ( ) NEW_LINE initial = [ ] NEW_LINE for i in range ( n1 , n2 ) : NEW_LINE INDENT if prime [ i ] : NEW_LINE INDENT initial . append ( i ) NEW_LINE DEDENT DEDENT now = [ ] NEW_LINE for a in initial : NEW_LINE INDENT for b in initial : NEW_LINE INDENT if a != b : NEW_LINE INDENT c = str ( a ) + str ( b ) NEW_LINE now . append ( int ( c ) ) NEW_LINE DEDENT DEDENT DEDENT current = [ ] NEW_LINE for x in now : NEW_LINE INDENT if prime [ x ] : NEW_LINE INDENT current . append ( x ) NEW_LINE DEDENT DEDENT current = set ( current ) NEW_LINE first = min ( current ) NEW_LINE second = max ( current ) NEW_LINE count = len ( current ) - 1 NEW_LINE curr = 1 NEW_LINE while curr < count : NEW_LINE INDENT c = first + second NEW_LINE first = second NEW_LINE second = c NEW_LINE curr += 1 NEW_LINE DEDENT print ( c ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x = 2 NEW_LINE y = 40 NEW_LINE fibonacciOfPrime ( x , y ) NEW_LINE DEDENT
Count of Array elements greater than all elements on its left and next K elements on its right | Python3 program to implement the above approach ; Function to print the count of Array elements greater than all elements on its left and next K elements on its right ; Iterate over the array ; If the stack is not empty and the element at the top of the stack is smaller than arr [ i ] ; Store the index of next greater element ; Pop the top element ; Insert the current index ; Stores the count ; Driver Code
import sys NEW_LINE def countElements ( arr , n , k ) : NEW_LINE INDENT s = [ ] NEW_LINE next_greater = [ n ] * ( n + 1 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( len ( s ) == 0 ) : NEW_LINE INDENT s . append ( i ) NEW_LINE continue NEW_LINE DEDENT while ( len ( s ) != 0 and arr [ s [ - 1 ] ] < arr [ i ] ) : NEW_LINE INDENT next_greater [ s [ - 1 ] ] = i NEW_LINE s . pop ( - 1 ) NEW_LINE DEDENT s . append ( i ) NEW_LINE DEDENT count = 0 NEW_LINE maxi = - sys . maxsize - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( next_greater [ i ] - i > k and maxi < arr [ i ] ) : NEW_LINE INDENT maxi = max ( maxi , arr [ i ] ) NEW_LINE count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 2 , 3 , 6 , 4 , 3 , 2 ] NEW_LINE K = 2 NEW_LINE n = len ( arr ) NEW_LINE print ( countElements ( arr , n , K ) ) NEW_LINE DEDENT
Maximize length of Subarray of 1 's after removal of a pair of consecutive Array elements | Python program to find the maximum count of 1 s ; If arr [ i - 2 ] = = 1 then we increment the count of occurences of 1 's ; Else we initialise the count with 0 ; If arr [ i + 2 ] = = 1 then we increment the count of occurences of 1 's ; Else we initialise the count with 0 ; We get the maximum count by skipping the current and the next element . ; Driver code
def maxLengthOf1s ( arr , n ) : NEW_LINE INDENT prefix = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( 2 , n ) : NEW_LINE INDENT if ( arr [ i - 2 ] == 1 ) : NEW_LINE INDENT prefix [ i ] = prefix [ i - 1 ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT prefix [ i ] = 0 NEW_LINE DEDENT DEDENT suffix = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( n - 3 , - 1 , - 1 ) : NEW_LINE INDENT if ( arr [ i + 2 ] == 1 ) : NEW_LINE INDENT suffix [ i ] = suffix [ i + 1 ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT suffix [ i ] = 0 NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT ans = max ( ans , prefix [ i + 1 ] + suffix [ i ] ) NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT n = 6 NEW_LINE arr = [ 1 , 1 , 1 , 0 , 1 , 1 ] NEW_LINE maxLengthOf1s ( arr , n ) NEW_LINE
Minimum number of operations required to obtain a given Binary String | Function to find the minimum number of operations required to obtain the string s ; Iterate the string s ; If first occurrence of 1 is found ; Mark the index ; Base case : If no 1 occurred ; No operations required ; Stores the character for which last operation was performed ; Stores minimum number of operations ; Iterate from pos to n ; Check if s [ i ] is 0 ; Check if last operation was performed because of 1 ; Set last to 0 ; Check if last operation was performed because of 0 ; Set last to 1 ; Return the answer ; Driver Code
def minOperations ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE pos = - 1 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT pos = i NEW_LINE break NEW_LINE DEDENT DEDENT if ( pos == - 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT last = 1 NEW_LINE ans = 1 NEW_LINE for i in range ( pos + 1 , n ) : NEW_LINE INDENT if ( s [ i ] == '0' ) : NEW_LINE INDENT if ( last == 1 ) : NEW_LINE INDENT ans += 1 NEW_LINE last = 0 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( last == 0 ) : NEW_LINE INDENT ans += 1 NEW_LINE last = 1 NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "10111" NEW_LINE print ( minOperations ( s ) ) NEW_LINE DEDENT
Find the Deepest Node in a Binary Tree | A Binary Tree Node Utility function to create a new tree node ; maxLevel : keeps track of maximum level seen so far . res : Value of deepest node so far . level : Level of root ; Update level and resue ; Returns value of deepest node ; Initialze result and max level ; Updates value " res " and " maxLevel " Note that res and maxLen are passed by reference . ; Driver Code
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 self . visited = False NEW_LINE DEDENT DEDENT def find ( root , level , maxLevel , res ) : NEW_LINE INDENT if ( root != None ) : NEW_LINE INDENT level += 1 NEW_LINE find ( root . left , level , maxLevel , res ) NEW_LINE if ( level > maxLevel [ 0 ] ) : NEW_LINE INDENT res [ 0 ] = root . data NEW_LINE maxLevel [ 0 ] = level NEW_LINE DEDENT find ( root . right , level , maxLevel , res ) NEW_LINE DEDENT DEDENT def deepestNode ( root ) : NEW_LINE INDENT res = [ - 1 ] NEW_LINE maxLevel = [ - 1 ] NEW_LINE find ( root , 0 , maxLevel , res ) NEW_LINE return res [ 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 . right . left = newNode ( 5 ) NEW_LINE root . right . right = newNode ( 6 ) NEW_LINE root . right . left . right = newNode ( 7 ) NEW_LINE root . right . right . right = newNode ( 8 ) NEW_LINE root . right . left . right . left = newNode ( 9 ) NEW_LINE print ( deepestNode ( root ) ) NEW_LINE DEDENT
Count the number of clumps in the given Array | Function to count the number of clumps in the given array arr [ ] ; Initialise count of clumps as 0 ; Traverse the arr [ ] ; Whenever a sequence of same value is encountered ; Return the count of clumps ; Driver Code ; length of the given array arr [ ] ; Function Call
def countClumps ( arr , N ) : NEW_LINE INDENT clumps = 0 NEW_LINE i = 0 NEW_LINE while ( i < N - 1 ) : NEW_LINE INDENT flag = 0 NEW_LINE while ( i + 1 < N and arr [ i ] == arr [ i + 1 ] ) : NEW_LINE INDENT flag = 1 NEW_LINE i += 1 NEW_LINE DEDENT if ( flag ) : NEW_LINE INDENT clumps += 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return clumps NEW_LINE DEDENT arr = [ 13 , 15 , 66 , 66 , 66 , 37 , 37 , 8 , 8 , 11 , 11 ] NEW_LINE N = len ( arr ) NEW_LINE print ( countClumps ( arr , N ) ) NEW_LINE
Find if string is K | Find if string is K - Palindrome or not using all characters exactly once Python 3 program to find if string is K - Palindrome or not using all characters exactly once ; when size of string is less than k ; when size of string is equal to k ; when size of string is greater than k to store the frequencies of the characters ; to store the count of characters whose number of occurrences is odd . ; iterating over the map ; Driver code
def iskPalindromesPossible ( s , k ) : NEW_LINE INDENT if ( len ( s ) < k ) : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE return NEW_LINE DEDENT if ( len ( s ) == k ) : NEW_LINE INDENT print ( " Possible " ) NEW_LINE return NEW_LINE DEDENT freq = dict . fromkeys ( s , 0 ) NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT freq [ s [ i ] ] += 1 NEW_LINE DEDENT count = 0 NEW_LINE for value in freq . values ( ) : NEW_LINE INDENT if ( value % 2 == 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT if ( count > k ) : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " poor " NEW_LINE K = 3 NEW_LINE iskPalindromesPossible ( str1 , K ) NEW_LINE str = " geeksforgeeks " NEW_LINE K = 10 NEW_LINE iskPalindromesPossible ( str , K ) NEW_LINE DEDENT