"Maximize product of subarray sum with its maximum element | C # program for the above approach ; Function to find the maximum product of the sum of the subarray with its maximum element ; Traverse the array arr [ ] ; Increment currSum by a [ i ] ; Maximize the value of currMax ; Maximize the value of largestSum ; If currSum goes less than 0 then update currSum = 0 ; Return the resultant value ; Function to maximize the product of the sum of the subarray with its maximum element ; Find the largest sum of the subarray ; Multiply each array element with - 1 ; Find the largest sum of the subarray with negation of all array element ; Return the resultant maximum value ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int Kadane ( int [ ] arr , int n ) { int largestSum = 0 , currMax = 0 ; int currSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { currSum += arr [ i ] ; currMax = Math . Max ( currMax , arr [ i ] ) ; largestSum = Math . Max ( largestSum , currMax * currSum ) ; if ( currSum < 0 ) { currMax = 0 ; currSum = 0 ; } } return largestSum ; } static int maximumWeight ( int [ ] arr , int n ) { int largestSum = Kadane ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = - arr [ i ] ; } largestSum = Math . Max ( largestSum , Kadane ( arr , n ) ) ; return largestSum ; } public static void Main ( ) { int [ ] arr = { 2 , - 3 , 8 , - 2 , 5 } ; int N = arr . Length ; Console . Write ( maximumWeight ( arr , N ) ) ; } }


"Maximize profit in buying and selling stocks with Rest condition | C # program for the above problem ; If there is only one day for buying and selling no profit can be made ; Array to store Maxprofit by resting on given day ; Array to store Maxprofit by buying or resting on the given day ; Array to store Maxprofit by selling on given day ; Initially there will 0 profit ; Buying on 1 st day results in negative profit ; Zero profit since selling before buying isn 't possible ; max of profit on ( i - 1 ) th day by resting and profit on ( i - 1 ) th day by selling . ; max of profit by resting on ith day and buying on ith day . ; max of profit by selling on ith day ; maxprofit ; Driver code"
Solution in C#:

using System ; class GFG { static int maxProfit ( int [ ] prices , int n ) { if ( n <= 1 ) return 0 ; int [ ] rest = new int [ n ] ; int [ ] hold = new int [ 9 ] ; int [ ] sold = new int [ 9 ] ; rest [ 0 ] = 0 ; hold [ 0 ] = - prices [ 0 ] ; sold [ 0 ] = 0 ; for ( int i = 1 ; i < n ; i ++ ) { rest [ i ] = Math . Max ( rest [ i - 1 ] , sold [ i - 1 ] ) ; hold [ i ] = Math . Max ( hold [ i - 1 ] , rest [ i - 1 ] - prices [ i ] ) ; sold [ i ] = hold [ i - 1 ] + prices [ i ] ; } return Math . Max ( rest [ n - 1 ] , sold [ n - 1 ] ) ; } static void Main ( ) { int [ ] price = { 2 , 4 , 5 , 0 , 2 } ; int n = price . Length ; Console . WriteLine ( maxProfit ( price , n ) ) ; } }


"Maximize profit possible by selling M products such that profit of a product is the number of products left of that supplier | C # program for the above approach ; Function to find the maximum profit by selling M number of products ; Initialize a Max - Heap to keep track of the maximum value ; Stores the maximum profit ; Traverse the array and push all the elements in max_heap ; Iterate a loop until M > 0 ; Decrement the value of M by 1 ; Pop the maximum element from the heap ; Update the maxProfit ; Push ( X - 1 ) to max heap ; Print the result ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void findMaximumProfit ( int [ ] arr , int M , int N ) { List < int > max_heap = new List < int > ( ) ; int maxProfit = 0 ; for ( int i = 0 ; i < N ; i ++ ) max_heap . Add ( arr [ i ] ) ; max_heap . Sort ( ) ; max_heap . Reverse ( ) ; while ( M > 0 ) { M -- ; int X = max_heap [ 0 ] ; max_heap . RemoveAt ( 0 ) ; maxProfit += X ; max_heap . Add ( X - 1 ) ; max_heap . Sort ( ) ; max_heap . Reverse ( ) ; } Console . Write ( maxProfit ) ; } static void Main ( ) { int [ ] arr = { 4 , 6 } ; int M = 4 ; int N = arr . Length ; findMaximumProfit ( arr , M , N ) ; } }


"Maximize profit that can be earned by selling an item among N buyers | C # Program to implement the above approach ; Function to find the maximum profit earned by selling an item among N buyers ; Stores the maximum profit ; Stores the price of the item ; Sort the array ; Traverse the array ; Count of buyers with budget >= arr [ i ] ; Update the maximum profit ; Return the maximum possible price ; Driver Code"
Solution in C#:

using System ; class GFG { public static int maximumProfit ( int [ ] arr ) { int ans = Int32 . MinValue ; int price = 0 ; Array . Sort ( arr ) ; int N = arr . Length ; for ( int i = 0 ; i < N ; i ++ ) { int count = ( N - i ) ; if ( ans < count * arr [ i ] ) { price = arr [ i ] ; ans = count * arr [ i ] ; } } return price ; } public static void Main ( String [ ] args ) { int [ ] arr = { 22 , 87 , 9 , 50 , 56 , 43 } ; Console . WriteLine ( maximumProfit ( arr ) ) ; } }


"Maximize profit that can be earned by selling an item among N buyers | C # program for the above approach ; Function to find the maximum profit earned by selling an item among N buyers ; Stores the maximum profit ; Stores the price of the item ; Traverse the array ; Count of buyers with budget >= arr [ i ] ; Increment count ; Update the maximum profit ; Return the maximum possible price ; Driver Code"
Solution in C#:

using System ; class GFG { public static int maximumProfit ( int [ ] arr ) { int ans = Int32 . MinValue ; int price = 0 ; int n = arr . Length ; for ( int i = 0 ; i < n ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ i ] <= arr [ j ] ) { count ++ ; } } if ( ans < count * arr [ i ] ) { price = arr [ i ] ; ans = count * arr [ i ] ; } } return price ; } public static void Main ( string [ ] args ) { int [ ] arr = { 22 , 87 , 9 , 50 , 56 , 43 } ; Console . Write ( maximumProfit ( arr ) ) ; } }


"Maximize profit when divisibility by two numbers have associated profits | C # implementation of the approach ; Function to return the maximum profit ; min ( x , y ) * n / lcm ( a , b ) ; Driver code"
Solution in C#:

using System ; class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static int maxProfit ( int n , int a , int b , int x , int y ) { int res = x * ( n / a ) ; res += y * ( n / b ) ; res -= Math . Min ( x , y ) * ( n / ( ( a * b ) / __gcd ( a , b ) ) ) ; return res ; } static void Main ( ) { int n = 6 , a = 6 , b = 2 , x = 8 , y = 2 ; Console . WriteLine ( maxProfit ( n , a , b , x , y ) ) ; } }


"Maximize ropes of consecutive length possible by connecting given ropes | C # program for the above approach ; Function to find maximized count of ropes of consecutive length ; Stores the maximum count of ropes of consecutive length ; Sort the ropes by their length ; Traverse the array ; If size of the current rope is less than or equal to current maximum possible size + 1 , update the range to curSize + ropes [ i ] ; If a rope of size ( curSize + 1 ) cannot be obtained ; Driver code ; Input ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int maxConsecutiveRope ( int [ ] ropes , int N ) { int curSize = 0 ; Array . Sort ( ropes ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( ropes [ i ] <= curSize + 1 ) { curSize = curSize + ropes [ i ] ; } else break ; } return curSize ; } public static void Main ( ) { int N = 5 ; int [ ] ropes = { 1 , 2 , 7 , 1 , 1 } ; Console . WriteLine ( maxConsecutiveRope ( ropes , N ) ) ; } }


"Maximize score of same | C # Program for the above approach ; Function to calculate the score of same - indexed subarrays selected from the arrays a [ ] and b [ ] ; Store the required result ; Iterate in the range [ 0 , N - 1 ] ; Consider the case of odd length subarray ; Update the maximum score ; Expanding the subarray in both directions with equal length so that mid point remains same ; Update both the scores ; Consider the case of even length subarray ; Update both the scores ; Print the result ; Driver Code"
Solution in C#:

using System ; public class GFG { static void maxScoreSubArray ( int [ ] a , int [ ] b , int n ) { int res = 0 ; for ( int mid = 0 ; mid < n ; mid ++ ) { int straightScore = a [ mid ] * b [ mid ] , reverseScore = a [ mid ] * a [ mid ] ; int prev = mid - 1 , next = mid + 1 ; res = Math . Max ( res , Math . Max ( straightScore , reverseScore ) ) ; while ( prev >= 0 && next < n ) { straightScore += ( a [ prev ] * b [ prev ] + a [ next ] * b [ next ] ) ; reverseScore += ( a [ prev ] * b [ next ] + a [ next ] * b [ prev ] ) ; res = Math . Max ( res , Math . Max ( straightScore , reverseScore ) ) ; prev -- ; next ++ ; } straightScore = 0 ; reverseScore = 0 ; prev = mid - 1 ; next = mid ; while ( prev >= 0 && next < n ) { straightScore += ( a [ prev ] * b [ prev ] + a [ next ] * b [ next ] ) ; reverseScore += ( a [ prev ] * b [ next ] + a [ next ] * b [ prev ] ) ; res = Math . Max ( res , Math . Max ( straightScore , reverseScore ) ) ; prev -- ; next ++ ; } } Console . WriteLine ( res ) ; } static public void Main ( ) { int [ ] A = { 13 , 4 , 5 } ; int [ ] B = { 10 , 22 , 2 } ; int N = A . Length ; maxScoreSubArray ( A , B , N ) ; } }


"Maximize score of same | C # program for the above approach ; Function to calculate the score of same - indexed subarrays selected from the arrays a [ ] and b [ ] ; Traverse the current subarray ; Finding the score without reversing the subarray ; Calculating the score of the reversed subarray ; Return the score of subarray ; Function to find the subarray with the maximum score ; Stores the maximum score and the starting and the ending point of subarray with maximum score ; Traverse all the subarrays ; Store the score of the current subarray ; Update the maximum score ; Print the maximum score ; Driver Code"
Solution in C#:

using System ; class GFG { static int currSubArrayScore ( int [ ] a , int [ ] b , int l , int r ) { int straightScore = 0 ; int reverseScore = 0 ; for ( int i = l ; i <= r ; i ++ ) { straightScore += a [ i ] * b [ i ] ; reverseScore += a [ r - ( i - l ) ] * b [ i ] ; } return Math . Max ( straightScore , reverseScore ) ; } static void maxScoreSubArray ( int [ ] a , int [ ] b , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { int currScore = currSubArrayScore ( a , b , i , j ) ; if ( currScore > res ) { res = currScore ; } } } Console . Write ( res ) ; } static public void Main ( ) { int [ ] A = { 13 , 4 , 5 } ; int [ ] B = { 10 , 22 , 2 } ; int N = A . Length ; maxScoreSubArray ( A , B , N ) ; } }


"Maximize subsequences having array elements not exceeding length of the subsequence | C # program for the above approach ; Function to calculate the number of subsequences that can be formed ; Stores the number of subsequences ; Iterate over the map ; Count the number of subsequences that can be formed from x . first ; Number of occurrences of x . first which are left ; Return the number of subsequences ; Function to create the maximum count of subsequences that can be formed ; Stores the frequency of [ ] arr ; Update the frequency ; Print the number of subsequences ; Driver code ; Given array [ ] arr ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static int No_Of_subsequences ( Dictionary < int , int > mp ) { int count = 0 ; int left = 0 ; foreach ( KeyValuePair < int , int > x in mp ) { if ( ! mp . ContainsKey ( x . Key ) ) mp . Add ( x . Key , x . Value + left ) ; count += ( x . Value / x . Key ) ; left = x . Value % x . Key ; } return count ; } public static void maximumsubsequences ( int [ ] arr , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1 ; } else { mp . Add ( arr [ i ] , 1 ) ; } } Console . WriteLine ( No_Of_subsequences ( mp ) ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 1 , 1 } ; int N = arr . Length ; maximumsubsequences ( arr , N ) ; } }


"Maximize sum by selecting M elements from the start or end of rows of a Matrix | C # program for the above approach ; Function to select m elements having maximum sum ; Base case ; If precomputed subproblem occurred ; Either skip the current row ; Iterate through all the possible segments of current row ; Check if it is possible to select elements from i to j ; Compuete the sum of i to j as calculated ; Store the computed answer and return ; Function to precompute the prefix sum for every row of the matrix ; Preprocessing to calculate sum from i to j ; Utility function to select m elements having maximum sum ; Preprocessing step ; Initialize dp array with - 1 ; Stores maximum sum of M elements ;  ; Driver Code ; Given N ; Given M ; Given matrix ; Function Call"
Solution in C#:

