codeparrot/xlcost-text-to-code huggingface dataset:
Median of all non | Function to calculate the median of all possible subsets by given operations ; Stores sum of elements of arr [ ] ; Traverse the array arr [ ] ; Update sum ; Sort the array ; DP [ i ] [ j ] : Stores total number of ways to form the sum j by either selecting ith element or not selecting ith item . ; Base case ; Fill dp [ i ] [ 0 ] ; Base case ; Fill all the DP states based on the mentioned DP relation ; If j is greater than or equal to arr [ i ] ; Update dp [ i ] [ j ] ; Update dp [ i ] [ j ] ; Stores all possible subset sum ; Traverse all possible subset sum ; Stores count of subsets whose sum is j ; Itearate over the range [ 1 , M ] ; Insert j into sumSub ; Stores middle element of sumSub ; Driver Code
def findMedianOfsubSum ( arr , N ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT arr . sort ( reverse = False ) NEW_LINE dp = [ [ 0 for i in range ( sum + 1 ) ] for j in range ( N ) ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT dp [ i ] [ 0 ] = 1 NEW_LINE DEDENT dp [ 0 ] [ arr [ 0 ] ] = 1 NEW_LINE for i in range ( 1 , N , 1 ) : NEW_LINE INDENT for j in range ( 1 , sum + 1 , 1 ) : NEW_LINE INDENT if ( j >= arr [ i ] ) : NEW_LINE INDENT dp [ i ] [ j ] = ( dp [ i - 1 ] [ j ] + dp [ i - 1 ] [ j - arr [ i ] ] ) NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = dp [ i - 1 ] [ j ] NEW_LINE DEDENT DEDENT DEDENT sumSub = [ ] NEW_LINE for j in range ( 1 , sum + 1 , 1 ) : NEW_LINE INDENT M = dp [ N - 1 ] [ j ] NEW_LINE for i in range ( 1 , M + 1 , 1 ) : NEW_LINE INDENT sumSub . append ( j ) NEW_LINE DEDENT DEDENT mid = sumSub [ len ( sumSub ) // 2 ] NEW_LINE return mid NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE print ( findMedianOfsubSum ( arr , N ) ) NEW_LINE DEDENT
Reduce array to a single element by repeatedly replacing adjacent unequal pairs with their maximum | Function to prthe index from where the operation can be started ; Initialize B ; Initialize save ; Make B equals to arr ; Sort the array B ; Traverse from N - 1 to 1 ; If B [ i ] & B [ i - 1 ] are unequal ; If all elements are same ; If arr [ 1 ] is maximum element ; If arr [ N - 1 ] is maximum element ; Find the maximum element ; Driver Code ; Given array arr ; Length of array ; Function Call
def printIndex ( arr , N ) : NEW_LINE INDENT B = [ 0 ] * ( N ) NEW_LINE save = - 1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT B [ i ] = arr [ i ] NEW_LINE DEDENT B = sorted ( B ) NEW_LINE for i in range ( N - 1 , 1 , - 1 ) : NEW_LINE INDENT if ( B [ i ] != B [ i - 1 ] ) : NEW_LINE INDENT save = B [ i ] NEW_LINE break NEW_LINE DEDENT DEDENT if ( save == - 1 ) : NEW_LINE INDENT print ( - 1 + " " ) NEW_LINE return NEW_LINE DEDENT if ( save == arr [ 0 ] and save != arr [ 1 ] ) : NEW_LINE INDENT print ( 1 ) NEW_LINE DEDENT elif ( save == arr [ N - 1 ] and save != arr [ N - 2 ] ) : NEW_LINE INDENT print ( N ) NEW_LINE DEDENT for i in range ( 1 , N - 1 ) : NEW_LINE INDENT if ( save == arr [ i ] and ( save != arr [ i - 1 ] or save != arr [ i + 1 ] ) ) : NEW_LINE INDENT print ( i + 1 ) NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , 3 , 4 , 4 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE printIndex ( arr , N ) NEW_LINE DEDENT
Lexicographically smallest subsequence possible by removing a character from given string | Function to find the lexicographically smallest subsequence of length N - 1 ; Generate all subsequence of length N - 1 ; Store main value of string str ; Erasing element at position i ; Sort the vector ; Print first element of vector ; Driver Code ; Given string S ; Function Call
def firstSubsequence ( s ) : NEW_LINE INDENT allsubseq = [ ] NEW_LINE k = [ ] NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT k = [ i for i in s ] NEW_LINE del k [ i ] NEW_LINE allsubseq . append ( " " . join ( k ) ) NEW_LINE DEDENT allsubseq = sorted ( allsubseq ) NEW_LINE print ( allsubseq [ 0 ] ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " geeksforgeeks " NEW_LINE firstSubsequence ( S ) NEW_LINE DEDENT
Maximum even sum subsequence of length K | Function to find the maximum even sum of any subsequence of length K ; If count of elements is less than K ; Stores maximum even subsequence sum ; Stores Even numbers ; Stores Odd numbers ; Traverse the array ; If current element is an odd number ; Insert odd number ; Insert even numbers ; Sort Odd [ ] array ; Sort Even [ ] array ; Stores current index Of Even [ ] array ; Stores current index Of Odd [ ] array ; If K is odd ; If count of elements in Even [ ] >= 1 ; Update maxSum ; Update i ; If count of elements in Even [ ] array is 0. ; Update K ; If count of elements in Even [ ] and odd [ ] >= 2 ; Update maxSum ; Update j . ; Update maxSum ; Update i ; Update K ; If count of elements in Even [ ] array >= 2. ; Update maxSum ; Update i . ; Update K . ; If count of elements in Odd [ ] array >= 2 ; Update maxSum ; Update i . ; Update K . ; Driver Code
def evenSumK ( arr , N , K ) : NEW_LINE INDENT if ( K > N ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT maxSum = 0 NEW_LINE Even = [ ] NEW_LINE Odd = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] % 2 ) : NEW_LINE INDENT Odd . append ( arr [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT Even . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT Odd . sort ( reverse = False ) NEW_LINE Even . sort ( reverse = False ) NEW_LINE i = len ( Even ) - 1 NEW_LINE j = len ( Odd ) - 1 NEW_LINE while ( K > 0 ) : NEW_LINE INDENT if ( K % 2 == 1 ) : NEW_LINE INDENT if ( i >= 0 ) : NEW_LINE INDENT maxSum += Even [ i ] NEW_LINE i -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 NEW_LINE DEDENT K -= 1 NEW_LINE DEDENT elif ( i >= 1 and j >= 1 ) : NEW_LINE INDENT if ( Even [ i ] + Even [ i - 1 ] <= Odd [ j ] + Odd [ j - 1 ] ) : NEW_LINE INDENT maxSum += Odd [ j ] + Odd [ j - 1 ] NEW_LINE j -= 2 NEW_LINE DEDENT else : NEW_LINE INDENT maxSum += Even [ i ] + Even [ i - 1 ] NEW_LINE i -= 2 NEW_LINE DEDENT K -= 2 NEW_LINE DEDENT elif ( i >= 1 ) : NEW_LINE INDENT maxSum += Even [ i ] + Even [ i - 1 ] NEW_LINE i -= 2 NEW_LINE K -= 2 NEW_LINE DEDENT elif ( j >= 1 ) : NEW_LINE INDENT maxSum += Odd [ j ] + Odd [ j - 1 ] NEW_LINE j -= 2 NEW_LINE K -= 2 NEW_LINE DEDENT DEDENT return maxSum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 4 , 10 , 3 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE K = 3 NEW_LINE print ( evenSumK ( arr , N , K ) ) NEW_LINE DEDENT
Program to find weighted median of a given array | Function to calculate weighted median ; Store pairs of arr [ i ] and W [ i ] ; Sort the list of pairs w . r . t . to their arr [ ] values ; If N is odd ; Traverse the set pairs from left to right ; Update sums ; If sum becomes > 0.5 ; If N is even ; For lower median traverse the set pairs from left ; Update sums ; When sum >= 0.5 ; For upper median traverse the set pairs from right ; Update sums ; When sum >= 0.5 ; Driver Code ; Given array arr [ ] ; Given weights W [ ] ; Function Call
def weightedMedian ( arr , W ) : NEW_LINE INDENT pairs = [ ] NEW_LINE for index in range ( len ( arr ) ) : NEW_LINE INDENT pairs . append ( [ arr [ index ] , W [ index ] ] ) NEW_LINE DEDENT pairs . sort ( key = lambda p : p [ 0 ] ) NEW_LINE if len ( arr ) % 2 != 0 : NEW_LINE INDENT sums = 0 NEW_LINE for element , weight in pairs : NEW_LINE INDENT sums += weight NEW_LINE if sums > 0.5 : NEW_LINE INDENT print ( " The ▁ Weighted ▁ Median " , end = ' ▁ ' ) NEW_LINE print ( " is ▁ element ▁ { } " . format ( element ) ) NEW_LINE DEDENT DEDENT DEDENT else : NEW_LINE INDENT sums = 0 NEW_LINE for element , weight in pairs : NEW_LINE INDENT sums += weight NEW_LINE if sums >= 0.5 : NEW_LINE INDENT print ( " Lower ▁ Weighted ▁ Median " , end = ' ▁ ' ) NEW_LINE print ( " is ▁ element ▁ { } " . format ( element ) ) NEW_LINE break NEW_LINE DEDENT DEDENT sums = 0 NEW_LINE for index in range ( len ( pairs ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT element = pairs [ index ] [ 0 ] NEW_LINE weight = pairs [ index ] [ 1 ] NEW_LINE sums += weight NEW_LINE if sums >= 0.5 : NEW_LINE INDENT print ( " Upper ▁ Weighted ▁ Median " , end = ' ▁ ' ) NEW_LINE print ( " is ▁ element ▁ { } " . format ( element ) ) NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 1 , 3 , 2 ] NEW_LINE W = [ 0.25 , 0.49 , 0.25 , 0.01 ] NEW_LINE weightedMedian ( arr , W ) NEW_LINE DEDENT
Kth smallest element from an array of intervals | Function to get the Kth smallest element from an array of intervals ; Store all the intervals so that it returns the minimum element in O ( 1 ) ; Insert all Intervals into the MinHeap ; Stores the count of popped elements ; Iterate over MinHeap ; Stores minimum element from all remaining intervals ; Remove minimum element ; Check if the minimum of the current interval is less than the maximum of the current interval ; Insert new interval ; Driver Code ; Intervals given ; Size of the arr
def KthSmallestNum ( arr , n , k ) : NEW_LINE INDENT pq = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT pq . append ( [ arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ] ) NEW_LINE DEDENT cnt = 1 NEW_LINE while ( cnt < k ) : NEW_LINE INDENT pq . sort ( reverse = True ) NEW_LINE interval = pq [ 0 ] NEW_LINE pq . remove ( pq [ 0 ] ) NEW_LINE if ( interval [ 0 ] < interval [ 1 ] ) : NEW_LINE INDENT pq . append ( [ interval [ 0 ] + 1 , interval [ 1 ] ] ) NEW_LINE DEDENT cnt += 1 NEW_LINE DEDENT pq . sort ( reverse = True ) NEW_LINE return pq [ 0 ] [ 0 ] + 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ 5 , 11 ] , [ 10 , 15 ] , [ 12 , 20 ] ] NEW_LINE n = len ( arr ) NEW_LINE k = 12 NEW_LINE print ( KthSmallestNum ( arr , n , k ) ) NEW_LINE DEDENT
Maximum Manhattan distance between a distinct pair from N coordinates | Python3 program for the above approach ; Function to calculate the maximum Manhattan distance ; Stores the maximum distance ; Find Manhattan distance using the formula | x1 - x2 | + | y1 - y2 | ; Updating the maximum ; Driver code ; Given co - ordinates ; Function call
import sys NEW_LINE def MaxDist ( A , N ) : NEW_LINE INDENT maximum = - sys . maxsize NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum = 0 NEW_LINE for j in range ( i + 1 , N ) : NEW_LINE INDENT Sum = ( abs ( A [ i ] [ 0 ] - A [ j ] [ 0 ] ) + abs ( A [ i ] [ 1 ] - A [ j ] [ 1 ] ) ) NEW_LINE maximum = max ( maximum , Sum ) NEW_LINE DEDENT DEDENT print ( maximum ) NEW_LINE DEDENT N = 3 NEW_LINE A = [ [ 1 , 2 ] , [ 2 , 3 ] , [ 3 , 4 ] ] NEW_LINE MaxDist ( A , N ) NEW_LINE
Longest increasing subsequence which forms a subarray in the sorted representation of the array | Function to find the length of the longest increasing sorted sequence ; Stores the count of all elements ; Store the original array ; Sort the array ; If adjacent element are not same ; Increment count ; Store frequency of each element ; Initialize a DP array ; Iterate over the array ar [ ] ; Length of the longest increasing sorted sequence ; Iterate over the array ; Current element ; If the element has been encountered the first time ; If all the x - 1 previous elements have already appeared ; Otherwise ; If all x - 1 elements have already been encountered ; Increment the count of the current element ; Update maximum subsequence size ; Return the maximum subsequence size ; Driver Code ; Function call
def LongestSequence ( a , n ) : NEW_LINE INDENT m = { i : 0 for i in range ( 100 ) } NEW_LINE ar = [ 0 for i in range ( n + 1 ) ] NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT ar [ i ] = a [ i - 1 ] NEW_LINE DEDENT a . sort ( reverse = False ) NEW_LINE c = 1 NEW_LINE m [ a [ 0 ] ] = c NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( a [ i ] != a [ i - 1 ] ) : NEW_LINE c += 1 NEW_LINE m [ a [ i ] ] = c NEW_LINE DEDENT cnt = { i : 0 for i in range ( 100 ) } NEW_LINE dp = [ [ 0 for i in range ( 3 ) ] for j in range ( n + 1 ) ] NEW_LINE cnt [ 0 ] = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT ar [ i ] = m [ ar [ i ] ] NEW_LINE cnt [ ar [ i ] ] += 1 NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT x = ar [ i ] NEW_LINE if ( dp [ x ] [ 0 ] == 0 ) : NEW_LINE if ( dp [ x - 1 ] [ 0 ] == cnt [ x - 1 ] ) : NEW_LINE INDENT dp [ x ] [ 1 ] = dp [ x - 1 ] [ 1 ] NEW_LINE dp [ x ] [ 2 ] = dp [ x - 1 ] [ 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT dp [ x ] [ 1 ] = dp [ x - 1 ] [ 0 ] NEW_LINE DEDENT dp [ x ] [ 2 ] = max ( dp [ x - 1 ] [ 0 ] , dp [ x ] [ 2 ] ) NEW_LINE if ( dp [ x - 1 ] [ 0 ] == cnt [ x - 1 ] ) : NEW_LINE INDENT dp [ x ] [ 2 ] = max ( dp [ x ] [ 2 ] , dp [ x - 1 ] [ 1 ] ) NEW_LINE DEDENT for j in range ( 3 ) : NEW_LINE INDENT dp [ x ] [ j ] += 1 NEW_LINE ans = max ( ans , dp [ x ] [ j ] ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 6 , 4 , 8 , 2 , 9 ] NEW_LINE N = len ( arr ) NEW_LINE print ( LongestSequence ( arr , N ) ) NEW_LINE DEDENT
Maximize the sum of Kth column of a Matrix | Function to maximize the Kth column sum ; Store all the elements of the resultant matrix of size N * N ; Store value of each elements of the matrix ; Fill all the columns < K ; Fill all the columns >= K ; Function to print the matrix ; Driver Code
def findMatrix ( N , K ) : NEW_LINE INDENT mat = [ [ 0 for i in range ( N ) ] for j in range ( N ) ] ; NEW_LINE element = 0 ; NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT for j in range ( 0 , K - 1 ) : NEW_LINE INDENT element += 1 ; NEW_LINE mat [ i ] [ j ] = element ; NEW_LINE DEDENT DEDENT for i in range ( 0 , N ) : NEW_LINE INDENT for j in range ( K - 1 , N ) : NEW_LINE INDENT element += 1 ; NEW_LINE mat [ i ] [ j ] = element ; NEW_LINE DEDENT DEDENT return mat ; NEW_LINE DEDENT def printMatrix ( mat , N ) : NEW_LINE INDENT for i in range ( 0 , N ) : NEW_LINE INDENT for j in range ( 0 , N ) : NEW_LINE INDENT print ( mat [ i ] [ j ] , end = " ▁ " ) ; NEW_LINE DEDENT print ( ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 ; K = 2 ; NEW_LINE mat = findMatrix ( N , K ) ; NEW_LINE printMatrix ( mat , N ) ; NEW_LINE DEDENT
Range sum queries based on given conditions | Function to calculate the sum between the given range as per value of m ; Stores the sum ; Condition for a to print the sum between ranges [ a , b ] ; Return sum ; Function to precalculate the sum of both the vectors ; Make Prefix sum array ; Function to compute the result for each query ; Take a dummy vector and copy the element of arr in brr ; Sort the dummy vector ; Compute prefix sum of both vectors ; Performs operations ; Function Call to find sum ; Function Call to find sum ; Driver Code ; Given arr [ ] ; Number of queries ; Given queries ; Function Call
def range_sum ( arr , a , b ) : NEW_LINE INDENT sum = 0 NEW_LINE if ( a - 2 < 0 ) : NEW_LINE INDENT sum = arr [ b - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT sum = ( arr [ b - 1 ] - arr [ a - 2 ] ) NEW_LINE DEDENT return sum NEW_LINE DEDENT def precompute_sum ( arr , brr ) : NEW_LINE INDENT N = len ( arr ) NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT arr [ i ] = arr [ i ] + arr [ i - 1 ] NEW_LINE brr [ i ] = brr [ i ] + brr [ i - 1 ] NEW_LINE DEDENT DEDENT def find_sum ( arr , q , Queries ) : NEW_LINE INDENT brr = arr . copy ( ) NEW_LINE N = len ( arr ) NEW_LINE brr . sort ( ) NEW_LINE precompute_sum ( arr , brr ) NEW_LINE for i in range ( q ) : NEW_LINE INDENT m = Queries [ i ] [ 0 ] NEW_LINE a = Queries [ i ] [ 1 ] NEW_LINE b = Queries [ i ] [ 2 ] NEW_LINE if ( m == 1 ) : NEW_LINE INDENT print ( range_sum ( arr , a , b ) , end = ' ▁ ' ) NEW_LINE DEDENT elif ( m == 2 ) : NEW_LINE INDENT print ( range_sum ( brr , a , b ) , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 0 , 6 , 4 , 2 , 7 , 2 , 7 ] NEW_LINE Q = 1 NEW_LINE Queries = [ [ 2 , 3 , 6 ] ] NEW_LINE find_sum ( arr , Q , Queries ) NEW_LINE DEDENT
XOR of all possible pairwise sum from two given Arrays | Python3 program to implement the above approach ; Function to calculate the XOR of the sum of every pair ; Stores the maximum bit ; Look for all the k - th bit ; Stores the modulo of elements B [ ] with ( 2 ^ ( k + 1 ) ) ; Calculate modulo of array B [ ] with ( 2 ^ ( k + 1 ) ) ; Sort the array C [ ] ; Stores the total number whose k - th bit is set ; Calculate and store the modulo of array A [ ] with ( 2 ^ ( k + 1 ) ) ; Lower bound to count the number of elements having k - th bit in the range ( 2 ^ k - x , 2 * 2 ^ ( k ) - x ) ; Add total number i . e ( r - l ) whose k - th bit is one ; Lower bound to count the number of elements having k - th bit in range ( 3 * 2 ^ k - x , 4 * 2 ^ ( k ) - x ) ; If count is even , Xor of k - th bit becomes zero , no need to add to the answer . If count is odd , only then , add to the final answer ; Return answer ; Driver code ; Function call
from bisect import bisect , bisect_left , bisect_right NEW_LINE def XorSum ( A , B , N ) : NEW_LINE INDENT maxBit = 29 NEW_LINE ans = 0 NEW_LINE for k in range ( maxBit ) : NEW_LINE INDENT C = [ 0 ] * N NEW_LINE for i in range ( N ) : NEW_LINE INDENT C [ i ] = B [ i ] % ( 1 << ( k + 1 ) ) NEW_LINE DEDENT C = sorted ( C ) NEW_LINE count = 0 NEW_LINE l , r = 0 , 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT x = A [ i ] % ( 1 << ( k + 1 ) ) NEW_LINE l = bisect_left ( C , ( 1 << k ) - x ) NEW_LINE r = bisect_left ( C , ( 1 << k ) * 2 - x ) NEW_LINE count += ( r - l ) NEW_LINE l = bisect_left ( C , ( 1 << k ) * 3 - x ) NEW_LINE r = bisect_left ( C , ( 1 << k ) * 4 - x ) NEW_LINE count += ( r - l ) NEW_LINE DEDENT if ( count & 1 ) : NEW_LINE INDENT ans += ( 1 << k ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 4 , 6 , 0 , 0 , 3 , 3 ] NEW_LINE B = [ 0 , 5 , 6 , 5 , 0 , 3 ] NEW_LINE N = len ( A ) NEW_LINE print ( XorSum ( A , B , N ) ) NEW_LINE DEDENT
Minimize count of Subsets with difference between maximum and minimum element not exceeding K | Function to find the minimum count of subsets of required type ; Stores the result ; Store the maximum and minimum element of the current subset ; Update current maximum ; If difference exceeds K ; Update count ; Update maximum and minimum to the current subset ; Driver Code
def findCount ( arr , N , K ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE result = 1 NEW_LINE cur_max = arr [ 0 ] NEW_LINE cur_min = arr [ 0 ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT cur_max = arr [ i ] NEW_LINE if ( cur_max - cur_min > K ) : NEW_LINE INDENT result += 1 NEW_LINE cur_max = arr [ i ] NEW_LINE cur_min = arr [ i ] NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT arr = [ 1 , 10 , 8 , 3 , 9 ] NEW_LINE K = 3 NEW_LINE N = len ( arr ) NEW_LINE print ( findCount ( arr , N , K ) ) NEW_LINE
Minimize the Sum of all the subarrays made up of the products of same | Python3 Program to implement the above approach ; Function to rearrange the second array such that the sum of its product of same indexed elements from both the arrays is minimized ; Stores ( i - 1 ) * ( n - i ) * a [ i ] for every i - th element ; Updating the value of pro according to the function ; Sort the array in reverse order ; Sort the products ; Updating the ans ; Return the ans ; Driver code ; Function call
mod = 1e9 + 7 NEW_LINE def findMinValue ( a , b ) : NEW_LINE INDENT n = len ( a ) NEW_LINE pro = [ 0 ] * ( n ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT pro [ i ] = ( ( i + 1 ) * ( n - i ) ) NEW_LINE pro [ i ] *= ( a [ i ] ) NEW_LINE DEDENT b . sort ( reverse = True ) NEW_LINE pro . sort ( ) NEW_LINE ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans += ( pro [ i ] % mod * b [ i ] ) % mod NEW_LINE ans %= mod NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 2 , 3 ] NEW_LINE b = [ 2 , 3 , 2 ] NEW_LINE print ( int ( findMinValue ( a , b ) ) ) NEW_LINE DEDENT
Sort Array such that smallest is at 0 th index and next smallest it at last index and so on | Python3 program for the above approach Function to perform the rearrangement ; Initialize variables ; Loop until i crosses j ; This check is to find the minimum values in the ascending order ; Condition to alternatively iterate variable i and j ; Perform swap operation ; Increment i ; Assign the value of min ; Perform swap ; Decrement i ; Assign the value of min ; Print the array ; Driver Code ; Given Array arr [ ] ; Function call
def rearrange ( a , N ) : NEW_LINE INDENT i = 0 NEW_LINE j = N - 1 NEW_LINE min = 0 NEW_LINE x = 0 NEW_LINE while ( i < j ) : NEW_LINE INDENT for k in range ( i , j + 1 ) : NEW_LINE INDENT if ( a [ k ] < a [ min ] ) : NEW_LINE INDENT min = k NEW_LINE DEDENT DEDENT if ( x % 2 == 0 ) : NEW_LINE INDENT temp = a [ i ] NEW_LINE a [ i ] = a [ min ] NEW_LINE a [ min ] = temp NEW_LINE i += 1 NEW_LINE min = i NEW_LINE DEDENT else : NEW_LINE INDENT temp = a [ j ] NEW_LINE a [ j ] = a [ min ] NEW_LINE a [ min ] = temp NEW_LINE j -= 1 NEW_LINE min = j NEW_LINE DEDENT x += 1 NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 3 , 3 , 4 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE rearrange ( arr , N ) NEW_LINE DEDENT
Maximum number of elements greater than X after equally distributing subset of array | Function to find the maximum number of elements greater than X by equally distributing ; Sorting the array ; Loop to iterate over the elements of the array ; If no more elements can become larger than x ; Driver Code
def redistribute ( arr , n , x ) : NEW_LINE INDENT arr . sort ( reverse = True ) NEW_LINE sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE if ( sum / ( i + 1 ) < x ) : NEW_LINE INDENT print ( i ) NEW_LINE break NEW_LINE DEDENT DEDENT if ( i == n ) : NEW_LINE INDENT print ( n ) NEW_LINE DEDENT DEDENT arr = [ 5 , 1 , 2 , 1 ] NEW_LINE x = 3 NEW_LINE redistribute ( arr , 4 , x ) NEW_LINE
Last element remaining by deleting two largest elements and replacing by their absolute difference if they are unequal | Python3 program for the above approach ; Function to print the remaining element ; Priority queue can be used to construct max - heap ; Insert all element of arr [ ] into priority queue . Default priority queue in Python is min - heap so use - 1 * arr [ i ] ; Perform operation until heap size becomes 0 or 1 ; Remove largest element ; Remove 2 nd largest element ; If extracted elements are not equal ; Find X - Y and push it to heap ; If heap size is 1 , then print the remaining element ; Else print " - 1" ; Driver Code ; Given array arr [ ] ; Size of array arr [ ] ; Function call
from queue import PriorityQueue NEW_LINE def final_element ( arr , n ) : NEW_LINE INDENT heap = PriorityQueue ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT heap . put ( - 1 * arr [ i ] ) NEW_LINE DEDENT while ( heap . qsize ( ) > 1 ) : NEW_LINE INDENT X = - 1 * heap . get ( ) NEW_LINE Y = - 1 * heap . get ( ) NEW_LINE if ( X != Y ) : NEW_LINE INDENT diff = abs ( X - Y ) NEW_LINE heap . put ( - 1 * diff ) NEW_LINE DEDENT DEDENT if ( heap . qsize ( ) == 1 ) : NEW_LINE INDENT print ( - 1 * heap . get ( ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 5 , 2 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE final_element ( arr , n ) NEW_LINE DEDENT