codeparrot/xlcost-text-to-code huggingface dataset:
Maximum removals possible from an array such that sum of its elements is greater than or equal to that of another array | Function to maximize the count of elements required to be removed from arr [ ] such that the sum of arr [ ] is greater than or equal to sum of the array brr [ ] ; Sort the array arr [ ] ; Stores index of smallest element of arr [ ] ; Stores sum of elements of the array arr [ ] ; Traverse the array arr [ ] ; Update sumArr ; Stores sum of elements of the array brr [ ] ; Traverse the array brr [ ] ; Update sumArr ; Stores count of removed elements ; Repeatedly remove the smallest element of arr [ ] ; Update sumArr ; Remove the smallest element ; If the sum of remaining elements in arr [ ] >= sum of brr [ ] ; Update cntRemElem ; Driver Code
def maxCntRemovedfromArray ( arr , N , brr , M ) : NEW_LINE INDENT arr . sort ( reverse = False ) NEW_LINE i = 0 NEW_LINE sumArr = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT sumArr += arr [ i ] NEW_LINE DEDENT sumBrr = 0 NEW_LINE for i in range ( M ) : NEW_LINE INDENT sumBrr += brr [ i ] NEW_LINE DEDENT cntRemElem = 0 NEW_LINE while ( i < N and sumArr >= sumBrr ) : NEW_LINE INDENT sumArr -= arr [ i ] NEW_LINE i += 1 NEW_LINE if ( sumArr >= sumBrr ) : NEW_LINE INDENT cntRemElem += 1 NEW_LINE DEDENT DEDENT return cntRemElem NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 4 , 6 ] NEW_LINE brr = [ 7 ] NEW_LINE N = len ( arr ) NEW_LINE M = len ( brr ) NEW_LINE print ( maxCntRemovedfromArray ( arr , N , brr , M ) ) NEW_LINE DEDENT
Find any two pairs ( a , b ) and ( c , d ) such that a d | Function to find two pairs ( a , b ) and ( c , d ) such that a < c and b > d ; If no such pair is found ; Driver Code
def findPair ( arr , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT a , b = arr [ i ] [ 0 ] , arr [ i ] [ 1 ] NEW_LINE for j in range ( i + 1 , N ) : NEW_LINE INDENT c , d = arr [ j ] [ 0 ] , arr [ j ] [ 1 ] NEW_LINE if ( a < c and b > d ) : NEW_LINE INDENT print ( " ( " , a , b , " ) , ▁ ( " , c , d , " ) " ) NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT print ( " NO ▁ SUCH ▁ PAIR ▁ EXIST " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ 3 , 7 ] , [ 21 , 23 ] , [ 4 , 13 ] , [ 1 , 2 ] , [ 7 , - 1 ] ] NEW_LINE findPair ( arr , 5 ) NEW_LINE DEDENT
Find any two pairs ( a , b ) and ( c , d ) such that a d | Function to find two pairs ( a , b ) and ( c , d ) such that a < c and b > d ; Sort the array in increasing order of first element of pairs ; Traverse the array ; If no such pair found ; Driver Code
def findPair ( arr , N ) : NEW_LINE INDENT arr . sort ( key = lambda x : x [ 0 ] ) NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT b = arr [ i - 1 ] [ 1 ] NEW_LINE d = arr [ i ] [ 1 ] NEW_LINE if ( b > d ) : NEW_LINE INDENT print ( " ( " , arr [ i - 1 ] [ 0 ] , b , " ) , ▁ ( " , arr [ i ] [ 0 ] , d , " ) " ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( " NO SUCH PAIR EXIST " ) ; NEW_LINE DEDENT arr = [ [ 3 , 7 ] , [ 21 , 23 ] , [ 4 , 13 ] , [ 1 , 2 ] , [ 7 , - 1 ] ] NEW_LINE findPair ( arr , 5 ) NEW_LINE
Print indices in non | Function to print the order of array elements generating non - decreasing quotient after division by X ; Stores the quotient and the order ; Traverse the array ; Sort the vector ; Print the order ; Driver Code
def printOrder ( order , N , X ) : NEW_LINE INDENT vect = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( order [ i ] % X == 0 ) : NEW_LINE INDENT vect . append ( [ order [ i ] // X , i + 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT vect . append ( [ order [ i ] // X + 1 , i + 1 ] ) NEW_LINE DEDENT DEDENT vect = sorted ( vect ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT print ( vect [ i ] [ 1 ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N , X = 3 , 3 NEW_LINE order = [ 2 , 7 , 4 ] NEW_LINE printOrder ( order , N , X ) NEW_LINE DEDENT
Shuffle 2 n integers as a1 | Function to reverse the array from the position ' start ' to position 'end ; Stores mid of start and end ; Traverse the array in the range [ start , end ] ; Stores arr [ start + i ] ; Update arr [ start + i ] ; Update arr [ end - i ] ; Utility function to shuffle the given array in the of form { a1 , b1 , a2 , b2 , ... . an , bn } ; Stores the length of the array ; If length of the array is 2 ; Stores mid of the { start , end } ; Divide array into two halves of even length ; Update mid ; Calculate the mid - points of both halves of the array ; Reverse the subarray made from mid1 to mid2 ; Reverse the subarray made from mid1 to mid ; Reverse the subarray made from mid to mid2 ; Recursively calls for both the halves of the array ; Function to shuffle the given array in the form of { a1 , b1 , a2 , b2 , ... . an , bn } ; Function Call ; Print the modified array ; Driver Code ; Given array ; Size of the array ; Shuffles the given array to the required permutation
' NEW_LINE def reverse ( arr , start , end ) : NEW_LINE INDENT mid = ( end - start + 1 ) // 2 NEW_LINE for i in range ( mid ) : NEW_LINE INDENT temp = arr [ start + i ] NEW_LINE arr [ start + i ] = arr [ end - i ] NEW_LINE arr [ end - i ] = temp NEW_LINE DEDENT return arr NEW_LINE DEDENT def shuffleArrayUtil ( arr , start , end ) : NEW_LINE INDENT i = 0 NEW_LINE l = end - start + 1 NEW_LINE if ( l == 2 ) : NEW_LINE INDENT return NEW_LINE DEDENT mid = start + l // 2 NEW_LINE if ( l % 4 ) : NEW_LINE INDENT mid -= 1 NEW_LINE DEDENT mid1 = start + ( mid - start ) // 2 NEW_LINE mid2 = mid + ( end + 1 - mid ) // 2 NEW_LINE arr = reverse ( arr , mid1 , mid2 - 1 ) NEW_LINE arr = reverse ( arr , mid1 , mid - 1 ) NEW_LINE arr = reverse ( arr , mid , mid2 - 1 ) NEW_LINE shuffleArrayUtil ( arr , start , mid - 1 ) NEW_LINE shuffleArrayUtil ( arr , mid , end ) NEW_LINE DEDENT def shuffleArray ( arr , N , start , end ) : NEW_LINE INDENT shuffleArrayUtil ( arr , start , end ) NEW_LINE for i in arr : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 3 , 5 , 2 , 4 , 6 ] NEW_LINE N = len ( arr ) NEW_LINE shuffleArray ( arr , N , 0 , N - 1 ) NEW_LINE DEDENT
Reduce sum of same | Function to check if elements of B can be rearranged such that A [ i ] + B [ i ] <= X ; Checks the given condition ; Sort A in ascending order ; Sort B in descending order ; Traverse the arrays A and B ; If A [ i ] + B [ i ] exceeds X ; Rearrangement not possible , set flag to false ; If flag is true ; Otherwise ; Driver Code ; Function Call
def rearrange ( A , B , N , X ) : NEW_LINE INDENT flag = True NEW_LINE A = sorted ( A ) NEW_LINE B = sorted ( B ) [ : : - 1 ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( A [ i ] + B [ i ] > X ) : NEW_LINE INDENT flag = False NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 2 , 3 ] NEW_LINE B = [ 1 , 1 , 2 ] NEW_LINE X = 4 NEW_LINE N = len ( A ) NEW_LINE rearrange ( A , B , N , X ) NEW_LINE DEDENT
Minimum increments or decrements by D required to make all array elements equal | Function to find minimum count of operations required to make all array elements equal by incrementing or decrementing array elements by d ; Sort the array ; Traverse the array ; If difference between two consecutive elements are not divisible by D ; Store minimum count of operations required to make all array elements equal by incrementing or decrementing array elements by d ; Stores middle element of the array ; Traverse the array ; Update count ; Driver Code ; Given N & D ; Given array arr [ ] ; Function Call
def numOperation ( arr , N , D ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE for i in range ( N - 1 ) : NEW_LINE INDENT if ( ( arr [ i + 1 ] - arr [ i ] ) % D != 0 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT DEDENT count = 0 NEW_LINE mid = arr [ N // 2 ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT count += abs ( mid - arr [ i ] ) // D NEW_LINE DEDENT print ( count ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 NEW_LINE D = 2 NEW_LINE arr = [ 2 , 4 , 6 , 8 ] NEW_LINE numOperation ( arr , N , D ) NEW_LINE DEDENT
Maximize sum of second minimums of each K length partitions of the array | Function to find the maximum sum of second smallest of each partition of size K ; Sort the array A in ascending order ; Store the maximum sum of second smallest of each partition of size K ; Select every ( K - 1 ) th element as second smallest element ; Update sum ; Print the maximum sum ; Driver Code ; Given size of partitions ; Given array A ; Size of the given array ; Function Call
def findSum ( A , N , K ) : NEW_LINE INDENT A . sort ( ) ; NEW_LINE sum = 0 ; NEW_LINE for i in range ( N // K , N , K - 1 ) : NEW_LINE INDENT sum += A [ i ] ; NEW_LINE DEDENT print ( sum ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT K = 4 ; NEW_LINE A = [ 2 , 3 , 1 , 4 , 7 , 5 , 6 , 1 ] ; NEW_LINE N = len ( A ) ; NEW_LINE findSum ( A , N , K ) ; NEW_LINE DEDENT
Minimize remaining array element by removing pairs and replacing them with their average | Function to find the smallest element left in the array by removing pairs and inserting their average ; Stores array elements such that the largest element present at top of PQ ; Traverse the array ; Insert arr [ i ] into PQ ; Iterate over elements of PQ while count of elements in PQ greater than 1 ; Stores largest element of PQ ; Pop the largest element of PQ ; Stores largest element of PQ ; Pop the largest element of PQ ; Insert the ceil value of average of top1 and top2 ; Driver Code
def findSmallestNumLeft ( arr , N ) : NEW_LINE INDENT PQ = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT PQ . append ( arr [ i ] ) NEW_LINE DEDENT PQ = sorted ( PQ ) NEW_LINE while ( len ( PQ ) > 1 ) : NEW_LINE INDENT top1 = PQ [ - 1 ] NEW_LINE del PQ [ - 1 ] NEW_LINE top2 = PQ [ - 1 ] NEW_LINE del PQ [ - 1 ] NEW_LINE PQ . append ( ( top1 + top2 + 1 ) // 2 ) NEW_LINE PQ = sorted ( PQ ) NEW_LINE DEDENT return PQ [ - 1 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 30 , 16 , 40 ] NEW_LINE N = len ( arr ) NEW_LINE print ( findSmallestNumLeft ( arr , N ) ) NEW_LINE DEDENT
Find the array element from indices not divisible by K having largest composite product of digits | Python3 program to implement the above approach ; Function to check if a number is a composite number or not ; Corner cases ; Check if number is divisible by 2 or 3 ; Check if number is a multiple of any other prime number ; Function to calculate the product of digits of a number ; Stores the product of digits ; Extract digits of a number ; Calculate product of digits ; Function to check if the product of digits of a number is a composite number or not ; Stores product of digits ; If product of digits is equal to 1 ; If product of digits is not prime ; Function to find the number with largest composite product of digits from the indices not divisible by k from the given array ; Traverse the array ; If index is divisible by k ; Check if product of digits is a composite number or not ; Sort the products ; Driver Code
from math import ceil , sqrt NEW_LINE def isComposite ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT for i in range ( 5 , ceil ( sqrt ( n ) ) , 6 ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def digitProduct ( number ) : NEW_LINE INDENT product = 1 NEW_LINE while ( number > 0 ) : NEW_LINE INDENT product *= ( number % 10 ) NEW_LINE number //= 10 NEW_LINE DEDENT return product NEW_LINE DEDENT def compositedigitProduct ( num ) : NEW_LINE INDENT res = digitProduct ( num ) NEW_LINE if ( res == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( isComposite ( res ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def largestCompositeDigitProduct ( a , n , k ) : NEW_LINE INDENT pq = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( ( i % k ) == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( compositedigitProduct ( a [ i ] ) ) : NEW_LINE INDENT b = digitProduct ( a [ i ] ) NEW_LINE pq . append ( [ b , a [ i ] ] ) NEW_LINE DEDENT DEDENT pq = sorted ( pq ) NEW_LINE return pq [ - 1 ] [ 1 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 233 , 144 , 89 , 71 , 13 , 21 , 11 , 34 , 55 , 23 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE ans = largestCompositeDigitProduct ( arr , n , k ) NEW_LINE print ( ans ) NEW_LINE DEDENT
Split array into K | Function to find the minimum sum of 2 nd smallest elements of each subset ; Sort the array ; Stores minimum sum of second elements of each subset ; Traverse first K 2 nd smallest elements ; Update their sum ; Prthe sum ; Driver Code ; Given Array ; Given size of the array ; Given subset lengths
def findMinimum ( arr , N , K ) : NEW_LINE INDENT arr = sorted ( arr ) NEW_LINE ans = 0 NEW_LINE for i in range ( 1 , 2 * ( N // K ) , 2 ) : NEW_LINE INDENT ans += arr [ i ] NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 11 , 20 , 5 , 7 , 8 , 14 , 2 , 17 , 16 , 10 ] NEW_LINE N = len ( arr ) NEW_LINE K = 5 NEW_LINE findMinimum ( arr , N , K ) NEW_LINE DEDENT
Find two non | Function to check if two non - intersecting subarrays with equal sum exists or not ; Sort the given array ; Traverse the array ; Check for duplicate elements ; If no duplicate element is present in the array ; Driver code ; Given array ; Size of array
def findSubarrays ( arr , N ) : NEW_LINE INDENT arr . sort ( ) ; NEW_LINE i = 0 ; NEW_LINE for i in range ( N - 1 ) : NEW_LINE INDENT if ( arr [ i ] == arr [ i + 1 ] ) : NEW_LINE INDENT print ( " YES " ) ; NEW_LINE return ; NEW_LINE DEDENT DEDENT print ( " NO " ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 3 , 0 , 1 , 2 , 0 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE findSubarrays ( arr , N ) ; NEW_LINE DEDENT
Selection Sort VS Bubble Sort | Function for bubble sort ; Iterate from 1 to n - 1 ; Iterate from 0 to n - i - 1 ; Driver Code
def Bubble_Sort ( arr , n ) : NEW_LINE INDENT flag = True NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT flag = False NEW_LINE for j in range ( n - i ) : NEW_LINE INDENT if ( arr [ j ] > arr [ j + 1 ] ) : NEW_LINE INDENT arr [ j ] , arr [ j + 1 ] = arr [ j + 1 ] , arr [ j ] NEW_LINE flag = True NEW_LINE DEDENT DEDENT if ( flag == False ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT n = 5 NEW_LINE arr = [ 2 , 0 , 1 , 4 , 3 ] NEW_LINE Bubble_Sort ( arr , n ) NEW_LINE print ( " The ▁ Sorted ▁ Array ▁ by ▁ using ▁ Bubble ▁ Sort ▁ is ▁ : ▁ " , end = ' ' ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT
Print all array elements appearing more than N / K times | Function to prall array elements whose frequency is greater than N / K ; Sort the array , arr [ ] ; Traverse the array ; Stores frequency of arr [ i ] ; Traverse array elements which is equal to arr [ i ] ; Update cnt ; Update i ; If frequency of arr [ i ] is greater than ( N / K ) ; Driver Code
def NDivKWithFreq ( arr , N , K ) : NEW_LINE INDENT arr = sorted ( arr ) NEW_LINE i = 0 NEW_LINE while i < N : NEW_LINE INDENT cnt = 1 NEW_LINE while ( ( i + 1 ) < N and arr [ i ] == arr [ i + 1 ] ) : NEW_LINE INDENT cnt += 1 NEW_LINE i += 1 NEW_LINE DEDENT if ( cnt > ( N // K ) ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT i += 1 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 2 , 6 , 6 , 6 , 6 , 7 , 10 ] NEW_LINE N = len ( arr ) NEW_LINE K = 4 NEW_LINE NDivKWithFreq ( arr , N , K ) NEW_LINE DEDENT
Sort M elements of given circular array starting from index K | Function to print the circular array ; Print the array ; Function to sort m elements of diven circular array starting from index k ; Traverse M elements ; Iterate from index k to k + m - 1 ; Check if the next element in the circular array is less than the current element ; Swap current element with the next element ; Print the circular array ; Driver Code ; Function Call
def printCircularArray ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT def sortCircularArray ( arr , n , k , m ) : NEW_LINE INDENT for i in range ( m ) : NEW_LINE INDENT for j in range ( k , k + m - 1 ) : NEW_LINE INDENT if ( arr [ j % n ] > arr [ ( j + 1 ) % n ] ) : NEW_LINE INDENT arr [ j % n ] , arr [ ( j + 1 ) % n ] = ( arr [ ( j + 1 ) % n ] , arr [ j % n ] ) NEW_LINE DEDENT DEDENT DEDENT printCircularArray ( arr , n ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 1 , 6 , 5 , 3 ] NEW_LINE K = 2 NEW_LINE M = 3 NEW_LINE N = len ( arr ) NEW_LINE sortCircularArray ( arr , N , K , M ) NEW_LINE DEDENT
Minimize cost required to complete all processes | Function to find minimum cost required to complete all the process ; Sort the array on descending order of Y ; Stores length of array ; Stores minimum cost required to complete all the process ; Stores minimum cost to initiate any process ; Traverse the array ; If minCostInit is less than the cost to initiate the process ; Update minCost ; Update minCostInit ; Update minCostInit ; Return minCost ; Driver Code ; Function Call
def minimumCostReqToCompthePrcess ( arr ) : NEW_LINE INDENT arr . sort ( key = lambda x : x [ 0 ] - x [ 1 ] ) NEW_LINE n = len ( arr ) NEW_LINE minCost = 0 NEW_LINE minCostInit = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if arr [ i ] [ 1 ] > minCostInit : NEW_LINE INDENT minCost += ( arr [ i ] [ 1 ] - minCostInit ) NEW_LINE minCostInit = arr [ i ] [ 1 ] NEW_LINE DEDENT minCostInit -= arr [ i ] [ 0 ] NEW_LINE DEDENT return minCost NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ [ 1 , 2 ] , [ 2 , 4 ] , [ 4 , 8 ] ] NEW_LINE print ( minimumCostReqToCompthePrcess ( arr ) ) NEW_LINE DEDENT
Split array into equal length subsets with maximum sum of Kth largest element of each subset | Function to find the maximum sum of Kth largest element of M equal length partition ; Stores sum of K_th largest element of equal length partitions ; If N is not divisible by M ; Stores length of each partition ; If K is greater than length of partition ; Sort array in descending porder ; Traverse the array ; Update maxSum ; Driver code
def maximumKthLargestsumPart ( arr , N , M , K ) : NEW_LINE INDENT maxSum = 0 NEW_LINE if ( N % M != 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT sz = ( N / M ) NEW_LINE if ( K > sz ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT arr = sorted ( arr ) NEW_LINE for i in range ( 0 , N // 2 ) : NEW_LINE INDENT t = arr [ i ] NEW_LINE arr [ i ] = arr [ N - i - 1 ] NEW_LINE arr [ N - i - 1 ] = t NEW_LINE DEDENT for i in range ( 1 , M + 1 ) : NEW_LINE INDENT maxSum += arr [ i * K - 1 ] NEW_LINE DEDENT return maxSum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] NEW_LINE M = 2 NEW_LINE K = 1 NEW_LINE N = len ( arr ) NEW_LINE print ( maximumKthLargestsumPart ( arr , N , M , K ) ) NEW_LINE DEDENT
Minimize difference between maximum and minimum array elements by exactly K removals | Function to minimize the difference of the maximum and minimum array elements by removing K elements ; Base Condition ; Sort the array ; Initialize left and right pointers ; Iterate for K times ; Removing right element to reduce the difference ; Removing the left element to reduce the difference ; Print the minimum difference ; Driver Code ; Function Call
def minimumRange ( arr , N , K ) : NEW_LINE INDENT if ( K >= N ) : NEW_LINE INDENT print ( 0 , end = ' ' ) ; NEW_LINE return ; NEW_LINE DEDENT arr . sort ( ) ; NEW_LINE left = 0 ; right = N - 1 ; NEW_LINE for i in range ( K ) : NEW_LINE INDENT if ( arr [ right - 1 ] - arr [ left ] < arr [ right ] - arr [ left + 1 ] ) : NEW_LINE INDENT right -= 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT left += 1 ; NEW_LINE DEDENT DEDENT print ( arr [ right ] - arr [ left ] , end = ' ' ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 10 , 12 , 14 , 21 , 54 , 61 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE K = 4 ; NEW_LINE minimumRange ( arr , N , K ) ; NEW_LINE DEDENT
Split array into K subsets to maximize sum of their second largest elements | Function to split array into K subsets having maximum sum of their second maximum elements ; Sort the array ; Stores the maximum possible sum of second maximums ; Add second maximum of current subset ; Proceed to the second maximum of next subset ; Print the maximum sum obtained ; Driver Code ; Given array arr [ ] ; Size of array ; Function Call
def splitArray ( arr , n , K ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE i = n - 1 NEW_LINE result = 0 NEW_LINE while ( K > 0 ) : NEW_LINE INDENT result += arr [ i - 1 ] NEW_LINE i -= 2 NEW_LINE K -= 1 NEW_LINE DEDENT print ( result ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 3 , 1 , 5 , 1 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE K = 2 NEW_LINE splitArray ( arr , N , K ) NEW_LINE DEDENT
Count subsequences for every array element in which they are the maximum | Function to merge the subarrays arr [ l . . m ] and arr [ m + 1 , . . r ] based on indices [ ] ; If a [ indices [ l ] ] is less than a [ indices [ j ] ] , add indice [ l ] to temp ; Else add indices [ j ] ; Add remaining elements ; Add remainging elements ; Recursive function to divide the array into to parts ; Recursive call for elements before mid ; Recursive call for elements after mid ; Merge the two sorted arrays ; Function to find the number of subsequences for each element ; Sorting the indices according to array arr [ ] ; Array to store output numbers ; Initialize subseq ; B [ i ] is 2 ^ i ; Doubling the subsequences ; Print the final output , array B [ ] ; Driver Code ; Given array ; Given length ; Function call
def merge ( indices , a , l , mid , r ) : NEW_LINE INDENT temp_ind = [ 0 ] * ( r - l + 1 ) NEW_LINE j = mid + 1 NEW_LINE i = 0 NEW_LINE temp_l = l NEW_LINE while ( l <= mid and j <= r ) : NEW_LINE INDENT if ( a [ indices [ l ] ] < a [ indices [ j ] ] ) : NEW_LINE INDENT temp_ind [ i ] = indices [ l ] NEW_LINE i += 1 NEW_LINE l += 1 NEW_LINE DEDENT else : NEW_LINE INDENT temp_ind [ i ] = indices [ j ] NEW_LINE i += 1 NEW_LINE j += 1 NEW_LINE DEDENT DEDENT while ( l <= mid ) : NEW_LINE INDENT temp_ind [ i ] = indices [ l ] NEW_LINE i += 1 NEW_LINE l += 1 NEW_LINE DEDENT while ( j <= r ) : NEW_LINE INDENT temp_ind [ i ] = indices [ j ] NEW_LINE i += 1 NEW_LINE j += 1 NEW_LINE DEDENT for k in range ( i ) : NEW_LINE INDENT indices [ temp_l ] = temp_ind [ k ] NEW_LINE temp_l += 1 NEW_LINE DEDENT DEDENT def divide ( indices , a , l , r ) : NEW_LINE INDENT if ( l >= r ) : NEW_LINE INDENT return NEW_LINE DEDENT mid = l // 2 + r // 2 NEW_LINE divide ( indices , a , l , mid ) NEW_LINE divide ( indices , a , mid + 1 , r ) NEW_LINE merge ( indices , a , l , mid , r ) NEW_LINE DEDENT def noOfSubsequences ( arr , N ) : NEW_LINE INDENT indices = N * [ 0 ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT indices [ i ] = i NEW_LINE DEDENT divide ( indices , arr , 0 , N - 1 ) NEW_LINE B = [ 0 ] * N NEW_LINE subseq = 1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT B [ indices [ i ] ] = subseq NEW_LINE subseq *= 2 NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT print ( B [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 3 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE noOfSubsequences ( arr , N ) NEW_LINE DEDENT