codeparrot/xlcost-text-to-code huggingface dataset:
Maximum sum submatrix | Function to find maximum sum submatrix ; Stores the number of rows and columns in the matrix ; Stores maximum submatrix sum ; Take each row as starting row ; Take each column as the starting column ; Take each row as the ending row ; Take each column as the ending column ; Stores the sum of submatrix having topleft index ( i , j ) and bottom right index ( k , l ) ; Iterate the submatrix row - wise and calculate its sum ; Update the maximum sum ; Prthe answer ; Driver Code
def maxSubmatrixSum ( matrix ) : NEW_LINE INDENT r = len ( matrix ) NEW_LINE c = len ( matrix [ 0 ] ) NEW_LINE maxSubmatrix = 0 NEW_LINE for i in range ( r ) : NEW_LINE INDENT for j in range ( c ) : NEW_LINE INDENT for k in range ( i , r ) : NEW_LINE INDENT for l in range ( j , c ) : NEW_LINE INDENT sumSubmatrix = 0 NEW_LINE for m in range ( i , k + 1 ) : NEW_LINE INDENT for n in range ( j , l + 1 ) : NEW_LINE INDENT sumSubmatrix += matrix [ m ] [ n ] NEW_LINE DEDENT DEDENT maxSubmatrix = max ( maxSubmatrix , sumSubmatrix ) NEW_LINE DEDENT DEDENT DEDENT DEDENT print ( maxSubmatrix ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT matrix = [ [ 0 , - 2 , - 7 , 0 ] , [ 9 , 2 , - 6 , 2 ] , [ - 4 , 1 , - 4 , 1 ] , [ - 1 , 8 , 0 , - 2 ] ] NEW_LINE maxSubmatrixSum ( matrix ) NEW_LINE DEDENT
Maximize sum of subsets from two arrays having no consecutive values | Function to calculate maximum subset sum ; Initialize array to store dp states ; Base Cases ; Pre initializing for dp [ 0 ] & dp [ 1 ] ; Calculating dp [ index ] based on above formula ; Prmaximum subset sum ; Given arrays ; Length of the array
def maximumSubsetSum ( arr1 , arr2 , length ) : NEW_LINE INDENT dp = [ 0 ] * ( length + 1 ) NEW_LINE if ( length == 1 ) : NEW_LINE INDENT print ( max ( arr1 [ 0 ] , arr2 [ 0 ] ) ) NEW_LINE return NEW_LINE DEDENT if ( length == 2 ) : NEW_LINE INDENT print ( max ( max ( arr1 [ 1 ] , arr2 [ 1 ] ) , max ( arr1 [ 0 ] , arr2 [ 0 ] ) ) ) NEW_LINE return NEW_LINE DEDENT else : NEW_LINE INDENT dp [ 0 ] = max ( arr1 [ 0 ] , arr2 [ 0 ] ) NEW_LINE dp [ 1 ] = max ( max ( arr1 [ 1 ] , arr2 [ 1 ] ) , max ( arr1 [ 0 ] , arr2 [ 0 ] ) ) NEW_LINE index = 2 NEW_LINE while ( index < length ) : NEW_LINE INDENT dp [ index ] = max ( max ( arr1 [ index ] , arr2 [ index ] ) , max ( max ( arr1 [ index ] + dp [ index - 2 ] , arr2 [ index ] + dp [ index - 2 ] ) , dp [ index - 1 ] ) ) NEW_LINE index += 1 NEW_LINE DEDENT print ( dp [ length - 1 ] ) NEW_LINE DEDENT DEDENT arr1 = [ - 1 , - 2 , 4 , - 4 , 5 ] NEW_LINE arr2 = [ - 1 , - 2 , - 3 , 4 , 10 ] NEW_LINE length = 5 NEW_LINE maximumSubsetSum ( arr1 , arr2 , length ) NEW_LINE
Modify array by replacing every array element with minimum possible value of arr [ j ] + | j | Function to find minimum value of arr [ j ] + | j - i | for every array index ; Stores minimum of a [ j ] + | i - j | upto position i ; Stores minimum of a [ j ] + | i - j | upto position i from the end ; Traversing and storing minimum of a [ j ] + | i - j | upto i ; Traversing and storing minimum of a [ j ] + | i - j | upto i from the end ; Traversing from [ 0 , N ] and storing minimum of a [ j ] + | i - j | from starting and end ; Print the required array ; Driver code ; Given array arr ; Size of the array ; Function Call
def minAtEachIndex ( n , arr ) : NEW_LINE INDENT dp1 = [ 0 ] * n NEW_LINE dp2 = [ 0 ] * n NEW_LINE i = 0 NEW_LINE dp1 [ 0 ] = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT dp1 [ i ] = min ( arr [ i ] , dp1 [ i - 1 ] + 1 ) NEW_LINE DEDENT dp2 [ n - 1 ] = arr [ n - 1 ] NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT dp2 [ i ] = min ( arr [ i ] , dp2 [ i + 1 ] + 1 ) NEW_LINE DEDENT v = [ ] NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT v . append ( min ( dp1 [ i ] , dp2 [ i ] ) ) NEW_LINE DEDENT for x in v : NEW_LINE INDENT print ( x , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 4 , 2 , 5 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE minAtEachIndex ( N , arr ) NEW_LINE DEDENT
Count all N | Function to prthe count of arrays satisfying given condition ; First element of array is set as 1 ; Since the first element of arr is 1 , the second element can 't be 1 ; Traverse the remaining indices ; If arr [ i ] = 1 ; If arr [ i ] a 1 ; Since last element needs to be 1 ; Driver Code ; Stores the count of arrays where arr [ 0 ] = arr [ N - 1 ] = 1 ; Since arr [ 0 ] and arr [ N - 1 ] can be any number from 1 to M ; Pranswer
def totalArrays ( N , M ) : NEW_LINE INDENT end_with_one = [ 0 ] * ( N + 1 ) ; NEW_LINE end_not_with_one = [ 0 ] * ( N + 1 ) ; NEW_LINE end_with_one [ 0 ] = 1 ; NEW_LINE end_not_with_one [ 0 ] = 0 ; NEW_LINE end_with_one [ 1 ] = 0 ; NEW_LINE end_not_with_one [ 1 ] = M - 1 ; NEW_LINE for i in range ( 2 , N ) : NEW_LINE INDENT end_with_one [ i ] = end_not_with_one [ i - 1 ] ; NEW_LINE end_not_with_one [ i ] = end_with_one [ i - 1 ] * ( M - 1 ) + end_not_with_one [ i - 1 ] * ( M - 2 ) ; NEW_LINE DEDENT return end_with_one [ N - 1 ] ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 ; NEW_LINE M = 3 ; NEW_LINE temp = totalArrays ( N , M ) ; NEW_LINE ans = M * temp ; NEW_LINE print ( ans ) ; NEW_LINE DEDENT
Count numbers from a given range whose product of digits is K | Function to find the product of digits of a number ; Stores product of digits of N ; Update res ; Update N ; Function to count numbers in the range [ 0 , X ] whose product of digit is K ; Stores count of numbers in the range [ L , R ] whose product of digit is K ; Iterate over the range [ L , R ] ; If product of digits of i equal to K ; Update cnt ; Driver Code
def prodOfDigit ( N ) : NEW_LINE INDENT res = 1 NEW_LINE while ( N ) : NEW_LINE INDENT res = res * ( N % 10 ) NEW_LINE N //= 10 NEW_LINE DEDENT return res NEW_LINE DEDENT def cntNumRange ( L , R , K ) : NEW_LINE INDENT cnt = 0 NEW_LINE for i in range ( L , R + 1 ) : NEW_LINE INDENT if ( prodOfDigit ( i ) == K ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT return cnt NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L , R , K = 20 , 10000 , 14 NEW_LINE print ( cntNumRange ( L , R , K ) ) NEW_LINE DEDENT
Median of Bitwise XOR of all submatrices starting from the top left corner | Function to find the median of bitwise XOR of all the submatrix whose topmost leftmost corner is ( 0 , 0 ) ; dp [ i ] [ j ] : Stores the bitwise XOR of submatrix having top left corner at ( 0 , 0 ) and bottom right corner at ( i , j ) ; Stores count of submatrix ; Base Case ; Base Case ; Fill dp using tabulation ; Fill dp [ i ] [ j ] ; Driver code
def findMedXOR ( mat , N , M ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( M ) ] for j in range ( N ) ] ; NEW_LINE med = [ 0 ] * ( N * M ) ; NEW_LINE dp [ 0 ] [ 0 ] = mat [ 0 ] [ 0 ] ; NEW_LINE med [ 0 ] = dp [ 0 ] [ 0 ] ; NEW_LINE len = 1 ; NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] ^ mat [ i ] [ 0 ] ; NEW_LINE med [ len ] = dp [ i ] [ 0 ] ; NEW_LINE len += 1 ; NEW_LINE DEDENT for i in range ( 1 , M ) : NEW_LINE INDENT dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] ^ mat [ 0 ] [ i ] ; NEW_LINE med [ len ] = dp [ 0 ] [ i ] ; NEW_LINE len += 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 ] = dp [ i - 1 ] [ j ] ^ dp [ i ] [ j - 1 ] ^ dp [ i - 1 ] [ j - 1 ] ^ mat [ i ] [ j ] ; NEW_LINE med [ len ] = dp [ i ] [ j ] ; NEW_LINE len += 1 NEW_LINE DEDENT DEDENT med . sort ( ) ; NEW_LINE if ( len % 2 == 0 ) : NEW_LINE INDENT return ( med [ ( len // 2 ) ] + med [ ( len // 2 ) - 1 ] ) / 2.0 ; NEW_LINE DEDENT return med [ len // 2 ] ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ [ 1 , 2 ] , [ 2 , 3 ] ] ; NEW_LINE N = len ( mat [ 0 ] ) ; NEW_LINE M = 2 ; NEW_LINE print ( findMedXOR ( mat , N , M ) ) ; NEW_LINE DEDENT
Count ways to tile an N | Function to count the ways to tile N * 1 board using 1 * 1 and 2 * 1 tiles ; dp [ i ] : Stores count of ways to tile i * 1 board using given tiles ; Base Case ; Iterate over the range [ 2 , N ] ; Fill dp [ i ] using the recurrence relation ; Driver Code ; Given N ; Function Call
def countWaysToTileBoard ( N ) : NEW_LINE INDENT dp = [ 0 ] * ( N + 1 ) NEW_LINE dp [ 0 ] = 1 NEW_LINE dp [ 1 ] = 2 NEW_LINE for i in range ( 2 , N + 1 ) : NEW_LINE INDENT dp [ i ] = ( 2 * dp [ i - 1 ] + dp [ i - 2 ] ) NEW_LINE DEDENT print ( dp [ N ] ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 2 NEW_LINE countWaysToTileBoard ( N ) NEW_LINE DEDENT
Subsequences generated by including characters or ASCII value of characters of given string | Function to print subsequences containing ASCII value of the characters or the the characters of the given string ; Base Case ; If length of the subsequence exceeds 0 ; Print the subsequence ; Stores character present at i - th index of str ; If the i - th character is not included in the subsequence ; Including the i - th character in the subsequence ; Include the ASCII value of the ith character in the subsequence ; Driver Code ; Stores length of str
def FindSub ( string , res , i ) : NEW_LINE INDENT if ( i == len ( string ) ) : NEW_LINE INDENT if ( len ( res ) > 0 ) : NEW_LINE INDENT print ( res , end = " ▁ " ) ; NEW_LINE DEDENT return ; NEW_LINE DEDENT ch = string [ i ] ; NEW_LINE FindSub ( string , res , i + 1 ) ; NEW_LINE FindSub ( string , res + ch , i + 1 ) ; NEW_LINE FindSub ( string , res + str ( ord ( ch ) ) , i + 1 ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " ab " ; NEW_LINE res = " " ; NEW_LINE N = len ( string ) ; NEW_LINE FindSub ( string , res , 0 ) ; NEW_LINE DEDENT
Minimize given flips required to reduce N to 0 | Function to find the minimum count of operations required to Reduce N to 0 ; Stores count of bits in N ; Recurrence relation ; Driver code
import math NEW_LINE def MinOp ( N ) : NEW_LINE INDENT if ( N <= 1 ) : NEW_LINE INDENT return N ; NEW_LINE DEDENT bit = ( int ) ( math . log ( N ) / math . log ( 2 ) ) + 1 ; NEW_LINE return ( ( 1 << bit ) - 1 ) - MinOp ( N - ( 1 << ( bit - 1 ) ) ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 ; NEW_LINE print ( MinOp ( N ) ) ; NEW_LINE DEDENT
Maximum non | Function to find the maximum product from the top left and bottom right cell of the given matrix grid [ ] [ ] ; Store dimension of grid ; Stores maximum product path ; Stores minimum product path ; Traverse the grid and update maxPath and minPath array ; Initialize to inf and - inf ; Base Case ; Calculate for row : ; Update the maximum ; Update the minimum ; Calculate for column : ; Update the maximum ; Update the minimum ; Update maxPath and minPath ; If negative product ; Otherwise ; Given matrix mat [ ] [ ] ; Function Call
def maxProductPath ( grid ) : NEW_LINE INDENT n , m = len ( grid ) , len ( grid [ 0 ] ) NEW_LINE maxPath = [ [ 0 for i in range ( m ) ] for j in range ( n ) ] NEW_LINE minPath = [ [ 0 for i in range ( m ) ] for j in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT mn = float ( " inf " ) NEW_LINE mx = float ( " - inf " ) NEW_LINE if ( i == 0 and j == 0 ) : NEW_LINE INDENT mx = grid [ i ] [ j ] NEW_LINE mn = grid [ i ] [ j ] NEW_LINE DEDENT if i > 0 : NEW_LINE INDENT tempmx = max ( ( maxPath [ i - 1 ] [ j ] * grid [ i ] [ j ] ) , ( minPath [ i - 1 ] [ j ] * grid [ i ] [ j ] ) ) NEW_LINE mx = max ( mx , tempmx ) NEW_LINE tempmn = min ( ( maxPath [ i - 1 ] [ j ] * grid [ i ] [ j ] ) , ( minPath [ i - 1 ] [ j ] * grid [ i ] [ j ] ) ) NEW_LINE mn = min ( mn , tempmn ) NEW_LINE DEDENT if ( j > 0 ) : NEW_LINE INDENT tempmx = max ( ( maxPath [ i ] [ j - 1 ] * grid [ i ] [ j ] ) , ( minPath [ i ] [ j - 1 ] * grid [ i ] [ j ] ) ) NEW_LINE mx = max ( mx , tempmx ) NEW_LINE tempmn = min ( ( maxPath [ i ] [ j - 1 ] * grid [ i ] [ j ] ) , ( minPath [ i ] [ j - 1 ] * grid [ i ] [ j ] ) ) NEW_LINE mn = min ( mn , tempmn ) NEW_LINE DEDENT maxPath [ i ] [ j ] = mx NEW_LINE minPath [ i ] [ j ] = mn NEW_LINE DEDENT DEDENT if ( maxPath [ n - 1 ] [ m - 1 ] < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return ( maxPath [ n - 1 ] [ m - 1 ] ) NEW_LINE DEDENT DEDENT mat = [ [ 1 , - 2 , 1 ] , [ 1 , - 2 , 1 ] , [ 3 , - 4 , 1 ] ] NEW_LINE print ( maxProductPath ( mat ) ) NEW_LINE
Count N | to keep the string in lexicographically sorted order use start index to add the vowels starting the from that index ; base case : if string length is 0 add to the count ; if last character in string is ' e ' add vowels starting from ' e ' i . e ' e ' , ' i ' , ' o ' , 'u ; decrease the length of string ; char arr [ 5 ] = { ' a ' , ' e ' , ' i ' , ' o ' , ' u ' } ; starting from index 0 add the vowels to strings
def countstrings ( n , start ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT cnt = 0 NEW_LINE DEDENT ' NEW_LINE INDENT for i in range ( start , 5 ) : NEW_LINE INDENT cnt += countstrings ( n - 1 , i ) NEW_LINE DEDENT return cnt NEW_LINE DEDENT def countVowelStrings ( n ) : NEW_LINE INDENT return countstrings ( n , 0 ) NEW_LINE DEDENT n = 2 NEW_LINE print ( countVowelStrings ( n ) ) NEW_LINE
Count N | Function to count N - length strings consisting of vowels only sorted lexicographically ; Driver Code ; Function Call
def findNumberOfStrings ( n ) : NEW_LINE INDENT return int ( ( n + 1 ) * ( n + 2 ) * ( n + 3 ) * ( n + 4 ) / 24 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 2 NEW_LINE print ( findNumberOfStrings ( N ) ) NEW_LINE DEDENT
Count unique paths with given sum in an N | Python3 program for the above approach ; Function for counting total no of paths possible with the sum is equal to X ; If the path of the sum from the root to current node is stored in sum ; If already computed ; Count different no of paths using all possible ways ; Return total no of paths ; Driver Code ; Stores the number of ways to obtains sums 0 to X ; Function call
mod = int ( 1e9 + 7 ) NEW_LINE def findTotalPath ( X , n , dp ) : NEW_LINE INDENT if ( X == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT ans = 0 NEW_LINE if ( dp [ X ] != - 1 ) : NEW_LINE INDENT return dp [ X ] NEW_LINE DEDENT for i in range ( 1 , min ( X , n ) + 1 ) : NEW_LINE INDENT ans = ans + findTotalPath ( X - i , n , dp ) % mod ; NEW_LINE ans %= mod ; NEW_LINE DEDENT dp [ X ] = ans NEW_LINE return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE X = 2 NEW_LINE dp = [ - 1 ] * ( X + 1 ) NEW_LINE print ( findTotalPath ( X , n , dp ) ) NEW_LINE DEDENT
Count sequences of positive integers having product X | Python3 program for the above approach ; Function to prthe total number of possible sequences with product X ; Precomputation of binomial coefficients ; Max length of a subsequence ; Ways dp array ; Fill i slots using all the primes ; Subtract ways for all slots that exactly fill less than i slots ; Total possible sequences ; Print the resultant count ; Driver Code ; Function call
bin = [ [ 0 for i in range ( 3000 ) ] for i in range ( 3000 ) ] NEW_LINE def countWays ( arr ) : NEW_LINE INDENT mod = 10 ** 9 + 7 NEW_LINE bin [ 0 ] [ 0 ] = 1 NEW_LINE for i in range ( 1 , 3000 ) : NEW_LINE INDENT bin [ i ] [ 0 ] = 1 NEW_LINE for j in range ( 1 , i + 1 ) : NEW_LINE INDENT bin [ i ] [ j ] = ( bin [ i - 1 ] [ j ] + bin [ i - 1 ] [ j - 1 ] ) % mod NEW_LINE DEDENT DEDENT n = 0 NEW_LINE for x in arr : NEW_LINE INDENT n += x NEW_LINE DEDENT ways = [ 0 ] * ( n + 1 ) NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT ways [ i ] = 1 NEW_LINE for j in range ( len ( arr ) ) : NEW_LINE INDENT ways [ i ] = ( ways [ i ] * bin [ arr [ j ] + i - 1 ] [ i - 1 ] ) % mod NEW_LINE DEDENT for j in range ( 1 , i ) : NEW_LINE INDENT ways [ i ] = ( ( ways [ i ] - bin [ i ] [ j ] * ways [ j ] ) % mod + mod ) % mod NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE for x in ways : NEW_LINE INDENT ans = ( ans + x ) % mod NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 1 ] NEW_LINE countWays ( arr ) NEW_LINE DEDENT
Maximum subsequence sum obtained by concatenating disjoint subarrays whose lengths are prime | Python3 program for the above approach ; Function to return all prime numbers smaller than N ; Create a boolean array " prime [ 0 . . n ] " ; Initialize all its entries as true memset ( seive , true , sizeof ( seive ) ) ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it ; Stores all prime numbers smaller than MAX ; Store all prime numbers ; If p is prime ; Function to build the auxiliary DP array from the start ; Base Case ; Stores all prime numbers < N ; Stores prefix sum ; Update prefix sum ; Iterate over range ; Update each state i . e . . when current element is excluded ; Find start & end index of subarrays when prime [ i ] is taken ; Check if starting point lies in the array ; Include the elements al al + 1 . . . ar ; Check if element lies before start of selected subarray ; Update value of dp [ i ] ; Function to find the maximum sum subsequence with prime length ; Auxiliary DP array ; Build DP array ; Print the result ; Driver Code ; Given arr [ ] ; Size of array ; Function Call
MAX = 100005 NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT seive = [ True for i in range ( MAX ) ] NEW_LINE for p in range ( 2 , MAX ) : NEW_LINE INDENT if p * p > MAX : NEW_LINE INDENT break NEW_LINE DEDENT if ( seive [ p ] == True ) : NEW_LINE INDENT for i in range ( p * p , MAX , p ) : NEW_LINE INDENT seive [ i ] = False NEW_LINE DEDENT DEDENT DEDENT v = [ ] NEW_LINE for p in range ( 2 , MAX ) : NEW_LINE INDENT if ( seive [ p ] ) : NEW_LINE INDENT v . append ( p ) NEW_LINE DEDENT DEDENT return v NEW_LINE DEDENT def build ( dp , arr , N ) : NEW_LINE INDENT dp [ 0 ] = 0 NEW_LINE dp [ 1 ] = 0 NEW_LINE prime = SieveOfEratosthenes ( ) NEW_LINE pref = [ 0 for i in range ( N + 1 ) ] NEW_LINE pref [ 0 ] = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT pref [ i ] = pref [ i - 1 ] + arr [ i - 1 ] NEW_LINE DEDENT for i in range ( 2 , N + 1 ) : NEW_LINE INDENT dp [ i ] = dp [ i - 1 ] NEW_LINE for j in range ( len ( prime ) + 1 ) : NEW_LINE INDENT r = i - 1 NEW_LINE l = r - prime [ j ] + 1 NEW_LINE if ( l < 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT temp = 0 NEW_LINE temp = pref [ r + 1 ] - pref [ l ] NEW_LINE if ( l - 2 >= 0 ) : NEW_LINE INDENT temp += dp [ l - 2 + 1 ] NEW_LINE DEDENT dp [ i ] = max ( dp [ i ] , temp ) NEW_LINE DEDENT DEDENT DEDENT def maxSumSubseq ( arr , N ) : NEW_LINE INDENT dp = [ 0 for i in range ( N + 1 ) ] NEW_LINE build ( dp , arr , N ) NEW_LINE print ( dp [ N ] ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 10 , 10 , 7 , 10 , 10 , 10 ] NEW_LINE N = len ( arr ) NEW_LINE maxSumSubseq ( arr , N ) NEW_LINE DEDENT
Maximum possible score that can be obtained by constructing a Binary Tree based on given conditions | Function to find the maximum score for one possible tree having N nodes N - 1 Edges ; Number of nodes ; Initialize dp [ ] [ ] ; Score with 0 vertices is 0 ; Traverse the nodes from 1 to N ; Find maximum scores for each sum ; Iterate over degree of new node ; Update the current state ; Return maximum score for N node tree having 2 ( N - 1 ) sum of degree ; Driver Code ; Given array of scores ; Function Call
def maxScore ( arr ) : NEW_LINE INDENT N = len ( arr ) NEW_LINE N += 1 NEW_LINE dp = [ [ - 100000 for i in range ( 2 * N ) ] for i in range ( N + 1 ) ] NEW_LINE dp [ 0 ] [ 0 ] = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT for s in range ( 1 , 2 * ( N - 1 ) + 1 ) : NEW_LINE INDENT j = 1 NEW_LINE while j <= N - 1 and j <= s : NEW_LINE INDENT dp [ i ] [ s ] = max ( dp [ i ] [ s ] , arr [ j - 1 ] + dp [ i - 1 ] [ s - j ] ) NEW_LINE j += 1 NEW_LINE DEDENT DEDENT DEDENT return dp [ N ] [ 2 * ( N - 1 ) ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 3 , 0 ] NEW_LINE print ( maxScore ( arr ) ) NEW_LINE DEDENT
Minimize cost of choosing and skipping array elements to reach end of the given array | Function to find the minimum cost to reach the end of the array from the first element ; Store the results ; Consider first index cost ; Find answer for each position i ; First Element ; Second Element ; For remaining element ; Consider min cost for skipping ; Last index represents the minimum total cost ; Driver Code ; Given X ; Given array cost [ ] ; Function Call
def minimumCost ( cost , n , x ) : NEW_LINE INDENT dp = [ 0 ] * ( n + 2 ) NEW_LINE dp [ 0 ] = cost [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( i == 1 ) : NEW_LINE INDENT dp [ i ] = cost [ i ] + dp [ i - 1 ] NEW_LINE DEDENT if ( i == 2 ) : NEW_LINE INDENT dp [ i ] = cost [ i ] + min ( dp [ i - 1 ] , x + dp [ i - 2 ] ) NEW_LINE DEDENT if ( i >= 3 ) : NEW_LINE INDENT dp [ i ] = ( cost [ i ] + min ( dp [ i - 1 ] , min ( x + dp [ i - 2 ] , 2 * x + dp [ i - 3 ] ) ) ) NEW_LINE DEDENT DEDENT print ( dp [ n - 1 ] ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = 4 NEW_LINE cost = [ 6 , 3 , 9 , 2 , 1 , 3 ] NEW_LINE N = len ( cost ) NEW_LINE minimumCost ( cost , N , X ) NEW_LINE DEDENT
Maximum possible sum of non | Function to find the maximum sum not exceeding K possible by selecting a subset of non - adjacent elements ; Base Case ; Not selecting current element ; If selecting current element is possible ; Return answer ; Driver Code ; Given array arr [ ] ; Given K ; Function Call
def maxSum ( a , n , k ) : NEW_LINE INDENT if ( n <= 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT option = maxSum ( a , n - 1 , k ) NEW_LINE if ( k >= a [ n - 1 ] ) : NEW_LINE INDENT option = max ( option , a [ n - 1 ] + maxSum ( a , n - 2 , k - a [ n - 1 ] ) ) NEW_LINE DEDENT return option NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 50 , 10 , 20 , 30 , 40 ] NEW_LINE N = len ( arr ) NEW_LINE K = 100 NEW_LINE print ( maxSum ( arr , N , K ) ) NEW_LINE DEDENT
Count possible binary strings of length N without P consecutive 0 s and Q consecutive 1 s | Function to check if a satisfy the given condition or not ; Stores the length of string ; Stores the previous character of the string ; Stores the count of consecutive equal characters ; Traverse the string ; If current character is equal to the previous character ; If count of consecutive 1 s is more than Q ; If count of consecutive 0 s is more than P ; Reset value of cnt ; If count of consecutive 1 s is more than Q ; If count of consecutive 0 s is more than P ; Function to count all distinct binary strings that satisfy the given condition ; Stores the length of str ; If length of str is N ; If str satisfy the given condition ; If str does not satisfy the given condition ; Append a character '0' at end of str ; Append a character '1' at end of str ; Return total count of binary strings ; Driver Code
def checkStr ( str , P , Q ) : NEW_LINE INDENT N = len ( str ) NEW_LINE prev = str [ 0 ] NEW_LINE cnt = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( str [ i ] == prev ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( prev == '1' and cnt >= Q ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( prev == '0' and cnt >= P ) : NEW_LINE INDENT return False NEW_LINE DEDENT cnt = 1 NEW_LINE DEDENT prev = str [ i ] NEW_LINE DEDENT if ( prev == '1' and cnt >= Q ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( prev == '0' and cnt >= P ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT def cntBinStr ( str , N , P , Q ) : NEW_LINE INDENT lenn = len ( str ) NEW_LINE if ( lenn == N ) : NEW_LINE INDENT if ( checkStr ( str , P , Q ) ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT X = cntBinStr ( str + '0' , N , P , Q ) NEW_LINE Y = cntBinStr ( str + '1' , N , P , Q ) NEW_LINE return X + Y NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE P = 2 NEW_LINE Q = 3 NEW_LINE print ( cntBinStr ( " " , N , P , Q ) ) NEW_LINE DEDENT