codeparrot/xlcost-text-to-code huggingface dataset:
Minimize cost of choosing and skipping array elements to reach end of the given array | C # program for the above approach ; 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
using System ; class GFG { static void minimumCost ( int [ ] cost , int n , int x ) { int [ ] dp = new int [ n + 2 ] ; dp [ 0 ] = cost [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( i == 1 ) dp [ i ] = cost [ i ] + dp [ i - 1 ] ; if ( i == 2 ) dp [ i ] = cost [ i ] + Math . Min ( dp [ i - 1 ] , x + dp [ i - 2 ] ) ; if ( i >= 3 ) dp [ i ] = cost [ i ] + Math . Min ( dp [ i - 1 ] , Math . Min ( x + dp [ i - 2 ] , 2 * x + dp [ i - 3 ] ) ) ; } Console . WriteLine ( dp [ n - 1 ] ) ; } public static void Main ( ) { int X = 4 ; int [ ] cost = { 6 , 3 , 9 , 2 , 1 , 3 } ; int N = cost . Length ; minimumCost ( cost , N , X ) ; } }
Maximum possible sum of non | C # program for the above approach ; 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
using System ; class GFG { public static int maxSum ( int [ ] a , int n , int k ) { if ( n <= 0 ) return 0 ; int option = maxSum ( a , n - 1 , k ) ; if ( k >= a [ n - 1 ] ) option = Math . Max ( option , a [ n - 1 ] + maxSum ( a , n - 2 , k - a [ n - 1 ] ) ) ; return option ; } public static void Main ( String [ ] args ) { int [ ] arr = { 50 , 10 , 20 , 30 , 40 } ; int N = arr . Length ; int K = 100 ; Console . WriteLine ( maxSum ( arr , N , K ) ) ; } }
Count possible binary strings of length N without P consecutive 0 s and Q consecutive 1 s | C # program to implement the above approach ; Function to check if a string 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
using System ; class GFG { static bool checkStr ( string str , int P , int Q ) { int N = str . Length ; char prev = str [ 0 ] ; int cnt = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( str [ i ] == prev ) { cnt ++ ; } else { if ( prev == '1' && cnt >= Q ) { return false ; } if ( prev == '0' && cnt >= P ) { return false ; } cnt = 1 ; } prev = str [ i ] ; } if ( prev == '1' && cnt >= Q ) { return false ; } if ( prev == '0' && cnt >= P ) { return false ; } return true ; } static int cntBinStr ( string str , int N , int P , int Q ) { int len = str . Length ; if ( len == N ) { if ( checkStr ( str , P , Q ) ) return 1 ; return 0 ; } int X = cntBinStr ( str + '0' , N , P , Q ) ; int Y = cntBinStr ( str + '1' , N , P , Q ) ; return X + Y ; } public static void Main ( ) { int N = 5 , P = 2 , Q = 3 ; Console . WriteLine ( cntBinStr ( " " , N , P , Q ) ) ; } }
Count possible permutations of given array satisfying the given conditions | C # Program to implement the above approach ; Function to get the value of binomial coefficient ; Stores the value of binomial coefficient ; Since C ( N , R ) = C ( N , N - R ) ; Calculate the value of C ( N , R ) ; Function to get the count of permutations of the array that satisfy the condition ; Stores count of permutations of the array that satisfy the given condition ; Stores the value of C ( 2 N , N ) ; Stores the value of catalan number ; Return answer ; Driver Code
using System ; class GFG { static int binCoff ( int N , int R ) { int res = 1 ; if ( R > ( N - R ) ) { R = ( N - R ) ; } for ( int i = 0 ; i < R ; i ++ ) { res *= ( N - i ) ; res /= ( i + 1 ) ; } return res ; } static int cntPermutation ( int N ) { int cntPerm ; int C_2N_N = binCoff ( 2 * N , N ) ; cntPerm = C_2N_N / ( N + 1 ) ; return cntPerm ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int N = arr . Length ; Console . WriteLine ( cntPermutation ( N / 2 ) ) ; } }
Smallest submatrix with Kth maximum XOR | C # Program for the above approach ; Function to print smallest index of Kth maximum Xor value of submatrices ; Dimensions of matrix ; Stores XOR values for every index ; Min heap to find the kth maximum XOR value ; Stores indices for corresponding XOR values ; Traversing matrix to calculate XOR values ; Insert calculated value in Min Heap ; If size exceeds k ; Remove the minimum ; Store smallest index containing xor [ i , j ] ; Stores the kth maximum element ; Print the required index ; Driver Code ; Function call
using System ; using System . Collections . Generic ; class GFG { static void smallestPosition ( int [ , ] m , int k ) { int n = m . GetLength ( 0 ) ; int mm = m . GetLength ( 1 ) ; int [ , ] xor = new int [ n , mm ] ; List < int > minHeap = new List < int > ( ) ; Dictionary < int , int [ ] > map = new Dictionary < int , int [ ] > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < mm ; j ++ ) { int a = i - 1 >= 0 ? xor [ i - 1 , j ] : 0 ; int b = j - 1 >= 0 ? xor [ i , j - 1 ] : 0 ; int c = ( i - 1 >= 0 && j - 1 >= 0 ) ? xor [ i - 1 , j - 1 ] : 0 ; xor [ i , j ] = m [ i , j ] ^ a ^ b ^ c ; minHeap . Add ( xor [ i , j ] ) ; minHeap . Sort ( ) ; if ( minHeap . Count > k ) { minHeap . RemoveAt ( 0 ) ; } if ( ! map . ContainsKey ( xor [ i , j ] ) ) map . Add ( xor [ i , j ] , new int [ ] { i , j } ) ; } } minHeap . Sort ( ) ; int kth_max_e = minHeap [ 0 ] ; Console . WriteLine ( ( map [ kth_max_e ] [ 0 ] + 1 ) + " ▁ " + ( map [ kth_max_e ] [ 1 ] + 1 ) ) ; } public static void Main ( String [ ] args ) { int [ , ] m = { { 1 , 2 , 3 } , { 2 , 2 , 1 } , { 2 , 4 , 2 } } ; int k = 1 ; smallestPosition ( m , k ) ; } }
Length of Longest Palindrome Substring | C # program for the above approach ; Function to obtain the length of the longest palindromic substring ; Length of given string ; Stores the maximum length ; Iterate over the string ; Iterate over the string ; Check for palindrome ; If string [ i , j - i + 1 ] is palindromic ; Return length of LPS ; Driver Code ; Given string ; Function call
using System ; class GFG { static int longestPalSubstr ( string str ) { int n = str . Length ; int maxLength = 1 , start = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { for ( int j = i ; j < str . Length ; j ++ ) { int flag = 1 ; for ( int k = 0 ; k < ( j - i + 1 ) / 2 ; k ++ ) if ( str [ i + k ] != str [ j - k ] ) flag = 0 ; if ( flag != 0 && ( j - i + 1 ) > maxLength ) { start = i ; maxLength = j - i + 1 ; } } } return maxLength ; } public static void Main ( ) { string str = " forgeeksskeegfor " ; Console . Write ( longestPalSubstr ( str ) ) ; } }
Maximize cost to empty an array by removing contiguous subarrays of equal elements | C # program for the above approach ; Initialize dp array ; Function that removes elements from array to maximize the cost ; Base case ; Check if an answer is stored ; Deleting count + 1 i . e . including the first element and starting a new sequence ; Removing [ left + 1 , i - 1 ] elements to continue with previous sequence ; Store the result ; Return answer ; Function to remove the elements ; Function call ; Driver Code ; Given array ; Function call
using System ; class GFG { static int [ , , ] dp = new int [ 101 , 101 , 101 ] ; static int helper ( int [ ] arr , int left , int right , int count , int m ) { if ( left > right ) return 0 ; if ( dp [ left , right , count ] != - 1 ) { return dp [ left , right , count ] ; } int ans = ( count + 1 ) * m + helper ( arr , left + 1 , right , 0 , m ) ; for ( int i = left + 1 ; i <= right ; ++ i ) { if ( arr [ i ] == arr [ left ] ) { ans = Math . Max ( ans , helper ( arr , left + 1 , i - 1 , 0 , m ) + helper ( arr , i , right , count + 1 , m ) ) ; } } dp [ left , right , count ] = ans ; return ans ; } static int maxPoints ( int [ ] arr , int n , int m ) { int len = n ; for ( int i = 0 ; i < 101 ; i ++ ) { for ( int j = 0 ; j < 101 ; j ++ ) { for ( int k = 0 ; k < 101 ; k ++ ) dp [ i , j , k ] = - 1 ; } } return helper ( arr , 0 , len - 1 , 0 , m ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 2 , 2 , 2 , 3 , 4 , 3 , 1 } ; int M = 3 ; int N = arr . Length ; Console . Write ( maxPoints ( arr , N , M ) ) ; } }
Minimum cost to convert M to N by repeated addition of its even divisors | C # program for the above approach ; Function to find the value of minimum steps to convert m to n ; Base Case ; If n exceeds m ; Iterate through all possible even divisors of m ; If m is divisible by i , then find the minimum cost ; Add the cost to convert m to m + i and recursively call next state ; Return min_cost ; Driver Code ; Function Call ; If conversion is not possible ; Print the cost
using System ; class GFG { static int inf = 1000000008 ; public static int minSteps ( int m , int n ) { if ( n == m ) return 0 ; if ( m > n ) return inf ; int min_cost = inf ; for ( int i = 2 ; i < m ; i += 2 ) { if ( m % i == 0 ) { min_cost = Math . Min ( min_cost , m / i + minSteps ( m + i , n ) ) ; } } return min_cost ; } public static void Main ( String [ ] args ) { int M = 6 ; int N = 24 ; int minimum_cost = minSteps ( M , N ) ; minimum_cost = minimum_cost == inf ? - 1 : minimum_cost ; Console . WriteLine ( minimum_cost ) ; } }
Represent a number as the sum of positive numbers ending with 9 | C # program for the above approach ; Function to find the minimum count of numbers ending with 9 to form N ; Extract last digit of N ; Calculate the last digit ; If the last digit satisfies the condition ; Driver Code
using System ; class GFG { static int minCountOfNumbers ( int N ) { int k = N % 10 ; int z = N - ( 9 * ( 9 - k ) ) ; if ( z >= 9 && z % 10 == 9 ) { return 10 - k ; } else return - 1 ; } public static void Main ( String [ ] args ) { int N = 156 ; Console . Write ( minCountOfNumbers ( N ) ) ; } }
Binary Matrix after flipping submatrices in given range for Q queries | C # program to implement the above approach ; Function to flip a submatrices ; Boundaries of the submatrix ; Iterate over the submatrix ; Check for 1 or 0 and flip accordingly ; Function to perform the queries ; Driver code ; Function call
using System ; class GFG { static void manipulation ( int [ , ] matrix , int [ ] q ) { int x1 = q [ 0 ] , y1 = q [ 1 ] , x2 = q [ 2 ] , y2 = q [ 3 ] ; for ( int i = x1 - 1 ; i < x2 ; i ++ ) { for ( int j = y1 - 1 ; j < y2 ; j ++ ) { if ( matrix [ i , j ] == 1 ) matrix [ i , j ] = 0 ; else matrix [ i , j ] = 1 ; } } } public static int [ ] GetRow ( int [ , ] matrix , int row ) { var rowLength = matrix . GetLength ( 1 ) ; var rowVector = new int [ rowLength ] ; for ( var i = 0 ; i < rowLength ; i ++ ) rowVector [ i ] = matrix [ row , i ] ; return rowVector ; } static void queries_fxn ( int [ , ] matrix , int [ , ] queries ) { for ( int i = 0 ; i < queries . GetLength ( 0 ) ; i ++ ) manipulation ( matrix , GetRow ( queries , i ) ) ; } public static void Main ( String [ ] args ) { int [ , ] matrix = { { 0 , 1 , 0 } , { 1 , 1 , 0 } } ; int [ , ] queries = { { 1 , 1 , 2 , 3 } , { 1 , 1 , 1 , 1 } , { 1 , 2 , 2 , 3 } } ; queries_fxn ( matrix , queries ) ; Console . Write ( " [ " ) ; for ( int i = 0 ; i < matrix . GetLength ( 0 ) ; i ++ ) { Console . Write ( " [ " ) ; for ( int j = 0 ; j < matrix . GetLength ( 1 ) ; j ++ ) Console . Write ( matrix [ i , j ] + " , ▁ " ) ; if ( i == matrix . Length - 1 ) Console . Write ( " ] " ) ; else Console . Write ( " ] , ▁ " ) ; } Console . Write ( " ] " ) ; } }
Minimum repeated addition of even divisors of N required to convert N to M | C # program for the above approach ; INF is the maximum value which indicates Impossible state ; Recursive Function that considers all possible even divisors of cur ; Indicates impossible state ; Return 0 if cur == M ; Initially it is set to INF that means we can 't transform cur to M ; Loop to find even divisors of cur ; if i is divisor of cur ; if i is even ; Finding divisors recursively for cur + i ; Check another divisor ; Find recursively for cur + ( cur / i ) ; Return the answer ; Function that finds the minimum operation to reduce N to M ; INF indicates impossible state ; Driver Code ; Given N and M ; Function Call
using System ; class GFG { static int INF = ( int ) 1e7 ; static int min_op ( int cur , int M ) { if ( cur > M ) return INF ; if ( cur == M ) return 0 ; int op = INF ; for ( int i = 2 ; i * i <= cur ; i ++ ) { if ( cur % i == 0 ) { if ( i % 2 == 0 ) { op = Math . Min ( op , 1 + min_op ( cur + i , M ) ) ; } if ( ( cur / i ) != i && ( cur / i ) % 2 == 0 ) { op = Math . Min ( op , 1 + min_op ( cur + ( cur / i ) , M ) ) ; } } } return op ; } static void min_operations ( int N , int M ) { int op = min_op ( N , M ) ; if ( op >= INF ) Console . Write ( " - 1" ) ; else Console . Write ( op + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int N = 6 , M = 24 ; min_operations ( N , M ) ; } }
Minimum length of subsequence having unit GCD | C # program for the above approach ; Function that finds the prime factors of a number ; To store the prime factor ; 2 s that divide n ; N must be odd at this point Skip one element ; Update the prime factor ; If n is a prime number greater than 2 ; Function that finds the shortest subsequence ; Check if the prime factor of first number , is also the prime factor of the rest numbers in array ; Set corresponding bit of prime factor to 1 , it means both these numbers have the same prime factor ; If no states encountered so far continue for this combination of bits ; Update this state with minimum ways to reach this state ; Function that print the minimum length of subsequence ; Find the prime factors of the first number ; Initialize the array with maximum steps , size of the array + 1 for instance ; Total number of set bits is equal to the total number of prime factors ; Indicates there is one way to reach the number under consideration ; State 0 corresponds to gcd of 1 ; If not found such subsequence then print " - 1" ; Else print the length ; Driver Code ; Given array [ ] arr ; Function Call
using System ; class GFG { private static int [ ] findPrimeFactors ( int n ) { int [ ] primeFactors = new int [ 9 ] ; int j = 0 ; if ( n % 2 == 0 ) { primeFactors [ j ++ ] = 2 ; while ( n % 2 == 0 ) n >>= 1 ; } for ( int i = 3 ; i * i <= n ; i += 2 ) { if ( n % i == 0 ) { primeFactors [ j ++ ] = i ; while ( n % i == 0 ) n /= i ; } } if ( n > 2 ) primeFactors [ j ++ ] = n ; int [ ] temp = new int [ j ] ; Array . Copy ( primeFactors , temp , j ) ; return temp ; } private static void findShortestSubsequence ( int [ ] dp , int [ ] a , int index , int [ ] primeFactors ) { int n = a . Length ; for ( int j = index ; j < n ; j ++ ) { int bitmask = 0 ; for ( int p = 0 ; p < primeFactors . Length ; p ++ ) { if ( a [ j ] % primeFactors [ p ] == 0 ) { bitmask ^= ( 1 << p ) ; } } for ( int i = 0 ; i < dp . Length ; i ++ ) { if ( dp [ i ] == n + 1 ) continue ; dp [ bitmask & i ] = Math . Min ( dp [ bitmask & i ] , dp [ i ] + 1 ) ; } } } private static void printMinimumLength ( int [ ] a ) { int min = a . Length + 1 ; for ( int i = 0 ; i < a . Length - 1 ; i ++ ) { int [ ] primeFactors = findPrimeFactors ( a [ i ] ) ; int n = primeFactors . Length ; int [ ] dp = new int [ 1 << n ] ; for ( i = 0 ; i < dp . Length ; i ++ ) dp [ i ] = a . Length + 1 ; int setBits = ( 1 << n ) - 1 ; dp [ setBits ] = 1 ; findShortestSubsequence ( dp , a , i + 1 , primeFactors ) ; min = Math . Min ( dp [ 0 ] , min ) ; } if ( min == a . Length + 1 ) Console . WriteLine ( - 1 ) ; else Console . WriteLine ( min ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 6 , 12 , 3 } ; printMinimumLength ( arr ) ; } }
Maximum sum of Bitwise XOR of all elements of two equal length subsets | C # program for the above approach ; Function that finds the maximum Bitwise XOR sum of the two subset ; Check if the current state is already computed ; Initialize answer to minimum value ; Iterate through all possible pairs ; Check whether ith bit and jth bit of mask is not set then pick the pair ; For all possible pairs find maximum value pick current a [ i ] , a [ j ] and set i , j th bits in mask ; Store the maximum value and return the answer ; Driver Code ; Given array [ ] arr ; Declare Initialize the dp states ; Function call
using System ; class GFG { public static int xorSum ( int [ ] a , int n , int mask , int [ ] dp ) { if ( dp [ mask ] != - 1 ) { return dp [ mask ] ; } int max_value = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( i != j && ( mask & ( 1 << i ) ) == 0 && ( mask & ( 1 << j ) ) == 0 ) { max_value = Math . Max ( max_value , ( a [ i ] ^ a [ j ] ) + xorSum ( a , n , ( mask | ( 1 << i ) | ( 1 << j ) ) , dp ) ) ; } } } return dp [ mask ] = max_value ; } public static void Main ( String [ ] args ) { int n = 4 ; int [ ] arr = { 1 , 2 , 3 , 4 } ; int [ ] dp = new int [ ( 1 << n ) + 5 ] ; for ( int i = 0 ; i < dp . Length ; i ++ ) dp [ i ] = - 1 ; Console . WriteLine ( xorSum ( arr , n , 0 , dp ) ) ; } }
Count of ways in which N can be represented as sum of Fibonacci numbers without repetition | C # program for the above approach ; Function to generate the fibonacci number ; First two number of fibonacci sqequence ; Function to find maximum ways to represent num as the sum of fibonacci number ; Generate the Canonical form of given number ; Reverse the number ; Base condition of dp1 and dp2 ; Iterate from 1 to cnt ; Calculate dp1 [ ] ; Calculate dp2 [ ] ; Return final ans ; Driver code ; Function call to generate the fibonacci numbers ; Given number ; Function call
using System ; class GFG { static long [ ] fib = new long [ 101 ] ; static long [ ] dp1 = new long [ 101 ] ; static long [ ] dp2 = new long [ 101 ] ; static long [ ] v = new long [ 101 ] ; static void fibonacci ( ) { fib [ 1 ] = 1 ; fib [ 2 ] = 2 ; for ( int i = 3 ; i <= 87 ; i ++ ) { fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] ; } } static long find ( long num ) { int cnt = 0 ; for ( int i = 87 ; i > 0 ; i -- ) { if ( num >= fib [ i ] ) { v [ cnt ++ ] = i ; num -= fib [ i ] ; } } for ( int i = 0 ; i < cnt / 2 ; i ++ ) { long t = v [ i ] ; v [ i ] = v [ cnt - i - 1 ] ; v [ cnt - i - 1 ] = t ; } dp1 [ 0 ] = 1 ; dp2 [ 0 ] = ( v [ 0 ] - 1 ) / 2 ; for ( int i = 1 ; i < cnt ; i ++ ) { dp1 [ i ] = dp1 [ i - 1 ] + dp2 [ i - 1 ] ; dp2 [ i ] = ( ( v [ i ] - v [ i - 1 ] ) / 2 ) * dp2 [ i - 1 ] + ( ( v [ i ] - v [ i - 1 ] - 1 ) / 2 ) * dp1 [ i - 1 ] ; } return ( dp1 [ cnt - 1 ] + dp2 [ cnt - 1 ] ) ; } static void Main ( ) { fibonacci ( ) ; int num = 13 ; Console . Write ( find ( num ) ) ; } }
Count of N | C # program for the above approach ; Function to find count of N - digit numbers with single digit XOR ; Range of numbers ; Calculate XOR of digits ; If XOR <= 9 , then increment count ; Print the count ; Driver Code ; Given number ; Function call
using System ; class GFG { public static void countNums ( int N ) { int l = ( int ) Math . Pow ( 10 , N - 1 ) , r = ( int ) Math . Pow ( 10 , N ) - 1 ; int count = 0 ; for ( int i = l ; i <= r ; i ++ ) { int xor = 0 , temp = i ; while ( temp > 0 ) { xor = xor ^ ( temp % 10 ) ; temp /= 10 ; } if ( xor <= 9 ) count ++ ; } Console . WriteLine ( count ) ; } public static void Main ( ) { int N = 2 ; countNums ( N ) ; } }
Maximize length of Non | C # program to implement the above approach ; Function to find the maximum length non decreasing subarray by reversing at most one subarray ; dp [ i , j ] be the longest subsequence of a [ 0. . . i ] with first j parts ; Maximum length sub - sequence of ( 0. . ) ; Maximum length sub - sequence of ( 0. . 1. . ) ; Maximum length sub - sequence of ( 0. . 1. .0 . . ) ; Maximum length sub - sequence of ( 0. . 1. .0 . .1 . . ) ; Find the max length subsequence ; Print the answer ; Driver code
using System ; class GFG { static void main_fun ( int [ ] arr , int n ) { int [ , ] dp = new int [ 4 , n ] ; if ( arr [ 0 ] == 0 ) dp [ 0 , 0 ] = 1 ; else dp [ 1 , 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) dp [ 0 , i ] = dp [ 0 , i - 1 ] + 1 ; else dp [ 0 , i ] = dp [ 0 , i - 1 ] ; } for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == 1 ) dp [ 1 , i ] = Math . Max ( dp [ 1 , i - 1 ] + 1 , dp [ 0 , i - 1 ] + 1 ) ; else dp [ 1 , i ] = dp [ 1 , i - 1 ] ; } for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) { dp [ 2 , i ] = Math . Max ( dp [ 2 , i - 1 ] + 1 , Math . Max ( dp [ 1 , i - 1 ] + 1 , dp [ 0 , i - 1 ] + 1 ) ) ; } else dp [ 2 , i ] = dp [ 2 , i - 1 ] ; } for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == 1 ) { dp [ 3 , i ] = Math . Max ( dp [ 3 , i - 1 ] + 1 , Math . Max ( dp [ 2 , i - 1 ] + 1 , Math . Max ( dp [ 1 , i - 1 ] + 1 , dp [ 0 , i - 1 ] + 1 ) ) ) ; } else dp [ 3 , i ] = dp [ 3 , i - 1 ] ; } int ans = Math . Max ( dp [ 2 , n - 1 ] , Math . Max ( dp [ 1 , n - 1 ] , Math . Max ( dp [ 0 , n - 1 ] , dp [ 3 , n - 1 ] ) ) ) ; Console . Write ( ans ) ; } public static void Main ( String [ ] args ) { int n = 4 ; int [ ] arr = { 0 , 1 , 0 , 1 } ; main_fun ( arr , n ) ; } }
Print the Maximum Subarray Sum | C # program for the above approach ; Function to print the elements of Subarray with maximum sum ; Initialize currMax and globalMax with first value of nums ; Iterate for all the elemensts of the array ; Update currMax ; Check if currMax is greater than globalMax ; Traverse in left direction to find start Index of subarray ; Decrement the start index ; Printing the elements of subarray with max sum ; Driver Code ; Given array [ ] arr ; Function call
using System ; using System . Collections . Generic ; class GFG { static void SubarrayWithMaxSum ( List < int > nums ) { int endIndex = 0 , currMax = nums [ 0 ] ; int globalMax = nums [ 0 ] ; for ( int i = 1 ; i < nums . Count ; ++ i ) { currMax = Math . Max ( nums [ i ] , nums [ i ] + currMax ) ; if ( currMax > globalMax ) { globalMax = currMax ; endIndex = i ; } } int startIndex = endIndex ; while ( startIndex >= 0 ) { globalMax -= nums [ startIndex ] ; if ( globalMax == 0 ) break ; startIndex -- ; } for ( int i = startIndex ; i <= endIndex ; ++ i ) { Console . Write ( nums [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { List < int > arr = new List < int > ( ) ; arr . Add ( - 2 ) ; arr . Add ( - 5 ) ; arr . Add ( 6 ) ; arr . Add ( - 2 ) ; arr . Add ( - 3 ) ; arr . Add ( 1 ) ; arr . Add ( 5 ) ; arr . Add ( - 6 ) ; SubarrayWithMaxSum ( arr ) ; } }