codeparrot/xlcost-text-to-code huggingface dataset:
Positive elements at even and negative at odd positions ( Relative order not maintained ) | Print array function ; Driver code ; before modification ; out of order positive element ; find out of order negative element in remaining array ; out of order negative element ; find out of order positive element in remaining array ; after modification
def printArray ( a , n ) : NEW_LINE INDENT for i in a : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT arr = [ 1 , - 3 , 5 , 6 , - 3 , 6 , 7 , - 4 , 9 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE printArray ( arr , n ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] >= 0 and i % 2 == 1 ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( arr [ j ] < 0 and j % 2 == 0 ) : NEW_LINE INDENT arr [ i ] , arr [ j ] = arr [ j ] , arr [ i ] NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT elif ( arr [ i ] < 0 and i % 2 == 0 ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( arr [ j ] >= 0 and j % 2 == 1 ) : NEW_LINE INDENT arr [ i ] , arr [ j ] = arr [ j ] , arr [ i ] NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT DEDENT printArray ( arr , n ) ; NEW_LINE
Segregate even and odd numbers | Set 3 | Python3 implementation of the above approach ; Driver code ; Function call
def arrayEvenAndOdd ( arr , n ) : NEW_LINE INDENT ind = 0 ; NEW_LINE a = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] % 2 == 0 ) : NEW_LINE INDENT a [ ind ] = arr [ i ] NEW_LINE ind += 1 NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] % 2 != 0 ) : NEW_LINE INDENT a [ ind ] = arr [ i ] NEW_LINE ind += 1 NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT arr = [ 1 , 3 , 2 , 4 , 7 , 6 , 9 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE arrayEvenAndOdd ( arr , n ) NEW_LINE
Segregate even and odd numbers | Set 3 | Function to segregate even odd numbers ; Swapping even and odd numbers ; Printing segregated array ; Driver Code
def arrayEvenAndOdd ( arr , n ) : NEW_LINE INDENT i = - 1 NEW_LINE j = 0 NEW_LINE while ( j != n ) : NEW_LINE INDENT if ( arr [ j ] % 2 == 0 ) : NEW_LINE INDENT i = i + 1 NEW_LINE arr [ i ] , arr [ j ] = arr [ j ] , arr [ i ] NEW_LINE DEDENT j = j + 1 NEW_LINE DEDENT for i in arr : NEW_LINE INDENT print ( str ( i ) + " ▁ " , end = ' ' ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 3 , 2 , 4 , 7 , 6 , 9 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE arrayEvenAndOdd ( arr , n ) NEW_LINE DEDENT
Find sum of all left leaves in a given Binary Tree | A binary tree node ; Pass in a sum variable as an accumulator ; Check whether this node is a leaf node and is left ; Pass 1 for left and 0 for right ; A wrapper over above recursive function ; Use the above recursive function to evaluate sum ; Let us construct the Binary Tree shown in the above figure
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 leftLeavesSumRec ( root , isLeft , summ ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return NEW_LINE DEDENT if root . left is None and root . right is None and isLeft == True : NEW_LINE INDENT summ [ 0 ] += root . key NEW_LINE DEDENT leftLeavesSumRec ( root . left , 1 , summ ) NEW_LINE leftLeavesSumRec ( root . right , 0 , summ ) NEW_LINE DEDENT def leftLeavesSum ( root ) : NEW_LINE INDENT summ = [ 0 ] NEW_LINE leftLeavesSumRec ( root , 0 , summ ) NEW_LINE return summ [ 0 ] NEW_LINE DEDENT root = Node ( 20 ) ; NEW_LINE root . left = Node ( 9 ) ; NEW_LINE root . right = Node ( 49 ) ; NEW_LINE root . right . left = Node ( 23 ) ; NEW_LINE root . right . right = Node ( 52 ) ; NEW_LINE root . right . right . left = Node ( 50 ) ; NEW_LINE root . left . left = Node ( 5 ) ; NEW_LINE root . left . right = Node ( 12 ) ; NEW_LINE root . left . right . right = Node ( 12 ) ; NEW_LINE print " Sum ▁ of ▁ left ▁ leaves ▁ is " , leftLeavesSum ( root ) NEW_LINE
Symmetric Tree ( Mirror Image of itself ) | Node structure ; Returns True if trees with roots as root1 and root 2 are mirror ; If both trees are empty , then they are mirror images ; For two trees to be mirror images , the following three conditions must be true 1 - Their root node 's key must be same  2 - left subtree of left tree and right subtree  of the right tree have to be mirror images  3 - right subtree of left tree and left subtree  of right tree have to be mirror images   ; If none of the above conditions is true then root1 and root2 are not mirror images ; returns true if the tree is symmetric i . e mirror image of itself ; Check if tree is mirror of itself ; Let 's construct the tree show in the above figure
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 isMirror ( root1 , root2 ) : NEW_LINE INDENT if root1 is None and root2 is None : NEW_LINE INDENT return True NEW_LINE DEDENT if ( root1 is not None and root2 is not None ) : NEW_LINE INDENT if root1 . key == root2 . key : NEW_LINE INDENT return ( isMirror ( root1 . left , root2 . right ) and isMirror ( root1 . right , root2 . left ) ) NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def isSymmetric ( root ) : NEW_LINE INDENT return isMirror ( root , root ) NEW_LINE DEDENT root = Node ( 1 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 2 ) NEW_LINE root . left . left = Node ( 3 ) NEW_LINE root . left . right = Node ( 4 ) NEW_LINE root . right . left = Node ( 4 ) NEW_LINE root . right . right = Node ( 3 ) NEW_LINE print " Symmetric " if isSymmetric ( root ) == True else " Not ▁ symmetric " NEW_LINE
Check if a matrix can be converted to another by repeatedly adding any value to X consecutive elements in a row or column | Function to check whether Matrix A [ ] [ ] can be transformed to Matrix B [ ] [ ] or not ; Traverse the matrix to perform horizontal operations ; Calculate difference ; Update next X elements ; Traverse the matrix to perform vertical operations ; Calculate difference ; Update next K elements ; A [ i ] [ j ] is not equal to B [ i ] [ j ] ; Conversion is not possible ; Conversion is possible ; Driver Code ; Input
def Check ( A , B , M , N , X ) : NEW_LINE INDENT for i in range ( M ) : NEW_LINE INDENT for j in range ( N - X + 1 ) : NEW_LINE INDENT if ( A [ i ] [ j ] != B [ i ] [ j ] ) : NEW_LINE INDENT diff = B [ i ] [ j ] - A [ i ] [ j ] NEW_LINE for k in range ( X ) : NEW_LINE INDENT A [ i ] [ j + k ] = A [ i ] [ j + k ] + diff NEW_LINE DEDENT DEDENT DEDENT DEDENT for i in range ( M - X + 1 ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT if ( A [ i ] [ j ] != B [ i ] [ j ] ) : NEW_LINE INDENT diff = B [ i ] [ j ] - A [ i ] [ j ] NEW_LINE for k in range ( X ) : NEW_LINE INDENT A [ i + k ] [ j ] = A [ i + k ] [ j ] + diff NEW_LINE DEDENT DEDENT DEDENT DEDENT for i in range ( M ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT if ( A [ i ] [ j ] != B [ i ] [ j ] ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT DEDENT return 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT M , N , X = 2 , 2 , 2 NEW_LINE A = [ [ 0 , 0 ] , [ 0 , 0 ] ] NEW_LINE B = [ [ 1 , 2 ] , [ 0 , 1 ] ] NEW_LINE if ( Check ( A , B , M , N , X ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Make all array elements equal by reducing array elements to half minimum number of times | Function to find minimum number of operations ; Initialize map ; Traverse the array ; Divide current array element until it reduces to 1 ; Traverse the map ; Find the maximum element having frequency equal to N ; Stores the minimum number of operations required ; Count operations required to convert current element to mx ; Print the answer ; Given array ; Size of the array
def minOperations ( arr , N ) : NEW_LINE INDENT mp = { } NEW_LINE for i in range ( N ) : NEW_LINE INDENT res = arr [ i ] NEW_LINE while ( res ) : NEW_LINE if res in mp : NEW_LINE INDENT mp [ res ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ res ] = 1 NEW_LINE DEDENT res //= 2 NEW_LINE DEDENT mx = 1 NEW_LINE for it in mp : NEW_LINE INDENT if ( mp [ it ] == N ) : NEW_LINE mx = it NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT res = arr [ i ] NEW_LINE while ( res != mx ) : NEW_LINE ans += 1 NEW_LINE res //= 2 NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT arr = [ 3 , 1 , 1 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE minOperations ( arr , N ) NEW_LINE
Find the player to reach at least N by multiplying with any value from given range | Function to find the winner ; Backtrack from N to 1 ; Driver Code
def Winner ( N ) : NEW_LINE INDENT player = True NEW_LINE while N > 1 : NEW_LINE INDENT X , Y = divmod ( N , ( 9 if player else 2 ) ) NEW_LINE N = X + 1 if Y else X NEW_LINE player = not player NEW_LINE DEDENT if player : NEW_LINE return ' B ' NEW_LINE else : NEW_LINE return ' A ' NEW_LINE DEDENT N = 10 NEW_LINE print ( Winner ( N ) ) NEW_LINE
Program to find largest element in an array | returns maximum in arr [ ] of size n ; driver code
def largest ( arr , n ) : NEW_LINE INDENT return max ( arr ) NEW_LINE DEDENT arr = [ 10 , 324 , 45 , 90 , 9808 ] NEW_LINE n = len ( arr ) NEW_LINE print ( largest ( arr , n ) ) NEW_LINE
Find the largest three distinct elements in an array | Python3 code to find largest three elements in an array ; It uses Tuned Quicksort with ; avg . case Time complexity = O ( nLogn ) ; to handle duplicate values ; Driver code
def find3largest ( arr , n ) : NEW_LINE INDENT arr = sorted ( arr ) NEW_LINE check = 0 NEW_LINE count = 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( count < 4 ) : NEW_LINE INDENT if ( check != arr [ n - i ] ) : NEW_LINE INDENT print ( arr [ n - i ] , end = " ▁ " ) NEW_LINE check = arr [ n - i ] NEW_LINE count += 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT arr = [ 12 , 45 , 1 , - 1 , 45 , 54 , 23 , 5 , 0 , - 10 ] NEW_LINE n = len ( arr ) NEW_LINE find3largest ( arr , n ) NEW_LINE
Minimum characters required to be removed to make frequency of each character unique | Function to find the minimum count of characters required to be deleted to make frequencies of all characters unique ; Stores frequency of each distinct character of str ; Store frequency of each distinct character such that the largest frequency is present at the top ; Stores minimum count of characters required to be deleted to make frequency of each character unique ; Traverse the string ; Update frequency of str [ i ] ; Traverse the map ; Insert current frequency into pq ; Traverse the priority_queue ; Stores topmost element of pq ; Pop the topmost element ; If pq is empty ; Return cntChar ; If frequent and topmost element of pq are equal ; If frequency of the topmost element is greater than 1 ; Insert the decremented value of frequent ; Update cntChar ; Driver Code ; Stores length of str
def minCntCharDeletionsfrequency ( str , N ) : NEW_LINE INDENT mp = { } NEW_LINE pq = [ ] NEW_LINE cntChar = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT mp [ str [ i ] ] = mp . get ( str [ i ] , 0 ) + 1 NEW_LINE DEDENT for it in mp : NEW_LINE INDENT pq . append ( mp [ it ] ) NEW_LINE DEDENT pq = sorted ( pq ) NEW_LINE while ( len ( pq ) > 0 ) : NEW_LINE INDENT frequent = pq [ - 1 ] NEW_LINE del pq [ - 1 ] NEW_LINE if ( len ( pq ) == 0 ) : NEW_LINE INDENT return cntChar NEW_LINE DEDENT if ( frequent == pq [ - 1 ] ) : NEW_LINE INDENT if ( frequent > 1 ) : NEW_LINE INDENT pq . append ( frequent - 1 ) NEW_LINE DEDENT cntChar += 1 NEW_LINE DEDENT pq = sorted ( pq ) NEW_LINE DEDENT return cntChar NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " abbbcccd " NEW_LINE N = len ( str ) NEW_LINE print ( minCntCharDeletionsfrequency ( str , N ) ) NEW_LINE DEDENT
Program for Mean and median of an unsorted array | Function for calculating mean ; Function for calculating median ; First we sort the array ; check for even case ; Driver code ; Function call
def findMean ( a , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT sum += a [ i ] NEW_LINE DEDENT return float ( sum / n ) NEW_LINE DEDENT def findMedian ( a , n ) : NEW_LINE INDENT sorted ( a ) NEW_LINE if n % 2 != 0 : NEW_LINE INDENT return float ( a [ int ( n / 2 ) ] ) NEW_LINE DEDENT return float ( ( a [ int ( ( n - 1 ) / 2 ) ] + a [ int ( n / 2 ) ] ) / 2.0 ) NEW_LINE DEDENT a = [ 1 , 3 , 4 , 2 , 7 , 5 , 8 , 6 ] NEW_LINE n = len ( a ) NEW_LINE print ( " Mean ▁ = " , findMean ( a , n ) ) NEW_LINE print ( " Median ▁ = " , findMedian ( a , n ) ) NEW_LINE
Find sum of all left leaves in a given Binary Tree | A binary tree node ; A constructor to create a new Node ; Return the sum of left leaf nodes ; Using a stack for Depth - First Traversal of the tree ; sum holds the sum of all the left leaves ; Check if currentNode 's left child is a leaf node ; if currentNode is a leaf , add its data to the sum ; Driver Code
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 sumOfLeftLeaves ( root ) : NEW_LINE INDENT if ( root is None ) : NEW_LINE INDENT return NEW_LINE DEDENT stack = [ ] NEW_LINE stack . append ( root ) NEW_LINE sum = 0 NEW_LINE while len ( stack ) > 0 : NEW_LINE INDENT currentNode = stack . pop ( ) NEW_LINE if currentNode . left is not None : NEW_LINE INDENT stack . append ( currentNode . left ) NEW_LINE if currentNode . left . left is None and currentNode . left . right is None : NEW_LINE INDENT sum = sum + currentNode . left . data NEW_LINE DEDENT DEDENT if currentNode . right is not None : NEW_LINE INDENT stack . append ( currentNode . right ) NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT root = Tree ( 20 ) ; NEW_LINE root . left = Tree ( 9 ) ; NEW_LINE root . right = Tree ( 49 ) ; NEW_LINE root . right . left = Tree ( 23 ) ; NEW_LINE root . right . right = Tree ( 52 ) ; NEW_LINE root . right . right . left = Tree ( 50 ) ; NEW_LINE root . left . left = Tree ( 5 ) ; NEW_LINE root . left . right = Tree ( 12 ) ; NEW_LINE root . left . right . right = Tree ( 12 ) ; NEW_LINE print ( ' Sum ▁ of ▁ left ▁ leaves ▁ is ▁ { } ' . format ( sumOfLeftLeaves ( root ) ) ) NEW_LINE
Find a pair with sum N having minimum absolute difference | Function to find the value of X and Y having minimum value of abs ( X - Y ) ; If N is an odd number ; If N is an even number ; Driver Code
def findXandYwithminABSX_Y ( N ) : NEW_LINE INDENT if ( N % 2 == 1 ) : NEW_LINE INDENT print ( ( N // 2 ) , ( N // 2 + 1 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ( N // 2 - 1 ) , ( N // 2 + 1 ) ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 12 NEW_LINE findXandYwithminABSX_Y ( N ) NEW_LINE DEDENT
Maximum sum possible by assigning alternate positive and negative sign to elements in a subsequence | Function to find the maximum sum subsequence ; Base Case ; If current state is already calculated then use it ; If current element is positive ; Update ans and recursively call with update value of flag ; Else current element is negative ; Update ans and recursively call with update value of flag ; Return maximum sum subsequence ; Function that finds the maximum sum of element of the subsequence with alternate + ve and - ve signs ; Create auxiliary array dp [ ] [ ] ; Function call ; Driver Code ; Given array arr [ ] ; Function call
def findMax ( a , dp , i , flag ) : NEW_LINE INDENT if ( i == len ( a ) ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( dp [ i ] [ flag ] != - 1 ) : NEW_LINE INDENT return dp [ i ] [ flag ] NEW_LINE DEDENT ans = 0 NEW_LINE if ( flag == 0 ) : NEW_LINE INDENT ans = max ( findMax ( a , dp , i + 1 , 0 ) , a [ i ] + findMax ( a , dp , i + 1 , 1 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT ans = max ( findMax ( a , dp , i + 1 , 1 ) , - 1 * a [ i ] + findMax ( a , dp , i + 1 , 0 ) ) NEW_LINE DEDENT dp [ i ] [ flag ] = ans NEW_LINE return ans NEW_LINE DEDENT def findMaxSumUtil ( arr , N ) : NEW_LINE INDENT dp = [ [ - 1 for i in range ( 2 ) ] for i in range ( N ) ] NEW_LINE print ( findMax ( arr , dp , 0 , 0 ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 1 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE findMaxSumUtil ( arr , N ) NEW_LINE DEDENT
Find the player to last modify a string such that even number of consonants and no vowels are left in the string | Function to find a winner of the game if both the player plays optimally ; Stores the count of vowels and consonants ; Traverse the string ; Check if character is vowel ; Increment vowels count ; Otherwise increment the consonants count ; Check if Player B wins ; Check if Player A wins ; Check if Player A wins ; If game ends in a Draw ; Given String s ; Function Call
def findWinner ( s ) : NEW_LINE INDENT vowels_count = 0 NEW_LINE consonants_count = 0 NEW_LINE p = len ( s ) NEW_LINE for i in range ( 0 , p ) : NEW_LINE INDENT if ( s [ i ] == ' a ' or s [ i ] == ' e ' or s [ i ] == ' i ' or s [ i ] == ' o ' or s [ i ] == ' u ' ) : NEW_LINE INDENT vowels_count = vowels_count + 1 NEW_LINE DEDENT else : NEW_LINE INDENT consonants_count = consonants_count + 1 NEW_LINE DEDENT DEDENT if ( vowels_count == 0 ) : NEW_LINE INDENT if ( consonants_count % 2 == 0 ) : NEW_LINE INDENT print ( " Player ▁ B " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Player ▁ A " ) NEW_LINE DEDENT DEDENT elif ( vowels_count == 1 and consonants_count % 2 != 0 ) : NEW_LINE INDENT print ( " Player ▁ A " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " D " ) NEW_LINE DEDENT DEDENT s = " abcd " NEW_LINE findWinner ( s ) NEW_LINE
K maximum sums of overlapping contiguous sub | Function to compute prefix - sum of the input array ; Update maxi by k maximum values from maxi and cand ; Here cand and maxi arrays are in non - increasing order beforehand . Now , j is the index of the next cand element and i is the index of next maxi element . Traverse through maxi array . If cand [ j ] > maxi [ i ] insert cand [ j ] at the ith position in the maxi array and remove the minimum element of the maxi array i . e . the last element and increase j by 1 i . e . take the next element from cand . ; Insert prefix_sum [ i ] to mini array if needed ; Traverse the mini array from left to right . If prefix_sum [ i ] is less than any element then insert prefix_sum [ i ] at that position and delete maximum element of the mini array i . e . the rightmost element from the array . ; Function to compute k maximum overlapping sub - array sums ; Compute the prefix sum of the input array . ; Set all the elements of mini as + infinite except 0 th . Set the 0 th element as 0. ; Set all the elements of maxi as - infinite . ; Initialize cand array . ; For each element of the prefix_sum array do : compute the cand array . take k maximum values from maxi and cand using maxmerge function . insert prefix_sum [ i ] to mini array if needed using insertMini function . ; Elements of maxi array is the k maximum overlapping sub - array sums . Print out the elements of maxi array . ; Test case 1 ; Test case 2
def prefix_sum ( arr , n ) : NEW_LINE INDENT pre_sum = list ( ) NEW_LINE pre_sum . append ( arr [ 0 ] ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT pre_sum . append ( pre_sum [ i - 1 ] + arr [ i ] ) NEW_LINE DEDENT return pre_sum NEW_LINE DEDENT def maxMerge ( maxi , cand ) : NEW_LINE INDENT k = len ( maxi ) NEW_LINE j = 0 NEW_LINE for i in range ( k ) : NEW_LINE INDENT if ( cand [ j ] > maxi [ i ] ) : NEW_LINE INDENT maxi . insert ( i , cand [ j ] ) NEW_LINE del maxi [ - 1 ] NEW_LINE j += 1 NEW_LINE DEDENT DEDENT DEDENT def insertMini ( mini , pre_sum ) : NEW_LINE INDENT k = len ( mini ) NEW_LINE for i in range ( k ) : NEW_LINE INDENT if ( pre_sum < mini [ i ] ) : NEW_LINE INDENT mini . insert ( i , pre_sum ) NEW_LINE del mini [ - 1 ] NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT def kMaxOvSubArray ( arr , k ) : NEW_LINE INDENT n = len ( arr ) NEW_LINE pre_sum = prefix_sum ( arr , n ) NEW_LINE mini = [ float ( ' inf ' ) for i in range ( k ) ] NEW_LINE mini [ 0 ] = 0 NEW_LINE maxi = [ - float ( ' inf ' ) for i in range ( k ) ] NEW_LINE cand = [ 0 for i in range ( k ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( k ) : NEW_LINE INDENT cand [ j ] = pre_sum [ i ] - mini [ j ] NEW_LINE DEDENT maxMerge ( maxi , cand ) NEW_LINE insertMini ( mini , pre_sum [ i ] ) NEW_LINE DEDENT for ele in maxi : NEW_LINE INDENT print ( ele , end = ' ▁ ' ) NEW_LINE DEDENT print ( ' ' ) NEW_LINE DEDENT arr1 = [ 4 , - 8 , 9 , - 4 , 1 , - 8 , - 1 , 6 ] NEW_LINE k1 = 4 NEW_LINE kMaxOvSubArray ( arr1 , k1 ) NEW_LINE arr2 = [ - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 ] NEW_LINE k2 = 3 NEW_LINE kMaxOvSubArray ( arr2 , k2 ) NEW_LINE
k smallest elements in same order using O ( 1 ) extra space | Function to print smallest k numbers in arr [ 0. . n - 1 ] ; For each arr [ i ] find whether it is a part of n - smallest with insertion sort concept ; find largest from first k - elements ; if largest is greater than arr [ i ] shift all element one place left ; make arr [ k - 1 ] = arr [ i ] ; print result ; Driver program
def printSmall ( arr , n , k ) : NEW_LINE INDENT for i in range ( k , n ) : NEW_LINE INDENT max_var = arr [ k - 1 ] NEW_LINE pos = k - 1 NEW_LINE for j in range ( k - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( arr [ j ] > max_var ) : NEW_LINE INDENT max_var = arr [ j ] NEW_LINE pos = j NEW_LINE DEDENT DEDENT if ( max_var > arr [ i ] ) : NEW_LINE INDENT j = pos NEW_LINE while ( j < k - 1 ) : NEW_LINE INDENT arr [ j ] = arr [ j + 1 ] NEW_LINE j += 1 NEW_LINE DEDENT arr [ k - 1 ] = arr [ i ] NEW_LINE DEDENT DEDENT for i in range ( 0 , k ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 1 , 5 , 8 , 9 , 6 , 7 , 3 , 4 , 2 , 0 ] NEW_LINE n = len ( arr ) NEW_LINE k = 5 NEW_LINE printSmall ( arr , n , k ) NEW_LINE
Find k pairs with smallest sums in two arrays | Python3 program to prints first k pairs with least sum from two arrays . ; Function to find k pairs with least sum such that one elemennt of a pair is from arr1 [ ] and other element is from arr2 [ ] ; Stores current index in arr2 [ ] for every element of arr1 [ ] . Initially all values are considered 0. Here current index is the index before which all elements are considered as part of output . ; Initialize current pair sum as infinite ; To pick next pair , traverse for all elements of arr1 [ ] , for every element , find corresponding current element in arr2 [ ] and pick minimum of all formed pairs . ; Check if current element of arr1 [ ] plus element of array2 to be used gives minimum sum ; Update index that gives minimum ; update minimum sum ; Driver code
import sys NEW_LINE def kSmallestPair ( arr1 , n1 , arr2 , n2 , k ) : NEW_LINE INDENT if ( k > n1 * n2 ) : NEW_LINE INDENT print ( " k ▁ pairs ▁ don ' t ▁ exist " ) NEW_LINE return NEW_LINE DEDENT index2 = [ 0 for i in range ( n1 ) ] NEW_LINE while ( k > 0 ) : NEW_LINE INDENT min_sum = sys . maxsize NEW_LINE min_index = 0 NEW_LINE for i1 in range ( 0 , n1 , 1 ) : NEW_LINE INDENT if ( index2 [ i1 ] < n2 and arr1 [ i1 ] + arr2 [ index2 [ i1 ] ] < min_sum ) : NEW_LINE INDENT min_index = i1 NEW_LINE min_sum = arr1 [ i1 ] + arr2 [ index2 [ i1 ] ] NEW_LINE DEDENT DEDENT print ( " ( " , arr1 [ min_index ] , " , " , arr2 [ index2 [ min_index ] ] , " ) " , end = " ▁ " ) NEW_LINE index2 [ min_index ] += 1 NEW_LINE k -= 1 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr1 = [ 1 , 3 , 11 ] NEW_LINE n1 = len ( arr1 ) NEW_LINE arr2 = [ 2 , 4 , 8 ] NEW_LINE n2 = len ( arr2 ) NEW_LINE k = 4 NEW_LINE kSmallestPair ( arr1 , n1 , arr2 , n2 , k ) NEW_LINE DEDENT
Maximize count of strings of length 3 that can be formed from N 1 s and M 0 s | Function that counts the number of strings of length 3 that can be made with given m 0 s and n 1 s ; Print the count of strings ; Driver Code ; Given count of 1 s and 0 s ; Function call
def number_of_strings ( N , M ) : NEW_LINE INDENT print ( min ( N , min ( M , ( N + M ) // 3 ) ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 NEW_LINE M = 19 NEW_LINE number_of_strings ( N , M ) NEW_LINE DEDENT