using System ; class GFG { public static int mElementsWithMaxSum ( int [ , ] matrix , int M , int block , int [ , ] dp ) { if ( block == matrix . GetLength ( 0 ) ) return 0 ; if ( dp [ block , M ] != - 1 ) return dp [ block , M ] ; int ans = mElementsWithMaxSum ( matrix , M , block + 1 , dp ) ; for ( int i = 0 ; i < GetRow ( matrix , block ) . Length ; i ++ ) { for ( int j = i ; j < GetRow ( matrix , block ) . Length ; j ++ ) { if ( j - i + 1 <= M ) { ans = Math . Max ( ans , matrix [ block , j ] - ( ( i - 1 ) >= 0 ? matrix [ block , i - 1 ] : 0 ) + mElementsWithMaxSum ( matrix , M - j + i - 1 , block + 1 , dp ) ) ; } } } return dp [ block , M ] = ans ; } public static void preComputing ( int [ , ] matrix , int N ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < GetRow ( matrix , i ) . Length ; j ++ ) { matrix [ i , j ] = ( j > 0 ? matrix [ i , j - 1 ] : 0 ) + matrix [ i , j ] ; } } } public static void mElementsWithMaxSumUtil ( int [ , ] matrix , int M , int N ) { preComputing ( matrix , N ) ; int [ , ] dp = new int [ N + 5 , M + 5 ] ; for ( int i = 0 ; i < N + 5 ; i ++ ) { for ( int j = 0 ; j < M + 5 ; j ++ ) { dp [ i , j ] = - 1 ; } } int sum = mElementsWithMaxSum ( matrix , M , 0 , dp ) ; Console . Write ( sum ) ; } 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 ; } public static void Main ( String [ ] args ) { int N = 3 ; int M = 4 ; int [ , ] matrix = { { 2 , 3 , 5 } , { - 1 , 7 , 0 } , { 8 , 10 , 0 } } ; mElementsWithMaxSumUtil ( matrix , M , N ) ; } }


"Maximize sum by selecting X different | C # program for the above approach ; Function to find maximum sum of at most N with different index array elements such that at most X are from [ ] A , Y are from [ ] B and Z are from C [ ] ; Base Cases ; Selecting i - th element from [ ] A ; Selecting i - th element from [ ] B ; Selecting i - th element from C [ ] ; i - th elements not selected from any of the arrays ; Select the maximum sum from all the possible calls ; Driver Code ; Given X , Y and Z ; Given [ ] A ; Given [ ] B ; Given C [ ] ; Given Size ; Function Call"
Solution in C#:

using System ; class GFG { static int FindMaxS ( int X , int Y , int Z , int n , int [ ] A , int [ ] B , int [ ] C ) { if ( X < 0 Y < 0 Z < 0 ) return int . MinValue ; if ( n < 0 ) return 0 ; int ch = A [ n ] + FindMaxS ( X - 1 , Y , Z , n - 1 , A , B , C ) ; int ca = B [ n ] + FindMaxS ( X , Y - 1 , Z , n - 1 , A , B , C ) ; int co = C [ n ] + FindMaxS ( X , Y , Z - 1 , n - 1 , A , B , C ) ; int no = FindMaxS ( X , Y , Z , n - 1 , A , B , C ) ; int maximum = Math . Max ( ch , Math . Max ( ca , Math . Max ( co , no ) ) ) ; return maximum ; } public static void Main ( String [ ] args ) { int X = 1 ; int Y = 1 ; int Z = 1 ; int [ ] A = { 10 , 0 , 5 } ; int [ ] B = { 5 , 10 , 0 } ; int [ ] C = { 0 , 5 , 10 } ; int n = B . Length ; Console . WriteLine ( FindMaxS ( X , Y , Z , n - 1 , A , B , C ) ) ; } }


"Maximize sum by selecting X different | C # program for the above approach ; Store overlapping subproblems of the recurrence relation ; Function to find maximum sum of at most N with different index array elements such that at most X are from A [ ] , Y are from B [ ] and Z are from C [ ] ; Base Cases ; If the subproblem already computed ; Selecting i - th element from A [ ] ; Selecting i - th element from B [ ] ; Selecting i - th element from C [ ] ; i - th elements not selected from any of the arrays ; Select the maximum sum from all the possible calls ; Driver Code ; Given X , Y and Z ; Given A [ ] ; Given B [ ] ; Given C [ ] ; Given Size ; Function Call"
Solution in C#:

using System ; class GFG { static int [ , , , ] dp = new int [ 50 , 50 , 50 , 50 ] ; static int FindMaxS ( int X , int Y , int Z , int n , int [ ] A , int [ ] B , int [ ] C ) { if ( X < 0 Y < 0 Z < 0 ) return Int32 . MinValue ; if ( n < 0 ) return 0 ; if ( dp [ n , X , Y , Z ] != - 1 ) { return dp [ n , X , Y , Z ] ; } int ch = A [ n ] + FindMaxS ( X - 1 , Y , Z , n - 1 , A , B , C ) ; int ca = B [ n ] + FindMaxS ( X , Y - 1 , Z , n - 1 , A , B , C ) ; int co = C [ n ] + FindMaxS ( X , Y , Z - 1 , n - 1 , A , B , C ) ; int no = FindMaxS ( X , Y , Z , n - 1 , A , B , C ) ; int maximum = Math . Max ( ch , Math . Max ( ca , Math . Max ( co , no ) ) ) ; return dp [ n , X , Y , Z ] = maximum ; } public static void Main ( string [ ] args ) { int X = 1 ; int Y = 1 ; int Z = 1 ; int [ ] A = { 10 , 0 , 5 } ; int [ ] B = { 5 , 10 , 0 } ; int [ ] C = { 0 , 5 , 10 } ; int n = B . Length ; for ( int i = 0 ; i < 50 ; i ++ ) for ( int j = 0 ; j < 50 ; j ++ ) for ( int k = 0 ; k < 50 ; k ++ ) for ( int l = 0 ; l < 50 ; l ++ ) dp [ i , j , k , l ] = - 1 ; Console . Write ( FindMaxS ( X , Y , Z , n - 1 , A , B , C ) ) ; } }


"Maximize sum by traversing diagonally from each cell of a given Matrix | C # program for the above approach ; Function to find the maximum sum ; Loop to traverse through the upper triangular matrix and update the maximum sum to ans ; Traverse through the lower triangular matrix ; Driver Code ; Given matrix ; Given dimension"
Solution in C#:

using System ; class GFG { static int MaximumSum ( int [ , ] arr , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int x = 0 , y = i , sum = 0 ; for ( int j = i ; j < n ; j ++ ) { sum += arr [ x ++ , y ++ ] ; } if ( sum > ans ) ans = sum ; } for ( int i = 1 ; i < n ; i ++ ) { int x = i , y = 0 , sum = 0 ; for ( int j = i ; j < n ; j ++ ) { sum += arr [ x ++ , y ++ ] ; } if ( sum > ans ) ans = sum ; } return ans ; } public static void Main ( String [ ] args ) { int [ , ] arr = { { 1 , 2 , 3 } , { 3 , 5 , 10 } , { 1 , 3 , 5 } } ; int n = arr . GetLength ( 0 ) ; Console . Write ( MaximumSum ( arr , n ) ) ; } }


"Maximize sum of Bitwise AND of same | C # program for the above approach ; Function to calculate sum of Bitwise AND of same indexed elements of the arrays p [ ] and arr [ ] ; Stores the resultant sum ; Traverse the array ; Update sum of Bitwise AND ; Return the value obtained ; Function to generate all permutations and calculate the maximum sum of Bitwise AND of same indexed elements present in any permutation and an array arr [ ] ; If the size of the array is N ; Calculate cost of permutation ; Generate all permutations ; Update chosen [ i ] ; Update the permutation p [ ] ; Generate remaining permutations ; Return the resultant sum ; Function to find the maximum sum of Bitwise AND of same indexed elements in a permutation of first N natural numbers and arr [ ] ; Stores the resultant maximum sum ; Stores the generated permutation P ; Function call to store result ; Print the result ; Driver Code ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int calcScore ( List < int > p , int [ ] arr ) { int ans = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { ans += ( p [ i ] & arr [ i ] ) ; } return ans ; } static int getMaxUtil ( List < int > p , int [ ] arr , int ans , bool [ ] chosen , int N ) { if ( p . Count == N ) { ans = Math . Max ( ans , calcScore ( p , arr ) ) ; return ans ; } for ( int i = 0 ; i < N ; i ++ ) { if ( chosen [ i ] ) { continue ; } chosen [ i ] = true ; p . Add ( i ) ; ans = getMaxUtil ( p , arr , ans , chosen , N ) ; chosen [ i ] = false ; p . Remove ( p . Count - 1 ) ; } return ans ; } static void getMax ( int [ ] arr , int N ) { int ans = 0 ; bool [ ] chosen = new bool [ N ] ; List < int > p = new List < int > ( ) ; int res = getMaxUtil ( p , arr , ans , chosen , N ) ; Console . Write ( res ) ; } public static void Main ( ) { int [ ] arr = { 4 , 2 , 3 , 6 } ; int N = arr . Length ; getMax ( arr , N ) ; } }


"Maximize sum of K corner elements in Array | C # program to maximize the sum of K elements in the array by taking only corner elements ; Function to return maximum sum ; Base case ; Pick the start index ; Pick the end index ; Recursive function call ; Return the readonly answer ; Function to find the maximized sum ; Driver code"
Solution in C#:

using System ; class GFG { static int maxSum ( int [ ] arr , int K , int start , int end , int max_sum ) { if ( K == 0 ) return max_sum ; int max_sum_start = max_sum + arr [ start ] ; int max_sum_end = max_sum + arr [ end ] ; int ans = Math . Max ( maxSum ( arr , K - 1 , start + 1 , end , max_sum_start ) , maxSum ( arr , K - 1 , start , end - 1 , max_sum_end ) ) ; return ans ; } static void maximizeSum ( int [ ] arr , int K , int n ) { int max_sum = 0 ; int start = 0 ; int end = n - 1 ; Console . Write ( maxSum ( arr , K , start , end , max_sum ) ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 8 , 4 , 4 , 8 , 12 , 3 , 2 , 9 } ; int K = 3 ; int n = arr . Length ; maximizeSum ( arr , K , n ) ; } }


"Maximize sum of K corner elements in Array | C # program to maximize the sum of K elements in the array by taking only corner elements ; Function to return maximum sum ; Initialize variables ; Iterate over first K elements of array and update the value for curr_points ; Update value for max_points ; j points to the end of the array ; Return the readonly result ; Driver code"
Solution in C#:

using System ; class GFG { public static int maxPointCount ( int [ ] arr , int K , int size ) { int curr_points = 0 ; int max_points = 0 ; for ( int i = 0 ; i < K ; i ++ ) curr_points += arr [ i ] ; max_points = curr_points ; int j = size - 1 ; for ( int i = K - 1 ; i >= 0 ; i -- ) { curr_points = curr_points + arr [ j ] - arr [ i ] ; max_points = Math . Max ( curr_points , max_points ) ; j -- ; } return max_points ; } public static void Main ( String [ ] args ) { int [ ] arr = { 8 , 4 , 4 , 8 , 12 , 3 , 2 , 9 } ; int K = 3 ; int n = arr . Length ; Console . Write ( maxPointCount ( arr , K , n ) ) ; } }


"Maximize sum of K elements selected from a Matrix such that each selected element must be preceded by selected row elements | C # program for the above approach ; Function to return the maximum of two elements ; Function to find the maximum sum of selecting K elements from the given 2D array [ , ] arr ; dp table of size ( K + 1 ) * ( N + 1 ) ; Initialize dp [ 0 , i ] = 0 ; Initialize dp [ i , 0 ] = 0 ; Selecting i elements ; Select i elements till jth row ; sum = 0 , to keep track of cummulative elements sum ; Traverse arr [ j , k ] until number of elements until k > i ; Select arr [ j , k - 1 ] th item ; Store the maxSum in dp [ i , j + 1 ] ; Return the maximum sum ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static int max ( int a , int b ) { return a > b ? a : b ; } static int maximumsum ( int [ , ] arr , int K , int N , int M ) { int sum = 0 , maxSum ; int i , j , k ; int [ , ] dp = new int [ K + 1 , N + 1 ] ; for ( i = 0 ; i <= N ; i ++ ) dp [ 0 , i ] = 0 ; for ( i = 0 ; i <= K ; i ++ ) dp [ i , 0 ] = 0 ; for ( i = 1 ; i <= K ; i ++ ) { for ( j = 0 ; j < N ; j ++ ) { sum = 0 ; maxSum = dp [ i , j ] ; for ( k = 1 ; k <= M && k <= i ; k ++ ) { sum += arr [ j , k - 1 ] ; maxSum = Math . Max ( maxSum , sum + dp [ i - k , j ] ) ; } dp [ i , j + 1 ] = maxSum ; } } return dp [ K , N ] ; } public static void Main ( String [ ] args ) { int [ , ] arr = { { 10 , 10 , 100 , 30 } , { 80 , 50 , 10 , 50 } } ; int N = 2 , M = 4 ; int K = 5 ; Console . Write ( maximumsum ( arr , K , N , M ) ) ; } }


