codeparrot/xlcost-text-to-code huggingface dataset:
Minimize difference between maximum and minimum of Array by at most K replacements | Function to find minimum difference between the maximum and the minimum elements arr [ ] by at most K replacements ; Check if turns are more than or equal to n - 1 then simply return zero ; Sort the array ; Set difference as the maximum possible difference ; Iterate over the array to track the minimum difference in k turns ; Return the answer ; Driver code ; Given array arr [ ] ; Given K replacements ; Function Call
def maxMinDifference ( arr , n , k ) : NEW_LINE INDENT if ( k >= n - 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT arr . sort ( ) NEW_LINE ans = arr [ n - 1 ] - arr [ 0 ] NEW_LINE i = k NEW_LINE j = n - 1 NEW_LINE while i >= 0 : NEW_LINE INDENT ans = min ( arr [ j ] - arr [ i ] , ans ) NEW_LINE i -= 1 NEW_LINE j -= 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 4 , 6 , 11 , 15 ] NEW_LINE N = len ( arr ) NEW_LINE K = 3 NEW_LINE print ( maxMinDifference ( arr , N , K ) ) NEW_LINE DEDENT
Check if a decreasing Array can be sorted using Triple cyclic shift | Python3 program for the above approach ; if array is 3 2 1 can ' t ▁ ▁ be ▁ sorted ▁ because ▁ 2 ▁ is ▁ in ▁ ▁ its ▁ correct ▁ position , ▁ 1 ▁ ▁ and ▁ 3 ▁ can ' t shift right because cyclic right shift takes place between 3 elements ; check if its possible to sort ; Number of swap is N / 2 always for this approach ; printing index of the cyclic right shift ; Driver code
def sortarray ( arr , N ) : NEW_LINE INDENT if ( N == 3 ) : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT elif ( N % 4 == 0 or N % 4 == 1 ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE print ( N // 2 ) NEW_LINE k = 1 NEW_LINE for l in range ( N // 4 ) : NEW_LINE INDENT print ( k , k + 1 , N ) NEW_LINE print ( k + 1 , N , N - 1 ) NEW_LINE k = k + 2 NEW_LINE N = N - 2 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 NEW_LINE arr = [ 5 , 4 , 3 , 2 , 1 ] NEW_LINE sortarray ( arr , N ) NEW_LINE DEDENT
Find least non | Python3 program to find the least non - overlapping number from a given set intervals ; Function to find the smallest non - overlapping number ; Create a visited array ; Find the first missing value ; Driver code
MAX = int ( 1e5 + 5 ) NEW_LINE def find_missing ( interval ) : NEW_LINE INDENT vis = [ 0 ] * ( MAX ) NEW_LINE for i in range ( len ( interval ) ) : NEW_LINE INDENT start = interval [ i ] [ 0 ] NEW_LINE end = interval [ i ] [ 1 ] NEW_LINE vis [ start ] += 1 NEW_LINE vis [ end + 1 ] -= 1 NEW_LINE DEDENT for i in range ( 1 , MAX ) : NEW_LINE INDENT vis [ i ] += vis [ i - 1 ] NEW_LINE if ( vis [ i ] == 0 ) : NEW_LINE INDENT print ( i ) NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT interval = [ [ 0 , 14 ] , [ 86 , 108 ] , [ 22 , 30 ] , [ 5 , 17 ] ] NEW_LINE find_missing ( interval ) NEW_LINE
Find least non | function to find the smallest non - overlapping number ; Sort the intervals based on their starting value ; Check if any missing value exist ; Finally print the missing value ; Driver code
def find_missing ( interval ) : NEW_LINE INDENT interval . sort ( ) NEW_LINE mx = 0 NEW_LINE for i in range ( len ( interval ) ) : NEW_LINE INDENT if ( interval [ i ] [ 0 ] > mx ) : NEW_LINE INDENT print ( mx ) NEW_LINE return NEW_LINE DEDENT else : NEW_LINE INDENT mx = max ( mx , interval [ i ] [ 1 ] + 1 ) NEW_LINE DEDENT DEDENT print ( mx ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT interval = [ [ 0 , 14 ] , [ 86 , 108 ] , [ 22 , 30 ] , [ 5 , 17 ] ] NEW_LINE find_missing ( interval ) ; NEW_LINE DEDENT
Maximum bitwise OR value of subsequence of length K | Function to convert bit array to decimal number ; Return the final result ; Function to find the maximum Bitwise OR value of subsequence of length K ; Initialize bit array of size 32 with all value as 0 ; Iterate for each index of bit [ ] array from 31 to 0 , and check if the ith value of bit array is 0 ; Check for maximum contributing element ; Update the bit array if max_contributing element is found ; Decrement the value of K ; Return the result ; Given array arr [ ] ; Length of subsequence ; Function call
def build_num ( bit ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 0 , 32 ) : NEW_LINE INDENT if ( bit [ i ] ) : NEW_LINE INDENT ans += ( 1 << i ) NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT def maximumOR ( arr , n , k ) : NEW_LINE INDENT bit = [ 0 ] * 32 NEW_LINE for i in range ( 31 , - 1 , - 1 ) : NEW_LINE INDENT if ( bit [ i ] == 0 and k > 0 ) : NEW_LINE INDENT temp = build_num ( bit ) NEW_LINE temp1 = temp NEW_LINE val = - 1 NEW_LINE for j in range ( 0 , n ) : NEW_LINE INDENT if ( temp1 < ( temp arr [ j ] ) ) : NEW_LINE INDENT temp1 = temp | arr [ j ] NEW_LINE val = arr [ j ] NEW_LINE DEDENT DEDENT if ( val != - 1 ) : NEW_LINE INDENT k -= 1 NEW_LINE for j in range ( 0 , 32 ) : NEW_LINE INDENT if ( val & ( 1 << j ) ) : NEW_LINE INDENT bit [ j ] += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT return build_num ( bit ) NEW_LINE DEDENT arr = [ 5 , 9 , 7 , 19 ] NEW_LINE k = 3 ; NEW_LINE n = len ( arr ) NEW_LINE print ( maximumOR ( arr , n , k ) ) NEW_LINE
Lexicographically smallest string after M operations | Function to find the lexicographical smallest string after performing M operations ; Size of the given string ; Declare an array a ; For each i , a [ i ] contain number of operations to update s [ i ] to 'a ; Check if m >= ar [ i ] , then update s [ i ] to ' a ' decrement k by a [ i ] ; Form a cycle of 26 ; Return the answer ; Driver code
def smallest_string ( s , m ) : NEW_LINE INDENT n = len ( s ) ; NEW_LINE l = list ( s ) NEW_LINE a = [ 0 ] * n ; NEW_LINE DEDENT ' NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT distance = ord ( s [ i ] ) - ord ( ' a ' ) ; NEW_LINE if ( distance == 0 ) : NEW_LINE INDENT a [ i ] = 0 ; NEW_LINE DEDENT else : NEW_LINE INDENT a [ i ] = 26 - distance ; NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if ( m >= a [ i ] ) : NEW_LINE INDENT l [ i ] = ' a ' ; NEW_LINE m = m - a [ i ] ; NEW_LINE DEDENT DEDENT m = m % 26 ; NEW_LINE for i in range ( len ( l ) - 1 ) : NEW_LINE INDENT print ( l [ i ] , end = " " ) NEW_LINE DEDENT print ( chr ( ord ( l [ n - 1 ] ) + m ) ) NEW_LINE DEDENT str = " aazzx " ; NEW_LINE m = 6 ; NEW_LINE smallest_string ( str , m ) ; NEW_LINE
Maximize jobs that can be completed under given constraint | Function to find maxiumum number of jobs ; Min Heap ; Sort ranges by start day ; Stores the minimum and maximum day in the ranges ; Iterating from min_day to max_day ; Insert the end day of the jobs which can be completed on i - th day in a priority queue ; Pop all jobs whose end day is less than current day ; If queue is empty , no job can be completed on the i - th day ; Increment the count of jobs completed ; Pop the job with least end day ; Return the jobs on the last day ; Driver code
def find_maximum_jobs ( N , ranges ) : NEW_LINE INDENT queue = [ ] NEW_LINE ranges . sort ( ) NEW_LINE min_day = ranges [ 0 ] [ 0 ] NEW_LINE max_day = 0 NEW_LINE for i in range ( N ) : NEW_LINE max_day = max ( max_day , ranges [ i ] [ 1 ] ) NEW_LINE index , count_jobs = 0 , 0 NEW_LINE for i in range ( min_day , max_day + 1 ) : NEW_LINE while ( index < len ( ranges ) and ranges [ index ] [ 0 ] <= i ) : NEW_LINE INDENT queue . append ( ranges [ index ] [ 1 ] ) NEW_LINE index += 1 NEW_LINE DEDENT queue . sort ( ) NEW_LINE while ( len ( queue ) > 0 and queue [ 0 ] < i ) : NEW_LINE INDENT queue . pop ( 0 ) NEW_LINE DEDENT if ( len ( queue ) == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT count_jobs += 1 NEW_LINE queue . pop ( 0 ) NEW_LINE return count_jobs NEW_LINE DEDENT N = 5 NEW_LINE ranges = [ ] NEW_LINE ranges . append ( ( 1 , 5 ) ) NEW_LINE ranges . append ( ( 1 , 5 ) ) NEW_LINE ranges . append ( ( 1 , 5 ) ) NEW_LINE ranges . append ( ( 2 , 3 ) ) NEW_LINE ranges . append ( ( 2 , 3 ) ) NEW_LINE print ( find_maximum_jobs ( N , ranges ) ) NEW_LINE
Minimize number of boxes by putting small box inside bigger one | Function to minimize the count ; Initial number of box ; Sort array of box size in increasing order ; Check is current box size is smaller than next box size ; Decrement box count Increment current box count Increment next box count ; Check if both box have same size ; Print the result ; Driver code
def minBox ( arr , n ) : NEW_LINE INDENT box = n NEW_LINE arr . sort ( ) NEW_LINE curr_box , next_box = 0 , 1 NEW_LINE while ( curr_box < n and next_box < n ) : NEW_LINE INDENT if ( arr [ curr_box ] < arr [ next_box ] ) : NEW_LINE INDENT box = box - 1 NEW_LINE curr_box = curr_box + 1 NEW_LINE next_box = next_box + 1 NEW_LINE DEDENT elif ( arr [ curr_box ] == arr [ next_box ] ) : NEW_LINE INDENT next_box = next_box + 1 NEW_LINE DEDENT DEDENT print ( box ) NEW_LINE DEDENT size = [ 1 , 2 , 3 ] NEW_LINE n = len ( size ) NEW_LINE minBox ( size , n ) NEW_LINE
Sort a string lexicographically using triple cyclic shifts | Python3 program for sorting a string using cyclic shift of three indices ; Store the indices which haven 't attained  its correct position ; Store the indices undergoing cyclic shifts ; If the element is not at it 's correct position ; Check if all 3 indices can be placed to respective correct indices in a single move ; If the i - th index is still not present in its correct position , store the index ; To check if swapping two indices places them in their correct position ; for i in range len ( indices ) : print ( indices [ i ] [ 0 ] , indices [ i ] [ 1 ] , indices [ i ] [ 2 ] ) ; Driver code
import math NEW_LINE def sortString ( arr , n , moves ) : NEW_LINE INDENT pos = [ ] NEW_LINE indices = [ ] NEW_LINE flag = False NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] != i ) : NEW_LINE INDENT if ( arr [ arr [ arr [ i ] ] ] == i and arr [ arr [ i ] ] != i ) : NEW_LINE INDENT temp = arr [ arr [ i ] ] NEW_LINE indices . append ( [ i , arr [ i ] , arr [ arr [ i ] ] ] ) NEW_LINE sw = arr [ i ] NEW_LINE arr [ i ] = arr [ arr [ i ] ] NEW_LINE arr [ sw ] = sw NEW_LINE sw = arr [ i ] NEW_LINE arr [ i ] = arr [ temp ] NEW_LINE arr [ temp ] = sw NEW_LINE DEDENT DEDENT if ( arr [ i ] != i ) : NEW_LINE INDENT pos . append ( i ) NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] != i ) : NEW_LINE INDENT pos1 = i NEW_LINE pos2 = arr [ i ] NEW_LINE pos3 = arr [ arr [ i ] ] NEW_LINE if ( pos3 != pos1 ) : NEW_LINE INDENT indices . append ( [ pos1 , pos2 , pos3 ] ) NEW_LINE arr [ pos1 ] , arr [ pos2 ] = arr [ pos2 ] , arr [ pos1 ] NEW_LINE arr [ pos1 ] , arr [ pos3 ] = arr [ pos3 ] , arr [ pos1 ] NEW_LINE pos . remove ( pos2 ) NEW_LINE if pos3 in pos : NEW_LINE INDENT pos . remove ( pos3 ) NEW_LINE DEDENT if ( arr [ pos1 ] == pos1 ) : NEW_LINE INDENT pos . remove ( pos1 ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( pos3 == pos [ 0 ] ) : NEW_LINE INDENT it = 0 NEW_LINE if ( pos [ 0 ] != pos [ - 1 ] ) : NEW_LINE INDENT it = it + 1 NEW_LINE pos3 = pos [ it ] NEW_LINE if ( pos [ it ] != pos [ - 1 ] and pos3 == pos2 ) : NEW_LINE INDENT it = it + 1 NEW_LINE pos3 = pos [ it ] NEW_LINE DEDENT elif ( pos [ it ] == pos [ - 1 ] and pos3 == pos2 ) : NEW_LINE INDENT flag = True NEW_LINE break NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT flag = True NEW_LINE break NEW_LINE DEDENT DEDENT indices . append ( [ pos1 , pos2 , pos3 ] ) NEW_LINE arr [ pos1 ] , arr [ pos2 ] = arr [ pos2 ] , arr [ pos1 ] NEW_LINE arr [ pos1 ] , arr [ pos3 ] = arr [ pos3 ] , arr [ pos1 ] NEW_LINE pos . remove ( pos2 ) NEW_LINE DEDENT DEDENT if ( arr [ i ] != i ) : NEW_LINE INDENT i = i - 1 NEW_LINE DEDENT DEDENT if ( flag == True or len ( indices ) > moves ) : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( len ( indices ) ) NEW_LINE DEDENT DEDENT s = " adceb " NEW_LINE arr = [ ] NEW_LINE for i in s : NEW_LINE INDENT arr . append ( ord ( i ) - ord ( ' a ' ) ) NEW_LINE DEDENT sortString ( arr , len ( s ) , math . floor ( len ( s ) / 2 ) ) NEW_LINE
Count of minimum reductions required to get the required sum K | Python3 program to find the count of minimum reductions required to get the required sum K ; Function to return the count of minimum reductions ; If the sum is already less than K ; Sort in non - increasing order of difference ; Driver Code ; Function Call
from typing import Any , List NEW_LINE def countReductions ( v : List [ Any ] , K : int ) -> int : NEW_LINE INDENT sum = 0 NEW_LINE for i in v : NEW_LINE INDENT sum += i [ 0 ] NEW_LINE DEDENT if ( sum <= K ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT v . sort ( key = lambda a : a [ 0 ] - a [ 1 ] ) NEW_LINE i = 0 NEW_LINE while ( sum > K and i < len ( v ) ) : NEW_LINE INDENT sum -= ( v [ i ] [ 0 ] - v [ i ] [ 1 ] ) NEW_LINE i += 1 NEW_LINE DEDENT if ( sum <= K ) : NEW_LINE INDENT return i NEW_LINE DEDENT return - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 NEW_LINE K = 25 NEW_LINE v = [ [ 0 , 0 ] for _ in range ( N ) ] NEW_LINE v [ 0 ] = [ 10 , 5 ] NEW_LINE v [ 1 ] = [ 20 , 9 ] NEW_LINE v [ 2 ] = [ 12 , 10 ] NEW_LINE v [ 3 ] = [ 4 , 2 ] NEW_LINE print ( countReductions ( v , K ) ) NEW_LINE DEDENT
Find K elements whose absolute difference with median of array is maximum | Function for calculating median ; Check for even case ; Function to find the K maximum absolute difference with the median of the array ; Sort the array ; Store median ; Find and store difference ; If diff [ i ] is greater print it . Else print diff [ j ] ; Driver code
def findMedian ( a , n ) : NEW_LINE INDENT if ( n % 2 != 0 ) : NEW_LINE INDENT return a [ int ( n / 2 ) ] NEW_LINE DEDENT return ( a [ int ( ( n - 1 ) / 2 ) ] + a [ int ( n / 2 ) ] ) / 2.0 NEW_LINE DEDENT def kStrongest ( arr , n , k ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE median = findMedian ( arr , n ) NEW_LINE diff = [ 0 ] * ( n ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT diff [ i ] = abs ( median - arr [ i ] ) NEW_LINE DEDENT i = 0 NEW_LINE j = n - 1 NEW_LINE while ( k > 0 ) : NEW_LINE INDENT if ( diff [ i ] > diff [ j ] ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT print ( arr [ j ] , end = " ▁ " ) NEW_LINE j -= 1 NEW_LINE DEDENT k -= 1 NEW_LINE DEDENT DEDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE k = 3 NEW_LINE n = len ( arr ) NEW_LINE kStrongest ( arr , n , k ) NEW_LINE
Sort an array by swapping elements of different type specified by another array | Function to check if it is possible to sort the array in non - decreasing order by swapping elements of different types ; Consider the array to be already sorted ; Checking if array is already sorted ; Check for a pair which is in decreasing order ; Count the frequency of each type of element ; type0 stores count of elements of type 0 ; type1 stores count of elements of type 1 ; Return true if array is already sorted ; Return false if all elements are of same type and array is unsorted ; Possible for all other cases ; Driver code
def sorting_possible ( a , b , n ) : NEW_LINE INDENT sorted = True NEW_LINE type1 = 0 NEW_LINE type0 = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( a [ i ] < a [ i - 1 ] ) : NEW_LINE INDENT sorted = False NEW_LINE break NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if ( b [ i ] == 0 ) : NEW_LINE INDENT type0 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT type1 += 1 NEW_LINE DEDENT DEDENT if ( sorted != False ) : NEW_LINE INDENT return True NEW_LINE DEDENT elif ( type1 == n or type0 == n ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT a = [ 15 , 1 , 2 , 17 , 6 ] NEW_LINE b = [ 1 , 1 , 0 , 1 , 1 ] NEW_LINE n = len ( a ) NEW_LINE res = sorting_possible ( a , b , n ) NEW_LINE if ( res != False ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Sort permutation of N natural numbers using triple cyclic right swaps | Function to sort the permutation with the given operations ; Visited array to check the array element is at correct position or not ; Loop to iterate over the elements of the given array ; Condition to check if the elements is at its correct position ; Condition to check if the element is included in any previous cyclic rotations ; Loop to find the cyclic rotations in required ; Condition to check if the cyclic rotation is a valid rotation ; Loop to find the index of the cyclic rotation for the current index ; Condition to if the cyclic rotation is a valid rotation ; Loop to find athe valid operations required to sort the permutation ; Total operation required ; Driver Code ; Function Call
def sortPermutation ( arr , n ) : NEW_LINE INDENT ans = [ ] NEW_LINE p = [ ] NEW_LINE visited = [ 0 ] * 200005 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( arr [ i ] == i ) : NEW_LINE INDENT visited [ i ] = 1 NEW_LINE continue NEW_LINE DEDENT else : NEW_LINE INDENT if ( visited [ i ] == False ) : NEW_LINE INDENT x = i NEW_LINE v = [ ] NEW_LINE while ( visited [ x ] == False ) : NEW_LINE INDENT visited [ x ] = 1 NEW_LINE v . append ( x ) NEW_LINE x = arr [ x ] NEW_LINE DEDENT if ( ( len ( v ) - 3 ) % 2 == 0 ) : NEW_LINE INDENT for i in range ( 1 , len ( v ) , 2 ) : NEW_LINE INDENT ans . append ( [ v [ 0 ] , v [ i ] , v [ i + 1 ] ] ) NEW_LINE DEDENT continue NEW_LINE DEDENT p . append ( v [ 0 ] ) NEW_LINE p . append ( v [ len ( v ) - 1 ] ) NEW_LINE for i in range ( 1 , len ( v ) - 1 , 2 ) : NEW_LINE INDENT ans . append ( [ v [ 0 ] , v [ i ] , v [ i + 1 ] ] ) NEW_LINE DEDENT DEDENT DEDENT DEDENT if ( len ( p ) % 4 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT for i in range ( 0 , len ( p ) , 4 ) : NEW_LINE INDENT ans . append ( [ p [ i ] , p [ i + 1 ] , p [ i + 2 ] ] ) NEW_LINE ans . append ( p [ i [ + 2 ] , p [ i ] , p [ i + 3 ] ] ) NEW_LINE DEDENT print ( len ( ans ) ) NEW_LINE for i in ans : NEW_LINE INDENT print ( i [ 0 ] , i [ 1 ] , i [ 2 ] ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 0 , 3 , 2 , 4 , 1 ] NEW_LINE n = 4 NEW_LINE sortPermutation ( arr , n ) NEW_LINE DEDENT
Maximize array sum by X increments when each element is divided by 10 | Python3 program for the above problem ; Initialize variables ; Add the current contribution of the element to the answer ; If the value is already maximum then we can 't  change it ; Moves required to move to the next multiple of 10 ; No of times we can add 10 to this value so that its value does not exceed 1000. ; Sort the array ; Adding the values to increase the numbers to the next multiple of 10 ; If the total moves are less than X then increase the answer ; If the moves exceed X then we cannot increase numbers ; If there still remain some moves ; Remaining moves ; Add minimim of increments and remaining / 10 to the answer ; Output the final answer ; Driver Code
def maximizeval10 ( a , n , k ) : NEW_LINE INDENT increments = 0 NEW_LINE ans = 0 NEW_LINE v = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans += ( a [ i ] // 10 ) NEW_LINE if ( a [ i ] == 1000 ) : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT v . append ( 10 - a [ i ] % 10 ) NEW_LINE increments += ( 100 - ( ( a [ i ] ) // 10 ) - 1 ) ; NEW_LINE DEDENT DEDENT v . sort ( ) NEW_LINE sum = 0 NEW_LINE for i in range ( len ( v ) ) : NEW_LINE INDENT sum += v [ i ] NEW_LINE if ( sum <= k ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( sum < k ) : NEW_LINE INDENT remaining = k - sum NEW_LINE ans += min ( increments , remaining // 10 ) NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 NEW_LINE X = 6 NEW_LINE A = [ 4 , 8 , 8 , 8 ] NEW_LINE maximizeval10 ( A , N , X ) NEW_LINE DEDENT
Split array into two subarrays such that difference of their maximum is minimum | Python3 Program to split a given array such that the difference between their maximums is minimized . ; Sort the array ; Return the difference between two highest elements ; Driver Program
def findMinDif ( arr , N ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE return ( arr [ N - 1 ] - arr [ N - 2 ] ) NEW_LINE DEDENT arr = [ 7 , 9 , 5 , 10 ] NEW_LINE N = len ( arr ) NEW_LINE print ( findMinDif ( arr , N ) ) NEW_LINE
Count of available non | Function to find the pivot index ; Function to implement Quick Sort ; Pivot is pointing to pivot index before which every element is smaller and after pivot , every element is greater ; Sort the array before pivot element ; Sort the array after pivot element ; Function to count the available intervals from the given range of numbers ; If range starts after 0 then an interval is available from 0 to start [ 0 ] ; When a new interval starts ; Since index variable i is being incremented , the current active interval will also get incremented ; When the current interval ends ; Since index variable j is being decremented , the currect active interval will also get decremented ; When start and end both are same there is no change in currActive ; If the end of interval is before the range so interval is available at the end ; Driver code ; Sort the start array ; Sort the end array ; Calling the function
def partition ( arr , l , h ) : NEW_LINE INDENT pivot = arr [ l ] NEW_LINE i = l + 1 NEW_LINE j = h NEW_LINE while ( i <= j ) : NEW_LINE INDENT while ( i <= h and arr [ i ] < pivot ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT while ( j > l and arr [ j ] > pivot ) : NEW_LINE INDENT j -= 1 NEW_LINE DEDENT if ( i < j ) : NEW_LINE INDENT temp = arr [ i ] NEW_LINE arr [ i ] = arr [ j ] NEW_LINE arr [ j ] = temp NEW_LINE i += 1 NEW_LINE j -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT i += 1 NEW_LINE DEDENT DEDENT arr [ l ] = arr [ j ] NEW_LINE arr [ j ] = pivot NEW_LINE return j NEW_LINE DEDENT def sortArray ( arr , l , h ) : NEW_LINE INDENT if ( l >= h ) : NEW_LINE INDENT return NEW_LINE DEDENT pivot = partition ( arr , l , h ) NEW_LINE sortArray ( arr , l , pivot - 1 ) NEW_LINE sortArray ( arr , pivot + 1 , h ) NEW_LINE DEDENT def findMaxIntervals ( start , end , n , R ) : NEW_LINE INDENT ans = 0 NEW_LINE prev = 0 NEW_LINE currActive = 0 NEW_LINE i = 0 NEW_LINE j = 0 NEW_LINE if ( start [ 0 ] > 0 ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT while ( i < n and j < n ) : NEW_LINE INDENT if ( start [ i ] < end [ j ] ) : NEW_LINE INDENT i += 1 NEW_LINE currActive += 1 NEW_LINE DEDENT elif ( start [ i ] > end [ j ] ) : NEW_LINE INDENT j += 1 NEW_LINE currActive -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT i += 1 NEW_LINE j += 1 NEW_LINE DEDENT if ( currActive == 0 ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT if ( end [ n - 1 ] < R ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT R = 10 NEW_LINE N = 3 NEW_LINE start = [ 2 , 5 , 8 ] NEW_LINE end = [ 3 , 9 , 10 ] NEW_LINE sortArray ( start , 0 , N - 1 ) NEW_LINE sortArray ( end , 0 , N - 1 ) NEW_LINE print ( findMaxIntervals ( start , end , N , R ) ) NEW_LINE DEDENT