codeparrot/xlcost-text-to-code huggingface dataset:
Maximise array sum after taking non | Python3 implementation of the approach ; To store the states of dp ; To check if a given state has been solved ; To store the prefix - sum ; Function to fill the prefix_sum [ ] with the prefix sum of the given array ; Function to find the maximum sum subsequence such that no two elements are adjacent ; Base case ; To check if a state has been solved ; Required recurrence relation ; Returning the value ; Driver code ; Finding prefix - sum ; Finding the maximum possible sum
maxLen = 10 NEW_LINE dp = [ 0 ] * maxLen ; NEW_LINE v = [ 0 ] * maxLen ; NEW_LINE prefix_sum = [ 0 ] * maxLen ; NEW_LINE def findPrefixSum ( arr , n ) : NEW_LINE INDENT prefix_sum [ 0 ] = arr [ 0 ] ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT prefix_sum [ i ] = arr [ i ] + prefix_sum [ i - 1 ] ; NEW_LINE DEDENT DEDENT def maxSum ( arr , i , n , k ) : NEW_LINE INDENT if ( i + k > n ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( v [ i ] ) : NEW_LINE INDENT return dp [ i ] ; NEW_LINE DEDENT v [ i ] = 1 ; NEW_LINE if ( i == 0 ) : NEW_LINE INDENT x = prefix_sum [ k - 1 ] ; NEW_LINE DEDENT else : NEW_LINE INDENT x = prefix_sum [ i + k - 1 ] - prefix_sum [ i - 1 ] ; NEW_LINE DEDENT dp [ i ] = max ( maxSum ( arr , i + 1 , n , k ) , x + maxSum ( arr , i + k + 1 , n , k ) ) ; NEW_LINE return dp [ i ] ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 3 , 7 , 6 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE k = 1 ; NEW_LINE findPrefixSum ( arr , n ) ; NEW_LINE print ( maxSum ( arr , 0 , n , k ) ) ; NEW_LINE DEDENT
Find maximum path sum in a 2D matrix when exactly two left moves are allowed | Python3 program to find maximum path sum in a 2D matrix when exactly two left moves are allowed ; Function to return the maximum path sum ; Copy last column i . e . starting and ending columns in another array ; Calculate suffix sum in each row ; Select the path we are going to follow ; Driver Code
import numpy as np NEW_LINE N = 3 NEW_LINE M = 3 NEW_LINE def findMaxSum ( arr ) : NEW_LINE INDENT sum = 0 ; NEW_LINE b = np . zeros ( ( N , M ) ) ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT b [ i ] [ M - 1 ] = arr [ i ] [ M - 1 ] ; NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( M - 2 , - 1 , - 1 ) : NEW_LINE INDENT b [ i ] [ j ] = arr [ i ] [ j ] + b [ i ] [ j + 1 ] ; NEW_LINE DEDENT DEDENT for i in range ( 1 , N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT sum = max ( sum , b [ i ] [ j ] + b [ i - 1 ] [ j ] ) ; NEW_LINE b [ i ] [ j ] = max ( b [ i ] [ j ] , b [ i - 1 ] [ j ] + arr [ i ] [ j ] ) ; NEW_LINE DEDENT DEDENT return sum ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ [ 3 , 7 , 4 ] , [ 1 , 9 , 6 ] , [ 1 , 7 , 7 ] ] ; NEW_LINE print ( findMaxSum ( arr ) ) ; NEW_LINE DEDENT
Queries to check if string B exists as substring in string A | Python3 implementation of the approach ; Function to return the modular inverse using Fermat 's little theorem ; Function to generate hash ; To store prefix - sum of rolling hash ; Multiplier for different values of i ; Generating hash value for string b ; Generating prefix - sum of hash of a ; Function that returns true if the required sub - string in a is equal to b ; To store hash of required sub - string of A ; If i = 0 then requires hash value ; Required hash if i != 0 ; Comparing hash with hash of B ; Driver Code ; Generating hash ; Queries ; Perform queries
mod = 3803 NEW_LINE d = 26 NEW_LINE hash_b = 0 NEW_LINE hash_a = [ ] NEW_LINE mul = [ ] NEW_LINE def mi ( x ) : NEW_LINE INDENT global mod NEW_LINE p = mod - 2 NEW_LINE s = 1 NEW_LINE while p != 1 : NEW_LINE INDENT if p % 2 == 1 : NEW_LINE INDENT s = ( s * x ) % mod NEW_LINE DEDENT x = ( x * x ) % mod NEW_LINE p //= 2 NEW_LINE DEDENT return ( s * x ) % mod NEW_LINE DEDENT def genHash ( a , b ) : NEW_LINE INDENT global hash_b , hash_a , mul , d , mod NEW_LINE hash_a = [ 0 ] * len ( a ) NEW_LINE mul = [ 0 ] * len ( a ) NEW_LINE for i in range ( len ( b ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT hash_b = ( hash_b * d + ( ord ( b [ i ] ) - 97 ) ) % mod NEW_LINE DEDENT mul [ 0 ] = 1 NEW_LINE hash_a [ 0 ] = ( ord ( a [ 0 ] ) - 97 ) % mod NEW_LINE for i in range ( 1 , len ( a ) ) : NEW_LINE INDENT mul [ i ] = ( mul [ i - 1 ] * d ) % mod NEW_LINE hash_a [ i ] = ( hash_a [ i - 1 ] + mul [ i ] * ( ord ( a [ i ] ) - 97 ) ) % mod NEW_LINE DEDENT DEDENT def checkEqual ( i , len_a , len_b ) : NEW_LINE INDENT global hash_b , hash_a , mul , d , mod NEW_LINE x = - 1 NEW_LINE if i == 0 : NEW_LINE INDENT x = hash_a [ len_b - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT x = ( hash_a [ i + len_b - 1 ] - hash_a [ i - 1 ] + 2 * mod ) % mod NEW_LINE x = ( x * mi ( mul [ i ] ) ) % mod NEW_LINE DEDENT if x == hash_b : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = " abababababa " NEW_LINE b = " aba " NEW_LINE genHash ( a , b ) NEW_LINE queries = [ 0 , 1 , 2 , 3 ] NEW_LINE q = len ( queries ) NEW_LINE for i in range ( q ) : NEW_LINE INDENT if checkEqual ( queries [ i ] , len ( a ) , len ( b ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT DEDENT
Number of ways to divide an array into K equal sum sub | Python3 implementation of the approach ; Array to store the states of DP ; Array to check if a state has been solved before ; To store the sum of the array elements ; Function to find the sum of all the array elements ; Function to return the number of ways ; If sum is not divisible by k answer will be zero ; Base case ; To check if a state has been solved before ; Sum of all the numbers from the beginning of the array ; Setting the current state as solved ; Recurrence relation ; Returning solved state ; Driver code ; Function call to find the sum of the array elements ; Print the number of ways
import numpy as np NEW_LINE max_size = 20 NEW_LINE max_k = 20 NEW_LINE dp = np . zeros ( ( max_size , max_k ) ) ; NEW_LINE v = np . zeros ( ( max_size , max_k ) ) ; NEW_LINE sum = 0 ; NEW_LINE def findSum ( arr , n ) : NEW_LINE INDENT global sum NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] ; NEW_LINE DEDENT DEDENT def cntWays ( arr , i , ck , k , n , curr_sum ) : NEW_LINE INDENT if ( sum % k != 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( i != n and ck == k + 1 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( i == n ) : NEW_LINE INDENT if ( ck == k + 1 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT DEDENT if ( v [ i ] [ ck ] ) : NEW_LINE INDENT return dp [ i ] [ ck ] ; NEW_LINE DEDENT curr_sum += arr [ i ] ; NEW_LINE v [ i ] [ ck ] = 1 ; NEW_LINE dp [ i ] [ ck ] = cntWays ( arr , i + 1 , ck , k , n , curr_sum ) ; NEW_LINE if ( curr_sum == ( sum / k ) * ck ) : NEW_LINE INDENT dp [ i ] [ ck ] += cntWays ( arr , i + 1 , ck + 1 , k , n , curr_sum ) ; NEW_LINE DEDENT return dp [ i ] [ ck ] ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , - 1 , 1 , - 1 , 1 , - 1 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE k = 2 ; NEW_LINE findSum ( arr , n ) ; NEW_LINE print ( cntWays ( arr , 0 , 1 , k , n , 0 ) ) ; NEW_LINE DEDENT
Maximum sum in an array such that every element has exactly one adjacent element to it | Python 3 implementation of the approach ; To store the states of dp ; Function to return the maximized sum ; Base case ; Checks if a state is already solved ; Recurrence relation ; Return the result ; Driver code
arrSize = 51 NEW_LINE dp = [ 0 for i in range ( arrSize ) ] NEW_LINE v = [ False for i in range ( arrSize ) ] NEW_LINE def sumMax ( i , arr , n ) : NEW_LINE INDENT if ( i >= n - 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( v [ i ] ) : NEW_LINE INDENT return dp [ i ] NEW_LINE DEDENT v [ i ] = True NEW_LINE dp [ i ] = max ( arr [ i ] + arr [ i + 1 ] + sumMax ( i + 3 , arr , n ) , sumMax ( i + 1 , arr , n ) ) NEW_LINE return dp [ i ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 1 , 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( sumMax ( 0 , arr , n ) ) NEW_LINE DEDENT
Maximum Sum Subsequence of length k | Python program to calculate the maximum sum of increasing subsequence of length k ; In the implementation dp [ n ] [ k ] represents maximum sum subsequence of length k and the subsequence is ending at index n . ; Initializing whole multidimensional dp array with value - 1 ; For each ith position increasing subsequence of length 1 is equal to that array ith value so initializing dp [ i ] [ 1 ] with that array value ; Starting from 1 st index as we have calculated for 0 th index . Computing optimized dp values in bottom - up manner ; check for increasing subsequence ; Proceed if value is pre calculated ; Check for all the subsequences ending at any j < i and try including element at index i in them for some length l . Update the maximum value for every length . ; The final result would be the maximum value of dp [ i ] [ k ] for all different i . ; When no subsequence of length k is possible sum would be considered zero ; Driver Code
def MaxIncreasingSub ( arr , n , k ) : NEW_LINE INDENT dp = [ - 1 ] * n NEW_LINE ans = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT dp [ i ] = [ - 1 ] * ( k + 1 ) NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT dp [ i ] [ 1 ] = arr [ i ] NEW_LINE DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT for j in range ( i ) : NEW_LINE INDENT if arr [ j ] < arr [ i ] : NEW_LINE INDENT for l in range ( 1 , k ) : NEW_LINE INDENT if dp [ j ] [ l ] != - 1 : NEW_LINE INDENT dp [ i ] [ l + 1 ] = max ( dp [ i ] [ l + 1 ] , dp [ j ] [ l ] + arr [ i ] ) NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if ans < dp [ i ] [ k ] : NEW_LINE INDENT ans = dp [ i ] [ k ] NEW_LINE DEDENT DEDENT return ( 0 if ans == - 1 else ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n , k = 8 , 3 NEW_LINE arr = [ 8 , 5 , 9 , 10 , 5 , 6 , 21 , 8 ] NEW_LINE ans = MaxIncreasingSub ( arr , n , k ) NEW_LINE print ( ans ) NEW_LINE DEDENT
Generate all unique partitions of an integer | Set 2 | Array to store the numbers used to form the required sum ; Function to print the array which contains the unique partitions which are used to form the required sum ; Function to find all the unique partitions remSum = remaining sum to form maxVal is the maximum number that can be used to make the partition ; If remSum == 0 that means the sum is achieved so print the array ; i will begin from maxVal which is the maximum value which can be used to form the sum ; Store the number used in forming sum gradually in the array ; Since i used the rest of partition cant have any number greater than i hence second parameter is i ; Driver code
dp = [ 0 for i in range ( 200 ) ] NEW_LINE count = 0 NEW_LINE def print1 ( idx ) : NEW_LINE INDENT for i in range ( 1 , idx , 1 ) : NEW_LINE INDENT print ( dp [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( " " , end ▁ = ▁ " " ) NEW_LINE DEDENT def solve ( remSum , maxVal , idx , count ) : NEW_LINE INDENT if ( remSum == 0 ) : NEW_LINE INDENT print1 ( idx ) NEW_LINE count += 1 NEW_LINE return NEW_LINE DEDENT i = maxVal NEW_LINE while ( i >= 1 ) : NEW_LINE INDENT if ( i > remSum ) : NEW_LINE INDENT i -= 1 NEW_LINE continue NEW_LINE DEDENT elif ( i <= remSum ) : NEW_LINE INDENT dp [ idx ] = i NEW_LINE solve ( remSum - i , i , idx + 1 , count ) NEW_LINE i -= 1 NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE count = 0 NEW_LINE solve ( n , n , 1 , count ) NEW_LINE DEDENT
Queries for bitwise OR in the given matrix | Python 3 implementation of the approach ; Array to store bit - wise prefix count ; Function to find the prefix sum ; Loop for each bit ; Loop to find prefix - count for each row ; Finding column - wise prefix count ; Function to return the result for a query ; To store the answer ; Loop for each bit ; To store the number of variables with ith bit set ; If count of variables with ith bit set is greater than 0 ; Driver code
bitscount = 32 NEW_LINE n = 3 NEW_LINE prefix_count = [ [ [ 0 for i in range ( n ) ] for j in range ( n ) ] for k in range ( bitscount ) ] NEW_LINE def findPrefixCount ( arr ) : NEW_LINE INDENT for i in range ( bitscount ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT prefix_count [ i ] [ j ] [ 0 ] = ( ( arr [ j ] [ 0 ] >> i ) & 1 ) NEW_LINE for k in range ( 1 , n ) : NEW_LINE INDENT prefix_count [ i ] [ j ] [ k ] = ( ( arr [ j ] [ k ] >> i ) & 1 ) NEW_LINE prefix_count [ i ] [ j ] [ k ] += prefix_count [ i ] [ j ] [ k - 1 ] NEW_LINE DEDENT DEDENT DEDENT for i in range ( bitscount ) : NEW_LINE INDENT for j in range ( 1 , n ) : NEW_LINE INDENT for k in range ( n ) : NEW_LINE INDENT prefix_count [ i ] [ j ] [ k ] += prefix_count [ i ] [ j - 1 ] [ k ] NEW_LINE DEDENT DEDENT DEDENT DEDENT def rangeOr ( x1 , y1 , x2 , y2 ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( bitscount ) : NEW_LINE INDENT if ( x1 == 0 and y1 == 0 ) : NEW_LINE INDENT p = prefix_count [ i ] [ x2 ] [ y2 ] NEW_LINE DEDENT elif ( x1 == 0 ) : NEW_LINE INDENT p = prefix_count [ i ] [ x2 ] [ y2 ] - prefix_count [ i ] [ x2 ] [ y1 - 1 ] NEW_LINE DEDENT elif ( y1 == 0 ) : NEW_LINE INDENT p = prefix_count [ i ] [ x2 ] [ y2 ] - prefix_count [ i ] [ x1 - 1 ] [ y2 ] NEW_LINE DEDENT else : NEW_LINE INDENT p = prefix_count [ i ] [ x2 ] [ y2 ] - prefix_count [ i ] [ x1 - 1 ] [ y2 ] - prefix_count [ i ] [ x2 ] [ y1 - 1 ] + prefix_count [ i ] [ x1 - 1 ] [ y1 - 1 ] ; NEW_LINE DEDENT if ( p != 0 ) : NEW_LINE INDENT ans = ( ans | ( 1 << i ) ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] NEW_LINE findPrefixCount ( arr ) NEW_LINE queries = [ [ 1 , 1 , 1 , 1 ] , [ 1 , 2 , 2 , 2 ] ] NEW_LINE q = len ( queries ) NEW_LINE for i in range ( q ) : NEW_LINE INDENT print ( rangeOr ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] , queries [ i ] [ 2 ] , queries [ i ] [ 3 ] ) ) NEW_LINE DEDENT DEDENT
Queries for bitwise AND in the given matrix | Python 3 implementation of the approach ; Array to store bit - wise prefix count ; Function to find the prefix sum ; Loop for each bit ; Loop to find prefix - count for each row ; Finding column - wise prefix count ; Function to return the result for a query ; To store the answer ; Loop for each bit ; To store the number of variables with ith bit set ; If count of variables with ith bit set is greater than 0 ; Driver code
bitscount = 32 NEW_LINE n = 3 NEW_LINE prefix_count = [ [ [ 0 for i in range ( n ) ] for j in range ( n ) ] for k in range ( bitscount ) ] NEW_LINE def findPrefixCount ( arr ) : NEW_LINE INDENT for i in range ( bitscount ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT prefix_count [ i ] [ j ] [ 0 ] = ( ( arr [ j ] [ 0 ] >> i ) & 1 ) NEW_LINE for k in range ( 1 , n ) : NEW_LINE INDENT prefix_count [ i ] [ j ] [ k ] = ( ( arr [ j ] [ k ] >> i ) & 1 ) NEW_LINE prefix_count [ i ] [ j ] [ k ] += prefix_count [ i ] [ j ] [ k - 1 ] NEW_LINE DEDENT DEDENT DEDENT for i in range ( bitscount ) : NEW_LINE INDENT for j in range ( 1 , n ) : NEW_LINE INDENT for k in range ( n ) : NEW_LINE INDENT prefix_count [ i ] [ j ] [ k ] += prefix_count [ i ] [ j - 1 ] [ k ] NEW_LINE DEDENT DEDENT DEDENT DEDENT def rangeOr ( x1 , y1 , x2 , y2 ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( bitscount ) : NEW_LINE INDENT if ( x1 == 0 and y1 == 0 ) : NEW_LINE INDENT p = prefix_count [ i ] [ x2 ] [ y2 ] NEW_LINE DEDENT elif ( x1 == 0 ) : NEW_LINE INDENT p = prefix_count [ i ] [ x2 ] [ y2 ] - prefix_count [ i ] [ x2 ] [ y1 - 1 ] NEW_LINE DEDENT elif ( y1 == 0 ) : NEW_LINE INDENT p = prefix_count [ i ] [ x2 ] [ y2 ] - prefix_count [ i ] [ x1 - 1 ] [ y2 ] NEW_LINE DEDENT else : NEW_LINE INDENT p = prefix_count [ i ] [ x2 ] [ y2 ] - prefix_count [ i ] [ x1 - 1 ] [ y2 ] - prefix_count [ i ] [ x2 ] [ y1 - 1 ] + prefix_count [ i ] [ x1 - 1 ] [ y1 - 1 ] ; NEW_LINE DEDENT if ( p == ( x2 - x1 + 1 ) * ( y2 - y1 + 1 ) ) : NEW_LINE INDENT ans = ( ans | ( 1 << i ) ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] NEW_LINE findPrefixCount ( arr ) NEW_LINE queries = [ [ 1 , 1 , 1 , 1 ] , [ 1 , 2 , 2 , 2 ] ] NEW_LINE q = len ( queries ) NEW_LINE for i in range ( q ) : NEW_LINE INDENT print ( rangeOr ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] , queries [ i ] [ 2 ] , queries [ i ] [ 3 ] ) ) NEW_LINE DEDENT DEDENT
Maximum sum such that no two elements are adjacent | Set 2 | Python 3 program to implement above approach ; variable to store states of dp ; variable to check if a given state has been solved ; Function to find the maximum sum subsequence such that no two elements are adjacent ; Base case ; To check if a state has been solved ; Required recurrence relation ; Returning the value ; Driver code
maxLen = 10 NEW_LINE dp = [ 0 for i in range ( maxLen ) ] NEW_LINE v = [ 0 for i in range ( maxLen ) ] NEW_LINE def maxSum ( arr , i , n ) : NEW_LINE INDENT if ( i >= n ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( v [ i ] ) : NEW_LINE INDENT return dp [ i ] NEW_LINE DEDENT v [ i ] = 1 NEW_LINE dp [ i ] = max ( maxSum ( arr , i + 1 , n ) , arr [ i ] + maxSum ( arr , i + 2 , n ) ) NEW_LINE return dp [ i ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 12 , 9 , 7 , 33 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maxSum ( arr , 0 , n ) ) NEW_LINE DEDENT
Optimal Strategy for a Game | Set 2 | Python3 program to find out maximum value from a given sequence of coins ; For both of your choices , the opponent gives you total sum minus maximum of his value ; Returns optimal value possible that a player can collect from an array of coins of size n . Note than n must be even ; Compute sum of elements ; Initialize memoization table ; Driver Code
MAX = 100 NEW_LINE memo = [ [ 0 for i in range ( MAX ) ] for j in range ( MAX ) ] NEW_LINE def oSRec ( arr , i , j , Sum ) : NEW_LINE INDENT if ( j == i + 1 ) : NEW_LINE INDENT return max ( arr [ i ] , arr [ j ] ) NEW_LINE DEDENT if ( memo [ i ] [ j ] != - 1 ) : NEW_LINE INDENT return memo [ i ] [ j ] NEW_LINE DEDENT memo [ i ] [ j ] = max ( ( Sum - oSRec ( arr , i + 1 , j , Sum - arr [ i ] ) ) , ( Sum - oSRec ( arr , i , j - 1 , Sum - arr [ j ] ) ) ) NEW_LINE return memo [ i ] [ j ] NEW_LINE DEDENT def optimalStrategyOfGame ( arr , n ) : NEW_LINE INDENT Sum = 0 NEW_LINE Sum = sum ( arr ) NEW_LINE for j in range ( MAX ) : NEW_LINE INDENT for k in range ( MAX ) : NEW_LINE INDENT memo [ j ] [ k ] = - 1 NEW_LINE DEDENT DEDENT return oSRec ( arr , 0 , n - 1 , Sum ) NEW_LINE DEDENT arr1 = [ 8 , 15 , 3 , 7 ] NEW_LINE n = len ( arr1 ) NEW_LINE print ( optimalStrategyOfGame ( arr1 , n ) ) NEW_LINE arr2 = [ 2 , 2 , 2 , 2 ] NEW_LINE n = len ( arr2 ) NEW_LINE print ( optimalStrategyOfGame ( arr2 , n ) ) NEW_LINE arr3 = [ 20 , 30 , 2 , 2 , 2 , 10 ] NEW_LINE n = len ( arr3 ) NEW_LINE print ( optimalStrategyOfGame ( arr3 , n ) ) NEW_LINE
Minimum cost to select K strictly increasing elements | Python3 program for the above approach ; Function to calculate min cost to choose k increasing elements ; If k elements are counted return 0 ; If all elements of array has been traversed then return MAX_VALUE ; To check if this is already calculated ; When i 'th elements  is not considered ; When the ith element is greater than previous element check if adding its cost makes total cost minimum ; Driver code
N = 1005 ; NEW_LINE K = 20 ; NEW_LINE n = 0 NEW_LINE k = 0 NEW_LINE dp = [ [ [ - 1 for k in range ( K + 1 ) ] for j in range ( N + 1 ) ] for i in range ( N + 1 ) ] NEW_LINE def minCost ( i , prev , cnt , ele , cost ) : NEW_LINE INDENT if ( cnt == k + 1 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( i == n + 1 ) : NEW_LINE INDENT return 100000 ; NEW_LINE DEDENT ans = dp [ i ] [ prev ] [ cnt ] ; NEW_LINE if ( ans != - 1 ) : NEW_LINE INDENT return ans ; NEW_LINE DEDENT ans = minCost ( i + 1 , prev , cnt , ele , cost ) ; NEW_LINE if ( ele [ i ] > ele [ prev ] ) : NEW_LINE INDENT ans = min ( ans , cost [ i ] + minCost ( i + 1 , i , cnt + 1 , ele , cost ) ) ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 ; NEW_LINE k = 2 ; NEW_LINE ele = [ 0 , 2 , 6 , 4 , 8 ] NEW_LINE cost = [ 0 , 40 , 20 , 30 , 10 ] NEW_LINE ans = minCost ( 1 , 0 , 1 , ele , cost ) ; NEW_LINE if ( ans == 100000 ) : NEW_LINE INDENT ans = - 1 ; NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT
Maximize the subarray sum after multiplying all elements of any subarray with X | Python3 implementation of the approach ; Function to return the maximum sum ; Base case ; If already calculated ; If no elements have been chosen ; Do not choose any element and use Kadane 's algorithm by taking max ; Choose the sub - array and multiply x ; Choose the sub - array and multiply x ; End the sub - array multiplication ; No more multiplication ; Memoize and return the answer ; Function to get the maximum sum ; Initialize dp with - 1 ; Iterate from every position and find the maximum sum which is possible ; Driver code
N = 5 NEW_LINE def func ( idx , cur , a , dp , n , x ) : NEW_LINE INDENT if ( idx == n ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( dp [ idx ] [ cur ] != - 1 ) : NEW_LINE INDENT return dp [ idx ] [ cur ] NEW_LINE DEDENT ans = 0 NEW_LINE if ( cur == 0 ) : NEW_LINE INDENT ans = max ( ans , a [ idx ] + func ( idx + 1 , 0 , a , dp , n , x ) ) NEW_LINE ans = max ( ans , x * a [ idx ] + func ( idx + 1 , 1 , a , dp , n , x ) ) NEW_LINE DEDENT elif ( cur == 1 ) : NEW_LINE INDENT ans = max ( ans , x * a [ idx ] + func ( idx + 1 , 1 , a , dp , n , x ) ) NEW_LINE ans = max ( ans , a [ idx ] + func ( idx + 1 , 2 , a , dp , n , x ) ) NEW_LINE DEDENT else : NEW_LINE INDENT ans = max ( ans , a [ idx ] + func ( idx + 1 , 2 , a , dp , n , x ) ) NEW_LINE DEDENT dp [ idx ] [ cur ] = ans NEW_LINE return dp [ idx ] [ cur ] NEW_LINE DEDENT def getMaximumSum ( a , n , x ) : NEW_LINE INDENT dp = [ [ - 1 for i in range ( 3 ) ] for j in range ( n ) ] NEW_LINE maxi = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT maxi = max ( maxi , func ( i , 0 , a , dp , n , x ) ) NEW_LINE DEDENT return maxi NEW_LINE DEDENT a = [ - 3 , 8 , - 2 , 1 , - 6 ] NEW_LINE n = len ( a ) NEW_LINE x = - 1 NEW_LINE print ( getMaximumSum ( a , n , x ) ) NEW_LINE