"Maximize sum of MEX values of each node in an N | C # program for the above approach ; Function to create an N - ary Tree ; Traverse the edges ; Add edges ; Function to get the maximum sum of MEX values of tree rooted at 1 ; Initialize mex ; Iterate through all children of node ; Recursively find maximum sum of MEX values of each node in tree rooted at u ; Store the maximum sum of MEX of among all subtrees ; Increase the size of tree rooted at current node ; Resulting MEX for the current node of the recursive call ; Driver Code ; Given N nodes ; Given N - 1 edges ; Stores the tree ; Generates the tree ; Returns maximum sum of MEX values of each node"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void makeTree ( List < int > [ ] tree , pair [ ] edges , int N ) { for ( int i = 0 ; i < N - 1 ; i ++ ) { int u = edges [ i ] . first ; int v = edges [ i ] . second ; tree [ u ] . Add ( v ) ; } } static pair dfs ( int node , List < int > [ ] tree ) { int mex = 0 ; int size = 1 ; foreach ( int u in tree [ node ] ) { pair temp = dfs ( u , tree ) ; mex = Math . Max ( mex , temp . first ) ; size += temp . second ; } return new pair ( mex + size , size ) ; } public static void Main ( String [ ] args ) { int N = 7 ; pair [ ] edges = { new pair ( 1 , 4 ) , new pair ( 1 , 5 ) , new pair ( 5 , 2 ) , new pair ( 5 , 3 ) , new pair ( 4 , 7 ) , new pair ( 7 , 6 ) } ; List < int > [ ] tree = new List < int > [ N + 1 ] ; for ( int i = 0 ; i < tree . Length ; i ++ ) tree [ i ] = new List < int > ( ) ; makeTree ( tree , edges , N ) ; Console . Write ( ( dfs ( 1 , tree ) . first ) ) ; } }


"Maximize sum of all elements which are not a part of the Longest Increasing Subsequence | C # program to find the Maximum sum of all elements which are not a part of longest increasing sub sequence ; Function to find maximum sum ; Find total sum of array ; Maintain a 2D array ; Update the dp array along with sum in the second row ; In case of greater length Update the length along with sum ; In case of equal length find length update length with minimum sum ; Find the sum that need to be subtracted from total sum ; Return the sum ; Driver code"
Solution in C#:

using System ; class GFG { static int findSum ( int [ ] arr , int n ) { int totalSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { totalSum += arr [ i ] ; } int [ , ] dp = new int [ 2 , n ] ; for ( int i = 0 ; i < n ; i ++ ) { dp [ 0 , i ] = 1 ; dp [ 1 , i ] = arr [ i ] ; } for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( arr [ i ] > arr [ j ] ) { if ( dp [ 0 , i ] < dp [ 0 , j ] + 1 ) { dp [ 0 , i ] = dp [ 0 , j ] + 1 ; dp [ 1 , i ] = dp [ 1 , j ] + arr [ i ] ; } else if ( dp [ 0 , i ] == dp [ 0 , j ] + 1 ) { dp [ 1 , i ] = Math . Min ( dp [ 1 , i ] , dp [ 1 , j ] + arr [ i ] ) ; } } } } int maxm = 0 ; int subtractSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( dp [ 0 , i ] > maxm ) { maxm = dp [ 0 , i ] ; subtractSum = dp [ 1 , i ] ; } else if ( dp [ 0 , i ] == maxm ) { subtractSum = Math . Min ( subtractSum , dp [ 1 , i ] ) ; } } return totalSum - subtractSum ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 6 , 1 , 2 , 3 , 8 } ; int n = arr . Length ; Console . Write ( findSum ( arr , n ) ) ; } }


"Maximize sum of an Array by flipping sign of all elements of a single subarray | C # program for the above approach ; Function to find the maximum sum after flipping a subarray ; Stores the total sum of array ; Initialize the maximum sum ; Iterate over all possible subarrays ; Initialize sum of the subarray before flipping sign ; Initialize sum of subarray after flipping sign int flip_sum = 0 ; ; Calculate the sum of original subarray ; Subtract the original subarray sum and add the flipped subarray sum to the total sum ; Return the max_sum ; Driver Code ; Function call"
Solution in C#:

using System ; class GFG { public static int maxSumFlip ( int [ ] a , int n ) { int total_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) total_sum += a [ i ] ; int max_sum = int . MinValue ; for ( int i = 0 ; i < n ; i ++ ) { int sum = 0 ; for ( int j = i ; j < n ; j ++ ) { sum += a [ j ] ; max_sum = Math . Max ( max_sum , total_sum - 2 * sum ) ; } } return Math . Max ( max_sum , total_sum ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { - 2 , 3 , - 1 , - 4 , - 2 } ; int N = arr . Length ; Console . WriteLine ( maxSumFlip ( arr , N ) ) ; } }


"Maximize sum of array by repeatedly removing an element from pairs whose concatenation is a multiple of 3 | C # program for the above approach ; Function to calculate sum of digits of an integer ; Function to calculate maximum sum of array after removing pairs whose concatenation is divisible by 3 ; Stores the sum of digits of array element ; Find the sum of digits ; If i is divisible by 3 ; Otherwise , if i modulo 3 is 1 ; Otherwise , if i modulo 3 is 2 ; Return the resultant sum of array elements ; Driver Code"
Solution in C#:

using System ; class GFG { static int getSum ( int n ) { int ans = 0 ; string str = n . ToString ( ) ; Char [ ] arr = str . ToCharArray ( ) ; foreach ( Char ch in arr ) { ans += ( int ) Char . GetNumericValue ( ch ) ; } return ans ; } static void getMax ( int [ ] arr ) { int maxRem0 = 0 ; int rem1 = 0 ; int rem2 = 0 ; foreach ( int i in arr ) { int digitSum = getSum ( i ) ; if ( digitSum % 3 == 0 ) maxRem0 = Math . Max ( maxRem0 , i ) ; else if ( digitSum % 3 == 1 ) rem1 += i ; else rem2 += i ; } Console . WriteLine ( maxRem0 + Math . Max ( rem1 , rem2 ) ) ; } static void Main ( ) { int [ ] arr = { 23 , 12 , 43 , 3 , 56 } ; getMax ( arr ) ; } }


"Maximize sum of array elements removed by performing the given operations | C # program for the above approach ; Function to find the maximum sum of the array arr [ ] where each element can be reduced to at most min [ i ] ; Stores the pair of arr [ i ] & min [ i ] ; Sorting vector of pairs ; Traverse the vector of pairs ; Add to the value of S ; Update K ; Driver code ; Given array arr [ ] , min [ ] ; Given K ; Function Call ; Print the value of S"
Solution in C#:

using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static int S ; static void findMaxSum ( int [ ] arr , int n , int [ ] min , int k ) { List < List < int > > A = new List < List < int > > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { A . Add ( new List < int > ( ) ) ; A [ i ] . Add ( arr [ i ] ) ; A [ i ] . Add ( min [ i ] ) ; } A = A . OrderBy ( lst => lst [ 0 ] ) . ToList ( ) ; A . Reverse ( ) ; int K = 0 ; for ( int i = 0 ; i < n ; i ++ ) { S += Math . Max ( A [ i ] [ 0 ] - K , A [ i ] [ 1 ] ) ; K += k ; } } static public void Main ( ) { int [ ] arr = { 3 , 5 , 2 , 1 } ; int [ ] min = { 3 , 2 , 1 , 3 } ; int N = arr . Length ; int K = 3 ; S = 0 ; findMaxSum ( arr , N , min , K ) ; Console . WriteLine ( S ) ; } }


"Maximize sum of averages of subsequences of lengths lying in a given range | C # program for the above approach ; Function to find the maximum sum of average of groups ; Sort the given array ; Stores the sum of averages ; Stores count of array element ; Add the current value to the variable sum ; Increment the count by 1 ; If the current size is X ; If the remaining elements can 't become a group ; Iterate until i is less than N ; Update the value of X ; Update the average ; Find the average ; Reset the sum and count ; Print maximum sum of averages ; Driver Code"
Solution in C#:

using System ; public class GFG { static void maxAverage ( int [ ] A , int N , int X , int Y ) { Array . Sort ( A ) ; int sum = 0 ; double res = 0 ; int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += A [ i ] ; count ++ ; if ( count == X ) { if ( N - i - 1 < X ) { i ++ ; int cnt = 0 ; while ( i < N ) { cnt ++ ; sum += A [ i ] ; i ++ ; } X = X + cnt ; res += ( double ) sum / ( double ) ( X ) ; break ; } res += ( double ) sum / ( double ) ( X ) ; sum = 0 ; count = 0 ; } } Console . WriteLine ( res ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 4 , 10 , 6 , 5 } ; int N = A . Length ; int X = 2 , Y = 3 ; maxAverage ( A , N , X , Y ) ; } }


"Maximize sum of consecutive differences in a circular array | C # program to maximize the sum of difference between consecutive elements in circular array ; Return the maximum Sum of difference between consecutive elements . ; Sorting the array . ; Subtracting a1 , a2 , a3 , ... . . , a ( n / 2 ) - 1 , an / 2 twice and adding a ( n / 2 ) + 1 , a ( n / 2 ) + 2 , a ( n / 2 ) + 3 , ... . . , an - 1 , an twice . ; Driver Program"
Solution in C#:

using System ; class MaxSum { static int maxSum ( int [ ] arr , int n ) { int sum = 0 ; Array . Sort ( arr ) ; for ( int i = 0 ; i < n / 2 ; i ++ ) { sum -= ( 2 * arr [ i ] ) ; sum += ( 2 * arr [ n - i - 1 ] ) ; } return sum ; } public static void Main ( ) { int [ ] arr = { 4 , 2 , 1 , 8 } ; int n = arr . Length ; Console . WriteLine ( maxSum ( arr , n ) ) ; } }


"Maximize sum of count of distinct prime factors of K array elements | Function to find the maximum sum of count of distinct prime factors of K array elements ; Stores the count of distinct primes ; Stores 1 and 0 at prime and non - prime indices respectively ; Initialize the count of factors to 0 ; Sieve of Eratosthenes ; Count of factors of a prime number is 1 ; Increment CountDistinct of all multiples of i ; Mark its multiples non - prime ; Stores the maximum sum of distinct prime factors of K array elements ; Stores the count of all distinct prime factors ; Traverse the array to find count of all array elements ; Maximum sum of K prime factors of array elements ; Check for the largest prime factor ; Increment sum ; Decrement its count and K ; Print the maximum sum ; Driver code ; Given array ; Size of the array ; Given value of K"
Solution in C#:

using System ; public class GFG { public static int MAX = 1000000 ; static void maxSumOfDistinctPrimeFactors ( int [ ] arr , int N , int K ) { int [ ] CountDistinct = new int [ MAX + 1 ] ; bool [ ] prime = new bool [ MAX + 1 ] ; for ( int i = 0 ; i <= MAX ; i ++ ) { CountDistinct [ i ] = 0 ; prime [ i ] = true ; } for ( int i = 2 ; i <= MAX ; i ++ ) { if ( prime [ i ] == true ) { CountDistinct [ i ] = 1 ; for ( int j = i * 2 ; j <= MAX ; j += i ) { CountDistinct [ j ] ++ ; prime [ j ] = false ; } } } int sum = 0 ; int [ ] PrimeFactor = new int [ 20 ] ; for ( int i = 0 ; i < N ; i ++ ) { PrimeFactor [ CountDistinct [ arr [ i ] ] ] ++ ; } for ( int i = 19 ; i >= 1 ; i -- ) { while ( PrimeFactor [ i ] > 0 ) { sum += i ; PrimeFactor [ i ] -- ; K -- ; if ( K == 0 ) break ; } if ( K == 0 ) break ; } Console . Write ( sum ) ; } static public void Main ( ) { int [ ] arr = { 6 , 9 , 12 } ; int N = arr . Length ; int K = 2 ; maxSumOfDistinctPrimeFactors ( arr , N , K ) ; } }


"Maximize sum of diagonal of a matrix by rotating all rows or all columns | C # program to implement the above approach ; Function to find maximum sum of diagonal elements of matrix by rotating either rows or columns ; Stores maximum diagonal sum of elements of matrix by rotating rows or columns ; Rotate all the columns by an integer in the range [ 0 , N - 1 ] ; Stores sum of diagonal elements of the matrix ; Calculate sum of diagonal elements of the matrix ; Update curr ; Update maxDiagonalSum ; Rotate all the rows by an integer in the range [ 0 , N - 1 ] ; Stores sum of diagonal elements of the matrix ; Calculate sum of diagonal elements of the matrix ; Update curr ; Update maxDiagonalSum ; Driver Code"
Solution in C#:

