codeparrot/xlcost-text-to-code huggingface dataset:
Count all possible N | Function to find the number of vowel permutations possible ; To avoid the large output value ; Initialize 2D dp array ; Initialize dp [ 1 ] [ i ] as 1 since string of length 1 will consist of only one vowel in the string ; Directed graph using the adjacency matrix ; Iterate over the range [ 1 , N ] ; Traverse the directed graph ; Traversing the list ; Update dp [ i + 1 ] [ u ] ; Stores total count of permutations ; Return count of permutations ; Driver code
def countVowelPermutation ( n ) : NEW_LINE INDENT MOD = 1e9 + 7 NEW_LINE dp = [ [ 0 for i in range ( 5 ) ] for j in range ( n + 1 ) ] NEW_LINE for i in range ( 5 ) : NEW_LINE INDENT dp [ 1 ] [ i ] = 1 NEW_LINE DEDENT relation = [ [ 1 ] , [ 0 , 2 ] , [ 0 , 1 , 3 , 4 ] , [ 2 , 4 ] , [ 0 ] ] NEW_LINE for i in range ( 1 , n , 1 ) : NEW_LINE INDENT for u in range ( 5 ) : NEW_LINE INDENT dp [ i + 1 ] [ u ] = 0 NEW_LINE for v in relation [ u ] : NEW_LINE INDENT dp [ i + 1 ] [ u ] += dp [ i ] [ v ] % MOD NEW_LINE DEDENT DEDENT DEDENT ans = 0 NEW_LINE for i in range ( 5 ) : NEW_LINE INDENT ans = ( ans + dp [ n ] [ i ] ) % MOD NEW_LINE DEDENT return int ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 2 NEW_LINE print ( countVowelPermutation ( N ) ) NEW_LINE DEDENT
Queries to calculate Bitwise OR of each subtree of a given node in an N | Maximum Number of nodes ; Adjacency list ; Stores Bitwise OR of each node ; Function to add edges to the Tree ; Traverse the edges ; Add edges ; Function to perform DFS Traversal on the given tree ; Initialize answer with bitwise OR of current node ; Iterate over each child of the current node ; Skip parent node ; Call DFS for each child ; Taking bitwise OR of the answer of the child to find node 's OR value ; Function to call DFS from the '= root for precomputing answers ; Function to calculate and print the Bitwise OR for Q queries ; Perform preprocessing ; Iterate over each given query ; Utility function to find and print bitwise OR for Q queries ; Function to add edges to graph ; Function call ; Driver Code ; Number of nodes ; Function call
N = 100005 ; NEW_LINE adj = [ [ ] for i in range ( N ) ] ; NEW_LINE answer = [ 0 for i in range ( N ) ] NEW_LINE def addEdgesToGraph ( Edges , N ) : NEW_LINE INDENT for i in range ( N - 1 ) : NEW_LINE INDENT u = Edges [ i ] [ 0 ] ; NEW_LINE v = Edges [ i ] [ 1 ] ; NEW_LINE adj [ u ] . append ( v ) ; NEW_LINE adj [ v ] . append ( u ) ; NEW_LINE DEDENT DEDENT def DFS ( node , parent , Val ) : NEW_LINE INDENT answer [ node ] = Val [ node ] ; NEW_LINE for child in adj [ node ] : NEW_LINE INDENT if ( child == parent ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT DFS ( child , node , Val ) ; NEW_LINE answer [ node ] = ( answer [ node ] answer [ child ] ) ; NEW_LINE DEDENT DEDENT def preprocess ( Val ) : NEW_LINE INDENT DFS ( 1 , - 1 , Val ) ; NEW_LINE DEDENT def findSubtreeOR ( Queries , Q , Val ) : NEW_LINE INDENT preprocess ( Val ) ; NEW_LINE for i in range ( Q ) : NEW_LINE INDENT print ( answer [ Queries [ i ] ] , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT def findSubtreeORUtil ( N , Edges , Val , Queries , Q ) : NEW_LINE INDENT addEdgesToGraph ( Edges , N ) ; NEW_LINE findSubtreeOR ( Queries , Q , Val ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 ; NEW_LINE Edges = [ [ 1 , 2 ] , [ 1 , 3 ] , [ 3 , 4 ] , [ 3 , 5 ] ] ; NEW_LINE Val = [ 0 , 2 , 3 , 4 , 8 , 16 ] ; NEW_LINE Queries = [ 2 , 3 , 1 ] ; NEW_LINE Q = len ( Queries ) NEW_LINE findSubtreeORUtil ( N , Edges , Val , Queries , Q ) ; NEW_LINE DEDENT
Maximize sum of K elements selected from a Matrix such that each selected element must be preceded by selected row elements | Python program for the above approach ; Function to return the maximum of two elements ; Function to find the maximum sum of selecting K elements from the given 2D array arr ; dp table of size ( K + 1 ) * ( N + 1 ) ; Initialize dp [ 0 ] [ i ] = 0 ; Initialize dp [ i ] [ 0 ] = 0 ; Selecting i elements ; Select i elements till jth row ; sum = 0 , to keep track of cummulative elements sum ; Traverse arr [ j ] [ k ] until number of elements until k > i ; Select arr [ j ] [ k - 1 ] th item ; Store the maxSum in dp [ i ] [ j + 1 ] ; Return the maximum sum ; Driver Code ; Function Call
import math ; NEW_LINE def max ( a , b ) : NEW_LINE INDENT if ( a > b ) : NEW_LINE INDENT return a ; NEW_LINE DEDENT else : NEW_LINE INDENT return b ; NEW_LINE DEDENT DEDENT def maximumsum ( arr , K , N , M ) : NEW_LINE INDENT sum = 0 ; NEW_LINE maxSum = 0 ; NEW_LINE dp = [ [ 0 for i in range ( N + 1 ) ] for j in range ( K + 1 ) ] NEW_LINE for i in range ( 0 , N + 1 ) : NEW_LINE INDENT dp [ 0 ] [ i ] = 0 ; NEW_LINE DEDENT for i in range ( 0 , K + 1 ) : NEW_LINE INDENT dp [ i ] [ 0 ] = 0 ; NEW_LINE DEDENT for i in range ( 1 , K + 1 ) : NEW_LINE INDENT for j in range ( 0 , N ) : NEW_LINE INDENT sum = 0 ; NEW_LINE maxSum = dp [ i ] [ j ] ; NEW_LINE for k in range ( 1 , i + 1 ) : NEW_LINE INDENT if ( k > M ) : NEW_LINE INDENT break ; NEW_LINE DEDENT sum += arr [ j ] [ k - 1 ] ; NEW_LINE maxSum = max ( maxSum , sum + dp [ i - k ] [ j ] ) ; NEW_LINE DEDENT dp [ i ] [ j + 1 ] = maxSum ; NEW_LINE DEDENT DEDENT return dp [ K ] [ N ] ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ 10 , 10 , 100 , 30 ] , [ 80 , 50 , 10 , 50 ] ] ; NEW_LINE N = 2 ; NEW_LINE M = 4 ; NEW_LINE K = 5 ; NEW_LINE print ( maximumsum ( arr , K , N , M ) ) ; NEW_LINE DEDENT
Subsequences of given string consisting of non | Function to find all the subsequences of the str1ing with non - repeating ch1aracters ; Base case ; Insert current subsequence ; If str1 [ i ] is not present in the current subsequence ; Insert str1 [ i ] into the set ; Insert str1 [ i ] into the current subsequence ; Remove str1 [ i ] from current subsequence ; Remove str1 [ i ] from the set ; Not including str1 [ i ] from the current subsequence ; Utility function to print all subsequences of str1ing with non - repeating ch1aracters ; Stores all possible subsequences with non - repeating ch1aracters ; Stores subsequence with non - repeating ch1aracters ; Traverse all possible subsequences containing non - repeating ch1aracters ; Print subsequence ; Driver Code
def FindSub ( sub , ch1 , str1 , res , i ) : NEW_LINE INDENT if ( i == len ( str1 ) ) : NEW_LINE INDENT sub . add ( res ) NEW_LINE return NEW_LINE DEDENT if ( str1 [ i ] not in ch1 ) : NEW_LINE INDENT ch1 . add ( str1 [ i ] ) NEW_LINE FindSub ( sub , ch1 , str1 , res + str1 [ i ] , i + 1 ) NEW_LINE res += str1 [ i ] NEW_LINE res = res [ 0 : len ( res ) - 1 ] NEW_LINE ch1 . remove ( str1 [ i ] ) NEW_LINE DEDENT FindSub ( sub , ch1 , str1 , res , i + 1 ) NEW_LINE DEDENT def printSubwithUniquech1ar ( str1 , N ) : NEW_LINE INDENT sub = set ( ) NEW_LINE ch1 = set ( ) NEW_LINE FindSub ( sub , ch1 , str1 , " " , 0 ) NEW_LINE temp = [ ] NEW_LINE for substr1ing in sub : NEW_LINE temp . append ( substr1ing ) NEW_LINE temp . sort ( reverse = False ) NEW_LINE for x in temp : NEW_LINE print ( x , end = " ▁ " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str2 = " abac " NEW_LINE N = len ( str2 ) NEW_LINE printSubwithUniquech1ar ( str2 , N ) NEW_LINE DEDENT
Count lexicographically increasing K | Function to count K - length strings from first N alphabets ; To keep track of column sum in dp ; Auxiliary 2d dp array ; Initialize dp [ 0 ] [ i ] = 1 and update the column_sum ; Iterate for K times ; Iterate for N times ; dp [ i ] [ j ] : Stores the number of ways to form i - length strings consisting of j letters ; Update the column_sum ; Print number of ways to arrange K - length strings with N alphabets ; Driver Code ; Given N and K ; Function Call
def waysToArrangeKLengthStrings ( N , K ) : NEW_LINE INDENT column_sum = [ 0 for i in range ( N + 1 ) ] NEW_LINE i = 0 NEW_LINE j = 0 NEW_LINE dp = [ [ 0 for i in range ( N + 1 ) ] for j in range ( K + 1 ) ] NEW_LINE for i in range ( N + 1 ) : NEW_LINE INDENT dp [ 0 ] [ i ] = 1 NEW_LINE column_sum [ i ] = 1 NEW_LINE DEDENT for i in range ( 1 , K + 1 ) : NEW_LINE INDENT for j in range ( 1 , N + 1 ) : NEW_LINE INDENT dp [ i ] [ j ] += column_sum [ j - 1 ] NEW_LINE column_sum [ j ] += dp [ i ] [ j ] NEW_LINE DEDENT DEDENT print ( dp [ K ] [ N ] ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE K = 2 NEW_LINE waysToArrangeKLengthStrings ( N , K ) NEW_LINE DEDENT
Count N | Function to count N - length strings consisting of vowels only sorted lexicographically ; Stores count of strings consisting of vowels sorted lexicographically of all possible lengths ; Initialize DP [ 1 ] [ 1 ] ; Traverse the matrix row - wise ; Base Case ; Return the result ; Driver Code ; Function Call
def findNumberOfStrings ( n ) : NEW_LINE INDENT DP = [ [ 0 for i in range ( 6 ) ] for i in range ( n + 1 ) ] NEW_LINE DP [ 1 ] [ 1 ] = 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , 6 ) : NEW_LINE INDENT if ( i == 1 ) : NEW_LINE INDENT DP [ i ] [ j ] = DP [ i ] [ j - 1 ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT DP [ i ] [ j ] = DP [ i ] [ j - 1 ] + DP [ i - 1 ] [ j ] NEW_LINE DEDENT DEDENT DEDENT return DP [ n ] [ 5 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 2 NEW_LINE print ( findNumberOfStrings ( N ) ) NEW_LINE DEDENT
Largest subtree sum for each vertex of given N | Python3 program for the above approach ; Function to perform the DFS Traversal on the given Tree ; To check if v is leaf vertex ; Initialize answer for vertex v ; Traverse adjacency list of v ; Update maximum subtree sum ; If v is leaf ; Function to calculate maximum subtree sum for each vertex ; Add Edegs to the list ; Calculate answer ; Prthe result ; Driver Code ; Given nodes ; Give N edges ; Given values ; Function Call
V = 3 NEW_LINE M = 2 NEW_LINE def dfs ( v , p ) : NEW_LINE INDENT isLeaf = 1 NEW_LINE ans [ v ] = - 10 ** 9 NEW_LINE for u in adj [ v ] : NEW_LINE INDENT if ( u == p ) : NEW_LINE INDENT continue NEW_LINE DEDENT isLeaf = 0 NEW_LINE dfs ( u , v ) NEW_LINE ans [ v ] = max ( ans [ u ] + vals [ v ] , max ( ans [ u ] , vals [ u ] ) ) NEW_LINE DEDENT if ( isLeaf ) : NEW_LINE INDENT ans [ v ] = vals [ v ] NEW_LINE DEDENT DEDENT def printAnswer ( n , edges , vals ) : NEW_LINE INDENT for i in range ( n - 1 ) : NEW_LINE INDENT u = edges [ i ] [ 0 ] - 1 NEW_LINE v = edges [ i ] [ 1 ] - 1 NEW_LINE adj [ u ] . append ( v ) NEW_LINE adj [ v ] . append ( u ) NEW_LINE DEDENT dfs ( 0 , - 1 ) NEW_LINE for x in ans : NEW_LINE INDENT print ( x , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 NEW_LINE edges = [ [ 1 , 2 ] , [ 1 , 3 ] , [ 3 , 4 ] ] NEW_LINE adj = [ [ ] for i in range ( N ) ] NEW_LINE ans = [ 0 for i in range ( N ) ] NEW_LINE vals = [ 1 , - 1 , 0 , 1 ] NEW_LINE printAnswer ( N , edges , vals ) NEW_LINE DEDENT
Queries to count frequencies of a given character in a given range of indices | Function to prcount of char y present in the range [ l , r ] ; Length of the string ; Stores the precomputed results ; Iterate the given string ; Increment dp [ i ] [ y - ' a ' ] by 1 ; Pre - compute ; Number of queries ; Traverse each query ; Print the result for each query ; Driver Code ; Given string ; Given Queries ; Function Call
def noOfChars ( s , queries ) : NEW_LINE INDENT n = len ( s ) NEW_LINE dp = [ [ 0 for i in range ( 26 ) ] for i in range ( n + 1 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT dp [ i + 1 ] [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE for j in range ( 26 ) : NEW_LINE INDENT dp [ i + 1 ] [ j ] += dp [ i ] [ j ] NEW_LINE DEDENT DEDENT q = len ( queries ) NEW_LINE for i in range ( q ) : NEW_LINE INDENT l = queries [ i ] [ 0 ] NEW_LINE r = queries [ i ] [ 1 ] NEW_LINE c = ord ( queries [ i ] [ 2 ] ) - ord ( ' a ' ) NEW_LINE print ( dp [ r ] - dp [ l - 1 ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " aabv " NEW_LINE queries = [ [ 1 , 2 , ' a ' ] , [ 2 , 3 , ' b ' ] ] NEW_LINE noOfChars ( S , queries ) NEW_LINE DEDENT
Minimum cost required to rearrange a given array to make it equal to another given array | Function to find lower_bound ; Function to find length of the longest common subsequence ; Find position where element is to be inserted ; Return the length of LCS ; Function to find the minimum cost required to convert the sequence A exactly same as B ; Auxiliary array ; Stores positions of elements of A [ ] Initialize index array with - 1 ; Update the index array with index of corresponding elements of B ; Place only A 's array values  with its mapped values  into nums array ; Find LCS ; No of elements to be added in array A [ ] ; Stores minimum cost ; Print the minimum cost ; Given array A [ ] ; Given C ; Size of arr A ; Size of arr B ; Function call
def LowerBound ( a , k , x ) : NEW_LINE INDENT l = - 1 NEW_LINE r = k NEW_LINE while ( l + 1 < r ) : NEW_LINE INDENT m = ( l + r ) >> 1 NEW_LINE if ( a [ m ] >= x ) : NEW_LINE INDENT r = m NEW_LINE DEDENT else : NEW_LINE INDENT l = m NEW_LINE DEDENT DEDENT return r NEW_LINE DEDENT def findLCS ( nums , N ) : NEW_LINE INDENT k = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT pos = LowerBound ( nums , k , nums [ i ] ) NEW_LINE nums [ pos ] = nums [ i ] NEW_LINE if ( k == pos ) : NEW_LINE INDENT k = pos + 1 NEW_LINE DEDENT DEDENT return k NEW_LINE DEDENT def minimumCost ( A , B , M , N , C ) : NEW_LINE INDENT nums = [ 0 ] * 100000 NEW_LINE index = [ - 1 ] * 100000 NEW_LINE for i in range ( N ) : NEW_LINE INDENT index [ B [ i ] ] = i NEW_LINE DEDENT k = 0 NEW_LINE for i in range ( M ) : NEW_LINE INDENT if ( index [ A [ i ] ] != - 1 ) : NEW_LINE INDENT k += 1 NEW_LINE nums [ k ] = index [ A [ i ] ] NEW_LINE DEDENT DEDENT lcs_length = findLCS ( nums , k ) NEW_LINE elements_to_be_added = N - lcs_length NEW_LINE min_cost = elements_to_be_added * C NEW_LINE print ( min_cost ) NEW_LINE DEDENT A = [ 1 , 6 , 3 , 5 , 10 ] NEW_LINE B = [ 3 , 1 , 5 ] NEW_LINE C = 2 NEW_LINE M = len ( A ) NEW_LINE N = len ( B ) NEW_LINE minimumCost ( A , B , M , N , C ) NEW_LINE
Maximize count of array elements required to obtain given sum | Function that count the maximum number of elements to obtain sum V ; Stores the maximum number of elements required to obtain V ; Base Case ; Initialize all table values as Infinite ; Find the max arr required for all values from 1 to V ; Go through all arr smaller than i ; If current coin value is less than i ; Update table [ i ] ; Return the final count ; Driver Code ; Given array ; Given sum V ; Function Call
def maxCount ( arr , m , V ) : NEW_LINE INDENT table = [ 0 for i in range ( V + 1 ) ] NEW_LINE table [ 0 ] = 0 NEW_LINE for i in range ( 1 , V + 1 , 1 ) : NEW_LINE INDENT table [ i ] = - 1 NEW_LINE for i in range ( 1 , V + 1 , 1 ) : NEW_LINE INDENT for j in range ( 0 , m , 1 ) : NEW_LINE INDENT if ( arr [ j ] <= i ) : NEW_LINE INDENT sub_res = table [ i - arr [ j ] ] NEW_LINE if ( sub_res != - 1 and sub_res + 1 > table [ i ] ) : NEW_LINE INDENT table [ i ] = sub_res + 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT return table [ V ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 25 , 10 , 5 ] NEW_LINE m = len ( arr ) NEW_LINE V = 30 NEW_LINE print ( f ' Maximum number of array elements required : { maxCount ( arr , m , V ) } ' ) NEW_LINE DEDENT
Maximize path sum from top | Function to get the maximum path sum from top - left cell to all other cells of the given matrix ; Store the maximum path sum Initialize the value of dp [ i ] [ j ] to 0. ; Base case ; Compute the value of dp [ i ] [ j ] using the recurrence relation ; Print maximum path sum from the top - left cell ; Driver code
def pathSum ( mat , N , M ) : NEW_LINE INDENT dp = [ [ 0 for x in range ( M ) ] for y in range ( N ) ] NEW_LINE dp [ 0 ] [ 0 ] = mat [ 0 ] [ 0 ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT dp [ i ] [ 0 ] = ( mat [ i ] [ 0 ] + dp [ i - 1 ] [ 0 ] ) NEW_LINE DEDENT for j in range ( 1 , M ) : NEW_LINE INDENT dp [ 0 ] [ j ] = ( mat [ 0 ] [ j ] + dp [ 0 ] [ j - 1 ] ) NEW_LINE DEDENT for i in range ( 1 , N ) : NEW_LINE INDENT for j in range ( 1 , M ) : NEW_LINE INDENT dp [ i ] [ j ] = ( mat [ i ] [ j ] + max ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) ) NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT print ( dp [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ [ 3 , 2 , 1 ] , [ 6 , 5 , 4 ] , [ 7 , 8 , 9 ] ] NEW_LINE N = 3 NEW_LINE M = 3 NEW_LINE pathSum ( mat , N , M ) NEW_LINE DEDENT
Length of Longest Palindrome Substring | Function to find the length of the longest palindromic subString ; Length of String str ; Stores the dp states ; All subStrings of length 1 are palindromes ; Check for sub - String of length 2 ; If adjacent character are same ; Update table [ i ] [ i + 1 ] ; Check for lengths greater than 2 k is length of subString ; Fix the starting index ; Ending index of subString of length k ; Check for palindromic subString str [ i , j ] ; Mark True ; Update the maximum length ; Return length of LPS ; Driver Code ; Given String str ; Function Call
def longestPalSubstr ( str ) : NEW_LINE INDENT n = len ( str ) ; NEW_LINE table = [ [ False for i in range ( n ) ] for j in range ( n ) ] ; NEW_LINE maxLength = 1 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT table [ i ] [ i ] = True ; NEW_LINE DEDENT start = 0 ; NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if ( str [ i ] == str [ i + 1 ] ) : NEW_LINE INDENT table [ i ] [ i + 1 ] = True ; NEW_LINE start = i ; NEW_LINE maxLength = 2 ; NEW_LINE DEDENT DEDENT for k in range ( 3 , n + 1 ) : NEW_LINE INDENT for i in range ( n - k + 1 ) : NEW_LINE INDENT j = i + k - 1 ; NEW_LINE if ( table [ i + 1 ] [ j - 1 ] and str [ i ] == str [ j ] ) : NEW_LINE INDENT table [ i ] [ j ] = True ; NEW_LINE if ( k > maxLength ) : NEW_LINE INDENT start = i ; NEW_LINE maxLength = k ; NEW_LINE DEDENT DEDENT DEDENT DEDENT return maxLength ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " forgeeksskeegfor " ; NEW_LINE print ( longestPalSubstr ( str ) ) ; NEW_LINE DEDENT
Maximize sum of an Array by flipping sign of all elements of a single subarray | Python3 program for the above approach ; Function to find the maximum sum after flipping a subarray ; Stores the total sum of array ; Initialize the maximum sum ; Iterate over all possible subarrays ; Initialize sum of the subarray before flipping sign ; Initialize sum of subarray after flipping sign ; Calculate the sum of original subarray ; Subtract the original subarray sum and add the flipped subarray sum to the total sum ; Return the max_sum ; Driver Code ; Function call
import sys NEW_LINE def maxSumFlip ( a , n ) : NEW_LINE INDENT total_sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT total_sum += a [ i ] NEW_LINE DEDENT max_sum = - sys . maxsize - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE flip_sum = 0 NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT sum += a [ j ] NEW_LINE max_sum = max ( max_sum , total_sum - 2 * sum ) NEW_LINE DEDENT DEDENT return max ( max_sum , total_sum ) NEW_LINE DEDENT arr = [ - 2 , 3 , - 1 , - 4 , - 2 ] NEW_LINE N = len ( arr ) NEW_LINE print ( maxSumFlip ( arr , N ) ) NEW_LINE
Minimum repeated addition of even divisors of N required to convert N to M | INF is the maximum value which indicates Impossible state ; Stores the dp states ; Recursive Function that considers all possible even divisors of cur ; Indicates impossible state ; Check dp [ cur ] is already calculated or not ; Initially it is set to INF that meanswe cur can 't be transform to M ; Loop to find even divisors of cur ; if i is divisor of cur ; if i is even ; Find recursively for cur + i ; Check another divisor ; Find recursively for cur + ( cur / i ) ; Finally store the current state result and return the answer ; Function that counts the minimum operation to reduce N to M ; Initialise dp state ; Function Call ; Driver code ; Given N and M ; Function Call ; INF indicates impossible state
INF = 10000007 ; NEW_LINE max_size = 100007 ; NEW_LINE dp = [ 0 for i in range ( max_size ) ] ; NEW_LINE def min_op ( cur , M ) : NEW_LINE INDENT if ( cur > M ) : NEW_LINE INDENT return INF ; NEW_LINE DEDENT if ( cur == M ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( dp [ cur ] != - 1 ) : NEW_LINE INDENT return dp [ cur ] ; NEW_LINE DEDENT op = INF ; NEW_LINE i = 2 NEW_LINE while ( i * i <= cur ) : NEW_LINE INDENT if ( cur % i == 0 ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT op = min ( op , 1 + min_op ( cur + i , M ) ) ; NEW_LINE DEDENT if ( ( cur // i ) != i and ( cur // i ) % 2 == 0 ) : NEW_LINE INDENT op = min ( op , 1 + min_op ( cur + ( cur // i ) , M ) ) NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT dp [ cur ] = op ; NEW_LINE return op NEW_LINE DEDENT def min_operations ( N , M ) : NEW_LINE INDENT for i in range ( N , M + 1 ) : NEW_LINE INDENT dp [ i ] = - 1 ; NEW_LINE DEDENT return min_op ( N , M ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 6 NEW_LINE M = 24 NEW_LINE op = min_operations ( N , M ) ; NEW_LINE if ( op >= INF ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( op ) NEW_LINE DEDENT DEDENT
Maximize Sum possible from an Array by the given moves | Function to find the maximum sum possible by given moves from the array ; Checking for boundary ; If previously computed subproblem occurs ; If element can be moved left ; Calculate maximum possible sum by moving left from current index ; If element can be moved right ; Calculate maximum possible sum by moving right from current index and update the maximum sum ; Store the maximum sum ; Driver Code ; Function call
def maxValue ( a , n , pos , moves , left , dp ) : NEW_LINE INDENT if ( moves == 0 or ( pos > n - 1 or pos < 0 ) ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( dp [ pos ] [ left ] != - 1 ) : NEW_LINE INDENT return dp [ pos ] [ left ] NEW_LINE DEDENT value = 0 NEW_LINE if ( left > 0 and pos >= 1 ) : NEW_LINE INDENT value = max ( value , a [ pos ] + maxValue ( a , n , pos - 1 , moves - 1 , left - 1 , dp ) ) NEW_LINE DEDENT if ( pos <= n - 1 ) : NEW_LINE INDENT value = max ( value , a [ pos ] + maxValue ( a , n , pos + 1 , moves - 1 , left , dp ) ) NEW_LINE DEDENT dp [ pos ] [ left ] = value NEW_LINE return dp [ pos ] [ left ] NEW_LINE DEDENT n = 5 NEW_LINE a = [ 1 , 5 , 4 , 3 , 2 ] NEW_LINE k = 1 NEW_LINE m = 4 NEW_LINE dp = [ [ - 1 for x in range ( k + 1 ) ] for y in range ( n + 1 ) ] NEW_LINE print ( a [ 0 ] + maxValue ( a , n , 1 , m , k , dp ) ) NEW_LINE
Maximize the Sum of a Subsequence from an Array based on given conditions | Function to select the array elements to maximize the sum of the selected elements ; If the entire array is solved ; Memoized subproblem ; Calculate sum considering the current element in the subsequence ; Calculate sum without considering the current element in the subsequence ; Update the maximum of the above sums in the dp [ ] [ ] table ; Driver Code ; Initialize the dp array ; Function call
def maximumSum ( a , count , index , n , dp ) : NEW_LINE INDENT if ( index == n ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( dp [ index ] [ count ] != - 1 ) : NEW_LINE INDENT return dp [ index ] [ count ] NEW_LINE DEDENT take_element = ( a [ index ] * count + maximumSum ( a , count + 1 , index + 1 , n , dp ) ) NEW_LINE dont_take = maximumSum ( a , count , index + 1 , n , dp ) NEW_LINE dp [ index ] [ count ] = max ( take_element , dont_take ) NEW_LINE return dp [ index ] [ count ] NEW_LINE DEDENT n = 5 NEW_LINE a = [ - 1 , - 9 , 0 , 5 , - 7 ] NEW_LINE dp = [ [ - 1 for x in range ( n + 1 ) ] for y in range ( n + 1 ) ] NEW_LINE print ( maximumSum ( a , 1 , 0 , n , dp ) ) NEW_LINE