codeparrot/xlcost-text-to-code huggingface dataset:
Maximum number of customers that can be satisfied with given quantity | Python3 program to find maximum number of customers that can be satisfied ; print maximum number of satisfied customers and their indexes ; Creating an vector of pair of total demand and customer number ; Sorting the customers according to their total demand ; Taking the first k customers that can be satisfied by total amount d ; Driver Code ; Initializing variables
v = [ ] NEW_LINE def solve ( n , d , a , b , arr ) : NEW_LINE INDENT first , second = 0 , 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT m = arr [ i ] [ 0 ] NEW_LINE t = arr [ i ] [ 1 ] NEW_LINE v . append ( [ a * m + b * t , i + 1 ] ) NEW_LINE DEDENT v . sort ( ) NEW_LINE ans = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if v [ i ] [ first ] <= d : NEW_LINE INDENT ans . append ( v [ i ] [ second ] ) NEW_LINE d -= v [ i ] [ first ] NEW_LINE DEDENT DEDENT print ( len ( ans ) ) NEW_LINE for i in range ( len ( ans ) ) : NEW_LINE INDENT print ( ans [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE d = 5 NEW_LINE a = 1 NEW_LINE b = 1 NEW_LINE arr = [ [ 2 , 0 ] , [ 3 , 2 ] , [ 4 , 4 ] , [ 10 , 0 ] , [ 0 , 1 ] ] NEW_LINE solve ( n , d , a , b , arr ) NEW_LINE DEDENT
Partition into two subarrays of lengths k and ( N | Function to calculate max_difference ; Sum of the array ; Sort the array in descending order ; Calculating max_difference ; Driver Code
def maxDifference ( arr , N , k ) : NEW_LINE INDENT S = 0 NEW_LINE S1 = 0 NEW_LINE max_difference = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT S += arr [ i ] NEW_LINE DEDENT arr . sort ( reverse = True ) NEW_LINE M = max ( k , N - k ) NEW_LINE for i in range ( M ) : NEW_LINE INDENT S1 += arr [ i ] NEW_LINE DEDENT max_difference = S1 - ( S - S1 ) NEW_LINE return max_difference NEW_LINE DEDENT arr = [ 8 , 4 , 5 , 2 , 10 ] NEW_LINE N = len ( arr ) NEW_LINE k = 2 NEW_LINE print ( maxDifference ( arr , N , k ) ) NEW_LINE
Minimum sum of product of two arrays | Function to find the minimum product ; Find product of current elements and update result . ; If both product and b [ i ] are negative , we must increase value of a [ i ] to minimize result . ; If both product and a [ i ] are negative , we must decrease value of a [ i ] to minimize result . ; Similar to above two cases for positive product . ; Check if current difference becomes higher than the maximum difference so far . ; Driver function
def minproduct ( a , b , n , k ) : NEW_LINE INDENT diff = 0 NEW_LINE res = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT pro = a [ i ] * b [ i ] NEW_LINE res = res + pro NEW_LINE if ( pro < 0 and b [ i ] < 0 ) : NEW_LINE INDENT temp = ( a [ i ] + 2 * k ) * b [ i ] NEW_LINE DEDENT elif ( pro < 0 and a [ i ] < 0 ) : NEW_LINE INDENT temp = ( a [ i ] - 2 * k ) * b [ i ] NEW_LINE DEDENT elif ( pro > 0 and a [ i ] < 0 ) : NEW_LINE INDENT temp = ( a [ i ] + 2 * k ) * b [ i ] NEW_LINE DEDENT elif ( pro > 0 and a [ i ] > 0 ) : NEW_LINE INDENT temp = ( a [ i ] - 2 * k ) * b [ i ] NEW_LINE DEDENT d = abs ( pro - temp ) NEW_LINE if ( d > diff ) : NEW_LINE INDENT diff = d NEW_LINE DEDENT DEDENT return res - diff NEW_LINE DEDENT a = [ 2 , 3 , 4 , 5 , 4 ] NEW_LINE b = [ 3 , 4 , 2 , 3 , 2 ] NEW_LINE n = 5 NEW_LINE k = 3 NEW_LINE print ( minproduct ( a , b , n , k ) ) NEW_LINE
Split n into maximum composite numbers | Function to calculate the maximum number of composite numbers adding upto n ; 4 is the smallest composite number ; stores the remainder when n is divided n is divided by 4 ; if remainder is 0 , then it is perfectly divisible by 4. ; if the remainder is 1 ; If the number is less then 9 , that is 5 , then it cannot be expressed as 4 is the only composite number less than 5 ; If the number is greater then 8 , and has a remainder of 1 , then express n as n - 9 a and it is perfectly divisible by 4 and for 9 , count 1. ; When remainder is 2 , just subtract 6 from n , so that n is perfectly divisible by 4 and count 1 for 6 which is subtracted . ; if the number is 7 , 11 which cannot be expressed as sum of any composite numbers ; when the remainder is 3 , then subtract 15 from it and n becomes perfectly divisible by 4 and we add 2 for 9 and 6 , which is getting subtracted to make n perfectly divisible by 4. ; Driver Code
def count ( n ) : NEW_LINE INDENT if ( n < 4 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT rem = n % 4 NEW_LINE if ( rem == 0 ) : NEW_LINE INDENT return n // 4 NEW_LINE DEDENT if ( rem == 1 ) : NEW_LINE INDENT if ( n < 9 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return ( n - 9 ) // 4 + 1 NEW_LINE DEDENT if ( rem == 2 ) : NEW_LINE INDENT return ( n - 6 ) // 4 + 1 NEW_LINE DEDENT if ( rem == 3 ) : NEW_LINE INDENT if ( n < 15 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return ( n - 15 ) // 4 + 2 NEW_LINE DEDENT DEDENT n = 90 NEW_LINE print ( count ( n ) ) NEW_LINE n = 143 NEW_LINE print ( count ( n ) ) NEW_LINE
Policemen catch thieves | Returns maximum number of thieves that can be caught . ; store indices in list ; track lowest current indices of thief : thi [ l ] , police : pol [ r ] ; can be caught ; increment the minimum index ; Driver program
def policeThief ( arr , n , k ) : NEW_LINE INDENT i = 0 NEW_LINE l = 0 NEW_LINE r = 0 NEW_LINE res = 0 NEW_LINE thi = [ ] NEW_LINE pol = [ ] NEW_LINE while i < n : NEW_LINE INDENT if arr [ i ] == ' P ' : NEW_LINE INDENT pol . append ( i ) NEW_LINE DEDENT elif arr [ i ] == ' T ' : NEW_LINE INDENT thi . append ( i ) NEW_LINE DEDENT i += 1 NEW_LINE DEDENT while l < len ( thi ) and r < len ( pol ) : NEW_LINE INDENT if ( abs ( thi [ l ] - pol [ r ] ) <= k ) : NEW_LINE INDENT res += 1 NEW_LINE l += 1 NEW_LINE r += 1 NEW_LINE DEDENT elif thi [ l ] < pol [ r ] : NEW_LINE INDENT l += 1 NEW_LINE DEDENT else : NEW_LINE INDENT r += 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr1 = [ ' P ' , ' T ' , ' T ' , ' P ' , ' T ' ] NEW_LINE k = 2 NEW_LINE n = len ( arr1 ) NEW_LINE print ( ( " Maximum ▁ thieves ▁ caught : ▁ { } " . format ( policeThief ( arr1 , n , k ) ) ) ) NEW_LINE arr2 = [ ' T ' , ' T ' , ' P ' , ' P ' , ' T ' , ' P ' ] NEW_LINE k = 2 NEW_LINE n = len ( arr2 ) NEW_LINE print ( ( " Maximum ▁ thieves ▁ caught : ▁ { } " . format ( policeThief ( arr2 , n , k ) ) ) ) NEW_LINE arr3 = [ ' P ' , ' T ' , ' P ' , ' T ' , ' T ' , ' P ' ] NEW_LINE k = 3 NEW_LINE n = len ( arr3 ) NEW_LINE print ( ( " Maximum ▁ thieves ▁ caught : ▁ { } " . format ( policeThief ( arr3 , n , k ) ) ) ) NEW_LINE DEDENT
Minimum rotations to unlock a circular lock | function for min rotation ; iterate till input and unlock code become 0 ; input and unlock last digit as reminder ; find min rotation ; update code and input ; Driver Code
def minRotation ( input , unlock_code ) : NEW_LINE INDENT rotation = 0 ; NEW_LINE while ( input > 0 or unlock_code > 0 ) : NEW_LINE INDENT input_digit = input % 10 ; NEW_LINE code_digit = unlock_code % 10 ; NEW_LINE rotation += min ( abs ( input_digit - code_digit ) , 10 - abs ( input_digit - code_digit ) ) ; NEW_LINE input = int ( input / 10 ) ; NEW_LINE unlock_code = int ( unlock_code / 10 ) ; NEW_LINE DEDENT return rotation ; NEW_LINE DEDENT input = 28756 ; NEW_LINE unlock_code = 98234 ; NEW_LINE print ( " Minimum ▁ Rotation ▁ = " , minRotation ( input , unlock_code ) ) ; NEW_LINE
Job Scheduling with two jobs allowed at a time | Python3 program to check if all jobs can be scheduled if two jobs are allowed at a time . ; making a pair of starting and ending time of job ; sorting according to starting time of job ; starting first and second job simultaneously ; Checking if starting time of next new job is greater than ending time of currently scheduled first job ; Checking if starting time of next new job is greater than ending time of ocurrently scheduled second job ; Driver Code ; startin = [ 1 , 2 , 4 ] starting time of jobs endin = [ 2 , 3 , 5 ] ending times of jobs
def checkJobs ( startin , endin , n ) : NEW_LINE INDENT a = [ ] NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT a . append ( [ startin [ i ] , endin [ i ] ] ) NEW_LINE DEDENT a . sort ( ) NEW_LINE tv1 = a [ 0 ] [ 1 ] NEW_LINE tv2 = a [ 1 ] [ 1 ] NEW_LINE for i in range ( 2 , n ) : NEW_LINE INDENT if ( a [ i ] [ 0 ] >= tv1 ) : NEW_LINE INDENT tv1 = tv2 NEW_LINE tv2 = a [ i ] [ 1 ] NEW_LINE DEDENT elif ( a [ i ] [ 0 ] >= tv2 ) : NEW_LINE INDENT tv2 = a [ i ] [ 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT return 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE print ( checkJobs ( startin , endin , n ) ) NEW_LINE DEDENT
Minimum cost for acquiring all coins with k extra coins allowed with every coin | Python3 program to acquire all n coins at minimum cost with multiple values of k . ; Converts coin [ ] to prefix sum array ; sort the coins values ; maintain prefix sum array ; Function to calculate min cost when we can get k extra coins after paying cost of one . ; calculate no . of coins needed ; return sum of from prefix array ; Driver code
import math as mt NEW_LINE def preprocess ( coin , n ) : NEW_LINE INDENT coin . sort ( ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT coin [ i ] += coin [ i - 1 ] NEW_LINE DEDENT DEDENT def minCost ( coin , n , k ) : NEW_LINE INDENT coins_needed = mt . ceil ( 1.0 * n / ( k + 1 ) ) NEW_LINE return coin [ coins_needed - 1 ] NEW_LINE DEDENT coin = [ 8 , 5 , 3 , 10 , 2 , 1 , 15 , 25 ] NEW_LINE n = len ( coin ) NEW_LINE preprocess ( coin , n ) NEW_LINE k = 3 NEW_LINE print ( minCost ( coin , n , k ) ) NEW_LINE k = 7 NEW_LINE print ( minCost ( coin , n , k ) ) NEW_LINE
Program for Worst Fit algorithm in Memory Management | Function to allocate memory to blocks as per worst fit algorithm ; Initially no block is assigned to any process ; pick each process and find suitable blocks according to its size ad assign to it ; Find the best fit block for current process ; If we could find a block for current process ; allocate block j to p [ i ] process ; Reduce available memory in this block . ; Driver code
def worstFit ( blockSize , m , processSize , n ) : NEW_LINE INDENT allocation = [ - 1 ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT wstIdx = - 1 NEW_LINE for j in range ( m ) : NEW_LINE INDENT if blockSize [ j ] >= processSize [ i ] : NEW_LINE INDENT if wstIdx == - 1 : NEW_LINE INDENT wstIdx = j NEW_LINE DEDENT elif blockSize [ wstIdx ] < blockSize [ j ] : NEW_LINE INDENT wstIdx = j NEW_LINE DEDENT DEDENT DEDENT if wstIdx != - 1 : NEW_LINE INDENT allocation [ i ] = wstIdx NEW_LINE blockSize [ wstIdx ] -= processSize [ i ] NEW_LINE DEDENT DEDENT print ( " Process ▁ No . ▁ Process ▁ Size ▁ Block ▁ no . " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( i + 1 , " ▁ " , processSize [ i ] , end = " ▁ " ) NEW_LINE if allocation [ i ] != - 1 : NEW_LINE INDENT print ( allocation [ i ] + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Allocated " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT blockSize = [ 100 , 500 , 200 , 300 , 600 ] NEW_LINE processSize = [ 212 , 417 , 112 , 426 ] NEW_LINE m = len ( blockSize ) NEW_LINE n = len ( processSize ) NEW_LINE worstFit ( blockSize , m , processSize , n ) NEW_LINE DEDENT
Maximize array sum after K negations | Set 1 | This function does k operations on array in a way that maximize the array sum . index -- > stores the index of current minimum element for j 'th operation ; Modify array K number of times ; Find minimum element in array for current operation and modify it i . e ; arr [ j ] -- > - arr [ j ] ; this the condition if we find 0 as minimum element , so it will useless to replace 0 by - ( 0 ) for remaining operations ; Modify element of array ; Calculate sum of array ; Driver code
def maximumSum ( arr , n , k ) : NEW_LINE INDENT for i in range ( 1 , k + 1 ) : NEW_LINE INDENT min = + 2147483647 NEW_LINE index = - 1 NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( arr [ j ] < min ) : NEW_LINE INDENT min = arr [ j ] NEW_LINE index = j NEW_LINE DEDENT DEDENT if ( min == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT arr [ index ] = - arr [ index ] NEW_LINE DEDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT return sum NEW_LINE DEDENT arr = [ - 2 , 0 , 5 , - 1 , 2 ] NEW_LINE k = 4 NEW_LINE n = len ( arr ) NEW_LINE print ( maximumSum ( arr , n , k ) ) NEW_LINE
Maximize array sum after K negations | Set 1 | Python3 program to find maximum array sum after at most k negations ; Sorting given array using in - built java sort function ; If we find a 0 in our sorted array , we stop ; Calculating sum ; Driver code
def sol ( arr , k ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE Sum = 0 NEW_LINE i = 0 NEW_LINE while ( k > 0 ) : NEW_LINE INDENT if ( arr [ i ] >= 0 ) : NEW_LINE INDENT k = 0 NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i ] = ( - 1 ) * arr [ i ] NEW_LINE k = k - 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT for j in range ( len ( arr ) ) : NEW_LINE INDENT Sum += arr [ j ] NEW_LINE DEDENT return Sum NEW_LINE DEDENT arr = [ - 2 , 0 , 5 , - 1 , 2 ] NEW_LINE print ( sol ( arr , 4 ) ) NEW_LINE
Maximize array sum after K negations | Set 1 | Function to calculate sum of the array ; Iterate from 0 to n - 1 ; Function to maximize sum ; Iterate from 0 to n - 1 ; Driver Code ; Function Call
def sumArray ( arr , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT return sum NEW_LINE DEDENT def maximizeSum ( arr , n , k ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE i = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( k and arr [ i ] < 0 ) : NEW_LINE INDENT arr [ i ] *= - 1 NEW_LINE k -= 1 NEW_LINE continue NEW_LINE DEDENT break NEW_LINE DEDENT if ( i == n ) : NEW_LINE INDENT i -= 1 NEW_LINE DEDENT if ( k == 0 or k % 2 == 0 ) : NEW_LINE INDENT return sumArray ( arr , n ) NEW_LINE DEDENT if ( i != 0 and abs ( arr [ i ] ) >= abs ( arr [ i - 1 ] ) ) : NEW_LINE INDENT i -= 1 NEW_LINE DEDENT arr [ i ] *= - 1 NEW_LINE return sumArray ( arr , n ) NEW_LINE DEDENT n = 5 NEW_LINE k = 4 NEW_LINE arr = [ - 3 , - 2 , - 1 , 5 , 6 ] NEW_LINE print ( maximizeSum ( arr , n , k ) ) NEW_LINE
Bin Packing Problem ( Minimize number of used Bins ) | Returns number of bins required using first fit online algorithm ; Initialize result ( Count of bins ) ; Create an array to store remaining space in bins there can be at most n bins ; Place items one by one ; Find the first bin that can accommodate weight [ i ] ; If no bin could accommodate weight [ i ] ; Driver program
def firstFit ( weight , n , c ) : NEW_LINE INDENT res = 0 NEW_LINE bin_rem = [ 0 ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT j = 0 NEW_LINE while ( j < res ) : NEW_LINE INDENT if ( bin_rem [ j ] >= weight [ i ] ) : NEW_LINE INDENT bin_rem [ j ] = bin_rem [ j ] - weight [ i ] NEW_LINE break NEW_LINE DEDENT j += 1 NEW_LINE DEDENT if ( j == res ) : NEW_LINE INDENT bin_rem [ res ] = c - weight [ i ] NEW_LINE res = res + 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT weight = [ 2 , 5 , 4 , 7 , 1 , 3 , 8 ] NEW_LINE c = 10 NEW_LINE n = len ( weight ) NEW_LINE print ( " Number ▁ of ▁ bins ▁ required ▁ in ▁ First ▁ Fit ▁ : ▁ " , firstFit ( weight , n , c ) ) NEW_LINE
Minimum cost to reduce A and B to 0 using square root or divide by 2 | Python program for the above approach ; Function to return the minimum cost of converting A and B to 0 ; If both A and B doesn 't change in  this recusrive call, then return INT_MAX  to save the code from going into  infinite loop ; Base Case ; If the answer of this recursive call is already memoised ; If A is reduced to A / 2 ; If B is reduced to B / 2 ; If both A and B is reduced to sqrt ( A * B ) ; Return the minimum of the value given by the above three subproblems , also memoize the value while returning ; Driver Code
import math as Math NEW_LINE def getMinOperations ( A , B , prevA , prevB , dp ) : NEW_LINE INDENT if ( A == prevA and B == prevB ) : NEW_LINE INDENT return 10 ** 9 ; NEW_LINE DEDENT if ( A == 0 and B == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( dp [ A ] [ B ] != - 1 ) : NEW_LINE INDENT return dp [ A ] [ B ] ; NEW_LINE DEDENT ans1 = getMinOperations ( A // 2 , B , A , B , dp ) ; NEW_LINE if ( ans1 != 10 ** 9 ) : NEW_LINE INDENT ans1 += 1 ; NEW_LINE DEDENT ans2 = getMinOperations ( A , B // 2 , A , B , dp ) ; NEW_LINE if ( ans2 != 10 ** 9 ) : NEW_LINE INDENT ans2 += 1 ; NEW_LINE DEDENT ans3 = getMinOperations ( Math . floor ( Math . sqrt ( A * B ) ) , Math . floor ( Math . sqrt ( A * B ) ) , A , B , dp ) ; NEW_LINE if ( ans3 != 10 ** 9 ) : NEW_LINE INDENT ans3 += 2 ; NEW_LINE DEDENT dp [ A ] [ B ] = min ( ans1 , min ( ans2 , ans3 ) ) NEW_LINE return dp [ A ] [ B ] ; NEW_LINE DEDENT A = 53 NEW_LINE B = 16 NEW_LINE mx = max ( A , B ) ; NEW_LINE dp = [ [ - 1 for i in range ( mx + 1 ) ] for i in range ( mx + 1 ) ] NEW_LINE print ( getMinOperations ( A , B , - 1 , - 1 , dp ) ) ; NEW_LINE
Maximize count of indices with same element by pairing rows from given Matrices | Function to find the maximum defined score ; If all students are assigned ; Check if row is not paired yet ; Check for all indexes ; If values at current indexes are same increase curSum ; Further recursive call ; Store the ans for current mask and return ; Utility function to find the maximum defined score ; Create a mask with all set bits 1 -> row is not paired yet 0 -> row is already paired ; Initialise dp array with - 1 ; Driver Code
def maxScoreSum ( a , b , row , mask , dp ) : NEW_LINE INDENT if ( row >= len ( a ) ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( dp [ mask ] != - 1 ) : NEW_LINE INDENT return dp [ mask ] NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( len ( a ) ) : NEW_LINE INDENT if ( mask & ( 1 << i ) ) : NEW_LINE INDENT newMask = mask ^ ( 1 << i ) NEW_LINE curSum = 0 NEW_LINE for j in range ( len ( a [ i ] ) ) : NEW_LINE INDENT if ( a [ row ] [ j ] == b [ i ] [ j ] ) : NEW_LINE INDENT curSum += 1 NEW_LINE DEDENT DEDENT ans = max ( ans , curSum + maxScoreSum ( a , b , row + 1 , newMask , dp ) ) NEW_LINE DEDENT DEDENT dp [ mask ] = ans NEW_LINE return dp [ mask ] NEW_LINE DEDENT def maxScoreSumUtil ( a , b , N , M ) : NEW_LINE INDENT row = 0 NEW_LINE mask = pow ( 2 , M ) - 1 NEW_LINE dp = [ - 1 ] * ( mask + 1 ) NEW_LINE return maxScoreSum ( a , b , row , mask , dp ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 3 NEW_LINE M = 3 NEW_LINE a = [ [ 1 , 1 , 0 ] , [ 1 , 0 , 1 ] , [ 0 , 0 , 1 ] ] NEW_LINE b = [ [ 1 , 0 , 0 ] , [ 0 , 0 , 1 ] , [ 1 , 1 , 0 ] ] NEW_LINE print ( maxScoreSumUtil ( a , b , N , M ) ) NEW_LINE DEDENT
Split given arrays into subarrays to maximize the sum of maximum and minimum in each subarrays | Function to find the maximum sum of differences of subarrays by splitting array into non - overlapping subarrays ; Stores the answer for prefix and initialize with zero ; Assume i - th index as right endpoint ; Choose the current value as the maximum and minimum ; Find the left endpoint and update the array dp [ ] ; Return answer ; Driver Code
def maxDiffSum ( arr , n ) : NEW_LINE INDENT dp = [ 0 ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT maxVal = arr [ i ] NEW_LINE minVal = arr [ i ] NEW_LINE for j in range ( i , - 1 , - 1 ) : NEW_LINE INDENT minVal = min ( minVal , arr [ j ] ) NEW_LINE maxVal = max ( maxVal , arr [ j ] ) NEW_LINE if ( j - 1 >= 0 ) : NEW_LINE INDENT dp [ i ] = max ( dp [ i ] , maxVal - minVal + dp [ j - 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] = max ( dp [ i ] , maxVal - minVal ) NEW_LINE DEDENT DEDENT DEDENT return dp [ n - 1 ] NEW_LINE DEDENT arr = [ 8 , 1 , 7 , 9 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE print ( maxDiffSum ( arr , N ) ) NEW_LINE
Count of N | python 3 program for the above approach ; Find the value of x raised to the yth power modulo MOD ; Stores the value of x ^ y ; Iterate until y is positive ; Function to perform the Modular Multiplicative Inverse using the Fermat 's little theorem ; Modular division x / y , find modular multiplicative inverse of y and multiply by x ; Function to find Binomial Coefficient C ( n , k ) in O ( k ) time ; Base Case ; Update the value of p and q ; Function to find the count of arrays having K as the first element satisfying the given criteria ; Stores the resultant count of arrays ; Find the factorization of K ; Stores the count of the exponent of the currentprime factor ; N is one last prime factor , for c = 1 -> C ( N - 1 + 1 , 1 ) = N ; Return the totol count ; Driver Code
MOD = 1000000007 NEW_LINE from math import sqrt NEW_LINE def modPow ( x , y ) : NEW_LINE INDENT r = 1 NEW_LINE a = x NEW_LINE while ( y > 0 ) : NEW_LINE INDENT if ( ( y & 1 ) == 1 ) : NEW_LINE INDENT r = ( r * a ) % MOD NEW_LINE DEDENT a = ( a * a ) % MOD NEW_LINE y /= 2 NEW_LINE DEDENT return r NEW_LINE DEDENT def modInverse ( x ) : NEW_LINE INDENT return modPow ( x , MOD - 2 ) NEW_LINE DEDENT def modDivision ( p , q ) : NEW_LINE INDENT return ( p * modInverse ( q ) ) % MOD NEW_LINE DEDENT def C ( n , k ) : NEW_LINE INDENT if ( k > n ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT p = 1 NEW_LINE q = 1 NEW_LINE for i in range ( 1 , k + 1 , 1 ) : NEW_LINE INDENT q = ( q * i ) % MOD NEW_LINE p = ( p * ( n - i + 1 ) ) % MOD NEW_LINE DEDENT return modDivision ( p , q ) NEW_LINE DEDENT def countArrays ( N , K ) : NEW_LINE INDENT res = 1 NEW_LINE for p in range ( 2 , int ( sqrt ( K ) ) , 1 ) : NEW_LINE INDENT c = 0 NEW_LINE while ( K % p == 0 ) : NEW_LINE INDENT K /= p NEW_LINE c += 1 NEW_LINE DEDENT res = ( res * C ( N - 1 + c , c ) ) % MOD NEW_LINE DEDENT if ( N > 1 ) : NEW_LINE INDENT res = ( res * N ) % MOD NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE K = 5 NEW_LINE print ( countArrays ( N , K ) ) NEW_LINE DEDENT
Minimum number of days to debug all programs | Python 3 program for the above approach ; Function to calculate the minimum work sessions ; Break condition ; All bits are set ; Check if already calculated ; Store the answer ; Check if ith bit is set or unset ; Including in current work session ; Including in next work session ; Resultant answer will be minimum of both ; Function to initialize DP array and solve the problem ; Initialize dp table with - 1 ; Resultant mask ; no . of minimum work sessions is even ; no . of minimum work sessions is odd ; Driver code
import sys NEW_LINE def minSessions ( codeTime , dp , ones , n , mask , currTime , WorkingSessionTime ) : NEW_LINE INDENT if ( currTime > WorkingSessionTime ) : NEW_LINE INDENT return sys . maxsize NEW_LINE DEDENT if ( mask == ones ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( dp [ mask ] [ currTime ] != - 1 ) : NEW_LINE INDENT return dp [ mask ] [ currTime ] NEW_LINE DEDENT ans = sys . maxsize NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( ( mask & ( 1 << i ) ) == 0 ) : NEW_LINE INDENT inc = minSessions ( codeTime , dp , ones , n , mask | ( 1 << i ) , currTime + codeTime [ i ] , WorkingSessionTime ) NEW_LINE inc_next = 1 + minSessions ( codeTime , dp , ones , n , mask | ( 1 << i ) , codeTime [ i ] , WorkingSessionTime ) NEW_LINE ans = min ( [ ans , inc , inc_next ] ) NEW_LINE DEDENT DEDENT dp [ mask ] [ currTime ] = ans NEW_LINE return ans NEW_LINE DEDENT def solve ( codeTime , n , WorkingSessionTime ) : NEW_LINE INDENT dp = [ [ - 1 for i in range ( 15 ) ] for j in range ( 1 << 14 ) ] NEW_LINE ones = ( 1 << n ) - 1 NEW_LINE ans = minSessions ( codeTime , dp , ones , n , 0 , 0 , WorkingSessionTime ) NEW_LINE if ( WorkingSessionTime < 6 ) : NEW_LINE INDENT if ( ans % 2 == 0 ) : NEW_LINE INDENT ans = ans // 2 NEW_LINE DEDENT else : NEW_LINE INDENT ans = ( ans / 2 ) + 1 NEW_LINE DEDENT DEDENT return int ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT codeTime = [ 1 , 2 , 3 , 1 , 1 , 3 ] NEW_LINE n = len ( codeTime ) NEW_LINE WorkingSessionTime = 4 NEW_LINE print ( solve ( codeTime , n , WorkingSessionTime ) ) NEW_LINE DEDENT