using System ; class GFG { static int N = 3 ; static int findMaximumDiagonalSumOMatrixf ( int [ , ] A ) { int maxDiagonalSum = Int32 . MinValue ; for ( int i = 0 ; i < N ; i ++ ) { int curr = 0 ; for ( int j = 0 ; j < N ; j ++ ) { curr += A [ j , ( i + j ) % N ] ; } maxDiagonalSum = Math . Max ( maxDiagonalSum , curr ) ; } for ( int i = 0 ; i < N ; i ++ ) { int curr = 0 ; for ( int j = 0 ; j < N ; j ++ ) { curr += A [ ( i + j ) % N , j ] ; } maxDiagonalSum = Math . Max ( maxDiagonalSum , curr ) ; } return maxDiagonalSum ; } public static void Main ( ) { int [ , ] mat = { { 1 , 1 , 2 } , { 2 , 1 , 2 } , { 1 , 2 , 2 } } ; Console . Write ( findMaximumDiagonalSumOMatrixf ( mat ) ) ; } }


"Maximize sum of given array by rearranging array such that the difference between adjacent elements is atmost 1 | C # program for the above approach ; Function to find maximum possible sum after changing the array elements as per the given constraints ; Length of given array ; Stores the frequency of elements in given array ; Update frequency ; stores the previously selected integer ; Stores the maximum possible sum ; Traverse over array count [ ] ; Run loop for each k ; Update ans ; Return maximum possible sum ; Driver Code ; Given array arr [ ] ; Function call"
Solution in C#:

using System ; class GFG { static long maxSum ( int [ ] a ) { int n = a . Length ; int [ ] count = new int [ n + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) count [ Math . Min ( a [ i ] , n ) ] ++ ; int size = 0 ; long ans = 0 ; for ( int k = 1 ; k <= n ; k ++ ) { while ( count [ k ] > 0 && size < k ) { size ++ ; ans += size ; count [ k ] -- ; } ans += k * count [ k ] ; } return ans ; } public static void Main ( ) { int [ ] arr = { 3 , 5 , 1 } ; Console . Write ( maxSum ( arr ) ) ; } }


"Maximize sum of minimum difference of divisors of nodes in N | C # program to maximize the sum of minimum difference of divisors of nodes in an n - ary tree ; Array to store the result at each node ; Function to get minimum difference between the divisors of a number ; Iterate from square root of N to N ; return absolute difference ; DFS function to calculate the maximum sum ; Store the min difference ; Add the maximum of all children to sub [ u ] ; Return maximum sum of node ' u ' to its parent ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int [ ] sub = new int [ 100005 ] ; static int minDivisorDifference ( int n ) { int num1 = 0 ; int num2 = 0 ; for ( int i = ( int ) Math . Sqrt ( n ) ; i <= n ; i ++ ) { if ( n % i == 0 ) { num1 = i ; num2 = n / i ; break ; } } return Math . Abs ( num1 - num2 ) ; } static int dfs ( List < int > [ ] g , int u , int par ) { sub [ u ] = minDivisorDifference ( u ) ; int mx = 0 ; foreach ( int c in g [ u ] ) { if ( c != par ) { int ans = dfs ( g , c , u ) ; mx = Math . Max ( mx , ans ) ; } } sub [ u ] += mx ; return sub [ u ] ; } public static void Main ( String [ ] args ) { List < int > [ ] g = new List < int > [ 100005 ] ; for ( int i = 0 ; i < g . Length ; i ++ ) g [ i ] = new List < int > ( ) ; int edges = 6 ; g [ 18 ] . Add ( 7 ) ; g [ 7 ] . Add ( 18 ) ; g [ 18 ] . Add ( 15 ) ; g [ 15 ] . Add ( 18 ) ; g [ 15 ] . Add ( 2 ) ; g [ 2 ] . Add ( 15 ) ; g [ 7 ] . Add ( 4 ) ; g [ 4 ] . Add ( 7 ) ; g [ 7 ] . Add ( 12 ) ; g [ 12 ] . Add ( 7 ) ; g [ 12 ] . Add ( 9 ) ; g [ 9 ] . Add ( 12 ) ; int root = 18 ; Console . Write ( dfs ( g , root , - 1 ) ) ; } }


"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"
Solution in C#:

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 ) ) ; } }


"Maximize sum of path from the Root to a Leaf node in N | C # program for the above approach ; Stores the maximum sum of a path ; Structure of a node in the tree ; Utility function to create a new node in the tree ; Recursive function to calculate the maximum sum in a path using DFS ; If current node is a leaf node ; Traversing all children of the current node ; Recursive call for all the children nodes ; Given Generic Tree ; Function Call"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int maxSumPath = 0 ; class Node { public int val ; public List < Node > child ; public Node ( int key ) { val = key ; child = new List < Node > ( ) ; } } static Node newNode ( int key ) { Node temp = new Node ( key ) ; return temp ; } static void DFS ( Node root , int sum ) { if ( root . child . Count == 0 ) { maxSumPath = Math . Max ( maxSumPath , sum ) ; return ; } for ( int i = 0 ; i < root . child . Count ; i ++ ) { DFS ( root . child [ i ] , sum + root . child [ i ] . val ) ; } } static void Main ( ) { Node root = newNode ( 1 ) ; ( root . child ) . Add ( newNode ( 2 ) ) ; ( root . child ) . Add ( newNode ( 3 ) ) ; ( root . child [ 0 ] . child ) . Add ( newNode ( 4 ) ) ; ( root . child [ 1 ] . child ) . Add ( newNode ( 6 ) ) ; ( root . child [ 0 ] . child ) . Add ( newNode ( 5 ) ) ; ( root . child [ 1 ] ) . child . Add ( newNode ( 7 ) ) ; ( root . child [ 1 ] . child ) . Add ( newNode ( 8 ) ) ; DFS ( root , root . val ) ; Console . Write ( maxSumPath ) ; } }


"Maximize sum of product of neighbouring elements of the element removed from Array | C # program for the above approach ; Stores the dp state where dp [ i ] [ j ] represents the maximum possible score in the subarray from index i to j ; Iterate through all possible lengths of the subarray ; Iterate through all the possible starting indices i having length len ; Stores the rightmost index of the current subarray ; Initial dp [ i ] [ j ] will be 0. ; Iterate through all possible values of k in range [ i + 1 , j - 1 ] ; Return the answer ; Driver Code ; Function Call ; Function Call"
Solution in C#:

using System ; public class GFG { static int maxMergingScore ( int [ ] A , int N ) { int [ , ] dp = new int [ 101 , 101 ] ; for ( int i = 0 ; i < 101 ; i ++ ) { { for ( int j = 0 ; j < 101 ; j ++ ) dp [ i , j ] = 0 ; } } for ( int len = 1 ; len < N ; ++ len ) { for ( int i = 0 ; i + len < N ; ++ i ) { int j = i + len ; dp [ i , j ] = 0 ; for ( int k = i + 1 ; k < j ; ++ k ) { dp [ i , j ] = Math . Max ( dp [ i , j ] , dp [ i , k ] + dp [ k , j ] + A [ i ] * A [ j ] ) ; } } } return dp [ 0 , N - 1 ] ; } static public void Main ( ) { int N = 4 ; int [ ] A = { 1 , 2 , 3 , 4 } ; Console . WriteLine ( maxMergingScore ( A , N ) ) ; N = 2 ; int [ ] B = { 1 , 55 } ; Console . WriteLine ( maxMergingScore ( B , N ) ) ; } }


"Maximize sum of remaining elements after every removal of the array half with greater sum | C # program to implement the above approach ; Function to find the maximum sum ; Base Case ; Check if ( mapped key is found in the dictionary ; Traverse the array ; Store left prefix sum ; Store right prefix sum ; Compare the left and right values ; Store the value in dp array ; Return the readonly answer ; Function to print maximum sum ; Stores prefix sum ; Store results of subproblems ; Traversing the array ; Add prefix sum of array ; Print the answer ; Driver Code ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int [ , ] dp = new int [ 100 , 100 ] ; static int maxweight ( int s , int e , Dictionary < int , int > pre ) { if ( s == e ) return 0 ; if ( dp [ s , e ] != - 1 ) return dp [ s , e ] ; int ans = 0 ; for ( int i = s ; i < e ; i ++ ) { int left = pre [ i ] - pre [ s - 1 ] ; int right = pre [ e ] - pre [ i ] ; if ( left < right ) ans = Math . Max ( ans , ( int ) ( left + maxweight ( s , i , pre ) ) ) ; if ( left == right ) ans = Math . Max ( ans , Math . Max ( left + maxweight ( s , i , pre ) , right + maxweight ( i + 1 , e , pre ) ) ) ; if ( left > right ) ans = Math . Max ( ans , right + maxweight ( i + 1 , e , pre ) ) ; dp [ s , e ] = ans ; } return dp [ s , e ] ; } static void maxSum ( int [ ] arr , int n ) { Dictionary < int , int > pre = new Dictionary < int , int > ( ) ; pre . Add ( - 1 , 0 ) ; pre . Add ( 0 , arr [ 0 ] ) ; for ( int i = 0 ; i < 100 ; i ++ ) { for ( int j = 0 ; j < 100 ; j ++ ) dp [ i , j ] = - 1 ; } for ( int i = 1 ; i < n ; i ++ ) pre . Add ( i , pre [ i - 1 ] + arr [ i ] ) ; Console . Write ( ( maxweight ( 0 , n - 1 , pre ) ) ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 6 , 2 , 3 , 4 , 5 , 5 } ; maxSum ( arr , 6 ) ; } }


"Maximize sum of remaining elements after every removal of the array half with greater sum | C # program to implement the above approach ; Function to find the maximum sum ; Base case len of array is 1 ; Stores the readonly result ; Traverse the array ; Store left prefix sum ; Store right prefix sum ; Compare the left and right ; If both are equal apply the optimal method ; Update with minimum ; Return the readonly ans ; Function to print maximum sum ; To store prefix sums ; Traversing the array ; Add prefix sum of the array ; Driver code ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int maxweight ( int s , int e , Dictionary < int , int > pre ) { if ( s == e ) return 0 ; int ans = 0 ; for ( int i = s ; i < e ; i ++ ) { int left = pre [ i ] - pre [ s - 1 ] ; int right = pre [ e ] - pre [ i ] ; if ( left < right ) ans = Math . Max ( ans , left + maxweight ( s , i , pre ) ) ; if ( left == right ) { ans = Math . Max ( ans , Math . Max ( left + maxweight ( s , i , pre ) , right + maxweight ( i + 1 , e , pre ) ) ) ; } if ( left > right ) ans = Math . Max ( ans , right + maxweight ( i + 1 , e , pre ) ) ; } return ans ; } static void maxSum ( List < int > arr ) { Dictionary < int , int > pre = new Dictionary < int , int > ( ) ; pre . Add ( - 1 , 0 ) ; pre . Add ( 0 , arr [ 0 ] ) ; for ( int i = 1 ; i < arr . Count ; i ++ ) { if ( pre [ i - 1 ] != 0 ) pre . Add ( i , pre [ i - 1 ] + arr [ i ] ) ; else pre . Add ( i , arr [ i ] ) ; } Console . WriteLine ( maxweight ( 0 , arr . Count - 1 , pre ) ) ; } public static void Main ( String [ ] args ) { List < int > arr = new List < int > ( ) ; arr . Add ( 6 ) ; arr . Add ( 2 ) ; arr . Add ( 3 ) ; arr . Add ( 4 ) ; arr . Add ( 5 ) ; arr . Add ( 5 ) ; maxSum ( arr ) ; } }


"Maximize sum of second minimums of each K length partitions of the array | C # program for the above approach ; Function to find the maximum sum of second smallest of each partition of size K ; Sort the array [ ] A in ascending order ; Store the maximum sum of second smallest of each partition of size K ; Select every ( K - 1 ) th element as second smallest element ; Update sum ; Print the maximum sum ; Driver Code ; Given size of partitions ; Given array [ ] A ; Size of the given array ; Function Call"
Solution in C#:

using System ; class GFG { static void findSum ( int [ ] A , int N , int K ) { Array . Sort ( A ) ; int sum = 0 ; for ( int i = N / K ; i < N ; i += K - 1 ) { sum += A [ i ] ; } Console . Write ( sum ) ; } public static void Main ( String [ ] args ) { int K = 4 ; int [ ] A = { 2 , 3 , 1 , 4 , 7 , 5 , 6 , 1 } ; int N = A . Length ; findSum ( A , N , K ) ; } }


"Maximize sum of subsets from two arrays having no consecutive values | C # program for the above approach ; Function to calculate maximum subset sum ; Initialize array to store dp states ; Base Cases ; Pre initializing for dp [ 0 ] & dp [ 1 ] ; Calculating dp [ index ] based on above formula ; Print maximum subset sum ; Driver Code ; Given arrays ; Length of the array"
Solution in C#:

