codeparrot/xlcost-text-to-code huggingface dataset:
Minimum number of jumps to reach end | C # program to find Minimum number of jumps to reach end ; Returns minimum number of jumps to reach arr [ h ] from arr [ l ] ; Base case : when source and destination are same ; When nothing is reachable from the given source ; Traverse through all the points reachable from arr [ l ] . Recursively get the minimum number of jumps needed to reach arr [ h ] from these reachable points . ; Driver code
using System ; class GFG { static int minJumps ( int [ ] arr , int l , int h ) { if ( h == l ) return 0 ; if ( arr [ l ] == 0 ) return int . MaxValue ; int min = int . MaxValue ; for ( int i = l + 1 ; i <= h && i <= l + arr [ l ] ; i ++ ) { int jumps = minJumps ( arr , i , h ) ; if ( jumps != int . MaxValue && jumps + 1 < min ) min = jumps + 1 ; } return min ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 6 , 3 , 2 , 3 , 6 , 8 , 9 , 5 } ; int n = arr . Length ; Console . Write ( " Minimum ▁ number ▁ of ▁ jumps ▁ to ▁ reach ▁ end ▁ is ▁ " + minJumps ( arr , 0 , n - 1 ) ) ; } }
Camel and Banana Puzzle | DP | C # program of the above approach ; Stores the overlapping state ; Recursive function to find the maximum number of bananas that can be transferred to A distance using memoization ; Base Case where count of bananas is less that the given distance ; Base Case where count of bananas is less that camel 's capacity ; Base Case where distance = 0 ; If the current state is already calculated ; Stores the maximum count of bananas ; Stores the number of trips to transfer B bananas using a camel of capacity C ; Loop to iterate over all the breakpoints in range [ 1 , A ] ; Recursive call over the remaining path ; Update the maxCount ; Memoize the current value ; Return answer ; Function to find the maximum number of bananas that can be transferred ; Initialize dp array with - 1 ; Function Call ; Driver Code
using System ; public class GFG { static int [ , ] dp = new int [ 1001 , 3001 ] ; static int recBananaCnt ( int A , int B , int C ) { if ( B <= A ) { return 0 ; } if ( B <= C ) { return B - A ; } if ( A == 0 ) { return B ; } if ( dp [ A , B ] != - 1 ) { return dp [ A , B ] ; } int maxCount = Int32 . MinValue ; int tripCount = B % C == 0 ? ( ( 2 * B ) / C ) - 1 : ( ( 2 * B ) / C ) + 1 ; for ( int i = 1 ; i <= A ; i ++ ) { int curCount = recBananaCnt ( A - i , B - tripCount * i , C ) ; if ( curCount > maxCount ) { maxCount = curCount ; dp [ A , B ] = maxCount ; } } return maxCount ; } static int maxBananaCnt ( int A , int B , int C ) { for ( int i = 0 ; i < 1001 ; i ++ ) for ( int j = 0 ; j < 3001 ; j ++ ) dp [ i , j ] = - 1 ; return recBananaCnt ( A , B , C ) ; } public static void Main ( string [ ] args ) { int A = 1000 ; int B = 3000 ; int C = 1000 ; Console . WriteLine ( maxBananaCnt ( A , B , C ) ) ; } }
Count of valid arrays of size P with elements in range [ 1 , N ] having duplicates at least M distance apart | C # program for the above approach ; Function to calculate the total number of arrays ; If the size of the array is P ; Check if all elements are used atlease once ; Check if this state is already calculated ; Initialize the result ; Use a number from the list of unused numbers ; There are ' unused ' number of favourable choices ; Use a number from already present number atlease M distance back ; There are ' used ▁ - ▁ M ' number of favourable choices ; Store the result ; Function to solve the problem ; Initialize DP table : dp [ i , j , j ] i : current position / index j : number of used elements k : number of unused elements ; Driver Code
using System ; public class GFG { static int calculate ( int position , int used , int unused , int P , int M , int [ , , ] dp ) { if ( position == P ) { return unused == 0 ? 1 : 0 ; } if ( dp [ position , used , unused ] != - 1 ) return dp [ position , used , unused ] ; int result = 0 ; if ( unused > 0 ) { result += calculate ( position + 1 , used + 1 , unused - 1 , P , M , dp ) * unused ; } if ( used > M ) { result += calculate ( position + 1 , used , unused , P , M , dp ) * ( used - M ) ; } return dp [ position , used , unused ] = result ; } static int solve ( int N , int P , int M ) { int [ , , ] dp = new int [ 101 , 101 , 101 ] ; 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 calculate ( 0 , 0 , N , P , M , dp ) ; } public static void Main ( String [ ] args ) { int N = 2 , M = 0 , P = 3 ; Console . WriteLine ( solve ( N , P , M ) ) ; } }
Minimum number of jumps to reach end | C # program to find Minimum number of jumps to reach end ; Returns Minimum number of jumps to reach end ; jumps [ 0 ] will hold the result ; Minimum number of jumps needed to reach last element from last elements itself is always 0 ; Start from the second element , move from right to left and construct the jumps [ ] array where jumps [ i ] represents minimum number of jumps needed to reach arr [ m - 1 ] from arr [ i ] ; If arr [ i ] is 0 then arr [ n - 1 ] can 't be reached from here ; If we can direcly reach to the end point from here then jumps [ i ] is 1 ; Otherwise , to find out the minimum number of jumps needed to reach arr [ n - 1 ] , check all the points reachable from here and jumps [ ] value for those points ; initialize min value ; following loop checks with all reachable points and takes the minimum ; Handle overflow ; or Integer . MAX_VALUE ; Driver Code
using System ; class GFG { public static int minJumps ( int [ ] arr , int n ) { int [ ] jumps = new int [ n ] ; int min ; jumps [ n - 1 ] = 0 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] == 0 ) { jumps [ i ] = int . MaxValue ; } else if ( arr [ i ] >= n - i - 1 ) { jumps [ i ] = 1 ; } else { min = int . MaxValue ; for ( int j = i + 1 ; j < n && j <= arr [ i ] + i ; j ++ ) { if ( min > jumps [ j ] ) { min = jumps [ j ] ; } } if ( min != int . MaxValue ) { jumps [ i ] = min + 1 ; } else { jumps [ i ] = min ; } } } return jumps [ 0 ] ; } public static void Main ( string [ ] args ) { int [ ] arr = new int [ ] { 1 , 3 , 6 , 1 , 0 , 9 } ; int size = arr . Length ; Console . WriteLine ( " Minimum ▁ number ▁ of " + " ▁ jumps ▁ to ▁ reach ▁ end ▁ is ▁ " + minJumps ( arr , size ) ) ; } }
Count N |  ; Stores the dp states ; Check if a number is a prime or not ; Function to generate all prime numbers that are less than or equal to n ; Base cases . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of as non - prime ; Function to find the count of N - digit numbers such that the sum of digits is a prime number ; If end of array is reached ; If the sum is equal to a prime number ; Otherwise ; If the dp - states are already computed ; If index = 1 , any digit from [ 1 - 9 ] can be placed . If N = 1 , 0 also can be placed . ; Otherwise , any digit from [ 0 - 9 ] can be placed . ; Return the answer . ; Driver Code ; Initializing dp array with - 1 ; Initializing prime array to true ; Find all primes less than or equal to 1000 , which is sufficient for N upto 100 ; Given Input ; Function call
using System ; public class GFG { public static int [ , ] dp = new int [ 100 , 1000 ] ; public static bool [ ] prime = new bool [ 1005 ] ; public static void SieveOfEratosthenes ( int n ) { prime [ 0 ] = prime [ 1 ] = false ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) prime [ i ] = false ; } } } public static int countOfNumbers ( int index , int sum , int N ) { if ( index == N + 1 ) { if ( prime [ sum ] == true ) { return 1 ; } return 0 ; } int val = dp [ index , sum ] ; if ( val != - 1 ) { return val ; } val = 0 ; if ( index == 1 ) { for ( int digit = ( N == 1 ? 0 : 1 ) ; digit <= 9 ; ++ digit ) { val += countOfNumbers ( index + 1 , sum + digit , N ) ; } } else { for ( int digit = 0 ; digit <= 9 ; ++ digit ) { val += countOfNumbers ( index + 1 , sum + digit , N ) ; } } return val ; } public static void Main ( String [ ] args ) { for ( int i = 0 ; i < 100 ; i ++ ) { for ( int j = 0 ; j < 1000 ; j ++ ) { dp [ i , j ] = - 1 ; } } for ( int i = 0 ; i < prime . Length ; i ++ ) prime [ i ] = true ; SieveOfEratosthenes ( 1000 ) ; int N = 6 ; Console . WriteLine ( countOfNumbers ( 1 , 0 , N ) ) ; } }
Maximum number of groups that can receive fresh donuts distributed in batches of size K | C # program for the above approach ; Stores the result of the same recursive calls ; Recursive function to find the maximum number of groups that will receive fresh donuts ; Store the result for the current state ; Store the key and check if it is present in the hashmap ; If already calculated ; If left is 0 ; Traverse the array [ ] arr ; Decrement arr [ i ] ; Update the maximum number of groups ; Increment arr [ i ] by 1 ; Otherwise , traverse the given array [ ] arr ; Decrement arr [ i ] ; Update the maximum number of groups ; Increment arr [ i ] by 1 ; Memoize the result and return it ; Function to find the maximum number of groups that will receive fresh donuts ; Stores count of remainder by K ; Traverse the array [ ] arr ; Hashmap to memoize the results ; Store the maximum number of groups ; Return the answer ; Driver Code
using System ; using System . Collections . Generic ; public class GFG { static Dictionary < String , int > memo ; public static int dfs ( int [ ] V , int left , int K ) { int q = 0 ; String key = string . Join ( " , " , V ) ; key += left . ToString ( ) ; if ( memo . ContainsKey ( key ) ) return memo [ key ] ; else if ( left == 0 ) { for ( int i = 1 ; i < K ; ++ i ) if ( V [ i ] > 0 ) { V [ i ] -- ; q = Math . Max ( q , 1 + dfs ( V , K - i , K ) ) ; V [ i ] ++ ; } } else { for ( int i = 1 ; i < K ; ++ i ) { if ( V [ i ] > 0 ) { V [ i ] -- ; int nleft = i <= left ? left - i : K + left - i ; q = Math . Max ( q , dfs ( V , nleft , K ) ) ; V [ i ] ++ ; } } } if ( memo . ContainsKey ( key ) ) memo [ key ] = q ; else memo . Add ( key , q ) ; return q ; } public static int maxGroups ( int K , int [ ] arr ) { int [ ] V = new int [ K ] ; foreach ( int x in arr ) V [ x % K ] ++ ; memo = new Dictionary < String , int > ( ) ; int ans = V [ 0 ] + dfs ( V , 0 , K ) ; return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 } ; int K = 3 ; Console . WriteLine ( maxGroups ( K , arr ) ) ; } }
Maximum Sum Alternating Subarray | C # implementation for the above approach ; Function to find the maximum alternating sum of a subarray for the given array ; Traverse the array ; Store sum of subarrays starting at even indices ; Update sum ; Traverse the array ; Store sum of subarrays starting at odd indices ; Update sum ; Driver code ; Given Input ; Function call
using System ; using System . Collections . Generic ; class GFG { static int alternatingSum ( int [ ] arr , int n ) { int sum = 0 ; int sumSoFar = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 1 ) { sumSoFar -= arr [ i ] ; } else { sumSoFar = Math . Max ( sumSoFar + arr [ i ] , arr [ i ] ) ; } sum = Math . Max ( sum , sumSoFar ) ; } sumSoFar = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { sumSoFar -= arr [ i ] ; } else { sumSoFar = Math . Max ( sumSoFar + arr [ i ] , arr [ i ] ) ; } sum = Math . Max ( sum , sumSoFar ) ; } return sum ; } public static void Main ( ) { int [ ] arr = { - 4 , - 10 , 3 , 5 } ; int n = arr . Length ; int ans = alternatingSum ( arr , n ) ; Console . Write ( ans ) ; } }
Smallest subarray with sum greater than a given value | C # program to find Smallest subarray with sum greater than a given value ; Returns length of smallest subarray with sum greater than x . If there is no subarray with given sum , then returns n + 1 ; Initialize length of smallest subarray as n + 1 ; Pick every element as starting point ; Initialize sum starting with current start ; If first element itself is greater ; Try different ending points for curremt start ; add last element to current sum ; If sum becomes more than x and length of this subarray is smaller than current smallest length , update the smallest length ( or result ) ; Driver Code
using System ; class GFG { static int smallestSubWithSum ( int [ ] arr , int n , int x ) { int min_len = n + 1 ; for ( int start = 0 ; start < n ; start ++ ) { int curr_sum = arr [ start ] ; if ( curr_sum > x ) return 1 ; for ( int end = start + 1 ; end < n ; end ++ ) { curr_sum += arr [ end ] ; if ( curr_sum > x && ( end - start + 1 ) < min_len ) min_len = ( end - start + 1 ) ; } } return min_len ; } static public void Main ( ) { int [ ] arr1 = { 1 , 4 , 45 , 6 , 10 , 19 } ; int x = 51 ; int n1 = arr1 . Length ; int res1 = smallestSubWithSum ( arr1 , n1 , x ) ; if ( res1 == n1 + 1 ) Console . WriteLine ( " Not ▁ Possible " ) ; else Console . WriteLine ( res1 ) ; int [ ] arr2 = { 1 , 10 , 5 , 2 , 7 } ; int n2 = arr2 . Length ; x = 9 ; int res2 = smallestSubWithSum ( arr2 , n2 , x ) ; if ( res2 == n2 + 1 ) Console . WriteLine ( " Not ▁ Possible " ) ; else Console . WriteLine ( res2 ) ; int [ ] arr3 = { 1 , 11 , 100 , 1 , 0 , 200 , 3 , 2 , 1 , 250 } ; int n3 = arr3 . Length ; x = 280 ; int res3 = smallestSubWithSum ( arr3 , n3 , x ) ; if ( res3 == n3 + 1 ) Console . WriteLine ( " Not ▁ Possible " ) ; else Console . WriteLine ( res3 ) ; } }
Smallest subarray with sum greater than a given value | O ( n ) solution for finding smallest subarray with sum greater than x ; Returns length of smallest subarray with sum greater than x . If there is no subarray with given sum , then returns n + 1 ; Initialize current sum and minimum length ; Initialize starting and ending indexes ; Keep adding array elements while current sum is smaller than or equal to x ; If current sum becomes greater than x . ; Update minimum length if needed ; remove starting elements ; Driver Code
using System ; class GFG { static int smallestSubWithSum ( int [ ] arr , int n , int x ) { int curr_sum = 0 , min_len = n + 1 ; int start = 0 , end = 0 ; while ( end < n ) { while ( curr_sum <= x && end < n ) curr_sum += arr [ end ++ ] ; while ( curr_sum > x && start < n ) { if ( end - start < min_len ) min_len = end - start ; curr_sum -= arr [ start ++ ] ; } } return min_len ; } static public void Main ( ) { int [ ] arr1 = { 1 , 4 , 45 , 6 , 10 , 19 } ; int x = 51 ; int n1 = arr1 . Length ; int res1 = smallestSubWithSum ( arr1 , n1 , x ) ; if ( res1 == n1 + 1 ) Console . WriteLine ( " Not ▁ Possible " ) ; else Console . WriteLine ( res1 ) ; int [ ] arr2 = { 1 , 10 , 5 , 2 , 7 } ; int n2 = arr2 . Length ; x = 9 ; int res2 = smallestSubWithSum ( arr2 , n2 , x ) ; if ( res2 == n2 + 1 ) Console . WriteLine ( " Not ▁ Possible " ) ; else Console . WriteLine ( res2 ) ; int [ ] arr3 = { 1 , 11 , 100 , 1 , 0 , 200 , 3 , 2 , 1 , 250 } ; int n3 = arr3 . Length ; x = 280 ; int res3 = smallestSubWithSum ( arr3 , n3 , x ) ; if ( res3 == n3 + 1 ) Console . WriteLine ( " Not ▁ Possible " ) ; else Console . WriteLine ( res3 ) ; } }
Sum of nodes on the longest path from root to leaf node | c # implementation to find the sum of nodes on the longest path from root to leaf node ; Node of a binary tree ; function to find the sum of nodes on the longest path from root to leaf node ; if true , then we have traversed a root to leaf path ; update maximum length and maximum sum according to the given conditions ; recur for left subtree ; recur for right subtree ; utility function to find the sum of nodes on the longest path from root to leaf node ; if tree is NULL , then sum is 0 ; finding the maximum sum ' maxSum ' for the maximum length root to leaf path ; required maximum sum ; Driver program to test above ; binary tree formation 4 ; / \ ; 2 5 ; / \ / \ ; 7 1 2 3 ; / ; 6
using System ; public class GFG { public class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; left = null ; right = null ; } } public static int maxLen ; public static int maxSum ; public static void sumOfLongRootToLeafPath ( Node root , int sum , int len ) { if ( root == null ) { if ( maxLen < len ) { maxLen = len ; maxSum = sum ; } else if ( maxLen == len && maxSum < sum ) { maxSum = sum ; } return ; } sumOfLongRootToLeafPath ( root . left , sum + root . data , len + 1 ) ; sumOfLongRootToLeafPath ( root . right , sum + root . data , len + 1 ) ; } public static int sumOfLongRootToLeafPathUtil ( Node root ) { if ( root == null ) { return 0 ; } maxSum = int . MinValue ; maxLen = 0 ; sumOfLongRootToLeafPath ( root , 0 , 0 ) ; return maxSum ; } public static void Main ( string [ ] args ) { Node root = new Node ( 4 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 5 ) ; root . left . left = new Node ( 7 ) ; root . left . right = new Node ( 1 ) ; root . right . left = new Node ( 2 ) ; root . right . right = new Node ( 3 ) ; root . left . right . left = new Node ( 6 ) ; Console . WriteLine ( " Sum ▁ = ▁ " + sumOfLongRootToLeafPathUtil ( root ) ) ; } }
Count L | C # program for the above approach ; Fumction to find the number of arrays of length L such that each element divides the next element ; Stores the number of sequences of length i that ends with j ; Initialize 2D array dp [ ] [ ] as 0 ; Base Case ; Iterate over the range [ 0 , l ] ; Iterate for all multiples of j ; Incrementing dp [ i + 1 ] [ k ] by dp [ i ] [ j ] as the next element is multiple of j ; Stores the number of arrays ; Add all array of length L that ends with i ; Return the resultant count ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int numberOfArrays ( int n , int l ) { int [ , ] dp = new int [ l + 1 , n + 1 ] ; for ( int i = 0 ; i < l + 1 ; i ++ ) { for ( int j = 0 ; j < n + 1 ; j ++ ) dp [ i , j ] = 0 ; } dp [ 0 , 1 ] = 1 ; for ( int i = 0 ; i < l ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { for ( int k = j ; k <= n ; k += j ) { dp [ i + 1 , k ] += dp [ i , j ] ; } } } int ans = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { ans += dp [ l , i ] ; } return ans ; } public static void Main ( ) { int N = 2 , L = 4 ; Console . Write ( numberOfArrays ( N , L ) ) ; } }
Count minimum steps to get the given desired array | C # program to count minimum number of operations to get the given arr array ; Returns count of minimum operations to convert a zero array to arr array with increment and doubling operations . This function computes count by doing reverse steps , i . e . , convert arr to zero array . ; Initialize result ( Count of minimum moves ) ; Keep looping while all elements of arr don 't become 0. ; To store count of zeroes in current arr array ; To find first odd element ; If odd number found ; If 0 , then increment zero_count ; All numbers are 0 ; All numbers are even ; Divide the whole array by 2 and increment result ; Make all odd numbers even by subtracting one and increment result . ; Driver Code
using System ; class GFG { static int [ ] arr = new int [ ] { 16 , 16 , 16 } ; static int countMinOperations ( int n ) { int result = 0 ; while ( true ) { int zero_count = 0 ; int i ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 1 ) break ; else if ( arr [ i ] == 0 ) zero_count ++ ; } if ( zero_count == n ) return result ; if ( i == n ) { for ( int j = 0 ; j < n ; j ++ ) arr [ j ] = arr [ j ] / 2 ; result ++ ; } for ( int j = i ; j < n ; j ++ ) { if ( arr [ j ] % 2 == 1 ) { arr [ j ] -- ; result ++ ; } } } } public static void Main ( ) { Console . Write ( " Minimum ▁ number ▁ of ▁ steps ▁ required ▁ to ▁ STRNEWLINE " + " get ▁ the ▁ given ▁ target ▁ array ▁ is ▁ " + countMinOperations ( arr . Length ) ) ; } }
Maximize sum of odd | C # program for the above approach ; Function to find the maximum sum of array elements chosen by Player A according to the given criteria ; Store the key ; Corner Case ; Check if all the elements can be taken or not ; If the difference is less than or equal to the available chances then pick all numbers ; Find the sum of array elements over the range [ start , N ] ; If yes then return that value ; Traverse over the range [ 1 , 2 * M ] ; Sum of elements for Player A ; Even chance sum can be obtained by subtracting the odd chances sum - total and picking up the maximum from that ; Storing the value in dictionary ; Return the maximum sum of odd chances ; Driver code ; Stores the precomputed values ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int recursiveChoosing ( int [ ] arr , int start , int M , Dictionary < Tuple < int , int > , int > dp ) { Tuple < int , int > key = new Tuple < int , int > ( start , M ) ; if ( start >= arr . Length ) { return 0 ; } if ( arr . Length - start <= 2 * M ) { int Sum = 0 ; for ( int i = start ; i < arr . Length ; i ++ ) { Sum = Sum + arr [ i ] ; } return Sum ; } int sum = 0 ; for ( int i = start ; i < arr . Length ; i ++ ) { sum = sum + arr [ i ] ; } int total = sum ; if ( dp . ContainsKey ( key ) ) { return dp [ key ] ; } int psa = 0 ; for ( int x = 1 ; x < 2 * M + 1 ; x ++ ) { int psb = recursiveChoosing ( arr , start + x , Math . Max ( x , M ) , dp ) ; psa = Math . Max ( psa , total - psb ) ; } dp [ key ] = psa ; return dp [ key ] ; } static void Main ( ) { int [ ] arr = { 2 , 7 , 9 , 4 , 4 } ; int N = arr . Length ; Dictionary < Tuple < int , int > , int > dp = new Dictionary < Tuple < int , int > , int > ( ) ; Console . Write ( recursiveChoosing ( arr , 0 , 1 , dp ) ) ; } }
Maximum Sum Subsequence made up of consecutive elements of different parity | C # program for the above approach ; Function to find the maximum sum of subsequence with consecutive terms having different parity ; Base Case ; Store the parity of number at the ith position ; If the dp state has already been calculated , return it ; If the array is traversed and no element has been selected yet then select the current element ; If the parity of the current and previously selected element are different , then select the current element ; Skip the current element and move to the next element ; Return the result ; Function to calculate the maximum sum subsequence with consecutive terms having different parity ; Initialize the dp [ ] array with - 1 ; Initially the prev value is set to say 2 , as the first element can anyways be selected ; Driver code
using System ; class GFG { static int maxSum ( int [ ] arr , int i , int n , int prev , bool is_selected , int [ , ] dp ) { if ( i == n ) { return 0 ; } int cur = Math . Abs ( arr [ i ] ) % 2 ; if ( dp [ i , prev ] != - 1 ) { return dp [ i , prev ] ; } if ( i == n - 1 && is_selected == false ) return dp [ i , prev ] = arr [ i ] ; if ( cur != prev ) { dp [ i , prev ] = arr [ i ] + maxSum ( arr , i + 1 , n , cur , true , dp ) ; } dp [ i , prev ] = Math . Max ( dp [ i , prev ] , maxSum ( arr , i + 1 , n , prev , is_selected , dp ) ) ; return dp [ i , prev ] ; } static void maxSumUtil ( int [ ] arr , int n ) { int [ , ] dp = new int [ 100 , 3 ] ; for ( int i = 0 ; i < 100 ; ++ i ) { for ( int j = 0 ; j < 3 ; ++ j ) { dp [ i , j ] = - 1 ; } } Console . Write ( maxSum ( arr , 0 , n , 2 , false , dp ) ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 6 , 8 , - 5 , 10 } ; int N = arr . Length ; maxSumUtil ( arr , N ) ; } }
Find minimum number of merge operations to make an array palindrome | C # program to find number of operations to make an array palindrome ; Returns minimum number of count operations required to make arr [ ] palindrome ; Initialize result ; Start from two corners ; If corner elements are same , problem reduces arr [ i + 1. . j - 1 ] ; If left element is greater , then we merge right two elements ; need to merge from tail . ; Else we merge left two elements ; Driver Code
using System ; class GFG { static int findMinOps ( int [ ] arr , int n ) { int ans = 0 ; for ( int i = 0 , j = n - 1 ; i <= j ; ) { if ( arr [ i ] == arr [ j ] ) { i ++ ; j -- ; } else if ( arr [ i ] > arr [ j ] ) { j -- ; arr [ j ] += arr [ j + 1 ] ; ans ++ ; } else { i ++ ; arr [ i ] += arr [ i - 1 ] ; ans ++ ; } } return ans ; } public static void Main ( ) { int [ ] arr = new int [ ] { 1 , 4 , 5 , 9 , 1 } ; Console . Write ( " Count ▁ of ▁ minimum ▁ operations ▁ is ▁ " + findMinOps ( arr , arr . Length ) ) ; } }