codeparrot/xlcost-text-to-code huggingface dataset:
Front and Back Search in unsorted array | Python program to implement front and back search ; Start searching from both ends ; Keep searching while two indexes do not cross . ; Driver code
def search ( arr , n , x ) : NEW_LINE INDENT front = 0 ; back = n - 1 NEW_LINE while ( front <= back ) : NEW_LINE INDENT if ( arr [ front ] == x or arr [ back ] == x ) : NEW_LINE INDENT return True NEW_LINE DEDENT front += 1 NEW_LINE back -= 1 NEW_LINE DEDENT return False NEW_LINE DEDENT arr = [ 10 , 20 , 80 , 30 , 60 , 50 , 110 , 100 , 130 , 170 ] NEW_LINE x = 130 NEW_LINE n = len ( arr ) NEW_LINE if ( search ( arr , n , x ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Even | Python3 code to find max ( X , Y ) / min ( X , Y ) after P turns ; 1 st test case ; 2 nd test case
def findValue ( X , Y , P ) : NEW_LINE INDENT if P % 2 == 0 : NEW_LINE INDENT return int ( max ( X , Y ) / min ( X , Y ) ) NEW_LINE DEDENT else : NEW_LINE INDENT return int ( max ( 2 * X , Y ) / min ( 2 * X , Y ) ) NEW_LINE DEDENT DEDENT X = 1 NEW_LINE Y = 2 NEW_LINE P = 1 NEW_LINE print ( findValue ( X , Y , P ) ) NEW_LINE X = 3 NEW_LINE Y = 7 NEW_LINE P = 2 NEW_LINE print ( ( findValue ( X , Y , P ) ) ) NEW_LINE
The painter 's partition problem | function to calculate sum between two indices in list ; bottom up tabular dp ; initialize table ; base cases k = 1 ; n = 1 ; 2 to k partitions for i in range ( 2 , k + 1 ) : 2 to n boards ; track minimum ; i - 1 th separator before position arr [ p = 1. . j ] ; required ; Driver Code ; Calculate size of array .
def sum ( arr , start , to ) : NEW_LINE INDENT total = 0 NEW_LINE for i in range ( start , to + 1 ) : NEW_LINE INDENT total += arr [ i ] NEW_LINE DEDENT return total NEW_LINE DEDENT def findMax ( arr , n , k ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( n + 1 ) ] for j in range ( k + 1 ) ] NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT dp [ 1 ] [ i ] = sum ( arr , 0 , i - 1 ) NEW_LINE DEDENT for i in range ( 1 , k + 1 ) : NEW_LINE INDENT dp [ i ] [ 1 ] = arr [ 0 ] NEW_LINE for j in range ( 2 , n + 1 ) : NEW_LINE INDENT best = 100000000 NEW_LINE for p in range ( 1 , j + 1 ) : NEW_LINE INDENT best = min ( best , max ( dp [ i - 1 ] [ p ] , sum ( arr , p , j - 1 ) ) ) NEW_LINE DEDENT dp [ i ] [ j ] = best NEW_LINE DEDENT DEDENT return dp [ k ] [ n ] NEW_LINE DEDENT arr = [ 10 , 20 , 60 , 50 , 30 , 40 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE print ( findMax ( arr , n , k ) ) NEW_LINE INDENT n = len ( arr ) NEW_LINE DEDENT k = 3 NEW_LINE print ( findMax ( arr , n , k ) ) NEW_LINE
Counting cross lines in an array | Function return count of cross line in an array ; Move elements of arr [ 0. . i - 1 ] , that are greater than key , to one position ahead of their current position ; increment cross line by one ; Driver Code
def countCrossLine ( arr , n ) : NEW_LINE INDENT count_crossline = 0 ; NEW_LINE i , key , j = 0 , 0 , 0 ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT key = arr [ i ] ; NEW_LINE j = i - 1 ; NEW_LINE while ( j >= 0 and arr [ j ] > key ) : NEW_LINE INDENT arr [ j + 1 ] = arr [ j ] ; NEW_LINE j = j - 1 ; NEW_LINE count_crossline += 1 ; NEW_LINE DEDENT arr [ j + 1 ] = key ; NEW_LINE DEDENT return count_crossline ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 3 , 1 , 2 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( countCrossLine ( arr , n ) ) ; NEW_LINE DEDENT
Recursive Programs to find Minimum and Maximum elements of array | function to return maximum element using recursion ; if n = 0 means whole array has been traversed ; Driver Code ; Function calling
def findMaxRec ( A , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return A [ 0 ] NEW_LINE DEDENT return max ( A [ n - 1 ] , findMaxRec ( A , n - 1 ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 1 , 4 , 45 , 6 , - 50 , 10 , 2 ] NEW_LINE n = len ( A ) NEW_LINE print ( findMaxRec ( A , n ) ) NEW_LINE DEDENT
Program to remove vowels from a String | Python program to remove vowels from a string Function to remove vowels ; Driver program
import re NEW_LINE def rem_vowel ( string ) : NEW_LINE INDENT return ( re . sub ( " [ aeiouAEIOU ] " , " " , string ) ) NEW_LINE DEDENT string = " GeeksforGeeks ▁ - ▁ A ▁ Computer ▁ Science ▁ Portal ▁ for ▁ Geeks " NEW_LINE print rem_vowel ( string ) NEW_LINE
Minimum number of swaps required to minimize sum of absolute differences between adjacent array elements | Function to find the minimum number of swaps required to sort the array in increasing order ; Stores the array elements with its index ; Sort the array in the increasing order ; Keeps the track of visited elements ; Stores the count of swaps required ; Traverse array elements ; If the element is already swapped or at correct position ; Find out the number of nodes in this cycle ; Update the value of j ; Move to the next element ; Increment cycle_size ; Update the ans by adding current cycle ; Function to find the minimum number of swaps required to sort the array in decreasing order ; Stores the array elements with its index ; Sort the array in the descending order ; Keeps track of visited elements ; Stores the count of resultant swap required ; Traverse array elements ; If the element is already swapped or at correct position ; Find out the number of node in this cycle ; Update the value of j ; Move to the next element ; Increment the cycle_size ; Update the ans by adding current cycle size ; Function to find minimum number of swaps required to minimize the sum of absolute difference of adjacent elements ; Sort in ascending order ; Sort in descending order ; Return the minimum value ; Drive Code
def minSwapsAsc ( arr , n ) : NEW_LINE INDENT arrPos = [ [ arr [ i ] , i ] for i in range ( n ) ] NEW_LINE arrPos = sorted ( arrPos ) NEW_LINE vis = [ False ] * ( n ) NEW_LINE ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( vis [ i ] or arrPos [ i ] [ 1 ] == i ) : NEW_LINE INDENT continue NEW_LINE DEDENT cycle_size = 0 NEW_LINE j = i NEW_LINE while ( not vis [ j ] ) : NEW_LINE INDENT vis [ j ] = 1 NEW_LINE j = arrPos [ j ] [ 1 ] NEW_LINE cycle_size += 1 NEW_LINE DEDENT if ( cycle_size > 0 ) : NEW_LINE INDENT ans += ( cycle_size - 1 ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT def minSwapsDes ( arr , n ) : NEW_LINE INDENT arrPos = [ [ 0 , 0 ] for i in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT arrPos [ i ] [ 0 ] = arr [ i ] NEW_LINE arrPos [ i ] [ 1 ] = i NEW_LINE DEDENT arrPos = sorted ( arrPos ) [ : : - 1 ] NEW_LINE vis = [ False ] * n NEW_LINE ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( vis [ i ] or arrPos [ i ] [ 1 ] == i ) : NEW_LINE INDENT continue NEW_LINE DEDENT cycle_size = 0 NEW_LINE j = i NEW_LINE while ( not vis [ j ] ) : NEW_LINE INDENT vis [ j ] = 1 NEW_LINE j = arrPos [ j ] [ 1 ] NEW_LINE cycle_size += 1 NEW_LINE DEDENT if ( cycle_size > 0 ) : NEW_LINE INDENT ans += ( cycle_size - 1 ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT def minimumSwaps ( arr ) : NEW_LINE INDENT S1 = minSwapsAsc ( arr , len ( arr ) ) NEW_LINE S2 = minSwapsDes ( arr , len ( arr ) ) NEW_LINE return min ( S1 , S2 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 4 , 2 , 5 , 1 ] NEW_LINE print ( minimumSwaps ( arr ) ) NEW_LINE DEDENT
Sort an array of 0 s , 1 s , 2 s and 3 s | Function to sort the array having array element only 0 , 1 , 2 , and 3 ; Iterate until mid <= j ; If arr [ mid ] is 0 ; Swap integers at indices i and mid ; Increment i ; Increment mid ; Otherwise if the value of arr [ mid ] is 3 ; Swap arr [ mid ] and arr [ j ] ; Decrement j ; Otherwise if the value of arr [ mid ] is either 1 or 2 ; Increment the value of mid ; Iterate until i <= j ; If arr [ i ] the value of is 2 ; Swap arr [ i ] and arr [ j ] ; Decrement j ; Otherwise , increment i ; Print the sorted array arr [ ] ; Driver Code
def sortArray ( arr , N ) : NEW_LINE INDENT i = 0 NEW_LINE j = N - 1 NEW_LINE mid = 0 NEW_LINE while ( mid <= j ) : NEW_LINE INDENT if ( arr [ mid ] == 0 ) : NEW_LINE INDENT arr [ i ] , arr [ mid ] = arr [ mid ] , arr [ i ] NEW_LINE i += 1 NEW_LINE mid += 1 NEW_LINE DEDENT elif ( arr [ mid ] == 3 ) : NEW_LINE INDENT arr [ mid ] , arr [ j ] = arr [ j ] , arr [ mid ] NEW_LINE j -= 1 NEW_LINE DEDENT elif ( arr [ mid ] == 1 or arr [ mid ] == 2 ) : NEW_LINE INDENT mid += 1 NEW_LINE DEDENT DEDENT while ( i <= j ) : NEW_LINE INDENT if ( arr [ i ] == 2 ) : NEW_LINE INDENT arr [ i ] , arr [ j ] = arr [ j ] , arr [ i ] NEW_LINE j -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT i += 1 NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 2 , 1 , 0 , 2 , 3 , 1 , 0 ] NEW_LINE N = len ( arr ) NEW_LINE sortArray ( arr , N ) NEW_LINE DEDENT
Check whether an array can be made strictly increasing by removing at most one element | Function to find if is it possible to make the array strictly increasing by removing at most one element ; Stores the count of numbers that are needed to be removed ; Store the index of the element that needs to be removed ; Traverse the range [ 1 , N - 1 ] ; If arr [ i - 1 ] is greater than or equal to arr [ i ] ; Increment the count by 1 ; Update index ; If count is greater than one ; If no element is removed ; If only the last or the first element is removed ; If a [ index ] is removed ; If a [ index - 1 ] is removed ; Driver Code
def check ( arr , n ) : NEW_LINE INDENT count = 0 NEW_LINE index = - 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i - 1 ] >= arr [ i ] ) : NEW_LINE INDENT count += 1 NEW_LINE index = i NEW_LINE DEDENT DEDENT if ( count > 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( count == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( index == n - 1 or index == 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( arr [ index - 1 ] < arr [ index + 1 ] ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( arr [ index - 2 ] < arr [ index ] ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 5 , 3 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE if ( check ( arr , N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Modify string by rearranging vowels in alphabetical order at their respective indices | Function to arrange the vowels in sorted order in the string at their respective places ; Store the size of the string ; Stores vowels of string S ; Traverse the string , S and push all the vowels to string vow ; If vow is empty , then print S and return ; Sort vow in alphabetical order ; Traverse the string , S ; Replace S [ i ] with vow [ j ] iif S [ i ] is a vowel , and increment j by 1 ; Print the string ; Driver Code
def sortVowels ( S ) : NEW_LINE INDENT n = len ( S ) ; NEW_LINE vow = " " ; NEW_LINE for i in range ( n ) : 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 vow += S [ i ] ; NEW_LINE DEDENT DEDENT if len ( vow ) == 0 : NEW_LINE INDENT print ( S , end = " " ) ; NEW_LINE return ; NEW_LINE DEDENT vow = list ( vow ) ; NEW_LINE vow . sort ( ) ; NEW_LINE j = 0 ; NEW_LINE for i in range ( n ) : 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 S [ i ] = vow [ j ] ; NEW_LINE j += 1 ; NEW_LINE DEDENT DEDENT print ( " " . join ( S ) , end = " " ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " geeksforgeeks " ; NEW_LINE sortVowels ( list ( S ) ) ; NEW_LINE DEDENT
Maximum number of buckets that can be filled | Function to find the maximum number of buckets that can be filled with the amount of water available ; Find the total available water ; Sort the array in ascending order ; Check if bucket can be filled with available water ; Print count of buckets ; Driver code
def getBuckets ( arr , N ) : NEW_LINE INDENT availableWater = N * ( N - 1 ) // 2 NEW_LINE arr . sort ( ) NEW_LINE i , Sum = 0 , 0 NEW_LINE while ( Sum <= availableWater ) : NEW_LINE INDENT Sum += arr [ i ] NEW_LINE i += 1 NEW_LINE DEDENT print ( i - 1 , end = " " ) NEW_LINE DEDENT arr = [ 1 , 5 , 3 , 4 , 7 , 9 ] NEW_LINE N = len ( arr ) NEW_LINE getBuckets ( arr , N ) ; NEW_LINE
Minimize the number of strictly increasing subsequences in an array | Set 2 | Function to find the number of strictly increasing subsequences in an array ; Sort the array ; Stores final count of subsequences ; Traverse the array ; Stores current element ; Stores frequency of the current element ; Count frequency of the current element ; If current element frequency is greater than count ; Print the final count ;  ; Given array ; Size of the array ; Function call to find the number of strictly increasing subsequences
def minimumIncreasingSubsequences ( arr , N ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE count = 0 NEW_LINE i = 0 NEW_LINE while ( i < N ) : NEW_LINE INDENT x = arr [ i ] NEW_LINE freqX = 0 NEW_LINE while ( i < N and arr [ i ] == x ) : NEW_LINE INDENT freqX += 1 NEW_LINE i += 1 NEW_LINE DEDENT count = max ( count , freqX ) NEW_LINE DEDENT print ( count ) NEW_LINE DEDENT / * Driver Code * / NEW_LINE arr = [ 2 , 1 , 2 , 1 , 4 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE minimumIncreasingSubsequences ( arr , N ) NEW_LINE
Count triplets from an array which can form quadratic equations with real roots | Function to count the number of triplets ( a , b , c ) Such that the equations ax ^ 2 + bx + c = 0 has real roots ; sort he array in ascending order ; store count of triplets ( a , b , c ) such that ax ^ 2 + bx + c = 0 has real roots ; base case ; Traverse the given array ; if value of a and c are equal to b ; increment a ; Decrement c ; condition for having real roots for a quadratic equation ; if b lies in between a and c ; update count ; update count ; increment a ; Decrement c ; for each pair two values are possible of a and c ; Driver code ;
def getcount ( arr , N ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE count = 0 NEW_LINE if ( N < 3 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT for b in range ( 0 , N ) : NEW_LINE INDENT a = 0 NEW_LINE c = N - 1 NEW_LINE d = arr [ b ] * arr [ b ] // 4 NEW_LINE while ( a < c ) : NEW_LINE INDENT if ( a == b ) : NEW_LINE INDENT a += 1 NEW_LINE continue NEW_LINE DEDENT if ( c == b ) : NEW_LINE INDENT c -= 1 NEW_LINE continue NEW_LINE DEDENT if ( arr [ a ] * arr ) <= d : NEW_LINE INDENT if ( a < b and b < c ) : NEW_LINE INDENT count += c - a - 1 NEW_LINE DEDENT else : NEW_LINE INDENT count += c - a NEW_LINE DEDENT a += 1 NEW_LINE DEDENT else : NEW_LINE INDENT c -= 1 NEW_LINE DEDENT DEDENT DEDENT return count * 2 NEW_LINE DEDENT arr = [ 3 , 6 , 10 , 13 , 21 ] NEW_LINE N = len ( arr ) NEW_LINE / * Function Call * / NEW_LINE print ( getcount ( arr , N ) ) NEW_LINE
Maximize difference between the sum of absolute differences of each element with the remaining array | Function to maximize difference of the sum of absolute difference of an element with the rest of the elements in the array ; Sort the array in ascending order ; Stores prefix sum at any instant ; Store the total array sum ; Initialize minimum and maximum absolute difference ; Traverse the array to find the total array sum ; Traverse the array arr [ ] ; Store the number of elements to its left ; Store the number of elements to its right ; Update the sum of elements on its left ; Store the absolute difference sum ; Update the Minimum ; Update the Maximum ; Update sum of elements on its left ; Prthe result ; Driven Code
def findMaxDifference ( arr , n ) : NEW_LINE INDENT arr = sorted ( arr ) NEW_LINE Leftsum = 0 NEW_LINE Totalsum = 0 NEW_LINE Min , Max = 10 ** 8 , - 10 ** 8 NEW_LINE for i in range ( n ) : NEW_LINE INDENT Totalsum += arr [ i ] NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT leftNumbers = i NEW_LINE rightNumbers = n - i - 1 NEW_LINE Totalsum = Totalsum - arr [ i ] NEW_LINE sum = ( leftNumbers * arr [ i ] ) - Leftsum + Totalsum - ( rightNumbers * arr [ i ] ) NEW_LINE Min = min ( Min , sum ) NEW_LINE Max = max ( Max , sum ) NEW_LINE Leftsum += arr [ i ] NEW_LINE DEDENT print ( Max - Min ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 4 , 7 ] NEW_LINE N = len ( arr ) NEW_LINE findMaxDifference ( arr , N ) NEW_LINE DEDENT
Minimum pairs required to be removed such that the array does not contain any pair with sum K | Function to find the maximum count of pairs required to be removed such that no pairs exist whose sum equal to K ; Stores maximum count of pairs required to be removed such that no pairs exist whose sum equal to K ; Base Case ; Sort the array ; Stores index of left pointer ; Stores index of right pointer ; Stores sum of left and right pointer ; If s equal to k ; Update cntPairs ; Update left ; Update right ; If s > k ; Update right ; Update left ; Return the cntPairs ; Driver Code ; Function call
def maxcntPairsSumKRemoved ( arr , k ) : NEW_LINE INDENT cntPairs = 0 NEW_LINE if not arr or len ( arr ) == 1 : NEW_LINE INDENT return cntPairs NEW_LINE DEDENT arr . sort ( ) NEW_LINE left = 0 NEW_LINE right = len ( arr ) - 1 NEW_LINE while left < right : NEW_LINE INDENT s = arr [ left ] + arr [ right ] NEW_LINE if s == k : NEW_LINE INDENT cntPairs += 1 NEW_LINE left += 1 NEW_LINE right -= 1 NEW_LINE DEDENT elif s > k : NEW_LINE INDENT right -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT left += 1 NEW_LINE DEDENT DEDENT return cntPairs NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE K = 5 NEW_LINE print ( maxcntPairsSumKRemoved ( arr , K ) ) NEW_LINE DEDENT
Minimize cost to split an array into K subsets such that the cost of each element is its product with its position in the subset | Function to find the minimum cost to split array into K subsets ; Sort the array in descending order ; Stores minimum cost to split the array into K subsets ; Stores position of elements of a subset ; Iterate over the range [ 1 , N ] ; Calculate the cost to select X - th element of every subset ; Update min_cost ; Update X ; Driver code ; Function call
def getMinCost ( arr , n , k ) : NEW_LINE INDENT arr . sort ( reverse = True ) NEW_LINE min_cost = 0 ; NEW_LINE X = 0 ; NEW_LINE for i in range ( 0 , n , k ) : NEW_LINE INDENT for j in range ( i , n , 1 ) : NEW_LINE INDENT if ( j < i + k ) : NEW_LINE INDENT min_cost += arr [ j ] * ( X + 1 ) ; NEW_LINE DEDENT DEDENT X += 1 ; NEW_LINE DEDENT return min_cost ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 9 , 20 , 7 , 8 ] ; NEW_LINE K = 2 ; NEW_LINE N = len ( arr ) ; NEW_LINE print ( getMinCost ( arr , N , K ) ) ; NEW_LINE DEDENT
Minimize difference between the largest and smallest array elements by K replacements | Function to find minimum difference between largest and smallest element after K replacements ; Sort array in ascending order ; Length of array ; Minimum difference ; Check for all K + 1 possibilities ; Return answer ; Driver Code ; Given array ; Prints the minimum possible difference
def minDiff ( A , K ) : NEW_LINE INDENT A . sort ( ) ; NEW_LINE n = len ( A ) ; NEW_LINE if ( n <= K ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT mindiff = A [ n - 1 ] - A [ 0 ] ; NEW_LINE if ( K == 0 ) : NEW_LINE INDENT return mindiff ; NEW_LINE DEDENT i = 0 ; NEW_LINE for j in range ( n - 1 - K , n ) : NEW_LINE INDENT mindiff = min ( mindiff , A [ j ] - A [ i ] ) ; NEW_LINE i += 1 ; NEW_LINE j += 1 ; NEW_LINE DEDENT return mindiff ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ - 1 , 3 , - 1 , 8 , 5 , 4 ] ; NEW_LINE K = 3 ; NEW_LINE print ( minDiff ( A , K ) ) ; NEW_LINE DEDENT
Minimize difference between the largest and smallest array elements by K replacements | Python3 program for above approach ; Function to find minimum difference between the largest and smallest element after K replacements ; Create a MaxHeap ; Create a MinHeap ; Update maxHeap and MinHeap with highest and smallest K elements respectively ; Insert current element into the MaxHeap ; If maxHeap size exceeds K + 1 ; Remove top element ; Insert current element into the MaxHeap ; If maxHeap size exceeds K + 1 ; Remove top element ; Store all max element from maxHeap ; Store all min element from minHeap ; Generating all K + 1 possibilities ; Return answer ; Given array ; Function call
import sys NEW_LINE def minDiff ( A , K ) : NEW_LINE INDENT if ( len ( A ) <= K + 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT maxHeap = [ ] NEW_LINE minHeap = [ ] NEW_LINE for n in A : NEW_LINE INDENT maxHeap . append ( n ) NEW_LINE maxHeap . sort ( ) NEW_LINE if ( len ( maxHeap ) > K + 1 ) : NEW_LINE INDENT del maxHeap [ 0 ] NEW_LINE DEDENT minHeap . append ( n ) NEW_LINE minHeap . sort ( ) NEW_LINE minHeap . reverse ( ) NEW_LINE if ( len ( minHeap ) > K + 1 ) : NEW_LINE INDENT del minHeap [ 0 ] NEW_LINE DEDENT DEDENT maxList = [ ] NEW_LINE while ( len ( maxHeap ) > 0 ) : NEW_LINE INDENT maxList . append ( maxHeap [ 0 ] ) NEW_LINE del maxHeap [ 0 ] NEW_LINE DEDENT minList = [ ] NEW_LINE while ( len ( minHeap ) > 0 ) : NEW_LINE INDENT minList . append ( minHeap [ 0 ] ) NEW_LINE del minHeap [ 0 ] NEW_LINE DEDENT mindiff = sys . maxsize NEW_LINE for i in range ( K ) : NEW_LINE INDENT mindiff = min ( mindiff , maxList [ i ] - minList [ K - i ] ) NEW_LINE DEDENT return mindiff NEW_LINE DEDENT A = [ - 1 , 3 , - 1 , 8 , 5 , 4 ] NEW_LINE K = 3 NEW_LINE print ( minDiff ( A , K ) ) NEW_LINE
Check if all K | Function to check all subset - sums of K - length subsets in A [ ] is greater that that in the array B [ ] or not ; Sort the array in ascending order ; Sort the array in descending order ; Stores sum of first K elements of A [ ] ; Stores sum of first K elements of B [ ] ; Traverse both the arrays ; Update sum1 ; Update sum2 ; If sum1 exceeds sum2 ; Driver Code
def checkSubsetSum ( A , B , N , K ) : NEW_LINE INDENT A . sort ( ) NEW_LINE B . sort ( reverse = True ) NEW_LINE sum1 = 0 NEW_LINE sum2 = 0 NEW_LINE for i in range ( K ) : NEW_LINE INDENT sum1 += A [ i ] NEW_LINE sum2 += B [ i ] NEW_LINE DEDENT if ( sum1 > sum2 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT A = [ 12 , 11 , 10 , 13 ] NEW_LINE B = [ 7 , 10 , 6 , 2 ] NEW_LINE N = len ( A ) NEW_LINE K = 3 NEW_LINE if ( checkSubsetSum ( A , B , N , K ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Sort given array to descending | Function to sort first K array elements in descending and last N - K in ascending order ; Sort the array in descending order ; Sort last ( N - K ) array elements in ascending order ; Driver Code
def sortArrayInDescAsc ( arr , N , K ) : NEW_LINE INDENT arr = sorted ( arr ) NEW_LINE arr = arr [ : : - 1 ] NEW_LINE for i in arr [ : K ] : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT for i in reversed ( arr [ K : ] ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 7 , 6 , 8 , 9 , 0 , 1 , 2 , 2 , 1 , 8 , 9 , 6 , 7 ] NEW_LINE N = len ( arr ) NEW_LINE K = 6 NEW_LINE sortArrayInDescAsc ( arr , N , K ) NEW_LINE DEDENT