using System ; class GFG { static void maximumSubsetSum ( int [ ] arr1 , int [ ] arr2 , int length ) { int [ ] dp = new int [ length + 1 ] ; if ( length == 1 ) { Console . WriteLine ( Math . Max ( arr1 [ 0 ] , arr2 [ 0 ] ) ) ; return ; } if ( length == 2 ) { Console . WriteLine ( Math . Max ( Math . Max ( arr1 [ 1 ] , arr2 [ 1 ] ) , Math . Max ( arr1 [ 0 ] , arr2 [ 0 ] ) ) ) ; return ; } else { dp [ 0 ] = Math . Max ( arr1 [ 0 ] , arr2 [ 0 ] ) ; dp [ 1 ] = Math . Max ( Math . Max ( arr1 [ 1 ] , arr2 [ 1 ] ) , Math . Max ( arr1 [ 0 ] , arr2 [ 0 ] ) ) ; int index = 2 ; while ( index < length ) { dp [ index ] = Math . Max ( Math . Max ( arr1 [ index ] , arr2 [ index ] ) , Math . Max ( Math . Max ( arr1 [ index ] + dp [ index - 2 ] , arr2 [ index ] + dp [ index - 2 ] ) , dp [ index - 1 ] ) ) ; ++ index ; } Console . WriteLine ( dp [ length - 1 ] ) ; } } static public void Main ( ) { int [ ] arr1 = { - 1 , - 2 , 4 , - 4 , 5 } ; int [ ] arr2 = { - 1 , - 2 , - 3 , 4 , 10 } ; int length = arr1 . Length ; maximumSubsetSum ( arr1 , arr2 , length ) ; } }


"Maximize sum of topmost elements of S stacks by popping at most N elements | C # program to maximize the sum of top of the stack values of S stacks by popping at most N elements ; Function for computing the maximum sum at the top of the stacks after popping at most N elements from S stack ; Constructing a dp matrix of dimensions ( S + 1 ) x ( N + 1 ) ; Loop over all i stacks ; Store the maximum of popping j elements up to the current stack by popping k elements from current stack and j - k elements from all previous stacks combined ; Store the maximum sum of popping N elements across all stacks ; dp [ S , N ] has the maximum sum ; Driver code ; Number of stacks ; Length of each stack ; Maximum elements that can be popped"
Solution in C#:

using System ; class GFG { static int maximumSum ( int S , int M , int N , int [ , ] stacks ) { int [ , ] dp = new int [ S + 1 , N + 1 ] ; for ( int i = 0 ; i < S ; i ++ ) { for ( int j = 0 ; j <= N ; j ++ ) { for ( int k = 0 ; k <= Math . Min ( j , M ) ; k ++ ) { dp [ i + 1 , j ] = Math . Max ( dp [ i + 1 , j ] , stacks [ i , k ] + dp [ i , j - k ] ) ; } } } int result = int . MinValue ; for ( int i = 0 ; i <= N ; i ++ ) { result = Math . Max ( result , dp [ S , i ] ) ; } return result ; } public static void Main ( String [ ] args ) { int S = 2 ; int M = 4 ; int [ , ] stacks = { { 2 , 6 , 4 , 5 } , { 1 , 6 , 15 , 10 } } ; int N = 3 ; Console . Write ( maximumSum ( S , M , N , stacks ) ) ; } }


"Maximize sum possible from an array by jumps of length i + K * arr [ i ] from any ith index | C # program for the above approach ; Function to find the maximum sum possible by jumps of length i + K * arr [ i ] from any i - th index ; Initialize an array dp [ ] ; Stores the maximum sum ; Iterate over the range [ N - 1 , 0 ] ; If length of the jump exceeds N ; Set dp [ i ] as arr [ i ] ; Otherwise , update dp [ i ] as sum of dp [ i + K * arr [ i ] ] and arr [ i ] ; Update the overall maximum sum ; Print the answer ; Driver Code"
Solution in C#:

using System ; class GFG { static void maxSum ( int [ ] arr , int N , int K ) { int [ ] dp = new int [ N + 2 ] ; for ( int i = 0 ; i < N + 2 ; i ++ ) { dp [ i ] = 0 ; } int maxval = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( ( i + K * arr [ i ] ) >= N ) { dp [ i ] = arr [ i ] ; } else { dp [ i ] = dp [ i + K * arr [ i ] ] + arr [ i ] ; } maxval = Math . Max ( maxval , dp [ i ] ) ; } Console . WriteLine ( maxval ) ; } static public void Main ( ) { int [ ] arr = { 2 , 1 , 3 , 1 , 2 } ; int N = arr . Length ; int K = 3 ; maxSum ( arr , N , K ) ; } }


"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"
Solution in C#:

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 ) ) ; } }


"Maximize the Expression | Bit Manipulation | C # implementation of the above approach ; Function to return the value of the maximized expression ; int can have 32 bits ; Consider the ith bit of D to be 1 ; Calculate the value of ( B AND bitOfD ) ; Check if bitOfD satisfies ( B AND D = D ) ; Check if bitOfD can maximize ( A XOR D ) ; Note that we do not need to consider ith bit of D to be 0 because if above condition are not satisfied then value of result will not change which is similar to considering bitOfD = 0 as result XOR 0 = result ; Driver code"
Solution in C#:

using System ; class GFG { static int MAX = 32 ; static int maximizeExpression ( int a , int b ) { int result = a ; for ( int bit = MAX - 1 ; bit >= 0 ; bit -- ) { int bitOfD = 1 << bit ; int x = b & bitOfD ; if ( x == bitOfD ) { int y = result & bitOfD ; if ( y == 0 ) { result = result ^ bitOfD ; } } } return result ; } public static void Main ( String [ ] args ) { int a = 11 , b = 14 ; Console . WriteLine ( maximizeExpression ( a , b ) ) ; } }


"Maximize the Sum of a Subsequence from an Array based on given conditions | C # program to implement the above approach ; Function to select the array elements to maximize the sum of the selected elements ; If the entire array is solved ; Memoized subproblem ; Calculate sum considering the current element in the subsequence ; Calculate sum without considering the current element in the subsequence ; Update the maximum of the above sums in the [ , ] dp table ; Driver Code ; Initialize the dp array ; Function call"
Solution in C#:

using System ; class GFG { public static int maximumSum ( int [ ] a , int count , int index , int n , int [ , ] dp ) { if ( index == n ) return 0 ; if ( dp [ index , count ] != - 1 ) return dp [ index , count ] ; int take_element = a [ index ] * count + maximumSum ( a , count + 1 , index + 1 , n , dp ) ; int dont_take = maximumSum ( a , count , index + 1 , n , dp ) ; return dp [ index , count ] = Math . Max ( take_element , dont_take ) ; } public static void Main ( String [ ] args ) { int n = 5 ; int [ ] a = { - 1 , - 9 , 0 , 5 , - 7 } ; int [ , ] dp = new int [ n + 1 , n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { for ( int j = 0 ; j < n + 1 ; j ++ ) { dp [ i , j ] = - 1 ; } } Console . WriteLine ( maximumSum ( a , 1 , 0 , n , dp ) ) ; } }


"Maximize the binary matrix by filpping submatrix once | C # program to find maximum number of ones after one flipping in Binary Matrix ; Return number of ones in square submatrix of size k x k starting from ( x , y ) ; Return maximum number of 1 s after flipping a submatrix ; Precomputing the number of 1 s ; Finding the maximum number of 1 s after flipping ; Driver code"
Solution in C#:

using System ; public class GFG { static readonly int R = 3 ; static readonly int C = 3 ; static int cal ( int [ , ] ones , int x , int y , int k ) { return ones [ x + k - 1 , y + k - 1 ] - ones [ x - 1 , y + k - 1 ] - ones [ x + k - 1 , y - 1 ] + ones [ x - 1 , y - 1 ] ; } static int sol ( int [ , ] mat ) { int ans = 0 ; int val = 0 ; int [ , ] ones = new int [ R + 1 , C + 1 ] ; for ( int i = 1 ; i <= R ; i ++ ) for ( int j = 1 ; j <= C ; j ++ ) { if ( mat [ i - 1 , j - 1 ] == 1 ) val = 1 ; ones [ i , j ] = ones [ i - 1 , j ] + ones [ i , j - 1 ] - ones [ i - 1 , j - 1 ] + ( val ) ; } for ( int k = 1 ; k <= Math . Min ( R , C ) ; k ++ ) for ( int i = 1 ; i + k - 1 <= R ; i ++ ) for ( int j = 1 ; j + k - 1 <= C ; j ++ ) ans = Math . Max ( ans , ( ones [ R , C ] + k * k - 2 * cal ( ones , i , j , k ) ) ) ; return ans ; } static public void Main ( ) { int [ , ] mat = { { 0 , 0 , 1 } , { 0 , 0 , 1 } , { 1 , 0 , 1 } } ; Console . WriteLine ( sol ( mat ) ) ; } }


"Maximize the bitwise OR of an array | C # program to maximize the Bitwise OR Sum in given array ; Function to maximize the bitwise OR sum ; Compute x ^ k ; Find prefix bitwise OR ; Find suffix bitwise OR ; Find maximum OR value ; Drivers code"
Solution in C#:

using System ; class GFG { public static long maxOR ( long [ ] arr , int n , int k , int x ) { long [ ] preSum = new long [ n + 1 ] ; long [ ] suffSum = new long [ n + 1 ] ; long res = 0 , pow = 1 ; for ( int i = 0 ; i < k ; i ++ ) pow *= x ; preSum [ 0 ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) preSum [ i + 1 ] = preSum [ i ] | arr [ i ] ; suffSum [ n ] = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) suffSum [ i ] = suffSum [ i + 1 ] | arr [ i ] ; res = 0 ; for ( int i = 0 ; i < n ; i ++ ) res = Math . Max ( res , preSum [ i ] | ( arr [ i ] * pow ) suffSum [ i + 1 ] ) ; return res ; } public static void Main ( ) { long [ ] arr = { 1 , 2 , 4 , 8 } ; int n = 4 ; int k = 2 , x = 3 ; long ans = maxOR ( arr , n , k , x ) ; Console . Write ( ans ) ; } }


"Maximize the count of adjacent element pairs with even sum by rearranging the Array | C # program for the above approach ; Function to find maximum count pair of adjacent elements with even sum ; Stores count of odd numbers ; Stores count of even numbers ; Traverse the array arr [ ] ; If arr [ i ] % 2 is 1 ; Else ; If odd and even both are greater than 0 ; Otherwise ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int maximumCount ( int [ ] arr , int N ) { int odd = 0 ; int even = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 != 0 ) odd ++ ; else even ++ ; } if ( odd != 0 && even != 0 ) return N - 2 ; else return N - 1 ; } public static void Main ( ) { int [ ] arr = { 9 , 13 , 15 , 3 , 16 , 9 , 13 , 18 } ; int N = arr . Length ; Console . Write ( maximumCount ( arr , N ) ) ; } }


"Maximize the diamonds by choosing different colour diamonds from adjacent boxes | C # implementation of the approach ; Function to return the maximized value ; Number of rows and columns ; Creating the Dp array ; Populating the first column ; Iterating over all the rows ; Getting the ( i - 1 ) th max value ; Adding it to the current cell ; Getting the max sum from the last column ; Driver code ; Columns are indexed 1 - based"
Solution in C#:

using System ; public class GFG { static int maxSum ( int [ , ] arr ) { int m = ( int ) arr . GetLength ( 0 ) ; int n = ( int ) arr . GetLength ( 1 ) - 1 ; int [ , ] dp = new int [ m + 1 , n + 2 ] ; memset ( arr , 0 , sizeof ( arr ) ) ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n + 1 ; j ++ ) { dp [ i , j ] = 0 ; } } for ( int i = 1 ; i < m ; ++ i ) dp [ i , 1 ] = arr [ i , 1 ] ; for ( int i = 1 ; i < n + 1 ; ++ i ) { for ( int j = 1 ; j < m ; ++ j ) { int mx = 0 ; for ( int k = 1 ; k < m ; ++ k ) { if ( k != j ) { if ( dp [ k , i - 1 ] > mx ) { mx = dp [ k , i - 1 ] ; } } } if ( mx != 0 && arr [ j , i ] != 0 ) { dp [ j , i ] = arr [ j , i ] + mx ; } } } int ans = - 1 ; for ( int i = 1 ; i <= m ; ++ i ) { if ( ( dp [ i , n ] ) != 0 ) ans = Math . Max ( ans , dp [ i , n ] ) ; } return ans ; } public static void Main ( String [ ] args ) { int [ , ] arr = { { 0 , 0 , 0 , 0 , 0 } , { 0 , 10 , 2 , 20 , 0 } , { 0 , 0 , 0 , 5 , 0 } , { 0 , 0 , 0 , 0 , 6 } , { 0 , 4 , 0 , 11 , 5 } , { 0 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 } } ; Console . WriteLine ( maxSum ( arr ) ) ; } }


