codeparrot/xlcost-text-to-code huggingface dataset:
Number of ways such that only K bars are visible from the left | Function to calculate the number of permutations of N , where only K bars are visible from the left . ; Only ascending order is possible ; N is placed at the first position The nest N - 1 are arranged in ( N - 1 ) ! ways ; Recursing ; Driver code ; Input ; Function call
def KvisibleFromLeft ( N , K ) : NEW_LINE INDENT if ( N == K ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( K == 1 ) : NEW_LINE INDENT ans = 1 NEW_LINE for i in range ( 1 , N , 1 ) : NEW_LINE INDENT ans *= i NEW_LINE DEDENT return ans NEW_LINE DEDENT return KvisibleFromLeft ( N - 1 , K - 1 ) + ( N - 1 ) * KvisibleFromLeft ( N - 1 , K ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE K = 2 NEW_LINE print ( KvisibleFromLeft ( N , K ) ) NEW_LINE DEDENT
Minimum deletions in Array to make difference of adjacent elements non | Function for finding minimum deletions so that the array becomes non decreasing and the difference between adjacent elements also becomes non decreasing ; initialize answer to a large value ; generating all subsets ; checking the first condition ; checking the second condition ; if both conditions are satisfied consider the answer for minimum ; Driver code ; Input ; Function call
def minimumDeletions ( A , N ) : NEW_LINE INDENT ans = 10 ** 8 NEW_LINE for i in range ( 1 , ( 1 << N ) ) : NEW_LINE INDENT temp = [ ] NEW_LINE for j in range ( N ) : NEW_LINE INDENT if ( ( i & ( 1 << j ) ) != 0 ) : NEW_LINE INDENT temp . append ( A [ j ] ) NEW_LINE DEDENT DEDENT flag = 0 NEW_LINE for j in range ( 1 , len ( temp ) ) : NEW_LINE INDENT if ( temp [ j ] < temp [ j - 1 ] ) : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT DEDENT for j in range ( 1 , len ( temp ) - 1 ) : NEW_LINE INDENT if ( temp [ j ] - temp [ j - 1 ] > temp [ j + 1 ] - temp [ j ] ) : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT ans = min ( ans , N - len ( temp ) ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 4 , 5 , 7 , 20 , 21 ] NEW_LINE N = len ( A ) NEW_LINE print ( minimumDeletions ( A , N ) ) NEW_LINE DEDENT
Find maximum subset | Function to calculate maximum sum possible by taking at most K elements that is divisibly by D ; Variable to store final answer ; Traverse all subsets ; Update ans if necessary conditions are satisfied ; Driver code ; Input ; Function call
def maximumSum ( A , N , K , D ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( ( 1 << N ) ) : NEW_LINE INDENT sum = 0 NEW_LINE c = 0 NEW_LINE for j in range ( N ) : NEW_LINE INDENT if ( i >> j & 1 ) : NEW_LINE INDENT sum += A [ j ] NEW_LINE c += 1 NEW_LINE DEDENT DEDENT if ( sum % D == 0 and c <= K ) : NEW_LINE INDENT ans = max ( ans , sum ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE K = 3 NEW_LINE D = 7 NEW_LINE A = [ 1 , 11 , 5 , 5 , 18 ] NEW_LINE print ( maximumSum ( A , N , K , D ) ) NEW_LINE DEDENT
Find the longest subsequence of a string that is a substring of another string | Function to find the longest subsequence that matches with the substring of other string ; Stores the lengths of strings X and Y ; Create a matrix ; Initialize the matrix ; Fill all the remaining rows ; If the characters are equal ; If not equal , then just move to next in subsequence string ; Find maximum length of the longest subsequence matching substring of other string ; Iterate through the last row of matrix ; Store the required string ; Backtrack from the cell ; If equal , then add the character to res string ; Return the required string ; Driver code
def longestSubsequence ( X , Y ) : NEW_LINE INDENT n = len ( X ) NEW_LINE m = len ( Y ) NEW_LINE mat = [ [ 0 for i in range ( m + 1 ) ] for j in range ( n + 1 ) ] NEW_LINE for i in range ( 0 , n + 1 ) : NEW_LINE INDENT for j in range ( 0 , m + 1 ) : NEW_LINE INDENT if ( i == 0 or j == 0 ) : NEW_LINE INDENT mat [ i ] [ j ] = 0 NEW_LINE DEDENT DEDENT DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , m + 1 ) : NEW_LINE INDENT if ( X [ i - 1 ] == Y [ j - 1 ] ) : NEW_LINE INDENT mat [ i ] [ j ] = 1 + mat [ i - 1 ] [ j - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT mat [ i ] [ j ] = mat [ i - 1 ] [ j ] NEW_LINE DEDENT DEDENT DEDENT len1 = 0 NEW_LINE col = 0 NEW_LINE for i in range ( 0 , m + 1 ) : NEW_LINE INDENT if ( mat [ n ] [ i ] > len1 ) : NEW_LINE INDENT len1 = mat [ n ] [ i ] NEW_LINE col = i NEW_LINE DEDENT DEDENT res = " " NEW_LINE i = n NEW_LINE j = col NEW_LINE while ( len1 > 0 ) : NEW_LINE INDENT if ( X [ i - 1 ] == Y [ j - 1 ] ) : NEW_LINE INDENT res = X [ i - 1 ] + res NEW_LINE i -= 1 NEW_LINE j -= 1 NEW_LINE len1 -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT i -= 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT X = " ABCD " NEW_LINE Y = " ACDBDCD " NEW_LINE print ( longestSubsequence ( X , Y ) ) NEW_LINE
Divide chocolate bar into pieces , minimizing the area of invalid pieces | Python3 program for the above approach ; Store valid dimensions ; Stores memoization ; Utility function to calculate minimum invalid area for Chocolate piece having dimension ( l , r ) ; Check whether current piece is valid or not If it is , then return zero for current dimension ; Making all possible horizontal cuts , one by one and calculating the sum of minimum invalid area for both the resulting pieces ; Making all possible vertical cuts , one by one and calculating the sum of minimum invalid area for both the resulting pieces ; Store the computed result ; Function to calculate minimum invalid area for Chocolate piece having dimension ( l , r ) ; Total number of valid dimensions ; Storing valid dimensions as for every ( x , y ) both ( x , y ) and ( y , x ) are valid ; Fill dp [ ] [ ] table with - 1 , indicating that results are not computed yet ; Stores minimum area ; PrminArea as the output ; Driver Code ; Given N & M ; Given valid dimensions ; Function Call
sz = 1001 NEW_LINE ok = [ [ 0 for i in range ( sz ) ] for i in range ( sz ) ] NEW_LINE dp = [ [ 0 for i in range ( sz ) ] for i in range ( sz ) ] NEW_LINE def minInvalidAreaUtil ( l , b ) : NEW_LINE INDENT global dp , ok NEW_LINE if ( dp [ l ] [ b ] == - 1 ) : NEW_LINE INDENT if ( ok [ l ] [ b ] ) : NEW_LINE INDENT dp [ l ] [ b ] = 0 NEW_LINE return dp [ l ] [ b ] NEW_LINE DEDENT ans = l * b NEW_LINE for i in range ( 1 , b ) : NEW_LINE INDENT ans = min ( ans , minInvalidAreaUtil ( l , i ) + minInvalidAreaUtil ( l , b - i ) ) NEW_LINE DEDENT for i in range ( 1 , l ) : NEW_LINE INDENT ans = min ( ans , minInvalidAreaUtil ( i , b ) + minInvalidAreaUtil ( l - i , b ) ) NEW_LINE DEDENT dp [ l ] [ b ] = ans NEW_LINE DEDENT return dp [ l ] [ b ] NEW_LINE DEDENT def minInvalidArea ( N , M , dimensions ) : NEW_LINE INDENT global dp , ok NEW_LINE K = len ( dimensions ) NEW_LINE for i in range ( K ) : NEW_LINE INDENT ok [ dimensions [ i ] [ 0 ] ] [ dimensions [ i ] [ 1 ] ] = 1 NEW_LINE ok [ dimensions [ i ] [ 1 ] ] [ dimensions [ i ] [ 0 ] ] = 1 NEW_LINE DEDENT for i in range ( sz ) : NEW_LINE INDENT for j in range ( sz ) : NEW_LINE INDENT dp [ i ] [ j ] = - 1 NEW_LINE DEDENT DEDENT minArea = minInvalidAreaUtil ( N , M ) NEW_LINE print ( minArea ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N , M = 10 , 10 NEW_LINE dimensions = [ [ 3 , 5 ] ] NEW_LINE minInvalidArea ( N , M , dimensions ) NEW_LINE DEDENT
Count unordered pairs of equal elements for all subarrays | Function to count all pairs ( i , j ) such that arr [ i ] equals arr [ j ] in all possible subarrays of the array ; Stores the size of the array ; Stores the positions of all the distinct elements ; Append index corresponding to arr [ i ] in the map ; Traverse the map M ; Traverse the array ; Update the value of ans ; Update the value of the sum ; Print the value of ans ; Driver Code
def countPairs ( arr ) : NEW_LINE INDENT N = len ( arr ) NEW_LINE ans = 0 NEW_LINE M = { } NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT if arr [ i ] in M : NEW_LINE INDENT M [ arr [ i ] ] . append ( i ) NEW_LINE DEDENT else : NEW_LINE INDENT M [ arr [ i ] ] = [ i ] NEW_LINE DEDENT DEDENT for key , value in M . items ( ) : NEW_LINE INDENT v = value NEW_LINE sum1 = 0 NEW_LINE for j in range ( len ( v ) ) : NEW_LINE INDENT ans += ( sum1 * ( N - v [ j ] ) ) NEW_LINE sum1 += v [ j ] + 1 NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 1 , 1 ] NEW_LINE countPairs ( arr ) NEW_LINE DEDENT
Number of alternating substrings from a given Binary String | Function to count number of alternating substrings from a given binary string ; Initialize dp array , where dp [ i ] [ j ] stores the number of alternating strings starts with i and having j elements . ; Traverse the string from the end ; If i is equal to N - 1 ; Otherwise , ; Increment count of substrings starting at i and has 0 in the beginning ; Increment count of substrings starting at i and has 1 in the beginning ; Stores the result ; Iterate in the range [ 0 , N - 1 ] ; Update ans ; Return the ans ; Driver code ; Given Input ; Function call
def countAlternatingSubstrings ( S , N ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( N ) ] for i in range ( 2 ) ] NEW_LINE for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( i == N - 1 ) : NEW_LINE INDENT if ( S [ i ] == '1' ) : NEW_LINE INDENT dp [ 1 ] [ i ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT dp [ 0 ] [ i ] = 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( S [ i ] == '0' ) : NEW_LINE INDENT dp [ 0 ] [ i ] = 1 + dp [ 1 ] [ i + 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT dp [ 1 ] [ i ] = 1 + dp [ 0 ] [ i + 1 ] NEW_LINE DEDENT DEDENT DEDENT ans = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT ans += max ( dp [ 0 ] [ i ] , dp [ 1 ] [ i ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = "0010" NEW_LINE N = len ( S ) NEW_LINE print ( countAlternatingSubstrings ( S , N ) ) NEW_LINE DEDENT
Count ways to place ' + ' and ' | Function to count number of ways ' + ' and ' - ' operators can be placed in front of array elements to make the sum of array elements equal to K ; Stores sum of the array ; Stores count of 0 s in A [ ] ; Traverse the array ; Update sum ; Update count of 0 s ; Conditions where no arrangements are possible which adds up to K ; Required sum ; Dp array ; Base cases ; Fill the dp array ; Return answer ; Driver Code ; Input ; Function call
def solve ( A , N , K ) : NEW_LINE INDENT sum = 0 NEW_LINE c = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum += A [ i ] NEW_LINE if ( A [ i ] == 0 ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT DEDENT if ( K > sum or ( sum + K ) % 2 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT sum = ( sum + K ) // 2 NEW_LINE dp = [ [ 0 for i in range ( sum + 1 ) ] for j in range ( N + 1 ) ] NEW_LINE for i in range ( sum + 1 ) : NEW_LINE INDENT dp [ 0 ] [ i ] = 0 NEW_LINE DEDENT for i in range ( N + 1 ) : NEW_LINE INDENT dp [ i ] [ 0 ] = 1 NEW_LINE DEDENT for i in range ( 1 , N + 1 , 1 ) : NEW_LINE INDENT for j in range ( 1 , sum + 1 , 1 ) : NEW_LINE INDENT if ( A [ i - 1 ] <= j and A [ i - 1 ] ) : NEW_LINE INDENT dp [ i ] [ j ] = ( dp [ i - 1 ] [ j ] + dp [ i - 1 ] [ j - A [ i - 1 ] ] ) NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = dp [ i - 1 ] [ j ] NEW_LINE DEDENT DEDENT DEDENT return dp [ N ] [ sum ] + pow ( 2 , c ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 1 , 2 , 3 ] NEW_LINE N = len ( A ) NEW_LINE K = 3 NEW_LINE print ( solve ( A , N , K ) ) NEW_LINE DEDENT
Rearrange an array to maximize sum of Bitwise AND of same | Function to implement recursive DP ; If i is equal to N ; If dp [ i ] [ mask ] is not equal to - 1 ; Iterate over the array B [ ] ; If current element is not yet selected ; Update dp [ i ] [ mask ] ; Return dp [ i ] [ mask ] ; Function to obtain maximum sum of Bitwise AND of same - indexed elements from the arrays A [ ] and B [ ] ; Stores all dp - states ; Returns the maximum value returned by the function maximizeAnd ( ) ; Driver Code
def maximizeAnd ( i , mask , A , B , N , dp ) : NEW_LINE INDENT if ( i == N ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( dp [ i ] [ mask ] != - 1 ) : NEW_LINE INDENT return dp [ i ] [ mask ] NEW_LINE DEDENT for j in range ( N ) : NEW_LINE INDENT if ( ( mask & ( 1 << j ) ) == 0 ) : NEW_LINE INDENT dp [ i ] [ mask ] = max ( dp [ i ] [ mask ] , ( A [ i ] & B [ j ] ) + maximizeAnd ( i + 1 , mask | ( 1 << j ) , A , B , N , dp ) ) NEW_LINE DEDENT DEDENT return dp [ i ] [ mask ] NEW_LINE DEDENT def maximizeAndUtil ( A , B , N ) : NEW_LINE INDENT temp = [ - 1 for i in range ( 1 << N + 1 ) ] NEW_LINE dp = [ temp for i in range ( N ) ] NEW_LINE return maximizeAnd ( 0 , 0 , A , B , N , dp ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 3 , 5 , 7 , 11 ] NEW_LINE B = [ 2 , 6 , 10 , 12 ] NEW_LINE N = len ( A ) NEW_LINE print ( maximizeAndUtil ( A , B , N ) ) NEW_LINE DEDENT
Find the winner of a game of removing at most 3 stones from a pile in each turn | Function to find the maximum score of Player 1 ; Base Case ; If the result is already computed , then return the result ; Variable to store maximum score ; Pick one stone ; Pick 2 stones ; Pick 3 stones ; Return the score of the player ; Function to find the winner of the game ; Create a 1D table , dp of size N ; Store the result ; Player 1 wins ; PLayer 2 wins ; Tie ; Given Input ; Function Call
def maximumStonesUtil ( arr , n , i , dp ) : NEW_LINE INDENT if ( i >= n ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT ans = dp [ i ] NEW_LINE if ( ans != - 1 ) : NEW_LINE INDENT return ans NEW_LINE DEDENT ans = - 2 ** 31 NEW_LINE ans = max ( ans , arr [ i ] - maximumStonesUtil ( arr , n , i + 1 , dp ) ) NEW_LINE if ( i + 1 < n ) : NEW_LINE INDENT ans = max ( ans , arr [ i ] + arr [ i + 1 ] - maximumStonesUtil ( arr , n , i + 2 , dp ) ) NEW_LINE DEDENT if ( i + 2 < n ) : NEW_LINE INDENT ans = max ( ans , arr [ i ] + arr [ i + 1 ] + arr [ i + 2 ] - maximumStonesUtil ( arr , n , i + 3 , dp ) ) NEW_LINE DEDENT return ans NEW_LINE DEDENT def maximumStones ( arr , n ) : NEW_LINE INDENT dp = [ - 1 ] * n NEW_LINE res = maximumStonesUtil ( arr , n , 0 , dp ) NEW_LINE if ( res > 0 ) : NEW_LINE INDENT return " Player1" NEW_LINE DEDENT elif ( res < 0 ) : NEW_LINE INDENT return " Player2" NEW_LINE DEDENT else : NEW_LINE INDENT return " Tie " NEW_LINE DEDENT DEDENT arr = [ 1 , 2 , 3 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maximumStones ( arr , n ) ) NEW_LINE
Count ways to reach the Nth station | Function to find the number of ways to reach Nth station ; Declares the DP [ ] array ; Only 1 way to reach station 1 ; Find the remaining states from the 2 nd station ; If the train A is present at station i - 1 ; If the train B is present at station i - 2 ; If train C is present at station i - 3 ; The total number of ways to reach station i ; Return the total count of ways ; Driver Code
def numberOfWays ( N ) : NEW_LINE INDENT DP = [ [ 0 for i in range ( 5 ) ] for i in range ( N + 1 ) ] NEW_LINE DP [ 1 ] [ 1 ] = 1 NEW_LINE DP [ 1 ] [ 2 ] = 1 NEW_LINE DP [ 1 ] [ 3 ] = 1 NEW_LINE DP [ 1 ] [ 4 ] = 1 NEW_LINE for i in range ( 2 , N + 1 ) : NEW_LINE INDENT if ( i - 1 > 0 and DP [ i - 1 ] [ 1 ] > 0 ) : NEW_LINE INDENT DP [ i ] [ 1 ] = DP [ i - 1 ] [ 4 ] NEW_LINE DEDENT if ( i - 2 > 0 and DP [ i - 2 ] [ 2 ] > 0 ) : NEW_LINE INDENT DP [ i ] [ 2 ] = DP [ i - 2 ] [ 4 ] NEW_LINE DEDENT if ( i - 3 > 0 and DP [ i - 3 ] [ 3 ] > 0 ) : NEW_LINE INDENT DP [ i ] [ 3 ] = DP [ i - 3 ] [ 4 ] NEW_LINE DEDENT DP [ i ] [ 4 ] = ( DP [ i ] [ 1 ] + DP [ i ] [ 2 ] + DP [ i ] [ 3 ] ) NEW_LINE DEDENT return DP [ N ] [ 4 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 15 NEW_LINE print ( numberOfWays ( N ) ) NEW_LINE DEDENT
Number of M | Function to find the number of M - length sorted arrays possible using numbers from the range [ 1 , N ] ; If size becomes equal to m , that means an array is found ; Include current element , increase size by 1 and remain on the same element as it can be included again ; Exclude current element ; Return the sum obtained in both the cases ; Driver Code ; Given Input ; Function Call
def countSortedArrays ( start , m , size , n ) : NEW_LINE INDENT if ( size == m ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( start > n ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT notTaken , taken = 0 , 0 NEW_LINE taken = countSortedArrays ( start , m , size + 1 , n ) NEW_LINE notTaken = countSortedArrays ( start + 1 , m , size , n ) NEW_LINE return taken + notTaken NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n , m = 2 , 3 NEW_LINE print ( countSortedArrays ( 1 , m , 0 , n ) ) NEW_LINE DEDENT
Maximum subarray sum possible after removing at most one subarray | Function to find maximum subarray sum possible after removing at most one subarray ; Calculate the preSum [ ] array ; Update the value of sum ; Update the value of maxSum ; Update the value of preSum [ i ] ; Calculate the postSum [ ] array ; Update the value of sum ; Update the value of maxSum ; Update the value of postSum [ i ] ; Stores the resultant maximum sum of subarray ; Update the value of ans ; Print resultant sum ; Driver code
def maximumSum ( arr , n ) : NEW_LINE INDENT preSum = [ 0 ] * n NEW_LINE sum = 0 NEW_LINE maxSum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum = max ( arr [ i ] , sum + arr [ i ] ) NEW_LINE maxSum = max ( maxSum , sum ) NEW_LINE preSum [ i ] = maxSum NEW_LINE DEDENT sum = 0 NEW_LINE maxSum = 0 NEW_LINE postSum = [ 0 ] * ( n + 1 ) NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT sum = max ( arr [ i ] , sum + arr [ i ] ) NEW_LINE maxSum = max ( maxSum , sum ) NEW_LINE postSum [ i ] = maxSum NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans = max ( ans , preSum [ i ] + postSum [ i + 1 ] ) NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT arr = [ 7 , 6 , - 1 , - 4 , - 5 , 7 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE maximumSum ( arr , N ) NEW_LINE
Count all unique outcomes possible by performing S flips on N coins | Function to recursively count the number of unique outcomes possible S flips are performed on N coins ; Base Cases ; Recursive Calls ; Driver Code
def numberOfUniqueOutcomes ( N , S ) : NEW_LINE INDENT if ( S < N ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( N == 1 or N == S ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return ( numberOfUniqueOutcomes ( N - 1 , S - 1 ) + numberOfUniqueOutcomes ( N - 1 , S - 2 ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N , S = 3 , 4 NEW_LINE print ( numberOfUniqueOutcomes ( N , S ) ) NEW_LINE DEDENT
Length of longest increasing subsequence in a string | Function to find length of longest increasing subsequence in a string ; Stores at every i - th index , the length of the longest increasing subsequence ending with character i ; Size of string ; Stores the length of LIS ; Iterate over each character of the string ; Store position of the current character ; Stores the length of LIS ending with current character ; Check for all characters less then current character ; Include current character ; Update length of longest increasing subsequence ; Updating LIS for current character ; Return the length of LIS ; Driver Code
def lisOtimised ( s ) : NEW_LINE INDENT dp = [ 0 ] * 30 NEW_LINE N = len ( s ) NEW_LINE lis = - 10 ** 9 NEW_LINE for i in range ( N ) : NEW_LINE INDENT val = ord ( s [ i ] ) - ord ( ' a ' ) NEW_LINE curr = 0 NEW_LINE for j in range ( val ) : NEW_LINE INDENT curr = max ( curr , dp [ j ] ) NEW_LINE DEDENT curr += 1 NEW_LINE lis = max ( lis , curr ) NEW_LINE dp [ val ] = max ( dp [ val ] , curr ) NEW_LINE DEDENT return lis NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " fdryutiaghfse " NEW_LINE print ( lisOtimised ( s ) ) NEW_LINE DEDENT
Sum of length of two smallest subsets possible from a given array with sum at least K | Python3 program for the above approach ; Function to calculate sum of lengths of two smallest subsets with sum >= K ; Sort the array in ascending order ; Stores suffix sum of the array ; Update the suffix sum array ; Stores all dp - states ; Initialize all dp - states with a maximum possible value ; Base Case ; Traverse the array arr [ ] ; Iterate over the range [ 0 , K ] ; If A [ i ] is equal to at least the required sum j for the current state ; If the next possible state doesn 't exist ; Otherwise , update the current state to the minimum of the next state and state including the current element A [ i ] ; Traverse the suffix sum array ; If suffix [ i ] - dp [ i ] [ K ] >= K ; Sum of lengths of the two smallest subsets is obtained ; Return - 1 , if there doesn 't  exist any subset of sum >= K ; Driver Code
MAX = 1e9 NEW_LINE def MinimumLength ( A , N , K ) : NEW_LINE INDENT A . sort ( ) NEW_LINE suffix = [ 0 ] * ( N + 1 ) NEW_LINE for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT suffix [ i ] = suffix [ i + 1 ] + A [ i ] NEW_LINE DEDENT dp = [ [ 0 ] * ( K + 1 ) ] * ( N + 1 ) NEW_LINE for i in range ( N + 1 ) : NEW_LINE INDENT for j in range ( K + 1 ) : NEW_LINE INDENT dp [ i ] [ j ] = MAX NEW_LINE DEDENT DEDENT dp [ N ] [ 0 ] = 0 NEW_LINE for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT for j in range ( K , - 1 , - 1 ) : NEW_LINE INDENT if ( j <= A [ i ] ) : NEW_LINE INDENT dp [ i ] [ j ] = A [ i ] NEW_LINE continue NEW_LINE DEDENT if ( dp [ i + 1 ] [ j - A [ i ] ] == MAX ) : NEW_LINE INDENT dp [ i ] [ j ] = MAX NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = min ( dp [ i + 1 ] [ j ] , dp [ i + 1 ] [ j - A [ i ] ] + A [ i ] ) NEW_LINE DEDENT DEDENT DEDENT for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( suffix [ i ] - dp [ i ] [ K ] >= K ) : NEW_LINE INDENT return N - i NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT arr = [ 7 , 4 , 5 , 6 , 8 ] NEW_LINE K = 13 NEW_LINE N = len ( arr ) NEW_LINE print ( MinimumLength ( arr , N , K ) ) NEW_LINE
Count distinct possible Bitwise XOR values of subsets of an array | Stores the mask of the vector ; Stores the current 20 of dp [ ] ; Function to store the mask of given integer ; Iterate over the range [ 0 , 20 ] ; If i - th bit 0 ; If dp [ i ] is zero ; Store the position in dp ; Increment the answer ; Return from the loop ; mask = mask XOR dp [ i ] ; Function to find the 20 of the set having Bitwise XOR of all the subset of the given array ; Traverse the array ; Prthe answer ; Driver Code ; Function Call
dp = [ 0 ] * 20 NEW_LINE ans = 0 NEW_LINE def insertVector ( mask ) : NEW_LINE INDENT global dp , ans NEW_LINE for i in range ( 20 ) : NEW_LINE INDENT if ( ( mask & 1 << i ) == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( not dp [ i ] ) : NEW_LINE INDENT dp [ i ] = mask NEW_LINE ans += 1 NEW_LINE return NEW_LINE DEDENT mask ^= dp [ i ] NEW_LINE DEDENT DEDENT def maxSizeSet ( arr , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT insertVector ( arr [ i ] ) NEW_LINE DEDENT print ( ( 1 << ans ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE maxSizeSet ( arr , N ) NEW_LINE DEDENT
Maximize sum possible from an array by jumps of length i + K * arr [ i ] from any ith index | Function to find the maximum sum possible by jumps of length i + K * arr [ i ] from any i - th index ; Initialize an array dp [ ] ; Stores the maximum sum ; Iterate over the range [ N - 1 , 0 ] ; If length of the jump exceeds N ; Set dp [ i ] as arr [ i ] ; Otherwise , update dp [ i ] as sum of dp [ i + K * arr [ i ] ] and arr [ i ] ; Update the overall maximum sum ; Print the answer ; Driver Code
def maxSum ( arr , N , K ) : NEW_LINE INDENT dp = [ 0 for i in range ( N + 2 ) ] NEW_LINE maxval = 0 NEW_LINE i = N - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT if ( ( i + K * arr [ i ] ) >= N ) : NEW_LINE INDENT dp [ i ] = arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] = dp [ i + K * arr [ i ] ] + arr [ i ] NEW_LINE DEDENT maxval = max ( maxval , dp [ i ] ) NEW_LINE i -= 1 NEW_LINE DEDENT print ( maxval ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 1 , 3 , 1 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE K = 3 NEW_LINE maxSum ( arr , N , K ) NEW_LINE DEDENT