codeparrot/xlcost-text-to-code huggingface dataset:
Maximize sum that can be obtained from two given arrays based on given conditions | C # program for the above approach ; Function to find the maximum sum that can be obtained from two given based on the following conditions ; Stores the maximum sum from 0 to i ; Initialize the value of dp [ 0 ] [ 0 ] and dp [ 0 ] [ 1 ] ; Traverse the array A [ ] and B [ ] ; If A [ i ] is considered ; If B [ i ] is not considered ; If B [ i ] is considered ; If i = 1 , then consider the value of dp [ i ] [ 1 ] as b [ i ] ; Return maximum Sum ; Driver Code ; Given Input ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int MaximumSum ( int [ ] a , int [ ] b , int n ) { int [ , ] dp = new int [ n , 2 ] ; dp [ 0 , 0 ] = a [ 0 ] ; dp [ 0 , 1 ] = b [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { dp [ i , 0 ] = Math . Max ( dp [ i - 1 , 0 ] , dp [ i - 1 , 1 ] ) + a [ i ] ; dp [ i , 1 ] = Math . Max ( dp [ i - 1 , 0 ] , dp [ i - 1 , 1 ] ) ; if ( i - 2 >= 0 ) { dp [ i , 1 ] = Math . Max ( dp [ i , 1 ] , Math . Max ( dp [ i - 2 , 0 ] , dp [ i - 2 , 1 ] ) + b [ i ] ) ; } else { dp [ i , 1 ] = Math . Max ( dp [ i , 1 ] , b [ i ] ) ; } } return Math . Max ( dp [ n - 1 , 0 ] , dp [ n - 1 , 1 ] ) ; } public static void Main ( ) { int [ ] A = { 10 , 1 , 10 , 10 } ; int [ ] B = { 5 , 50 , 1 , 5 } ; int N = A . Length ; Console . Write ( MaximumSum ( A , B , N ) ) ; } }
Lexicographically Kth | C # program for the above approach ; Function to fill dp array ; Initialize all the entries with 0 ; Update dp [ 0 ] [ 0 ] to 1 ; Traverse the dp array ; Update the value of dp [ i ] [ j ] ; Recursive function to find the Kth lexicographical smallest string ; Handle the base cases ; If there are more than or equal to K strings which start with a , then the first character is ' a ' ; Otherwise the first character of the resultant string is ' b ' ; Function to find the Kth lexicographical smallest string ; Function call to fill the dp array ; Print the resultant string ; Driver code ; Given Input ; Function Call
using System ; class GFG { static int MAX = 30 ; static void findNumString ( int X , int Y , int [ , ] dp ) { for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j < MAX ; j ++ ) { dp [ i , j ] = 0 ; } } dp [ 0 , 0 ] = 1 ; for ( int i = 0 ; i <= X ; ++ i ) { for ( int j = 0 ; j <= Y ; ++ j ) { if ( i > 0 ) { dp [ i , j ] += dp [ i - 1 , j ] ; } if ( j > 0 ) { dp [ i , j ] += dp [ i , j - 1 ] ; } } } } static string kthString ( int X , int Y , int K , int [ , ] dp ) { string x1 = " " ; string y1 = " " ; for ( int i = 0 ; i < Y ; i ++ ) { x1 += ' b ' ; } for ( int i = 0 ; i < X ; i ++ ) { y1 += ' a ' ; } if ( X == 0 ) return x1 ; if ( Y == 0 ) return y1 ; if ( K <= dp [ X - 1 , Y ] ) { return ( " a " + kthString ( X - 1 , Y , K , dp ) ) ; } else { return ( " b " + kthString ( X , Y - 1 , K - dp [ X - 1 , Y ] , dp ) ) ; } } static void kthStringUtil ( int X , int Y , int K ) { int [ , ] dp = new int [ MAX , MAX ] ; findNumString ( X , Y , dp ) ; Console . WriteLine ( kthString ( X , Y , K , dp ) ) ; } static void Main ( ) { int X = 4 ; int Y = 3 ; int K = 4 ; kthStringUtil ( X , Y , K ) ; } }
Number of ways such that only K bars are visible from the left | C # program for the above approach ; 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
using System ; class GFG { static int KvisibleFromLeft ( int N , int K ) { if ( N == K ) return 1 ; if ( K == 1 ) { int ans = 1 ; for ( int i = 1 ; i < N ; i ++ ) ans *= i ; return ans ; } return KvisibleFromLeft ( N - 1 , K - 1 ) + ( N - 1 ) * KvisibleFromLeft ( N - 1 , K ) ; } public static void Main ( String [ ] args ) { int N = 5 , K = 2 ; Console . Write ( KvisibleFromLeft ( N , K ) ) ; } }
Minimum deletions in Array to make difference of adjacent elements non | C # program for the above approach ; 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
using System ; using System . Collections . Generic ; class GFG { static int minimumDeletions ( int [ ] A , int N ) { int ans = Int32 . MaxValue ; for ( int i = 1 ; i < ( 1 << N ) ; i ++ ) { List < int > temp = new List < int > ( ) ; for ( int j = 0 ; j < N ; j ++ ) { if ( ( i & ( 1 << j ) ) != 0 ) { temp . Add ( A [ j ] ) ; } } int flag = 0 ; for ( int j = 1 ; j < temp . Count ; j ++ ) if ( temp [ j ] < temp [ j - 1 ] ) flag = 1 ; for ( int j = 1 ; j < temp . Count - 1 ; j ++ ) if ( temp [ j ] - temp [ j - 1 ] > temp [ j + 1 ] - temp [ j ] ) flag = 1 ; if ( flag == 0 ) { ans = Math . Min ( ans , N - temp . Count ) ; } } return ans ; } public static void Main ( ) { int [ ] A = { 1 , 4 , 5 , 7 , 20 , 21 } ; int N = A . Length ; Console . Write ( minimumDeletions ( A , N ) ) ; } }
Minimum deletions in Array to make difference of adjacent elements non | C # program for the above approach ; the maximum value of A [ i ] ; Function for finding minimum deletions so that the array becomes non - decreasing and the difference between adjacent elements is also non - decreasing ; initialize the dp table and set all values to 0 pref [ i ] [ j ] will contain min ( dp [ i ] [ 0 ] , dp [ i ] [ 1 ] , ... dp [ i ] [ j ] ) ; find the maximum sized valid set possible and then subtract its size from N to get minimum number of deletions ; when selecting only the current element and deleting all elements from 0 to i - 1 inclusive ; if this is true , moving from index j to i is possible ; we can get min ( dp [ j ] [ 0 ] , . . dp [ j ] ) from pref array ; ; construct the prefix array for this element ; take the max set size from dp [ N - 1 ] [ 0 ] to dp [ N - 1 ] [ MAX ] ; Driver Code ; Function call
using System ; class GFG { static int MAX = 100001 ; static int minimumDeletions ( int [ ] A , int N ) { int [ , ] dp = new int [ N , MAX ] ; int [ , ] pref = new int [ N , MAX ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < MAX ; j ++ ) { dp [ i , j ] = 0 ; pref [ i , j ] = 0 ; } } for ( int i = 0 ; i < N ; i ++ ) { dp [ i , 0 ] = 1 ; for ( int j = i - 1 ; j >= 0 ; j -- ) { if ( A [ i ] >= A [ j ] ) { int diff = A [ i ] - A [ j ] ; dp [ i , diff ] = Math . Max ( dp [ i , diff ] , pref [ j , diff ] + 1 ) ; } } pref [ i , 0 ] = dp [ i , 0 ] ; for ( int j = 1 ; j < MAX ; j ++ ) pref [ i , j ] = Math . Max ( dp [ i , j ] , pref [ i , j - 1 ] ) ; } int maxSetSize = - 1 ; for ( int i = 0 ; i < MAX ; i ++ ) maxSetSize = Math . Max ( maxSetSize , dp [ N - 1 , i ] ) ; return N - maxSetSize ; } public static void Main ( String [ ] args ) { int [ ] A = { 1 , 4 , 5 , 7 , 20 , 21 } ; int N = A . Length ; Console . Write ( minimumDeletions ( A , N ) ) ; } }
Find maximum subset | C # program for tha above approach ; 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
using System ; using System . Collections . Generic ; class GFG { static int maximumSum ( List < int > A , int N , int K , int D ) { int ans = 0 ; for ( int i = 0 ; i < ( 1 << N ) ; i ++ ) { int sum = 0 ; int c = 0 ; for ( int j = 0 ; j < N ; j ++ ) { if ( ( i >> j & 1 ) != 0 ) { sum += A [ j ] ; c ++ ; } } if ( sum % D == 0 && c <= K ) ans = Math . Max ( ans , sum ) ; } return ans ; } public static void Main ( ) { int N = 5 , K = 3 , D = 7 ; List < int > A = new List < int > ( ) { 1 , 11 , 5 , 5 , 18 } ; Console . Write ( maximumSum ( A , N , K , D ) ) ; } }
Find the longest subsequence of a string that is a substring of another string | C # program for the above approach ; 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
using System ; using System . Collections . Generic ; class GFG { static string longestSubsequence ( string X , string Y ) { int i , j ; int n = X . Length ; int m = Y . Length ; int [ , ] mat = new int [ n + 1 , m + 1 ] ; for ( i = 0 ; i < n + 1 ; i ++ ) { for ( j = 0 ; j < m + 1 ; j ++ ) { if ( i == 0 j == 0 ) mat [ i , j ] = 0 ; } } for ( i = 1 ; i < n + 1 ; i ++ ) { for ( j = 1 ; j < m + 1 ; j ++ ) { if ( X [ i - 1 ] == Y [ j - 1 ] ) { mat [ i , j ] = 1 + mat [ i - 1 , j - 1 ] ; } else { mat [ i , j ] = mat [ i - 1 , j ] ; } } } int len = 0 , col = 0 ; for ( i = 0 ; i < m + 1 ; i ++ ) { if ( mat [ n , i ] > len ) { len = mat [ n , i ] ; col = i ; } } string res = " " ; i = n ; j = col ; while ( len > 0 ) { if ( X [ i - 1 ] == Y [ j - 1 ] ) { res = X [ i - 1 ] + res ; i -- ; j -- ; len -- ; } else { i -- ; } } return res ; } public static void Main ( ) { string X = " ABCD " ; string Y = " ACDBDCD " ; Console . Write ( longestSubsequence ( X , Y ) ) ; } }
Number of alternating substrings from a given Binary String | C # program for the above approach ; 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
using System ; class GFG { static int countAlternatingSubstrings ( string S , int N ) { int [ , ] dp = new int [ 2 , N ] ; for ( int i = 0 ; i < 2 ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { dp [ i , j ] = 0 ; } } for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( i == N - 1 ) { if ( S [ i ] == '1' ) dp [ 1 , i ] = 1 ; else dp [ 0 , i ] = 1 ; } else { if ( S [ i ] == '0' ) dp [ 0 , i ] = 1 + dp [ 1 , i + 1 ] ; else dp [ 1 , i ] = 1 + dp [ 0 , i + 1 ] ; } } int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { ans += Math . Max ( dp [ 0 , i ] , dp [ 1 , i ] ) ; } return ans ; } public static void Main ( ) { string S = "0010" ; int N = S . Length ; Console . Write ( countAlternatingSubstrings ( S , N ) ) ; } }
Count ways to place ' + ' and ' | C # program for the above approach ; 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
using System ; public class GFG { static int solve ( int [ ] A , int N , int K ) { int sum = 0 ; int c = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += A [ i ] ; if ( A [ i ] == 0 ) c ++ ; } if ( ( K > sum ) || ( ( ( sum + K ) % 2 ) != 0 ) ) return 0 ; sum = ( sum + K ) / 2 ; int [ , ] dp = new int [ N + 1 , sum + 1 ] ; for ( int i = 0 ; i <= sum ; i ++ ) dp [ 0 , i ] = 0 ; for ( int i = 0 ; i <= N ; i ++ ) dp [ i , 0 ] = 1 ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= sum ; j ++ ) { if ( ( A [ i - 1 ] <= j ) && ( A [ i - 1 ] != 0 ) ) dp [ i , j ] = dp [ i - 1 , j ] + dp [ i - 1 , j - A [ i - 1 ] ] ; else dp [ i , j ] = dp [ i - 1 , j ] ; } } return dp [ N , sum ] + ( int ) Math . Pow ( 2 , c ) ; } static public void Main ( ) { int [ ] A = { 1 , 1 , 2 , 3 } ; int N = A . Length ; int K = 3 ; Console . Write ( solve ( A , N , K ) ) ; } }
Rearrange an array to maximize sum of Bitwise AND of same | C # program for the above approach ; 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
using System ; class GFG { static int maximizeAnd ( int i , int mask , int [ ] A , int [ ] B , int N , int [ , ] dp ) { if ( i == N ) return 0 ; if ( dp [ i , mask ] != - 1 ) return dp [ i , mask ] ; for ( int j = 0 ; j < N ; ++ j ) { if ( ( mask & ( 1 << j ) ) == 0 ) { dp [ i , mask ] = Math . Max ( dp [ i , mask ] , ( A [ i ] & B [ j ] ) + maximizeAnd ( i + 1 , mask | ( 1 << j ) , A , B , N , dp ) ) ; } } return dp [ i , mask ] ; } static int maximizeAndUtil ( int [ ] A , int [ ] B , int N ) { int [ , ] dp = new int [ N , ( 1 << N ) + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < ( 1 << N ) + 1 ; j ++ ) { dp [ i , j ] = - 1 ; } } return maximizeAnd ( 0 , 0 , A , B , N , dp ) ; } static void Main ( ) { int [ ] A = { 3 , 5 , 7 , 11 } ; int [ ] B = { 2 , 6 , 10 , 12 } ; int N = A . Length ; Console . Write ( maximizeAndUtil ( A , B , N ) ) ; } }
Count numbers from a given range whose adjacent digits are not co |  ; Function to count numbers whose adjacent digits are not co - prime ; Base Case If the entire String is traversed ; If the subproblem has already been computed ; GCD of current and previous digits is not equal to 1 ; Current position is 0 ; All encountered digits until now are 0 s ; Return the total possible valid numbers ; Function to count numbers whose adjacent digits are not co - prime ; Convert R to String . ; Length of String ; Initialize dp array with - 1 ; Function call with initial values of bound , allZeros , previous as 1 , 1 , 0 ; Subtract 1 from the answer , as 0 is included ; Driver Code ; Input ; Function call
using System ; class GFG { static int [ , , , ] dp = new int [ 100 , 2 , 10 , 2 ] ; static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } static int noncoprimeCount ( int i , int N , char [ ] S , int bound , int prev , int allZeros ) { if ( i == N ) return 1 ; int val = dp [ i , bound , prev , allZeros ] ; if ( val != - 1 ) return val ; int cnt = 0 ; for ( int j = 0 ; j <= ( bound != 0 ? ( S [ i ] - '0' ) : 9 ) ; ++ j ) { if ( ( __gcd ( j , prev ) != 1 ) || ( i == 0 ) allZeros == 1 ) { cnt += noncoprimeCount ( i + 1 , N , S , bound != 0 & ( j == ( S [ i ] - '0' ) ) ? 1 : 0 , j , ( allZeros != 0 & ( j == 0 ) ) ? 1 : 0 ) ; } } return val = cnt ; } static void noncoprimeCountUtil ( int R ) { String S = String . Join ( " " , R ) ; int N = S . Length ; for ( int i = 0 ; i < 100 ; i ++ ) for ( int j = 0 ; j < 2 ; j ++ ) for ( int k = 0 ; k < 10 ; k ++ ) for ( int l = 0 ; l < 2 ; l ++ ) dp [ i , j , k , l ] = - 1 ; int ans = noncoprimeCount ( 0 , N , S . ToCharArray ( ) , 1 , 0 , 1 ) ; Console . Write ( ans - 1 + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int N = 10000 ; noncoprimeCountUtil ( N ) ; } }
Find the winner of a game of removing at most 3 stones from a pile in each turn | C # program for the above approach ; 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 ; Driver Code ; Given Input ; Function Call
using System ; public class GFG { static int ans = 0 ; static int maximumStonesUtil ( int [ ] arr , int n , int i , int [ ] dp ) { if ( i >= n ) return 0 ; ans = dp [ i ] ; if ( ans != - 1 ) return ans ; ans = int . MinValue ; ans = Math . Max ( ans , arr [ i ] - maximumStonesUtil ( arr , n , i + 1 , dp ) ) ; if ( i + 1 < n ) ans = Math . Max ( ans , arr [ i ] + arr [ i + 1 ] - maximumStonesUtil ( arr , n , i + 2 , dp ) ) ; if ( i + 2 < n ) ans = Math . Max ( ans , arr [ i ] + arr [ i + 1 ] + arr [ i + 2 ] - maximumStonesUtil ( arr , n , i + 3 , dp ) ) ; return ans ; } static String maximumStones ( int [ ] arr , int n ) { int [ ] dp = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) dp [ i ] = - 1 ; int res = maximumStonesUtil ( arr , n , 0 , dp ) ; if ( res > 0 ) return " Player1" ; else if ( res < 0 ) return " Player2" ; else return " Tie " ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 7 } ; int n = arr . Length ; Console . Write ( maximumStones ( arr , n ) ) ; } }
Count ways to reach the Nth station | C # program for the above approach ; Function to find the number of ways to reach Nth station ; Declares the DP [ ] array ; Initialize 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
using System ; class GFG { static int numberOfWays ( int N ) { int [ , ] DP = new int [ N + 1 , 5 ] ; for ( int i = 0 ; i < N + 1 ; i ++ ) { for ( int j = 0 ; j < 5 ; j ++ ) { DP [ i , j ] = 0 ; } } DP [ 1 , 1 ] = 1 ; DP [ 1 , 2 ] = 1 ; DP [ 1 , 3 ] = 1 ; DP [ 1 , 4 ] = 1 ; for ( int i = 2 ; i <= N ; i ++ ) { if ( i - 1 > 0 && DP [ i - 1 , 1 ] > 0 ) DP [ i , 1 ] = DP [ i - 1 , 4 ] ; if ( i - 2 > 0 && DP [ i - 2 , 2 ] > 0 ) DP [ i , 2 ] = DP [ i - 2 , 4 ] ; if ( i - 3 > 0 && DP [ i - 3 , 3 ] > 0 ) DP [ i , 3 ] = DP [ i - 3 , 4 ] ; DP [ i , 4 ] = ( DP [ i , 1 ] + DP [ i , 2 ] + DP [ i , 3 ] ) ; } return DP [ N , 4 ] ; } static public void Main ( ) { int N = 15 ; Console . WriteLine ( numberOfWays ( N ) ) ; } }
Number of M | C # program for the above approach ; 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
using System ; class GFG { static int countSortedArrays ( int start , int m , int size , int n ) { if ( size == m ) return 1 ; if ( start > n ) return 0 ; int notTaken = 0 , taken = 0 ; taken = countSortedArrays ( start , m , size + 1 , n ) ; notTaken = countSortedArrays ( start + 1 , m , size , n ) ; return taken + notTaken ; } public static void Main ( ) { int n = 2 , m = 3 ; Console . WriteLine ( countSortedArrays ( 1 , m , 0 , n ) ) ; } }
Maximum subarray sum possible after removing at most one subarray | C # program for the above approach ; 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 the resultant sum ; Driver code
using System ; class GFG { public static void maximumSum ( int [ ] arr , int n ) { long [ ] preSum = new long [ n ] ; long sum = 0 ; long maxSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = Math . Max ( arr [ i ] , sum + arr [ i ] ) ; maxSum = Math . Max ( maxSum , sum ) ; preSum [ i ] = maxSum ; } sum = 0 ; maxSum = 0 ; long [ ] postSum = new long [ n + 1 ] ; for ( int i = n - 1 ; i >= 0 ; i -- ) { sum = Math . Max ( arr [ i ] , sum + arr [ i ] ) ; maxSum = Math . Max ( maxSum , sum ) ; postSum [ i ] = maxSum ; } long ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans = Math . Max ( ans , preSum [ i ] + postSum [ i + 1 ] ) ; } Console . WriteLine ( ans ) ; } static void Main ( ) { int [ ] arr = { 7 , 6 , - 1 , - 4 , - 5 , 7 , 1 } ; maximumSum ( arr , arr . Length ) ; } }
Count all unique outcomes possible by performing S flips on N coins | C # program for the above approach ; Function to recursively count the number of unique outcomes possible S flips are performed on N coins ; Base Cases ; Recursive Calls ; Driver Code
using System ; class GFG { static int numberOfUniqueOutcomes ( int N , int S ) { if ( S < N ) return 0 ; if ( N == 1 N == S ) return 1 ; return ( numberOfUniqueOutcomes ( N - 1 , S - 1 ) + numberOfUniqueOutcomes ( N - 1 , S - 2 ) ) ; } static public void Main ( ) { int N = 3 , S = 4 ; Console . WriteLine ( numberOfUniqueOutcomes ( N , S ) ) ; } }
Length of longest increasing subsequence in a string | C # program for the above approach ; 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
using System ; using System . Collections . Generic ; class GFG { static int mn = - 2147483648 ; static int lisOtimised ( string s ) { int [ ] dp = new int [ 30 ] ; Array . Clear ( dp , 0 , 30 ) ; int N = s . Length ; int lis = mn ; for ( int i = 0 ; i < N ; i ++ ) { int val = ( int ) s [ i ] - 97 ; int curr = 0 ; for ( int j = 0 ; j < val ; j ++ ) { curr = Math . Max ( curr , dp [ j ] ) ; } curr ++ ; lis = Math . Max ( lis , curr ) ; dp [ val ] = Math . Max ( dp [ val ] , curr ) ; } return lis ; } public static void Main ( ) { string s = " fdryutiaghfse " ; Console . Write ( lisOtimised ( s ) ) ; } }
Sum of length of two smallest subsets possible from a given array with sum at least K | C # 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
using System ; class GFG { static int MAX = ( int ) ( 1e9 ) ; static int MinimumLength ( int [ ] A , int N , int K ) { Array . Sort ( A ) ; int [ ] suffix = new int [ N + 1 ] ; for ( int i = N - 1 ; i >= 0 ; i -- ) suffix [ i ] = suffix [ i + 1 ] + A [ i ] ; int [ , ] dp = new int [ N + 1 , K + 1 ] ; for ( int i = 0 ; i <= N ; i ++ ) for ( int j = 0 ; j <= K ; j ++ ) dp [ i , j ] = MAX ; dp [ N , 0 ] = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { for ( int j = K ; j >= 0 ; j -- ) { if ( j <= A [ i ] ) { dp [ i , j ] = A [ i ] ; continue ; } if ( dp [ i + 1 , j - A [ i ] ] == MAX ) dp [ i , j ] = MAX ; else dp [ i , j ] = Math . Min ( dp [ i + 1 , j ] , dp [ i + 1 , j - A [ i ] ] + A [ i ] ) ; } } for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( suffix [ i ] - dp [ i , K ] >= K ) { return N - i ; } } return - 1 ; } public static void Main ( string [ ] args ) { int [ ] arr = { 7 , 4 , 5 , 6 , 8 } ; int K = 13 ; int N = arr . Length ; Console . WriteLine ( MinimumLength ( arr , N , K ) ) ; } }