"Maximize the division result of Array using given operations | C # implementation to maximize the result of division of the given array elements ; Function to find the max result ; Sort the array in descending order ; Loop to divide in this order arr [ 0 ] / ( arr [ 1 ] / arr [ 2 ] / ... . arr [ n - 2 ] / arr [ n - 1 ] ) ; Return the readonly result ; Driver code"
Solution in C#:

using System ; class GFG { static float maxDivision ( int [ ] arr , int n ) { Array . Sort ( arr ) ; Array . Reverse ( arr ) ; float mxdiv = arr [ 1 ] ; for ( int i = 2 ; i < n ; ++ i ) mxdiv = mxdiv / arr [ i ] ; return arr [ 0 ] / mxdiv ; } public static void Main ( String [ ] args ) { int [ ] arr = { 100 , 1000 , 10 , 2 } ; int n = arr . Length ; Console . Write ( ( int ) maxDivision ( arr , n ) ) ; } }


"Maximize the given number by replacing a segment of digits with the alternate digits given | C # implementation of the approach ; Function to return the maximized number ; Iterate till the end of the string ; Check if it is greater or not ; Replace with the alternate till smaller ; Return original s in case no change took place ; Driver Code"
Solution in C#:

using System ; class GFG { static String get_maximum ( char [ ] s , int [ ] a ) { int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] - '0' < a [ s [ i ] - '0' ] ) { int j = i ; while ( j < n && ( s [ j ] - '0' <= a [ s [ j ] - '0' ] ) ) { s [ j ] = ( char ) ( '0' + a [ s [ j ] - '0' ] ) ; j ++ ; } return String . Join ( " " , s ) ; } } return String . Join ( " " , s ) ; } public static void Main ( String [ ] args ) { String s = "1337" ; int [ ] a = { 0 , 1 , 2 , 5 , 4 , 6 , 6 , 3 , 1 , 9 } ; Console . WriteLine ( get_maximum ( s . ToCharArray ( ) , 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"
Solution in C#:

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 ) ) ; } }


"Maximize the largest number K such that bitwise and of K till N is 0 | C # program for the above approach ; Function to find maximum value of k which makes bitwise AND zero . ; Take k = N initially ; Start traversing from N - 1 till 0 ; Whenever we get AND as 0 we stop and return ; Driver Code"
Solution in C#:

using System ; class GFG { static int findMaxK ( int N ) { int K = N ; for ( int i = N - 1 ; i >= 0 ; i -- ) { K &= i ; if ( K == 0 ) { return i ; } } return 0 ; } public static void Main ( String [ ] args ) { int N = 5 ; Console . Write ( findMaxK ( N ) ) ; } }


"Maximize the largest number K such that bitwise and of K till N is 0 | package whatever do not write package name here ; Function to find maximum value of k which makes bitwise AND zero . ; Finding the power less than N ; Driver Code"
Solution in C#:

using System ; class GFG { static int findMaxK ( int N ) { int p = ( int ) ( Math . Log ( N ) / Math . Log ( 2 ) ) ; return ( int ) Math . Pow ( 2 , p ) ; } public static void Main ( String [ ] args ) { int N = 5 ; Console . Write ( findMaxK ( N ) - 1 ) ; } }


"Maximize the last Array element as per the given conditions | C # Program to implement the above approach ; Function to find the maximum possible value that can be placed at the last index ; Sort the array elements in ascending order ; If the first element is is not equal to 1 ; Traverse the array to make difference between adjacent elements <= 1 ; Driver Code"
Solution in C#:

using System ; class GFG { public static int maximizeFinalElement ( int [ ] arr , int n ) { Array . Sort ( arr ) ; if ( arr [ 0 ] != 1 ) arr [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] - arr [ i - 1 ] > 1 ) { arr [ i ] = arr [ i - 1 ] + 1 ; } } return arr [ n - 1 ] ; } public static void Main ( String [ ] args ) { int n = 4 ; int [ ] arr = { 3 , 1 , 3 , 4 } ; int max = maximizeFinalElement ( arr , n ) ; Console . WriteLine ( max ) ; } }


"Maximize the length of upper boundary formed by placing given N rectangles horizontally or vertically | C # program for the above approach ; Function to find maximum length of the upper boundary formed by placing each of the rectangles either horizontally or vertically ; Stores the intermediate transition states ; Place the first rectangle horizontally ; Place the first rectangle vertically ; Place horizontally ; Stores the difference in height of current and previous rectangle ; Take maximum out of two options ; Place Vertically ; Stores the difference in height of current and previous rectangle ; Take maximum out two options ; Print maximum of horizontal or vertical alignment of the last rectangle ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void maxBoundary ( int N , List < Tuple < int , int > > V ) { int [ , ] dp = new int [ N , 2 ] ; dp [ 0 , 0 ] = V [ 0 ] . Item1 ; dp [ 0 , 1 ] = V [ 0 ] . Item2 ; for ( int i = 1 ; i < N ; i ++ ) { dp [ i , 0 ] = V [ i ] . Item1 ; int height1 = Math . Abs ( V [ i - 1 ] . Item2 - V [ i ] . Item2 ) ; int height2 = Math . Abs ( V [ i - 1 ] . Item1 - V [ i ] . Item2 ) ; dp [ i , 0 ] += Math . Max ( height1 + dp [ i - 1 , 0 ] , height2 + dp [ i - 1 , 1 ] ) ; dp [ i , 1 ] = V [ i ] . Item2 ; int vertical1 = Math . Abs ( V [ i ] . Item1 - V [ i - 1 ] . Item2 ) ; int vertical2 = Math . Abs ( V [ i ] . Item1 - V [ i - 1 ] . Item1 ) ; dp [ i , 1 ] += Math . Max ( vertical1 + dp [ i - 1 , 0 ] , vertical2 + dp [ i - 1 , 1 ] ) ; } Console . WriteLine ( Math . Max ( dp [ N - 1 , 0 ] , dp [ N - 1 , 1 ] ) ) ; } static void Main ( ) { int N = 5 ; List < Tuple < int , int > > V = new List < Tuple < int , int > > ( ) ; V . Add ( new Tuple < int , int > ( 2 , 5 ) ) ; V . Add ( new Tuple < int , int > ( 3 , 8 ) ) ; V . Add ( new Tuple < int , int > ( 1 , 10 ) ) ; V . Add ( new Tuple < int , int > ( 7 , 14 ) ) ; V . Add ( new Tuple < int , int > ( 2 , 5 ) ) ; maxBoundary ( N , V ) ; } }


"Maximize the maximum subarray sum after removing atmost one element | C # implementation of the approach ; Function to return the maximum sub - array sum ; Initialized ; Traverse in the array ; Increase the sum ; If sub - array sum is more than the previous ; If sum is negative ; Function that returns the maximum sub - array sum after removing an element from the same sub - array ; Maximum sub - array sum using Kadane 's Algorithm ; Re - apply Kadane 's with minor changes ; Increase the sum ; If sub - array sum is greater than the previous ; If elements are 0 , no removal ; If elements are more , then store the minimum value in the sub - array obtained till now ; If sum is negative ; Re - initialize everything ; Driver code"
Solution in C#:

using System ; class GFG { static int maxSubArraySum ( int [ ] a , int size ) { int max_so_far = int . MinValue , max_ending_here = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; if ( max_ending_here < 0 ) max_ending_here = 0 ; } return max_so_far ; } static int maximizeSum ( int [ ] a , int n ) { int cnt = 0 ; int mini = int . MaxValue ; int minSubarray = int . MaxValue ; int sum = maxSubArraySum ( a , n ) ; int max_so_far = int . MinValue , max_ending_here = 0 ; for ( int i = 0 ; i < n ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; cnt ++ ; minSubarray = Math . Min ( a [ i ] , minSubarray ) ; if ( sum == max_ending_here ) { if ( cnt == 1 ) mini = Math . Min ( mini , 0 ) ; else mini = Math . Min ( mini , minSubarray ) ; } if ( max_ending_here < 0 ) { max_ending_here = 0 ; cnt = 0 ; minSubarray = int . MaxValue ; } } return sum - mini ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 2 , 3 , - 2 , 3 } ; int n = a . Length ; Console . WriteLine ( maximizeSum ( a , n ) ) ; } }


"Maximize the minimum array element by M subarray increments of size S | C # program for the above approach ; Function to return index of minimum element in the array ; Initialize a [ 0 ] as minValue ; Traverse the array ; If a [ i ] < existing minValue ; Return the minimum index ; Function that maximize the minimum element of array after incrementing subarray of size S by 1 , M times ; Iterating through the array or M times ; Find minimum element index ; Increment the minimum value ; Storing the left index and right index ; Incrementing S - 1 minimum elements to the left and right of minValue ; Reached extreme left ; Reached extreme right ; Left value is minimum ; Right value is minimum ; Find the minValue in A [ ] after M operations ; Return the minimum value ; Driver code ; Function Call"
Solution in C#:

using System ; class GFG { static int min1 ( int [ ] a , int n ) { int minIndex = 0 , minValue = a [ 0 ] , i ; for ( i = 1 ; i < n ; i ++ ) { if ( a [ i ] < minValue ) { minValue = a [ i ] ; minIndex = i ; } } return minIndex ; } static int maximizeMin ( int [ ] A , int N , int S , int M ) { int minIndex , left , right , i , j ; for ( i = 0 ; i < M ; i ++ ) { minIndex = min1 ( A , N ) ; A [ minIndex ] ++ ; left = minIndex - 1 ; right = minIndex + 1 ; for ( j = 0 ; j < S - 1 ; j ++ ) { if ( left == - 1 ) A [ right ++ ] ++ ; else if ( right == N ) A [ left -- ] ++ ; else { if ( A [ left ] < A [ right ] ) A [ left -- ] ++ ; else A [ right ++ ] ++ ; } } } minIndex = min1 ( A , N ) ; return A [ minIndex ] ; } static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 } ; int N = arr . Length ; int S = 2 , M = 3 ; Console . Write ( maximizeMin ( arr , N , S , M ) ) ; } }


"Maximize the minimum difference between any element pair by selecting K elements from given Array | C # implementation for the above approach ; To check if selection of K elements is possible such that difference between them is greater than dif ; Selecting first element in the sorted array ; prev is the previously selected element initially at index 0 as first element is already selected ; Check if selection of K - 1 elements from array with a minimum difference of dif is possible ; If the current element is atleast dif difference apart from the previously selected element then select the current element and increase the count ; If selection of K elements with a min difference of dif is possible then return true ; Prev will become current element for the next iteration ; If selection of K elements with minimum difference of dif is not possible then return false ; Minimum largest difference possible is 1 ; Check if selection of K elements is possible with a minimum difference of dif ; If dif is greater than previous ans we update ans ; Continue to search for better answer . Try finding greater dif ; K elements cannot be selected ; Driver code ; arr should be in a sorted order"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static bool isPossibleToSelect ( int [ ] arr , int N , int dif , int K ) { int count = 1 ; int prev = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] >= ( prev + dif ) ) { count ++ ; if ( count == K ) return true ; prev = arr [ i ] ; } } return false ; } static int binarySearch ( int [ ] arr , int left , int right , int K , int N ) { int ans = 1 ; while ( left <= right ) { int dif = left + ( right - left ) / 2 ; if ( isPossibleToSelect ( arr , N , dif , K ) ) { ans = Math . Max ( ans , dif ) ; left = dif + 1 ; } else right = dif - 1 ; } return ans ; } public static void Main ( ) { int N , K ; N = 7 ; K = 4 ; int [ ] arr = { 1 , 4 , 9 , 0 , 2 , 13 , 3 } ; Array . Sort ( arr ) ; Console . Write ( binarySearch ( arr , 0 , arr [ N - 1 ] , K , N ) ) ; } }


"Maximize the missing values in given time in HH : MM format | C # program for the above approach ; Function to find the maximum time by replacing ' ? ' by any digits ; Convert the string to the character array ; If the 0 th index is ' ? ' ; If the 1 st index is ' ? ' ; If the 3 rd index is ' ? ' ; If the 4 th index is ' ? ' ; Return new string ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class gfg { public static void maxTime ( String S ) { char [ ] s = S . ToCharArray ( ) ; if ( s [ 0 ] == ' ? ' ) { if ( s [ 1 ] <= '3' s [ 1 ] == ' ? ' ) s [ 0 ] = '2' ; else s [ 0 ] = '1' ; } if ( s [ 1 ] == ' ? ' ) { if ( s [ 0 ] != '2' ) { s [ 1 ] = '9' ; } else s [ 1 ] = '3' ; } if ( s [ 3 ] == ' ? ' ) s [ 3 ] = '5' ; if ( s [ 4 ] == ' ? ' ) s [ 4 ] = '9' ; Console . Write ( new String ( s ) ) ; } public static void Main ( String [ ] args ) { String S = " ? 4:5 ? " ; maxTime ( S ) ; } }


