codeparrot/xlcost-text-to-code huggingface dataset:
Subsequence X of length K such that gcd ( X [ 0 ] , X [ 1 ] ) + ( X [ 2 ] , X [ 3 ] ) + ... is maximized | C # program to find the sum of the addition of all possible subsets ; Recursive function to find the maximum value of the given recurrence ; If we get K elements ; If we have reached the end and K elements are not there ; If the state has been visited ; Iterate for every element as the next possible element and take the element which gives the maximum answer ; If this element is the first element in the individual pair in the subsequence then simply recurrence with the last element as i - th index ; If this element is the second element in the individual pair , the find gcd with the previous element and add to the answer and recur for the next element ; Driver Code
using System ; class GFG { static int MAX = 100 ; static int recur ( int ind , int cnt , int last , int [ ] a , int n , int k , int [ , ] dp ) { if ( cnt == k ) return 0 ; if ( ind == n ) return ( int ) - 1e9 ; if ( dp [ ind , cnt ] != - 1 ) return dp [ ind , cnt ] ; int ans = 0 ; for ( int i = ind ; i < n ; i ++ ) { if ( cnt % 2 == 0 ) ans = Math . Max ( ans , recur ( i + 1 , cnt + 1 , i , a , n , k , dp ) ) ; else ans = Math . Max ( ans , __gcd ( a [ last ] , a [ i ] ) + recur ( i + 1 , cnt + 1 , 0 , a , n , k , dp ) ) ; } return dp [ ind , cnt ] = ans ; } static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 4 , 5 , 3 , 7 , 8 , 10 , 9 , 8 } ; int n = a . Length ; int k = 4 ; int [ , ] dp = new int [ n , MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < MAX ; j ++ ) { dp [ i , j ] = - 1 ; } } Console . WriteLine ( recur ( 0 , 0 , 0 , a , n , k , dp ) ) ; } }
Maximum number of given operations to remove the entire string | C # implementation of the approach ; Function to return the maximum number of given operations required to remove the given string entirely ; If length of the string is zero ; Single operation can delete the entire string ; To store the prefix of the string which is to be deleted ; Prefix s [ 0. . i ] ; To store the substring s [ i + 1. . .2 * i + 1 ] ; If the prefix s [ 0. . . i ] can be deleted ; 1 operation to remove the current prefix and then recursively find the count of operations for the substring s [ i + 1. . . n - 1 ] ; Entire string has to be deleted ; Driver code
using System ; class GFG { static int find ( string s ) { if ( s . Length == 0 ) return 0 ; int c = 1 ; string d = " " ; for ( int i = 0 ; i < s . Length ; i ++ ) { d += s [ i ] ; string s2 = " " ; if ( i + d . Length < s . Length ) { s2 = s . Substring ( i + 1 , d . Length ) ; } if ( s2 == d ) { c = 1 + find ( s . Substring ( i + 1 ) ) ; break ; } } return c ; } public static void Main ( string [ ] args ) { string s = " abababab " ; Console . Write ( find ( s ) ) ; } }
Divide the array in K segments such that the sum of minimums is maximized | C # program to find the sum of the minimum of all the segments ; Function to maximize the sum of the minimums ; If k segments have been divided ; If we are at the end ; If we donot reach the end then return a negative number that cannot be the sum ; If at the end but k segments are not formed ; If the state has not been visited yet ; If the state has not been visited ; Get the minimum element in the segment ; Iterate and try to break at every index and create a segment ; Find the minimum element in the segment ; Find the sum of all the segments trying all the possible combinations ; Return the answer by memoizing it ; Driver Code ; Initialize dp array with - 1
using System ; class GFG { static int MAX = 10 ; public static int maximizeSum ( int [ ] a , int n , int ind , int k , int [ ] dp ) { if ( k == 0 ) { if ( ind == n ) return 0 ; else return - 1000000000 ; } else if ( ind == n ) return - 1000000000 ; else if ( dp [ ind , k ] != - 1 ) return dp [ ind , k ] ; else { int ans = 0 ; int mini = a [ ind ] ; for ( int i = ind ; i < n ; i ++ ) { mini = Math . Min ( mini , a [ i ] ) ; ans = Math . Max ( ans , maximizeSum ( a , n , i + 1 , k - 1 , dp ) + mini ) ; } return dp [ ind , k ] = ans ; } } public static void Main ( String [ ] args ) { int [ ] a = { 5 , 7 , 4 , 2 , 8 , 1 , 6 } ; int k = 3 ; int n = a . Length ; int [ , ] dp = new int [ MAX , MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j < MAX ; j ++ ) dp [ i , j ] = - 1 ; } Console . WriteLine ( maximizeSum ( a , n , 0 , k , dp ) ) ; } }
Mobile Numeric Keypad Problem | Set 2 | C # implementation of the approach ; Function to return the count of numbers possible ; Array of list storing possible direction for each number from 0 to 9 list [ i ] stores possible moves from index i ; Initializing list ; Storing values for n = 1 ; To store the values for n = i ; Loop to iterate through each index ; For each move possible from j Increment the value of possible move positions by Arr [ j ] ; Update Arr [ ] for next iteration ; Find the count of numbers possible ; Driver code
using System ; class GFG { static int MAX = 10 ; static int getCount ( int n ) { int [ ] [ ] list = new int [ MAX ] [ ] ; list [ 0 ] = new int [ ] { 0 , 8 } ; list [ 1 ] = new int [ ] { 1 , 2 , 4 } ; list [ 2 ] = new int [ ] { 2 , 1 , 3 , 5 } ; list [ 3 ] = new int [ ] { 3 , 6 , 2 } ; list [ 4 ] = new int [ ] { 4 , 1 , 7 , 5 } ; list [ 5 ] = new int [ ] { 5 , 4 , 6 , 2 , 8 } ; list [ 6 ] = new int [ ] { 6 , 3 , 5 , 9 } ; list [ 7 ] = new int [ ] { 7 , 4 , 8 } ; list [ 8 ] = new int [ ] { 8 , 5 , 0 , 7 , 9 } ; list [ 9 ] = new int [ ] { 9 , 6 , 8 } ; int [ ] Arr = new int [ ] { 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 } ; for ( int i = 2 ; i <= n ; i ++ ) { int [ ] Arr2 = new int [ MAX ] ; for ( int j = 0 ; j < MAX ; j ++ ) { for ( int x = 0 ; x < list [ j ] . Length ; x ++ ) { Arr2 [ list [ j ] [ x ] ] += Arr [ j ] ; } } for ( int j = 0 ; j < MAX ; j ++ ) Arr [ j ] = Arr2 [ j ] ; } int sum = 0 ; for ( int i = 0 ; i < MAX ; i ++ ) sum += Arr [ i ] ; return sum ; } public static void Main ( ) { int n = 2 ; Console . WriteLine ( getCount ( n ) ) ; } }
Optimally accommodate 0 s and 1 s from a Binary String into K buckets | C # implementation of the approach ; 2 - D dp array saving different states dp [ i ] [ j ] = minimum value of accommodation till i 'th index of the string  using j+1 number of buckets. ; Function to find the minimum required sum using dynamic programming ; If both start and bucket reached end then return 0 else that arrangement is not possible so return INT_MAX ; Corner case ; If state if already calculated then return its answer ; Start filling zeroes and ones which to be accommodated in jth bucket then ans for current arrangement will be ones * zeroes + recur ( i + 1 , bucket + 1 ) ; If this arrangement is not possible then don 't calculate further ; Function to initialize the dp and call solveUtil ( ) method to get the answer ; Start with 0 - th index and 1 bucket ; Driver code ; K buckets
using System ; class GFG { static int [ , ] dp ; static int solveUtil ( int start , int bucket , string str , int K ) { int N = str . Length ; if ( start == N ) { if ( bucket == K ) { return 0 ; } return Int32 . MaxValue ; } if ( bucket == K ) { return Int32 . MaxValue ; } if ( dp [ start , bucket ] != - 1 ) { return dp [ start , bucket ] ; } int zeroes = 0 ; int ones = 0 ; int ans = Int32 . MaxValue ; for ( int i = start ; i < N ; ++ i ) { if ( str [ i ] == '1' ) { ones ++ ; } else { zeroes ++ ; } if ( ones * zeroes > ans ) { break ; } int temp = solveUtil ( i + 1 , bucket + 1 , str , K ) ; if ( temp != Int32 . MaxValue ) { ans = Math . Min ( ans , temp + ( ones * zeroes ) ) ; } } return dp [ start , bucket ] = ans ; } static int solve ( string str , int K ) { int N = str . Length ; dp = new int [ N , K ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < K ; j ++ ) { dp [ i , j ] = - 1 ; } } int ans = solveUtil ( 0 , 0 , str , K ) ; return ans == Int32 . MaxValue ? - 1 : ans ; } static void Main ( ) { string S = "0101" ; int K = 2 ; Console . WriteLine ( solve ( S , K ) ) ; } }
Probability of getting more heads than tails when N biased coins are tossed | C # implementation of the above approach ; Function to return the probability when number of heads is greater than the number of tails ; Declaring the DP table ; Base case ; Iterating for every coin ; j represents the numbers of heads ; If number of heads is equal to zero there there is only one possibility ; When the number of heads is greater than ( n + 1 ) / 2 it means that heads are greater than tails as no of tails + no of heads is equal to n for any permutation of heads and tails ; Driver Code ; 1 based indexing ; Number of coins ; Function call
using System ; class GFG { static double Probability ( double [ ] p , int n ) { double [ , ] dp = new double [ n + 1 , n + 1 ] ; dp [ 0 , 0 ] = 1.0 ; for ( int i = 1 ; i <= n ; i += 1 ) { for ( int j = 0 ; j <= i ; j += 1 ) { if ( j == 0 ) dp [ i , j ] = dp [ i - 1 , j ] * ( 1.0 - p [ i ] ) ; else dp [ i , j ] = dp [ i - 1 , j ] * ( 1.0 - p [ i ] ) + dp [ i - 1 , j - 1 ] * p [ i ] ; } } double ans = 0.0 ; for ( int i = ( n + 1 ) / 2 ; i <= n ; i += 1 ) ans += dp [ n , i ] ; return ans ; } static public void Main ( ) { double [ ] p = { 0.0 , 0.3 , 0.4 , 0.7 } ; int n = p . Length - 1 ; Console . Write ( Probability ( p , n ) ) ; } }
Number of ways to get a given sum with n number of m | C # function to calculate the number of ways to achieve sum x in n no of throwsVal ; Function to calculate recursively the number of ways to get sum in given throwsVal and [ 1. . m ] values ; Base condition 1 ; Base condition 2 ; If value already calculated dont move into re - computation ; Recursively moving for sum - i in throwsVal - 1 no of throwsVal left ; Inserting present values in dp ; Driver code
using System ; class GFG { static int [ , ] dp = new int [ 55 , 55 ] ; static int NoofWays ( int face , int throwsVal , int sum ) { if ( sum == 0 && throwsVal == 0 ) { return 1 ; } if ( sum < 0 throwsVal == 0 ) { return 0 ; } if ( dp [ throwsVal , sum ] != - 1 ) { return dp [ throwsVal , sum ] ; } int ans = 0 ; for ( int i = 1 ; i <= face ; i ++ ) { ans += NoofWays ( face , throwsVal - 1 , sum - i ) ; } return dp [ throwsVal , sum ] = ans ; } static public void Main ( ) { int faces = 6 , throwsVal = 3 , sum = 12 ; for ( int i = 0 ; i < 55 ; i ++ ) { for ( int j = 0 ; j < 55 ; j ++ ) { dp [ i , j ] = - 1 ; } } Console . WriteLine ( NoofWays ( faces , throwsVal , sum ) ) ; } }
Longest sub | C # implementation of the approach ; Function to return the length of the longest required sub - sequence ; Sort the array ; To store the resultant length ; If array contains only one element then it divides itself ; Every element divides itself ; Count for all numbers which are lesser than a [ i ] ; If a [ i ] % a [ j ] then update the maximum subsequence length , dp [ i ] = Math . max ( dp [ i ] , 1 + dp [ j ] ) where j is in the range [ 0 , i - 1 ] ; If array contains only one element then i = j which doesn 't satisfy the condition ; Driver code
using System ; class GFG { static int find ( int n , int [ ] a ) { Array . Sort ( a ) ; int res = 1 ; int [ ] dp = new int [ n ] ; dp [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { dp [ i ] = 1 ; for ( int j = 0 ; j < i ; j ++ ) { if ( a [ i ] % a [ j ] == 0 ) { dp [ i ] = Math . Max ( dp [ i ] , 1 + dp [ j ] ) ; } } res = Math . Max ( res , dp [ i ] ) ; } return ( res == 1 ) ? - 1 : res ; } public static void Main ( ) { int [ ] a = { 2 , 4 , 6 , 1 , 3 , 11 } ; int n = a . Length ; Console . WriteLine ( find ( n , a ) ) ; } }
Maximize the happiness of the groups on the Trip | C # implementation of the approach ; Function to return the maximized happiness ; Two arrays similar to 0 1 knapsack problem ; To store the happiness of the current goup ; Current person is a child ; Woman ; Man ; Old Person ; Group 's happiness is the sum of happiness  of the people in the group multiplie  the number of people ; Solution using 0 1 knapsack ; Driver code ; Number of seats ; Groups
using System ; class GFG { static int maxHappiness ( int A , int N , String [ ] v ) { String str ; int [ ] val = new int [ N ] ; int [ ] wt = new int [ N ] ; int c = 0 ; for ( int i = 0 ; i < N ; i ++ ) { str = v [ i ] ; c = 0 ; for ( int j = 0 ; j < str . Length ; j ++ ) { if ( str [ j ] == ' c ' ) c += 4 ; else if ( str [ j ] == ' w ' ) c += 3 ; else if ( str [ j ] == ' m ' ) c += 2 ; else c ++ ; } c *= str . Length ; val [ i ] = c ; wt [ i ] = str . Length ; } int [ , ] k = new int [ N + 1 , A + 1 ] ; for ( int i = 0 ; i <= N ; i ++ ) { for ( int w = 0 ; w <= A ; w ++ ) { if ( i == 0 w == 0 ) k [ i , w ] = 0 ; else if ( wt [ i - 1 ] <= w ) { k [ i , w ] = Math . Max ( val [ i - 1 ] + k [ i - 1 , w - wt [ i - 1 ] ] , k [ i - 1 , w ] ) ; } else { k [ i , w ] = k [ i - 1 , w ] ; } } } return k [ N , A ] ; } public static void Main ( ) { int A = 5 ; String [ ] v = { " mmo " , " oo " , " cmw " , " cc " , " c " } ; int N = v . Length ; Console . WriteLine ( maxHappiness ( A , N , v ) ) ; } }
0 | C # implementation of the approach ; To store states of DP ; To check if a state has been solved ; Function to compute the states ; Base case ; Check if a state has been solved ; Setting a state as solved ; Returning the solved state ; Function to pre - compute the states dp [ 0 ] [ 0 ] , dp [ 0 ] [ 1 ] , . . , dp [ 0 ] [ C_MAX ] ; Function to answer a query in O ( 1 ) ; Driver code ; Performing required pre - computation ; Perform queries
using System ; class GFG { static int C_MAX = 30 ; static int max_arr_len = 10 ; static int [ , ] dp = new int [ max_arr_len , C_MAX + 1 ] ; static bool [ , ] v = new bool [ max_arr_len , C_MAX + 1 ] ; static int findMax ( int i , int r , int [ ] w , int n ) { if ( r < 0 ) return Int32 . MaxValue ; if ( i == n ) return 0 ; if ( v [ i , r ] ) return dp [ i , r ] ; v [ i , r ] = true ; dp [ i , r ] = Math . Max ( w [ i ] + findMax ( i + 1 , r - w [ i ] , w , n ) , findMax ( i + 1 , r , w , n ) ) ; return dp [ i , r ] ; } static void preCompute ( int [ ] w , int n ) { for ( int i = C_MAX ; i >= 0 ; i -- ) findMax ( 0 , i , w , n ) ; } static int ansQuery ( int w ) { return dp [ 0 , w ] ; } public static void Main ( ) { int [ ] w = { 3 , 8 , 9 } ; int n = w . Length ; preCompute ( w , n ) ; int [ ] queries = { 11 , 10 , 4 } ; int q = queries . Length ; for ( int i = 0 ; i < q ; i ++ ) Console . WriteLine ( ansQuery ( queries [ i ] ) ) ; } }
Maximise array sum after taking non | C # implementation of the approach ; To store the states of dp ; To check if a given state has been solved ; To store the prefix - sum ; Function to fill the prefix_sum [ ] with the prefix sum of the given array ; Function to find the maximum sum subsequence such that no two elements are adjacent ; Base case ; To check if a state has been solved ; Required recurrence relation ; Returning the value ; Driver code ; Finding prefix - sum ; Finding the maximum possible sum
using System ; class GFG { static int maxLen = 10 ; static int [ ] dp = new int [ maxLen ] ; static bool [ ] v = new bool [ maxLen ] ; static int [ ] prefix_sum = new int [ maxLen ] ; static void findPrefixSum ( int [ ] arr , int n ) { prefix_sum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { prefix_sum [ i ] = arr [ i ] + prefix_sum [ i - 1 ] ; } } static int maxSum ( int [ ] arr , int i , int n , int k ) { if ( i + k > n ) { return 0 ; } if ( v [ i ] ) { return dp [ i ] ; } v [ i ] = true ; int x ; if ( i == 0 ) { x = prefix_sum [ k - 1 ] ; } else { x = prefix_sum [ i + k - 1 ] - prefix_sum [ i - 1 ] ; } dp [ i ] = Math . Max ( maxSum ( arr , i + 1 , n , k ) , x + maxSum ( arr , i + k + 1 , n , k ) ) ; return dp [ i ] ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 7 , 6 } ; int n = arr . Length ; int k = 1 ; findPrefixSum ( arr , n ) ; Console . Write ( maxSum ( arr , 0 , n , k ) ) ; } }
Find maximum path sum in a 2D matrix when exactly two left moves are allowed | C # program to find maximum path sum in a 2D matrix when exactly two left moves are allowed ; Function to return the maximum path sum ; Copy last column i . e . starting and ending columns in another array ; Calculate suffix sum in each row ; Select the path we are going to follow ; Driver Code
using System ; class GFG { static int N = 3 ; static int M = 3 ; static int findMaxSum ( int [ , ] arr ) { int sum = 0 ; int [ , ] b = new int [ N , M ] ; for ( int i = 0 ; i < N ; i ++ ) { b [ i , M - 1 ] = arr [ i , M - 1 ] ; } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = M - 2 ; j >= 0 ; j -- ) { b [ i , j ] = arr [ i , j ] + b [ i , j + 1 ] ; } } for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { sum = Math . Max ( sum , b [ i , j ] + b [ i - 1 , j ] ) ; b [ i , j ] = Math . Max ( b [ i , j ] , b [ i - 1 , j ] + arr [ i , j ] ) ; } } return sum ; } public static void Main ( ) { int [ , ] arr = { { 3 , 7 , 4 } , { 1 , 9 , 6 } , { 1 , 7 , 7 } } ; Console . WriteLine ( findMaxSum ( arr ) ) ; } }
Queries to check if string B exists as substring in string A | C # implementation of the approach ; Function to return the modular inverse using Fermat 's little theorem ; Function to generate hash ; To store prefix - sum of rolling hash ; Multiplier for different values of i ; Generating hash value for string b ; Generating prefix - sum of hash of a ; Function that returns true if the required sub - string in a is equal to b ; To store hash of required sub - string of A ; If i = 0 then requires hash value ; Required hash if i != 0 ; Comparing hash with hash of B ; Driver code ; Generating hash ; Queries ; Perform queries
using System ; class GFG { static int mod = 3803 ; static int d = 26 ; static int hash_b ; static int [ ] hash_a ; static int [ ] mul ; static int mi ( int x ) { int p = mod - 2 ; int s = 1 ; while ( p != 1 ) { if ( p % 2 == 1 ) { s = ( s * x ) % mod ; } x = ( x * x ) % mod ; p /= 2 ; } return ( s * x ) % mod ; } static void genHash ( char [ ] a , char [ ] b ) { hash_a = new int [ a . Length ] ; mul = new int [ a . Length ] ; for ( int i = b . Length - 1 ; i >= 0 ; i -- ) { hash_b = ( hash_b * d + ( b [ i ] - 97 ) ) % mod ; } mul [ 0 ] = 1 ; hash_a [ 0 ] = ( a [ 0 ] - 97 ) % mod ; for ( int i = 1 ; i < a . Length ; i ++ ) { mul [ i ] = ( mul [ i - 1 ] * d ) % mod ; hash_a [ i ] = ( hash_a [ i - 1 ] + mul [ i ] * ( a [ i ] - 97 ) ) % mod ; } } static Boolean checkEqual ( int i , int len_a , int len_b ) { int x ; if ( i == 0 ) { x = hash_a [ len_b - 1 ] ; } else { x = ( hash_a [ i + len_b - 1 ] - hash_a [ i - 1 ] + 2 * mod ) % mod ; x = ( x * mi ( mul [ i ] ) ) % mod ; } if ( x == hash_b ) { return true ; } return false ; } public static void Main ( String [ ] args ) { String a = " abababababa " ; String b = " aba " ; genHash ( a . ToCharArray ( ) , b . ToCharArray ( ) ) ; int [ ] queries = { 0 , 1 , 2 , 3 } ; int q = queries . Length ; for ( int i = 0 ; i < q ; i ++ ) { if ( checkEqual ( queries [ i ] , a . Length , b . Length ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } } }
Number of ways to divide an array into K equal sum sub | C # implementation of the approach ; Array to store the states of DP ; Array to check if a state has been solved before ; To store the sum of the array elements ; Function to find the sum of all the array elements ; Function to return the number of ways ; If sum is not divisible by k answer will be zero ; Base case ; To check if a state has been solved before ; Sum of all the numbers from the beginning of the array ; Setting the current state as solved ; Recurrence relation ; Returning solved state ; Driver code ; Function call to find the sum of the array elements ; Print the number of ways
using System ; class GFG { static int max_size = 20 ; static int max_k = 20 ; static int [ , ] dp = new int [ max_size , max_k ] ; static Boolean [ , ] v = new Boolean [ max_size , max_k ] ; static int sum = 0 ; static void findSum ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; } static int cntWays ( int [ ] arr , int i , int ck , int k , int n , int curr_sum ) { if ( sum % k != 0 ) return 0 ; if ( i != n && ck == k + 1 ) return 0 ; if ( i == n ) { if ( ck == k + 1 ) return 1 ; else return 0 ; } if ( v [ i , ck ] ) return dp [ i , ck ] ; curr_sum += arr [ i ] ; v [ i , ck ] = true ; dp [ i , ck ] = cntWays ( arr , i + 1 , ck , k , n , curr_sum ) ; if ( curr_sum == ( sum / k ) * ck ) dp [ i , ck ] += cntWays ( arr , i + 1 , ck + 1 , k , n , curr_sum ) ; return dp [ i , ck ] ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , - 1 , 1 , - 1 , 1 , - 1 } ; int n = arr . Length ; int k = 2 ; findSum ( arr , n ) ; Console . WriteLine ( cntWays ( arr , 0 , 1 , k , n , 0 ) ) ; } }
Maximum sum in an array such that every element has exactly one adjacent element to it | C # implementation of the approach ; To store the states of dp ; Function to return the maximized sum ; Base case ; Checks if a state is already solved ; Recurrence relation ; Return the result ; Driver code
using System ; class GFG { static int arrSize = 51 ; static int [ ] dp = new int [ arrSize ] ; static bool [ ] v = new bool [ arrSize ] ; static int sumMax ( int i , int [ ] arr , int n ) { if ( i >= n - 1 ) return 0 ; if ( v [ i ] ) return dp [ i ] ; v [ i ] = true ; dp [ i ] = Math . Max ( arr [ i ] + arr [ i + 1 ] + sumMax ( i + 3 , arr , n ) , sumMax ( i + 1 , arr , n ) ) ; return dp [ i ] ; } public static void Main ( ) { int [ ] arr = { 1 , 1 , 1 , 1 } ; int n = arr . Length ; Console . WriteLine ( sumMax ( 0 , arr , n ) ) ; } }
Maximum Sum Subsequence of length k | C # program to calculate the maximum sum of increasing subsequence of length k ; In the implementation dp [ n , k ] represents maximum sum subsequence of length k and the subsequence is ending at index n . ; Initializing whole multidimensional dp array with value - 1 ; For each ith position increasing subsequence of length 1 is equal to that array ith value so initializing dp [ i , 1 ] with that array value ; Starting from 1 st index as we have calculated for 0 th index . Computing optimized dp values in bottom - up manner ; check for increasing subsequence ; Proceed if value is pre calculated ; Check for all the subsequences ending at any j < i and try including element at index i in them for some length l . Update the maximum value for every length . ; The final result would be the maximum value of dp [ i , k ] for all different i . ; When no subsequence of length k is possible sum would be considered zero ; Driver code
using System ; class GFG { static int MaxIncreasingSub ( int [ ] arr , int n , int k ) { int [ , ] dp = new int [ n , k + 1 ] ; int ans = - 1 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < k + 1 ; j ++ ) dp [ i , j ] = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { dp [ i , 1 ] = arr [ i ] ; } for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( arr [ j ] < arr [ i ] ) { for ( int l = 1 ; l <= k - 1 ; l ++ ) { if ( dp [ j , l ] != - 1 ) { dp [ i , l + 1 ] = Math . Max ( dp [ i , l + 1 ] , dp [ j , l ] + arr [ i ] ) ; } } } } } for ( int i = 0 ; i < n ; i ++ ) { if ( ans < dp [ i , k ] ) ans = dp [ i , k ] ; } return ( ans == - 1 ) ? 0 : ans ; } public static void Main ( String [ ] args ) { int n = 8 , k = 3 ; int [ ] arr = { 8 , 5 , 9 , 10 , 5 , 6 , 21 , 8 } ; int ans = MaxIncreasingSub ( arr , n , k ) ; Console . WriteLine ( ans ) ; } }