"Maximize the number N by inserting given digit at any position | C # program for above approach ; Function to find the maximum value of N after inserting the digit K ; Convert it into N to string ; Stores the maximum value of N after inserting K ; Iterate till all digits that are not less than K ; Add the current digit to the string result ; Add digit ' K ' to result ; Iterate through all remaining characters ; Add current digit to result ; Print the maximum number formed ; Driver Code"
Solution in C#:

using System ; class GFG { public static void maximizeNumber ( int N , int K ) { String s = N . ToString ( ) ; int L = s . Length ; string result = " " ; int i = 0 ; while ( ( i < L ) && ( K <= ( ( int ) s [ i ] - ( int ) '0' ) ) ) { result += ( s [ i ] ) ; ++ i ; } result += ( ( char ) ( K + ( int ) '0' ) ) ; while ( i < L ) { result += ( s [ i ] ) ; ++ i ; } Console . Write ( result ) ; } static void Main ( ) { int N = 6673 , K = 6 ; maximizeNumber ( N , K ) ; } }


"Maximize the number by rearranging bits | An efficient C # program to find minimum number formed by bits of a given number . ; Returns maximum number formed by bits of a given number . ; _popcnt32 ( a ) gives number of 1 's present in binary  representation of a. ; If along 32 bits are set . ; find a number witn n least significant set bits . ; Now shift result by 32 - n ; Driver Code"
Solution in C#:

using System ; class GFG { static long _popcnt32 ( long n ) { long count = 0 ; while ( n != 0 ) { n = n & ( n - 1 ) ; count ++ ; } return count ; } static long maximize ( long a ) { long n = _popcnt32 ( a ) ; if ( n == 32 ) return a ; long res = ( 1 << Convert . ToInt32 ( n ) ) - 1 ; return ( res << ( 32 - Convert . ToInt32 ( n ) ) ) ; } static void Main ( ) { long a = 3 ; Console . WriteLine ( maximize ( a ) ) ; } }


"Maximize the number of indices such that element is greater than element to its left | C # program of the above approach ; Function to find the maximum pairs such that arr [ i + 1 ] > arr [ i ] ; To store the frequency of the element in [ ] arr ; Store the frequency in map M ; To find the maximum frequency store in map M ; Print the maximum number of possible pairs ; Driver Code"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static void countPairs ( int [ ] arr , int N ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1 ; } else { mp . Add ( arr [ i ] , 1 ) ; } } int maxFreq = 0 ; foreach ( KeyValuePair < int , int > it in mp ) { maxFreq = Math . Max ( maxFreq , it . Value ) ; } Console . Write ( N - maxFreq + "
 " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 8 , 5 , 9 , 8 , 8 , 7 , 7 , 5 , 7 , 7 } ; int N = arr . Length ; countPairs ( arr , N ) ; } }


"Maximize the number of palindromic Strings | C # program for the above approach ; To check if there is any string of odd length ; If there is at least 1 string of odd length . ; If all the strings are of even length . ; Count of 0 's in all  the strings ; Count of 1 's in  all the strings ; If z is even and o is even then ans will be N . ; Otherwise ans will be N - 1. ; Driver code"
Solution in C#:

using System ; class GFG { static int max_palindrome ( string [ ] s , int n ) { int flag = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] . Length % 2 != 0 ) { flag = 1 ; } } if ( flag == 1 ) { return n ; } int z = 0 ; int o = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < s [ i ] . Length ; j ++ ) { if ( s [ i ] [ j ] == '0' ) z += 1 ; else o += 1 ; } } if ( o % 2 == 0 && z % 2 == 0 ) { return n ; } else { return n - 1 ; } } public static void Main ( ) { int n = 3 ; string [ ] s = { "1110" , "100110" , "010101" } ; Console . WriteLine ( max_palindrome ( s , n ) ) ; } }


"Maximize the number of times a character can be removed from substring 01 from given Binary String | C # program for the above approach ; Function to find the maximum moves that can be performed on a string ; Stores 0 s in suffix ; Stores 1 s in prefix ; Iterate over the characters of the string ; Iterate until i is greater than or equal to 0 ; If N is equal to x + y ; Return answer ; Driver code ; Input ; Function call"
Solution in C#:

using System ; class GFG { static int maxOperations ( String S , int N ) { int X = 0 ; int Y = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] == '0' ) break ; Y ++ ; } for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( S [ i ] == '1' ) break ; X ++ ; } if ( N == X + Y ) return 0 ; return N - ( X + Y ) - 1 ; } static void Main ( ) { String S = "001111" ; int N = S . Length ; Console . WriteLine ( maxOperations ( S , N ) ) ; } }


"Maximize the numbers of splits in an Array having sum divisible by 3 | C # implementation of the above approach ; Prefix array storing right most index with prefix sums 0 , 1 , 2 ; dp array ; Current prefix sum ; Calculating the prefix sum modulo 3 ; We dont have a left pointer with prefix sum C ; We cannot consider i as a right pointer of any segment ; We have a left pointer pre [ C ] with prefix sum C ; i is the rightmost index of prefix sum C ; Driver code"
Solution in C#:

using System ; class GFG { static int calculate_maximum_splits ( int [ ] arr , int N ) { int [ ] pre = { 0 , - 1 , - 1 } ; int [ ] dp = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { dp [ i ] = 0 ; } int C = 0 ; for ( int i = 0 ; i < N ; i ++ ) { C = C + arr [ i ] ; C = C % 3 ; if ( pre [ C ] == - 1 ) { if ( 1 <= i ) dp [ i ] = dp [ i - 1 ] ; } else { dp [ i ] = Math . Max ( dp [ i - 1 ] , dp [ pre [ C ] ] + 1 ) ; } pre [ C ] = i ; } return dp [ N - 1 ] ; } public static void Main ( string [ ] args ) { int [ ] arr = { 2 , 36 , 1 , 9 , 2 , 0 , 1 , 8 , 1 } ; int N = arr . Length ; Console . Write ( calculate_maximum_splits ( arr , N ) ) ; } }


"Maximize the product of four factors of a Number | C # implementation of above approach ; Declare the ArrayList of factors for storing the ; function to find out the factors of a number ; Loop until the i reaches the sqrt ( n ) ; Check if i is a factor of n ; if both the factors are same we only push one factor ; factor1 is pushed ; factor2 is pushed ; Function to find the maximum product ; Initialize the product with - 1 ; Find the sum of factors and store it in s ; Compare whether it is equal to the n ; product of factors ; Check whether we have a better p now if yes update ; Driver Code ; initializes the List with the divisors of n ; prints out the maximised product ."
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static List < int > factors = new List < int > ( 10 ) ; static void findFactors ( int n ) { for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( ( n / i ) == i ) { factors . Insert ( factors . Count , i ) ; } else { factors . Insert ( factors . Count , n / i ) ; factors . Insert ( factors . Count , i ) ; } } } } static int findProduct ( int n ) { int product = - 1 ; int si = factors . Count ; for ( int i = 0 ; i < si ; i ++ ) { for ( int j = 0 ; j < si ; j ++ ) { for ( int k = 0 ; k < si ; k ++ ) { for ( int l = 0 ; l < si ; l ++ ) { int s = factors [ i ] + factors [ j ] + factors [ k ] + factors [ l ] ; if ( s == n ) { int p = factors [ i ] * factors [ j ] * factors [ k ] * factors [ l ] ; if ( p > product ) { product = p ; } } } } } } return product ; } public static void Main ( String [ ] args ) { int n = 10 ; findFactors ( n ) ; Console . WriteLine ( findProduct ( n ) ) ; } }


"Maximize the product of four factors of a Number | C # implementation of above approach ; For calculation of a ^ b ; Function to check ; every odd and number less than 3. ; every number divisible by 4. ; every number divisible by 6. ; every number divisible by 10. ; for every even number which is not divisible by above values . ; Driver code"
Solution in C#:

using System ; public class GFG { static int modExp ( int a , int b ) { int result = 1 ; while ( b > 0 ) { if ( b == 1 ) result = result * a ; a = a * a ; b /= 2 ; } return result ; } static int check ( int num ) { if ( num == 1 num < 3 ) return - 1 ; else if ( num % 4 == 0 ) return modExp ( num / 4 , 4 ) ; else if ( num % 6 == 0 ) return modExp ( num / 3 , 2 ) * modExp ( num / 6 , 2 ) ; else if ( num % 10 == 0 ) return modExp ( num / 5 , 2 ) * ( num / 10 ) * ( num / 2 ) ; else return - 1 ; } static public void Main ( ) { int num = 10 ; Console . WriteLine ( check ( num ) ) ; } }


"Maximize the profit after selling the tickets | Set 2 ( For elements in range [ 1 , 10 ^ 6 ] ) | C # program for the above approach ; Function to find maximum profit after selling K tickets ; Frequency array to store freq of every element of the array ; Modify the arr [ ] so that the array is sorted in O ( N ) ; Variable to store answer ; Traverse the array while K > 0 and j >= 0 ; If arr [ i ] > arr [ j ] then ticket can be brought from counter [ j + 1 , N ] ; If arr [ j ] = = arr [ i ] decrement j until arr [ j ] != arr [ i ] ; Sell tickets from counter [ j + 1 , N ] ; All elements of array are equal Send tickets from each counter 1 time until K > 0. ; Converting answer from long long to int ; Driver Code ; Given Input ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int maxAmount ( int n , int k , int [ ] arr ) { int i ; int [ ] A = new int [ 1000001 ] ; Array . Clear ( A , 0 , 1000001 ) ; for ( i = 0 ; i < n ; i ++ ) { A [ arr [ i ] ] ++ ; } int j = 0 ; for ( i = 0 ; i < 1000001 ; i ++ ) { while ( A [ i ] != 0 ) { arr [ j ++ ] = i ; A [ i ] -- ; } } int ans = 0 ; int mod = 1000000007 ; i = n - 1 ; j = n - 2 ; while ( k > 0 && j >= 0 ) { if ( arr [ i ] > arr [ j ] ) { ans = ans + Math . Min ( k , ( i - j ) ) * arr [ i ] ; k = k - ( i - j ) ; arr [ i ] -- ; } else { while ( j >= 0 && arr [ j ] == arr [ i ] ) j -- ; if ( j < 0 ) break ; ans = ans + Math . Min ( k , ( i - j ) ) * arr [ i ] ; k = k - ( i - j ) ; arr [ i ] -- ; } } while ( k > 0 && arr [ i ] != 0 ) { ans = ans + Math . Min ( n , k ) * arr [ i ] ; k -= n ; arr [ i ] -- ; } ans = ans % mod ; int x = ans ; return x ; } public static void Main ( ) { int n = 5 ; int k = 3 ; int [ ] arr = { 4 , 3 , 6 , 2 , 4 } ; int ans = maxAmount ( n , k , arr ) ; Console . Write ( ans ) ; } }


"Maximize the profit by selling at | C # implementation of above approach : ; Function to find profit ; Calculating profit for each gadget ; sort the profit array in descending order ; variable to calculate total profit ; check for best M profits ; Driver Code"
Solution in C#:

using System ; class GFG { static int solve ( int N , int M , int [ ] cp , int [ ] sp ) { int [ ] profit = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) profit [ i ] = sp [ i ] - cp [ i ] ; Array . Sort ( profit ) ; Array . Reverse ( profit ) ; int sum = 0 ; for ( int i = 0 ; i < M ; i ++ ) { if ( profit [ i ] > 0 ) sum += profit [ i ] ; else break ; } return sum ; } public static void Main ( ) { int N = 5 , M = 3 ; int [ ] CP = { 5 , 10 , 35 , 7 , 23 } ; int [ ] SP = { 11 , 10 , 0 , 9 , 19 } ; Console . Write ( solve ( N , M , CP , SP ) ) ; } }


"Maximize the rightmost element of an array in k operations in Linear Time | C # program for the above approach ; Function to calculate maximum value of Rightmost element ; Initializing ans to store Maximum valued rightmost element ; Calculating maximum value of Rightmost element ; returning rightmost element ; Driver Code ; Given Input ; Function Call"
Solution in C#:

using System ; public class GFG { static int maxRightmostElement ( int N , int k , int p , int [ ] arr ) { int ans = arr [ N - 1 ] ; for ( int i = N - 2 ; i >= 0 ; i -- ) { int d = Math . Min ( arr [ i ] / p , k / ( N - 1 - i ) ) ; k -= d * ( N - 1 - i ) ; ans += d * p ; } return ans ; } public static void Main ( string [ ] args ) { int N = 4 , k = 5 , p = 2 ; int [ ] arr = { 3 , 8 , 1 , 4 } ; Console . WriteLine ( maxRightmostElement ( N , k , p , arr ) ) ; } }


"Maximize the rightmost element of an array in k operations in Linear Time | Function to calculate maximum value of Rightmost element ; Calculating maximum value of Rightmost element ; Checking if arr [ i ] is operationable ; Performing operation of i - th element ; Decreasing the value of k by 1 ; returning the rightmost element ; Driver Code ; Given Input ; Function Call"
Solution in C#:

using System ; public class GFG { static int maxRightmostElement ( int N , int k , int p , int [ ] arr ) { while ( k > 0 ) { for ( int i = N - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] >= p ) { arr [ i ] = arr [ i ] - p ; arr [ i + 1 ] = arr [ i + 1 ] + p ; break ; } } k -- ; } return arr [ N - 1 ] ; } static public void Main ( ) { int N = 4 , k = 5 , p = 2 ; int [ ] arr = { 3 , 8 , 1 , 4 } ; Console . WriteLine ( maxRightmostElement ( N , k , p , arr ) ) ; } }


"Maximize the size of array by deleting exactly k sub | C # implementation of the approach ; Sieve of Eratosthenes ; Function to return the size of the maximized array ; Insert the indices of composite numbers int num = 0 ; ; Compute the number of prime between two consecutive composite num = 0 ; ; Sort the diff vector ; Compute the prefix sum of diff vector ; Impossible case ; Delete sub - arrays of length 1 ; Find the number of primes to be deleted when deleting the sub - arrays ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int N = 1000005 ; static int [ ] prime = new int [ N ] ; static void seive ( ) { for ( int i = 2 ; i < N ; i ++ ) { if ( prime [ i ] == 0 ) { for ( int j = i + i ; j < N ; j += i ) { prime [ j ] = 1 ; } } } prime [ 1 ] = 1 ; } static int maxSizeArr ( int [ ] arr , int n , int k ) { List < int > v = new List < int > ( ) ; List < int > diff = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] == 1 ) { v . Add ( i ) ; } } for ( int i = 1 ; i < v . Count ; i ++ ) { diff . Add ( v [ i ] - v [ i - 1 ] - 1 ) ; } diff . Sort ( ) ; for ( int i = 1 ; i < diff . Count ; i ++ ) { diff [ i ] = diff [ i ] + diff [ i - 1 ] ; } if ( k > n || ( k == 0 && v . Count > 0 ) ) { return - 1 ; } else if ( v . Count <= k ) { return ( n - k ) ; } else if ( v . Count > k ) { int tt = v . Count - k ; int sum = 0 ; sum += diff [ tt - 1 ] ; int res = n - ( v . Count + sum ) ; return res ; } return 1 ; } public static void Main ( String [ ] args ) { seive ( ) ; int [ ] arr = { 2 , 4 , 2 , 2 , 4 , 2 , 4 , 2 } ; int n = arr . Length ; int k = 2 ; Console . WriteLine ( maxSizeArr ( arr , n , k ) ) ; } }


"Maximize the smallest array element by incrementing all elements in a K | C # program for above approach ; Function to check if the smallest value of v is achievable or not ; Create array to store previous moves ; Remove previous moves ; Add balance to ans ; Update contiguous subarray of length k ; Number of moves should not exceed m ; Function to find the maximum value of the smallest array element that can be obtained ; Perform Binary search ; Driver Code ; Given Input ; Function Call"
Solution in C#:

using System ; public class GFG { static long n , m , k , l , r , i ; static bool check ( long v , long [ ] a ) { long tec = 0 , ans = 0 ; long [ ] b = new long [ ( int ) ( n + k + 1 ) ] ; for ( int i = 0 ; i < n ; i ++ ) { tec -= b [ i ] ; if ( a [ i ] + tec < v ) { long mov = v - a [ i ] - tec ; ans = ans + mov ; tec += mov ; b [ i + ( int ) k ] = mov ; } } return ans <= m ; } static long FindLargest ( long [ ] a ) { l = 1 ; r = ( long ) Math . Pow ( 10 , 10 ) ; while ( r - l > 0 ) { long tm = ( l + r + 1 ) / 2 ; if ( check ( tm , a ) ) l = tm ; else r = tm - 1 ; } return l ; } public static void Main ( String [ ] args ) { long [ ] a = { 2 , 2 , 2 , 2 , 1 , 1 } ; m = 2 ; k = 3 ; n = a . Length ; Console . WriteLine ( FindLargest ( a ) ) ; } }


"Maximize the subarray sum after multiplying all elements of any subarray with X | C # implementation of the approach ; Function to return the maximum sum ; Base case ; If already calculated ; If no elements have been chosen ; Do not choose any element and use Kadane 's algorithm by taking max ; Choose the sub - array and multiply x ; Choose the sub - array and multiply x ; End the sub - array multiplication ; else No more multiplication ; Memoize and return the answer ; Function to get the maximum sum ; Initialize dp with - 1 ; Iterate from every position and find the maximum sum which is possible ; Driver code"
Solution in C#:

using System ; class GFG { static int N = 5 ; static int func ( int idx , int cur , int [ ] a , int [ , ] dp , int n , int x ) { if ( idx == n ) { return 0 ; } if ( dp [ idx , cur ] != - 1 ) { return dp [ idx , cur ] ; } int ans = 0 ; if ( cur == 0 ) { ans = Math . Max ( ans , a [ idx ] + func ( idx + 1 , 0 , a , dp , n , x ) ) ; ans = Math . Max ( ans , x * a [ idx ] + func ( idx + 1 , 1 , a , dp , n , x ) ) ; } else if ( cur == 1 ) { ans = Math . Max ( ans , x * a [ idx ] + func ( idx + 1 , 1 , a , dp , n , x ) ) ; ans = Math . Max ( ans , a [ idx ] + func ( idx + 1 , 2 , a , dp , n , x ) ) ; } { ans = Math . Max ( ans , a [ idx ] + func ( idx + 1 , 2 , a , dp , n , x ) ) ; } return dp [ idx , cur ] = ans ; } static int getMaximumSum ( int [ ] a , int n , int x ) { int [ , ] dp = new int [ n , 3 ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < 3 ; j ++ ) { dp [ i , j ] = - 1 ; } } int maxi = 0 ; for ( int i = 0 ; i < n ; i ++ ) { maxi = Math . Max ( maxi , func ( i , 0 , a , dp , n , x ) ) ; } return maxi ; } public static void Main ( String [ ] args ) { int [ ] a = { - 3 , 8 , - 2 , 1 , - 6 } ; int n = a . Length ; int x = - 1 ; Console . WriteLine ( getMaximumSum ( a , n , x ) ) ; } }


"Maximize the sum of Kth column of a Matrix | C # program to implement the above approach ; Function to maximize the Kth column sum ; Store all the elements of the resultant matrix of size N * N ; Store value of each elements of the matrix ; Fill all the columns < K ; Fill all the columns >= K ; Function to print the matrix ; Driver Code"
Solution in C#:

using System ; class GFG { static int [ , ] findMatrix ( int N , int K ) { int [ , ] mat = new int [ N , N ] ; int element = 1 ; for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < K - 1 ; ++ j ) { mat [ i , j ] = element ++ ; } } for ( int i = 0 ; i < N ; ++ i ) { for ( int j = K - 1 ; j < N ; ++ j ) { mat [ i , j ] = element ++ ; } } return mat ; } static void printMatrix ( int [ , ] mat , int N ) { for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < N ; ++ j ) { Console . Write ( mat [ i , j ] + " ▁ " ) ; } Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { int N = 3 , K = 2 ; int [ , ] mat = findMatrix ( N , K ) ; printMatrix ( mat , N ) ; } }


"Maximize the sum of arr [ i ] * i | C # program to find the maximum value of i * arr [ i ] ; Sort the array ; Finding the sum of arr [ i ] * i ; Driver code"
Solution in C#:

using System ; class GFG { static int maxSum ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += ( arr [ i ] * i ) ; return sum ; } static public void Main ( ) { int [ ] arr = { 3 , 5 , 6 , 1 } ; int n = arr . Length ; Console . WriteLine ( maxSum ( arr , n ) ) ; } }


"Maximize the sum of array by multiplying prefix of array with | C # implementation of the approach ; To store sum ; To store ending indices of the chosen prefix array vect ; Adding the absolute value of a [ i ] ; If i == 0 then there is no index to be flipped in ( i - 1 ) position ; print the maximised sum ; print the ending indices of the chosen prefix arrays ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void maxSum ( int [ ] a , int n ) { List < int > l = new List < int > ( ) ; int s = 0 ; for ( int i = 0 ; i < n ; i ++ ) { s += Math . Abs ( a [ i ] ) ; if ( a [ i ] >= 0 ) continue ; if ( i == 0 ) l . Add ( i + 1 ) ; else { l . Add ( i + 1 ) ; l . Add ( i ) ; } } Console . WriteLine ( s ) ; for ( int i = 0 ; i < l . Count ; i ++ ) Console . Write ( l [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int n = 4 ; int [ ] a = { 1 , - 2 , - 3 , 4 } ; maxSum ( a , n ) ; } }


"Maximize the sum of differences of consecutive elements after removing exactly K elements | C # implementation of the approach ; Function to return the maximized sum ; Remove any k internal elements ; Driver code"
Solution in C#:

using System ; class GFG { static int findSum ( int [ ] arr , int n , int k ) { if ( k <= n - 2 ) return ( arr [ n - 1 ] - arr [ 0 ] ) ; return 0 ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . Length ; int k = 1 ; Console . WriteLine ( findSum ( arr , n , k ) ) ; } }


"Maximize the sum of modulus with every Array element | C # program to find the maximum sum of modulus with every array element ; Function to return the maximum sum of modulus with every array element ; Sum of array elements ; Return the answer ; Driver Code"
Solution in C#:

using System ; class GFG { static int maxModulosum ( int [ ] a , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; } return sum - n ; } public static void Main ( String [ ] args ) { int [ ] a = new int [ ] { 3 , 4 , 6 } ; int n = a . Length ; Console . Write ( maxModulosum ( a , n ) ) ; } }


"Maximize the sum of products of the degrees between any two vertices of the tree | C # implementation of above approach ; Function to return the maximum possible sum ; Initialize degree for node u to 2 ; If u is the leaf node or the root node ; Initialize degree for node v to 2 ; If v is the leaf node or the root node ; Update the sum ; Driver code"
Solution in C#:

using System ; class GFG { static int maxSum ( int N ) { int ans = 0 ; for ( int u = 1 ; u <= N ; u ++ ) { for ( int v = 1 ; v <= N ; v ++ ) { if ( u == v ) continue ; int degreeU = 2 ; if ( u == 1 u == N ) degreeU = 1 ; int degreeV = 2 ; if ( v == 1 v == N ) degreeV = 1 ; ans += ( degreeU * degreeV ) ; } } return ans ; } static void Main ( ) { int N = 6 ; Console . WriteLine ( maxSum ( N ) ) ; } }


"Maximize the sum of selected numbers from an array to make it empty | C # implementation of the approach ; Function to maximise the sum of selected numbers by deleting occurences of Ai - 1 and Ai + 1 ; Number of elements in the array ; Largest element in the array ; An array to count the occurence of each element ; ans to store the result ; Using the above mentioned approach ; if occurence is greater than 0 ; add it to ans ; decrease i - 1 th element by 1 ; decrease ith element by 1 ; decrease i ; Driver code"
Solution in C#:

using System ; using System . Linq ; class GFG { static int getMaximumSum ( int [ ] arr ) { int n = arr . Length ; int max = arr . Max ( ) ; int [ ] freq = new int [ max + 1 ] ; for ( int j = 0 ; j < n ; j ++ ) { freq [ arr [ j ] ] ++ ; } int ans = 0 , i = max ; while ( i > 0 ) { if ( freq [ i ] > 0 ) { ans += i ; freq [ i - 1 ] -- ; freq [ i ] -- ; } else { i -- ; } } return ans ; } public static void Main ( string [ ] args ) { int [ ] a = { 1 , 2 , 3 } ; Console . Write ( getMaximumSum ( a ) ) ; } }


"Maximize the sum of sum of the Array by removing end elements | C # program to maximize the sum of sum of the Array by removing end elements ; Function to find the maximum sum of sum ; Compute the sum of whole array ; Traverse and remove the minimum value from an end to maximum the sum value ; If the left end element is smaller than right end ; Remove the left end element ; If the right end element is smaller than left end ; Remove the right end element ; Add the remaining element sum in the result ; Return the maximum sum of sum ; Driver code"
Solution in C#:

using System ; class GFG { static int maxRemainingSum ( int [ ] arr , int n ) { int i , sum = 0 ; for ( i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; i = 0 ; int j = n - 1 ; int result = 0 ; while ( i < j ) { if ( arr [ i ] < arr [ j ] ) { sum -= arr [ i ] ; i ++ ; } else { sum -= arr [ j ] ; j -- ; } result += sum ; } return result ; } public static void Main ( ) { int [ ] arr = { 3 , 1 , 7 , 2 , 1 } ; int N = arr . Length ; Console . Write ( maxRemainingSum ( arr , N ) ) ; } }