"Maximum score assigned to a subsequence of numerically consecutive and distinct array elements | C # program for the above approach ; Function to find the maximum score with unique element in the subset ; Base Case ; Check if the previously picked element differs by 1 from the current element ; Calculate score by including the current element ; Calculate score by excluding the current element ; Return maximum of the two possibilities ; Driver Code ; Given arrays ; Function Call"
Solution in C#:

using System ; class GFG { public static int maximumSum ( int [ ] a , int [ ] b , int n , int index , int lastpicked ) { if ( index == n ) return 0 ; int option1 = 0 , option2 = 0 ; if ( lastpicked == - 1 a [ lastpicked ] != a [ index ] ) option1 = b [ index ] + maximumSum ( a , b , n , index + 1 , index ) ; option2 = maximumSum ( a , b , n , index + 1 , lastpicked ) ; return Math . Max ( option1 , option2 ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 3 , 3 , 1 } ; int [ ] brr = { - 1 , 2 , 10 , 20 , - 10 , - 9 } ; int N = arr . Length ; Console . WriteLine ( maximumSum ( arr , brr , N , 0 , - 1 ) ) ; } }


"Maximum score of deleting an element from an Array based on given condition | C # implementation to find the maximum score of the deleting a element from an array ; Function to find the maximum score of the deleting an element from an array ; Creating a map to keep the frequency of numbers ; Loop to iterate over the elements of the array ; Creating a DP array to keep count of max score at ith element and it will be filled in the bottom Up manner ; Loop to choose the elements of the array to delete from the array ; Driver Code ; Function call"
Solution in C#:

using System ; using System . Linq ; using System . Collections . Generic ; class GFG { static int findMaximumScore ( int [ ] a , int n ) { Dictionary < int , int > freq = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq . ContainsKey ( a [ i ] ) ) { freq [ a [ i ] ] = freq [ a [ i ] ] + 1 ; } else { freq . Add ( a [ i ] , 1 ) ; } } int [ ] dp = new int [ a . Max ( ) + 1 ] ; dp [ 0 ] = 0 ; dp [ 1 ] = freq [ 1 ] ; for ( int i = 2 ; i < dp . Length ; i ++ ) dp [ i ] = Math . Max ( dp [ i - 1 ] , dp [ i - 2 ] + freq [ i ] * i ) ; return dp [ dp . Length - 1 ] ; } public static void Main ( String [ ] args ) { int n ; n = 3 ; int [ ] a = { 1 , 2 , 3 } ; Console . Write ( findMaximumScore ( a , n ) ) ; } }


"Maximum score possible after performing given operations on an Array | C # program to find the maximum Score after given operations ; Memoizing by the use of a table ; Function to calculate maximum score ; Bse case ; If the same state has already been computed ; Sum of array in range ( l , r ) ; If the operation is even - numbered the score is decremented ; Exploring all paths , and storing maximum value in DP table to avoid further repetitive recursive calls ; Function to find the max score ; Prefix sum array ; Calculating prefix_sum ; Initialising the DP table , - 1 represents the subproblem hasn 't been solved yet ; Driver code"
Solution in C#:

using System ; public class GFG { static int [ , , ] dp = new int [ 100 , 100 , 100 ] ; static int MaximumScoreDP ( int l , int r , int [ ] prefix_sum , int num ) { if ( l > r ) return 0 ; if ( dp [ l , r , num ] != - 1 ) return dp [ l , r , num ] ; int current_sum = prefix_sum [ r ] - ( l - 1 >= 0 ? prefix_sum [ l - 1 ] : 0 ) ; if ( num % 2 == 0 ) current_sum *= - 1 ; dp [ l , r , num ] = current_sum + Math . Max ( MaximumScoreDP ( l + 1 , r , prefix_sum , num + 1 ) , MaximumScoreDP ( l , r - 1 , prefix_sum , num + 1 ) ) ; return dp [ l , r , num ] ; } static int findMaxScore ( int [ ] a , int n ) { int [ ] prefix_sum = new int [ n ] ; prefix_sum [ 0 ] = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { prefix_sum [ i ] = prefix_sum [ i - 1 ] + a [ i ] ; } for ( int i = 0 ; i < 100 ; i ++ ) { for ( int j = 0 ; j < 100 ; j ++ ) { for ( int l = 0 ; l < 100 ; l ++ ) dp [ i , j , l ] = - 1 ; } } return MaximumScoreDP ( 0 , n - 1 , prefix_sum , 1 ) ; } public static void Main ( String [ ] args ) { int n = 6 ; int [ ] A = { 1 , 2 , 3 , 4 , 2 , 6 } ; Console . Write ( findMaxScore ( A , n ) ) ; } }


"Maximum score possible after performing given operations on an Array | C # program to find the maximum score after given operations ; Function to calculate maximum score recursively ; Base case ; Sum of array in range ( l , r ) ; If the operation is even - numbered the score is decremented ; Exploring all paths , by removing leftmost and rightmost element and selecting the maximum value ; Function to find the max score ; Prefix sum array ; Calculating prefix_sum ; Driver code"
Solution in C#:

using System ; class GFG { static int maxScore ( int l , int r , int [ ] prefix_sum , int num ) { if ( l > r ) return 0 ; int current_sum = prefix_sum [ r ] - ( l - 1 >= 0 ? prefix_sum [ l - 1 ] : 0 ) ; if ( num % 2 == 0 ) current_sum *= - 1 ; return current_sum + Math . Max ( maxScore ( l + 1 , r , prefix_sum , num + 1 ) , maxScore ( l , r - 1 , prefix_sum , num + 1 ) ) ; } static int findMaxScore ( int [ ] a , int n ) { int [ ] prefix_sum = new int [ n ] ; prefix_sum [ 0 ] = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { prefix_sum [ i ] = prefix_sum [ i - 1 ] + a [ i ] ; } return maxScore ( 0 , n - 1 , prefix_sum , 1 ) ; } public static void Main ( String [ ] args ) { int n = 6 ; int [ ] A = { 1 , 2 , 3 , 4 , 2 , 6 } ; Console . Write ( findMaxScore ( A , n ) ) ; } }


"Maximum score possible by removing substrings made up of single distinct character | C # program for the above approach ; Function to check if the string s consists of a single distinct character or not ; Function to calculate the maximum score possible by removing substrings ; If string is empty ; If length of string is 1 ; Store the maximum result ; Try to remove all substrings that satisfy the condition and check for resultant string after removal ; Store the substring { s [ i ] , . . , s [ j ] } ; Check if the substring contains only a single distinct character ; Return the maximum score ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static bool isUnique ( string s ) { HashSet < char > set = new HashSet < char > ( ) ; foreach ( char c in s ) set . Add ( c ) ; return set . Count == 1 ; } static int maxScore ( string s , int [ ] a ) { int n = s . Length ; if ( n == 0 ) return 0 ; if ( n == 1 ) return a [ 0 ] ; int mx = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { string sub = s . Substring ( i , j + 1 - i ) ; if ( isUnique ( sub ) ) mx = Math . Max ( mx , a [ sub . Length - 1 ] + maxScore ( s . Substring ( 0 , i ) + s . Substring ( j + 1 ) , a ) ) ; } } return mx ; } static void Main ( ) { string s = "011" ; int [ ] a = { 1 , 3 , 1 } ; Console . Write ( maxScore ( s , a ) ) ; } }


"Maximum score possible by removing substrings made up of single distinct character | C # program for the above approach ; Initialize a dictionary to store the precomputed results ; Function to calculate the maximum score possible by removing substrings ; If s is present in dp [ ] array ; Base Cases : ; If length of string is 0 ; If length of string is 1 ; Put head pointer at start ; Initialize the max variable ; Generate the substrings using two pointers ; If s [ head ] and s [ tail ] are different ; Move head to tail and break ; Store the substring ; Update the maximum ; Move the tail ; Store the score ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static Dictionary < string , int > dp = new Dictionary < string , int > ( ) ; static int maxScore ( string s , int [ ] a ) { if ( dp . ContainsKey ( s ) ) return dp [ s ] ; int n = s . Length ; if ( n == 0 ) return 0 ; if ( n == 1 ) return a [ 0 ] ; int head = 0 ; int mx = - 1 ; while ( head < n ) { int tail = head ; while ( tail < n ) { if ( s [ tail ] != s [ head ] ) { head = tail ; break ; } string sub = s . Substring ( head , tail + 1 - head ) ; mx = Math . Max ( mx , a [ sub . Length - 1 ] + maxScore ( s . Substring ( 0 , head ) + s . Substring ( tail + 1 , s . Length - tail - 1 ) , a ) ) ; tail += 1 ; } if ( tail == n ) break ; } dp . Add ( s , mx ) ; return mx ; } static public void Main ( ) { string s = " abb " ; int [ ] a = { 1 , 3 , 1 } ; Console . WriteLine ( ( maxScore ( s , a ) ) ) ; } }


"Maximum score possible from an array with jumps of at most length K | C # program for the above approach ; Function to count the maximum score of an index ; Base Case ; If the value for the current index is pre - calculated ; Calculate maximum score for all the steps in the range from i + 1 to i + k ; Score for index ( i + j ) ; Update dp [ i ] and return the maximum value ; Function to get maximum score possible from the array A [ ] ; Array to store memoization ; Initialize dp [ ] with - 1 ; Driver Code"
Solution in C#:

using System ; class GFG { static int maxScore ( int i , int [ ] A , int K , int N , int [ ] dp ) { if ( i >= N - 1 ) return A [ N - 1 ] ; if ( dp [ i ] != - 1 ) return dp [ i ] ; int score = int . MinValue ; for ( int j = 1 ; j <= K ; j ++ ) { score = Math . Max ( score , maxScore ( i + j , A , K , N , dp ) ) ; } return dp [ i ] = score + A [ i ] ; } static void getScore ( int [ ] A , int N , int K ) { int [ ] dp = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) dp [ i ] = - 1 ; Console . WriteLine ( maxScore ( 0 , A , K , N , dp ) ) ; } static public void Main ( ) { int [ ] A = { 100 , - 30 , - 50 , - 15 , - 20 , - 30 } ; int K = 3 ; int N = A . Length ; getScore ( A , N , K ) ; } }


"Maximum segment value after putting k breakpoints in a number | C # program to find the maximum segment value after putting k breaks . ; Function to Find Maximum Number ; Maximum segment length ; Find value of first segment of seg_len ; Find value of remaining segments using sliding window ; To find value of current segment , first remove leading digit from previous value ; Then add trailing digit ; Driver code"
Solution in C#:

using System ; class GFG { static int findMaxSegment ( string s , int k ) { int seg_len = s . Length - k ; int res = 0 ; for ( int i = 0 ; i < seg_len ; i ++ ) res = res * 10 + ( s [ i ] - '0' ) ; int seg_len_pow = ( int ) Math . Pow ( 10 , seg_len - 1 ) ; int curr_val = res ; for ( int i = 1 ; i <= ( s . Length - seg_len ) ; i ++ ) { curr_val = curr_val - ( s [ i - 1 ] - '0' ) * seg_len_pow ; curr_val = curr_val * 10 + ( s [ i + seg_len - 1 ] - '0' ) ; res = Math . Max ( res , curr_val ) ; } return res ; } public static void Main ( ) { String s = "8754" ; int k = 2 ; Console . WriteLine ( " Maximum ▁ number ▁ = ▁ " + findMaxSegment ( s , k ) ) ; } }


"Maximum set bit sum in array without considering adjacent elements | C # program to maximum set bit sum in array without considering adjacent elements ; Function to count total number of set bits in an integer ; Maximum sum of set bits ; Calculate total number of set bits for every element of the array ; find total set bits for each number and store back into the array ; current max excluding i ; current max including i ; return max of incl and excl ; Driver code"
Solution in C#:

using System ; class GFG { static int bit ( int n ) { int count = 0 ; while ( n > 0 ) { count ++ ; n = n & ( n - 1 ) ; } return count ; } static int maxSumOfBits ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = bit ( arr [ i ] ) ; } int incl = arr [ 0 ] ; int excl = 0 ; int excl_new ; for ( int i = 1 ; i < n ; i ++ ) { excl_new = ( incl > excl ) ? incl : excl ; incl = excl + arr [ i ] ; excl = excl_new ; } return ( ( incl > excl ) ? incl : excl ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 4 , 5 , 6 , 7 , 20 , 25 } ; int n = arr . Length ; Console . WriteLine ( maxSumOfBits ( arr , n ) ) ; } }


"Maximum size of square such that all submatrices of that size have sum less than K | C # implementation to find the maximum size square submatrix such that their sum is less than K ; Size of matrix ; Function to preprocess the matrix for computing the sum of every possible matrix of the given size ; Loop to copy the first row of the matrix into the aux matrix ; Computing the sum column - wise ; Computing row wise sum ; Function to find the sum of a submatrix with the given indices ; Overall sum from the top to right corner of matrix ; Removing the sum from the top corer of the matrix ; Remove the overlapping sum ; Add the sum of top corner which is subtracted twice ; Function to check whether square sub matrices of size mid satisfy the condition or not ; Iterating throught all possible submatrices of given size ; Function to find the maximum square size possible with the such that every submatrix have sum less than the given sum ; Search space ; Binary search for size ; Check if the mid satisfies the given condition ; Driver Code"
Solution in C#:

using System ; class GFG { static readonly int N = 4 ; static readonly int M = 5 ; static void preProcess ( int [ , ] mat , int [ , ] aux ) { for ( int i = 0 ; i < M ; i ++ ) aux [ 0 , i ] = mat [ 0 , i ] ; for ( int i = 1 ; i < N ; i ++ ) for ( int j = 0 ; j < M ; j ++ ) aux [ i , j ] = mat [ i , j ] + aux [ i - 1 , j ] ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 1 ; j < M ; j ++ ) aux [ i , j ] += aux [ i , j - 1 ] ; } static int sumQuery ( int [ , ] aux , int tli , int tlj , int rbi , int rbj ) { int res = aux [ rbi , rbj ] ; if ( tli > 0 ) res = res - aux [ tli - 1 , rbj ] ; if ( tlj > 0 ) res = res - aux [ rbi , tlj - 1 ] ; if ( tli > 0 && tlj > 0 ) res = res + aux [ tli - 1 , tlj - 1 ] ; return res ; } static bool check ( int mid , int [ , ] aux , int K ) { bool satisfies = true ; for ( int x = 0 ; x < N ; x ++ ) { for ( int y = 0 ; y < M ; y ++ ) { if ( x + mid - 1 <= N - 1 && y + mid - 1 <= M - 1 ) { if ( sumQuery ( aux , x , y , x + mid - 1 , y + mid - 1 ) > K ) satisfies = false ; } } } return ( satisfies == true ) ; } static int maximumSquareSize ( int [ , ] mat , int K ) { int [ , ] aux = new int [ N , M ] ; preProcess ( mat , aux ) ; int low = 1 , high = Math . Min ( N , M ) ; int mid ; while ( high - low > 1 ) { mid = ( low + high ) / 2 ; if ( check ( mid , aux , K ) ) { low = mid ; } else high = mid ; } if ( check ( high , aux , K ) ) return high ; return low ; } public static void Main ( String [ ] args ) { int K = 30 ; int [ , ] mat = { { 1 , 2 , 3 , 4 , 6 } , { 5 , 3 , 8 , 1 , 2 } , { 4 , 6 , 7 , 5 , 5 } , { 2 , 4 , 8 , 9 , 4 } } ; Console . Write ( maximumSquareSize ( mat , K ) ) ; } }


"Maximum size of square such that all submatrices of that size have sum less than K | C # implementation to find the maximum size square submatrix such that their sum is less than K ; Size of matrix ; Function to preprocess the matrix for computing the sum of every possible matrix of the given size ; Loop to copy the first row of the matrix into the aux matrix ; Computing the sum column - wise ; Computing row wise sum ; Function to find the sum of a submatrix with the given indices ; Overall sum from the top to right corner of matrix ; Removing the sum from the top corer of the matrix ; Remove the overlapping sum ; Add the sum of top corner which is subtracted twice ; Function to find the maximum square size possible with the such that every submatrix have sum less than the given sum ; Loop to choose the size of matrix ; Loop to find the sum of the matrix of every possible submatrix ; Driver Code"
Solution in C#:

using System ; public class GFG { static readonly int N = 4 ; static readonly int M = 5 ; static void preProcess ( int [ , ] mat , int [ , ] aux ) { for ( int i = 0 ; i < M ; i ++ ) aux [ 0 , i ] = mat [ 0 , i ] ; for ( int i = 1 ; i < N ; i ++ ) for ( int j = 0 ; j < M ; j ++ ) aux [ i , j ] = mat [ i , j ] + aux [ i - 1 , j ] ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 1 ; j < M ; j ++ ) aux [ i , j ] += aux [ i , j - 1 ] ; } static int sumQuery ( int [ , ] aux , int tli , int tlj , int rbi , int rbj ) { int res = aux [ rbi , rbj ] ; if ( tli > 0 ) res = res - aux [ tli - 1 , rbj ] ; if ( tlj > 0 ) res = res - aux [ rbi , tlj - 1 ] ; if ( tli > 0 && tlj > 0 ) res = res + aux [ tli - 1 , tlj - 1 ] ; return res ; } static int maximumSquareSize ( int [ , ] mat , int K ) { int [ , ] aux = new int [ N , M ] ; preProcess ( mat , aux ) ; for ( int i = Math . Min ( N , M ) ; i >= 1 ; i -- ) { bool satisfies = true ; for ( int x = 0 ; x < N ; x ++ ) { for ( int y = 0 ; y < M ; y ++ ) { if ( x + i - 1 <= N - 1 && y + i - 1 <= M - 1 ) { if ( sumQuery ( aux , x , y , x + i - 1 , y + i - 1 ) > K ) satisfies = false ; } } } if ( satisfies == true ) return ( i ) ; } return 0 ; } public static void Main ( String [ ] args ) { int K = 30 ; int [ , ] mat = { { 1 , 2 , 3 , 4 , 6 } , { 5 , 3 , 8 , 1 , 2 } , { 4 , 6 , 7 , 5 , 5 } , { 2 , 4 , 8 , 9 , 4 } } ; Console . Write ( maximumSquareSize ( mat , K ) ) ; } }


"Maximum size of sub | C # implementation of the approach ; Function that compares a and b ; Function to return length of longest subarray that satisfies one of the given conditions ; Driver Code ; Print the required answer"
Solution in C#:

using System ; class GFG { static int cmp ( int a , int b ) { if ( a > b ) return 1 ; else if ( a == b ) return 0 ; else return - 1 ; } static int maxSubarraySize ( int [ ] arr , int n ) { int ans = 1 ; int anchor = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int c = cmp ( arr [ i - 1 ] , arr [ i ] ) ; if ( c == 0 ) anchor = i ; else if ( i == n - 1 || c * cmp ( arr [ i ] , arr [ i + 1 ] ) != - 1 ) { ans = Math . Max ( ans , i - anchor + 1 ) ; anchor = i ; } } return ans ; } static void Main ( ) { int [ ] arr = { 9 , 4 , 2 , 10 , 7 , 8 , 8 , 1 , 9 } ; int n = arr . Length ; Console . WriteLine ( maxSubarraySize ( arr , n ) ) ; } }


"Maximum size of subset of given array such that a triangle can be formed by any three integers as the sides of the triangle | C # program for the above approach ; Function to find the maximum size of the subset of the given array such that a triangle can be formed from any three integers of the subset as sides ; Sort arr [ ] in increasing order ; Stores the maximum size of a valid subset of the given array ; Stores the starting index of the current window ; Stores the last index of the current window ; Iterate over the array arr [ ] ; Increment j till the value of arr [ i ] + arr [ i + 1 ] > arr [ j ] holds true ; Update the value of maxSize ; Return Answer ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int maximizeSubset ( int [ ] arr , int N ) { Array . Sort ( arr ) ; int maxSize = 0 ; int i = 0 ; int j = i + 2 ; while ( i < N - 2 ) { if ( j >= N i + 1 >= N ) break ; while ( j < N && arr [ i ] + arr [ i + 1 ] > arr [ j ] ) { j ++ ; } maxSize = Math . Max ( maxSize , j - i ) ; i ++ ; j = Math . Max ( j , i + 2 ) ; } return maxSize ; } public static void Main ( ) { int [ ] arr = { 2 , 7 , 4 , 1 , 6 , 9 , 5 , 3 } ; int N = arr . Length ; Console . Write ( maximizeSubset ( arr , N ) ) ; } }


"Maximum size of subset such that product of all subset elements is a factor of N | C # program for the above approach ; Function to find the maximum size of the subset such that the product of subset elements is a factor of N ; Base Case ; Stores maximum size of valid subset ; Traverse the given array ; If N % arr [ i ] = 0 , include arr [ i ] in a subset and recursively call for the remaining array integers ; Return Answer ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int maximizeSubset ( int N , int [ ] arr , int M , int x ) { if ( x == M ) { return 0 ; } int ans = 0 ; for ( int i = x ; i < M ; i ++ ) { if ( N % arr [ i ] == 0 ) { ans = Math . Max ( ans , maximizeSubset ( N / arr [ i ] , arr , M , x + 1 ) + 1 ) ; } } return ans ; } public static void Main ( ) { int N = 64 ; int [ ] arr = { 1 , 2 , 4 , 8 , 16 , 32 } ; int M = arr . Length ; Console . Write ( maximizeSubset ( N , arr , M , 0 ) ) ; } }


"Maximum size rectangle binary sub | C # program to find largest rectangle with all 1 s in a binary matrix ; Finds the maximum area under the histogram represented by histogram . See below article for details . https : www . geeksforgeeks . org / largest - rectangle - under - histogram / ; Create an empty stack . The stack holds indexes of hist [ ] array . The bars stored in stack are always in increasing order of their heights . ; Top of stack ; Initialize max area in ; current row ( or histogram ) Initialize area with ; current top Run through all bars of given histogram ( or row ) ; If this bar is higher than the bar on top stack , push it to stack ; If this bar is lower than top of stack , then calculate area of rectangle with stack top as the smallest ( or minimum height ) bar . ' i ' is ' right ▁ index ' for the top and element before top in stack is ' left ▁ index ' ; Now pop the remaining bars from stack and calculate area with every popped bar as the smallest bar ; Returns area of the largest rectangle with all 1 s in A [ ] [ ] ; Calculate area for first row and initialize it as result ; iterate over row to find maximum rectangular area considering each row as histogram ; if A [ i ] [ j ] is 1 then add A [ i - 1 ] [ j ] ; Update result if area with current row ( as last row of rectangle ) is more ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static int maxHist ( int R , int C , int [ ] row ) { Stack < int > result = new Stack < int > ( ) ; int top_val ; int max_area = 0 ; int area = 0 ; int i = 0 ; while ( i < C ) { if ( result . Count == 0 || row [ result . Peek ( ) ] <= row [ i ] ) { result . Push ( i ++ ) ; } else { top_val = row [ result . Peek ( ) ] ; result . Pop ( ) ; area = top_val * i ; if ( result . Count > 0 ) { area = top_val * ( i - result . Peek ( ) - 1 ) ; } max_area = Math . Max ( area , max_area ) ; } } while ( result . Count > 0 ) { top_val = row [ result . Peek ( ) ] ; result . Pop ( ) ; area = top_val * i ; if ( result . Count > 0 ) { area = top_val * ( i - result . Peek ( ) - 1 ) ; } max_area = Math . Max ( area , max_area ) ; } return max_area ; } public static int maxRectangle ( int R , int C , int [ ] [ ] A ) { int result = maxHist ( R , C , A [ 0 ] ) ; for ( int i = 1 ; i < R ; i ++ ) { for ( int j = 0 ; j < C ; j ++ ) { if ( A [ i ] [ j ] == 1 ) { A [ i ] [ j ] += A [ i - 1 ] [ j ] ; } } result = Math . Max ( result , maxHist ( R , C , A [ i ] ) ) ; } return result ; } public static void Main ( string [ ] args ) { int R = 4 ; int C = 4 ; int [ ] [ ] A = new int [ ] [ ] { new int [ ] { 0 , 1 , 1 , 0 } , new int [ ] { 1 , 1 , 1 , 1 } , new int [ ] { 1 , 1 , 1 , 1 } , new int [ ] { 1 , 1 , 0 , 0 } } ; Console . Write ( " Area ▁ of ▁ maximum ▁ rectangle ▁ is ▁ " + maxRectangle ( R , C , A ) ) ; } }


"Maximum size square Sub | C # program for the above approach ; Function to find the maximum size of matrix with sum <= K ; N size of rows and M size of cols ; To store the prefix sum of matrix ; Create prefix sum ; Traverse each rows ; Update the prefix sum till index i x j ; To store the maximum size of matrix with sum <= K ; Traverse the sum matrix ; Index out of bound ; Maximum possible size of matrix ; Binary Search ; Find middle index ; Check whether sum <= K or not If Yes check for other half of the search ; Else check it in first half ; Update the maximum size matrix ; Print the readonly answer ; Driver Code ; Given target sum ; Function Call"
Solution in C#:

 using System ; class GFG { static void findMaxMatrixSize ( int [ , ] arr , int K ) { int i , j ; int n = arr . GetLength ( 0 ) ; int m = arr . GetLength ( 1 ) ; int [ , ] sum = new int [ n + 1 , m + 1 ] ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= m ; j ++ ) { if ( i == 0 j == 0 ) { sum [ i , j ] = 0 ; continue ; } sum [ i , j ] = arr [ i - 1 , j - 1 ] + sum [ i - 1 , j ] + sum [ i , j - 1 ] - sum [ i - 1 , j - 1 ] ; } } int ans = 0 ; for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 ; j <= m ; j ++ ) { if ( i + ans - 1 > n j + ans - 1 > m ) break ; int mid , lo = ans ; int hi = Math . Min ( n - i + 1 , m - j + 1 ) ; while ( lo < hi ) { mid = ( hi + lo + 1 ) / 2 ; if ( sum [ i + mid - 1 , j + mid - 1 ] + sum [ i - 1 , j - 1 ] - sum [ i + mid - 1 , j - 1 ] - sum [ i - 1 , j + mid - 1 ] <= K ) { lo = mid ; } else { hi = mid - 1 ; } } ans = Math . Max ( ans , lo ) ; } } Console . Write ( ans + "
 " ) ; } public static void Main ( String [ ] args ) { int [ , ] arr = { { 1 , 1 , 3 , 2 , 4 , 3 , 2 } , { 1 , 1 , 3 , 2 , 4 , 3 , 2 } , { 1 , 1 , 3 , 2 , 4 , 3 , 2 } } ; int K = 4 ; findMaxMatrixSize ( arr , K ) ; } }


"Maximum spiral sum in Binary Tree | C # implementation to find maximum spiral sum ; A binary tree node has data , pointer to left child and a pointer to right child ; function to find the maximum sum contiguous subarray . implements kadane 's algorithm  ; to store the maximum value that is ending up to the current index ; to store the maximum value encountered so far ; traverse the array elements ; if max_ending_here < 0 , then it could not possibly contribute to the maximum sum further ; else add the value arr [ i ] to max_ending_here ; update max_so_far ; required maxium sum contiguous subarray value ; Function to find maximum spiral sum ; if tree is empty ; Create two stacks to store alternate levels For levels from right to left ; For levels from left to right ; ArrayList to store spiral order traversal of the binary tree ; Push first level to first stack ' s1' ; traversing tree in spiral form until there are elements in any one of the stacks ; traverse current level from s1 and push nodes of next level to s2 ; push temp - data to ' arr ' ; Note that right is pushed before left ; traverse current level from s2 and push nodes of next level to s1 ; push temp - data to ' arr ' ; Note that left is pushed before right ; required maximum spiral sum ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; public class MaxSpiralSum { public class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } ; static int maxSum ( List < int > arr ) { int max_ending_here = int . MinValue ; int max_so_far = int . MinValue ; for ( int i = 0 ; i < arr . Count ; i ++ ) { if ( max_ending_here < 0 ) max_ending_here = arr [ i ] ; else max_ending_here += arr [ i ] ; max_so_far = Math . Max ( max_so_far , max_ending_here ) ; } return max_so_far ; } public static int maxSpiralSum ( Node root ) { if ( root == null ) return 0 ; Stack < Node > s1 = new Stack < Node > ( ) ; Stack < Node > s2 = new Stack < Node > ( ) ; List < int > arr = new List < int > ( ) ; s1 . Push ( root ) ; while ( s1 . Count != 0 s2 . Count != 0 ) { while ( s1 . Count != 0 ) { Node temp = s1 . Pop ( ) ; arr . Add ( temp . data ) ; if ( temp . right != null ) s2 . Push ( temp . right ) ; if ( temp . left != null ) s2 . Push ( temp . left ) ; } while ( s2 . Count != 0 ) { Node temp = s2 . Pop ( ) ; arr . Add ( temp . data ) ; if ( temp . left != null ) s1 . Push ( temp . left ) ; if ( temp . right != null ) s1 . Push ( temp . right ) ; } } return maxSum ( arr ) ; } public static void Main ( String [ ] args ) { Node root = new Node ( - 2 ) ; root . left = new Node ( - 3 ) ; root . right = new Node ( 4 ) ; root . left . left = new Node ( 5 ) ; root . left . right = new Node ( 1 ) ; root . right . left = new Node ( - 2 ) ; root . right . right = new Node ( - 1 ) ; root . left . left . left = new Node ( - 3 ) ; root . right . right . right = new Node ( 2 ) ; Console . Write ( " Maximum ▁ Spiral ▁ Sum ▁ = ▁ " + maxSpiralSum ( root ) ) ; } }


"Maximum splits in binary string such that each substring is divisible by given odd number | C # program to split a given binary string into maximum possible segments divisible by given odd number K ; Function to calculate maximum splits possible ; Driver code"
Solution in C#:

using System ; class GFG { static void max_segments ( string str , int K ) { int n = str . Length ; int s = 0 ; int sum = 0 ; int count = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { int a = str [ i ] - '0' ; sum += a * ( int ) Math . Pow ( 2 , s ) ; s ++ ; if ( sum != 0 && sum % K == 0 ) { count ++ ; sum = 0 ; s = 0 ; } } if ( sum != 0 ) { Console . Write ( " - 1" ) ; } else { Console . Write ( count ) ; } } public static void Main ( ) { string str = "10111001" ; int K = 5 ; max_segments ( str , K ) ; } }


"Maximum steps to transform 0 to X with bitwise AND | C # code to find the maximum possible effort ; Function to get no . of set bits in binary representation of positive integer n ; Driver code"
Solution in C#:

using System ; class GFG { static int countSetBits ( int n ) { int count = 0 ; while ( n != 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } public static void Main ( String [ ] args ) { int i = 3 ; Console . Write ( countSetBits ( i ) ) ; } }


"Maximum students to pass after giving bonus to everybody and not exceeding 100 marks | C # Implementation of above approach . ; Function to return the number of students that can pass ; maximum marks ; maximum bonus marks that can be given ; counting the number of students that can pass ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; using System . Collections ; using System . Linq ; class GFG { static int check ( int n , List < int > marks ) { int x = marks . Max ( ) ; int bonus = 100 - x ; int c = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( marks [ i ] + bonus >= 50 ) c += 1 ; } return c ; } public static void Main ( ) { int n = 5 ; List < int > marks = new List < int > ( new int [ ] { 0 , 21 , 83 , 45 , 64 } ) ; Console . WriteLine ( check ( n , marks ) ) ; } }


"Maximum sub | C # implementation of the approach ; Function to return the maximum sum of the sub - sequence such that two consecutive elements have a difference of at least 3 in their indices in the given array ; If there is a single element in the array ; Either select it or don 't ; If there are two elements ; Either select the first element or don 't ; Either select the first or the second element or don 't select any element ; Either select the first element or don 't ; Either select the first or the second element or don 't select any element ; Either select first , second , third or nothing ; For the rest of the elements ; Either select the best sum till previous_index or select the current element + best_sum till index - 3 ; Driver code"
Solution in C#:

using System ; class GFG { static int max_sum ( int [ ] a , int n ) { int [ ] dp = new int [ n ] ; if ( n == 1 ) { dp [ 0 ] = Math . Max ( 0 , a [ 0 ] ) ; } else if ( n == 2 ) { dp [ 0 ] = Math . Max ( 0 , a [ 0 ] ) ; dp [ 1 ] = Math . Max ( a [ 1 ] , dp [ 0 ] ) ; } else if ( n >= 3 ) { dp [ 0 ] = Math . Max ( 0 , a [ 0 ] ) ; dp [ 1 ] = Math . Max ( a [ 1 ] , Math . Max ( 0 , a [ 0 ] ) ) ; dp [ 2 ] = Math . Max ( a [ 2 ] , Math . Max ( a [ 1 ] , Math . Max ( 0 , a [ 0 ] ) ) ) ; int i = 3 ; while ( i < n ) { dp [ i ] = Math . Max ( dp [ i - 1 ] , a [ i ] + dp [ i - 3 ] ) ; i ++ ; } } return dp [ n - 1 ] ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , - 2 , 4 , 3 } ; int n = arr . Length ; Console . Write ( max_sum ( arr , n ) ) ; } }


"Maximum sub | C # implementation to find the maximum area sub - matrix having count of 1 ' s ▁ one ▁ more ▁ than ▁ count ▁ of ▁ 0' s ; Function to find the length of longest subarray having count of 1 ' s ▁ one ▁ more ▁ than ▁ count ▁ of ▁ 0' s ; unordered_map ' um ' implemented as hash table ; Traverse the given array ; Accumulating sum ; When subarray starts form index '0' ; Make an entry for ' sum ' if it is not present in ' um ' ; Check if ' sum - 1' is present in ' um ' or not ; Update ' start ' , ' finish ' and maxLength ; Required maximum length ; Function to find the maximum area sub - matrix having count of 1 ' s ▁ one ▁ more ▁ than ▁ count ▁ of ▁ 0' s ; Variables to store final and intermediate results ; Set the left column ; Initialize all elements of temp as 0 ; Set the right column for the left column set by outer loop ; Calculate sum between current left and right for every row ' i ' , consider '0' as ' - 1' ; Function to set the ' start ' and ' finish ' variables having index values of temp [ ] which contains the longest subarray of temp [ ] having count of 1 ' s ▁ ▁ one ▁ more ▁ than ▁ count ▁ of ▁ 0' s ; Compare with maximum area so far and accordingly update the final variables ; Print final values ; Driver code"
Solution in C#:

 using System ; using System . Collections . Generic ; using System . Collections ; class GFG { static int lenOfLongSubarr ( int [ ] arr , int n , ref int start , ref int finish ) { Dictionary < int , int > um = new Dictionary < int , int > ( ) ; int sum = 0 , maxLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( sum == 1 ) { start = 0 ; finish = i ; maxLen = i + 1 ; } else if ( ! um . ContainsKey ( sum ) ) um [ sum ] = i ; if ( um . ContainsKey ( sum - 1 ) ) { if ( maxLen < ( i - um [ sum - 1 ] ) ) start = um [ sum - 1 ] + 1 ; finish = i ; maxLen = i - um [ sum - 1 ] ; } } return maxLen ; } static void largestSubmatrix ( int [ , ] mat , int n ) { int finalLeft = 0 , finalRight = 0 , finalTop = 0 , finalBottom = 0 ; int maxArea = 0 , len , start = 0 , finish = 0 ; int [ ] temp = new int [ n ] ; for ( int left = 0 ; left < n ; left ++ ) { Array . Fill ( temp , 0 ) ; for ( int right = left ; right < n ; right ++ ) { for ( int i = 0 ; i < n ; ++ i ) temp [ i ] += mat [ i , right ] == 0 ? - 1 : 1 ; len = lenOfLongSubarr ( temp , n , ref start , ref finish ) ; if ( ( len != 0 ) && ( maxArea < ( finish - start + 1 ) * ( right - left + 1 ) ) ) { finalLeft = left ; finalRight = right ; finalTop = start ; finalBottom = finish ; maxArea = ( finish - start + 1 ) * ( right - left + 1 ) ; } } } Console . Write ( " ( Top , ▁ Left ) : ▁ ( " + finalTop + " , ▁ " + finalLeft + " )
 " ) ; Console . Write ( " ( Bottom , ▁ Right ) : ▁ ( " + finalBottom + " , ▁ " + finalRight + " )
 " ) ; Console . Write ( " Maximum ▁ area : ▁ " + maxArea ) ; } public static void Main ( string [ ] args ) { int [ , ] mat = new int [ , ] { { 1 , 0 , 0 , 1 } , { 0 , 1 , 1 , 1 } , { 1 , 0 , 0 , 0 } , { 0 , 1 , 0 , 1 } } ; int n = 4 ; largestSubmatrix ( mat , n ) ; } }


"Maximum subarray product modulo M | C # program for above approach ; Function to find maximum subarray product modulo M and minimum length of the subarray ; Stores maximum subarray product modulo M and minimum length of the subarray ; Stores the minimum length of subarray having maximum product ; Traverse the array ; Stores the product of a subarray ; Calculate Subarray whose start index is i ; Multiply product by arr [ i ] ; If product greater than ans ; Update ans ; Update length ; Print maximum subarray product mod M ; Print minimum length of subarray having maximum product ; Driver code"
Solution in C#:

using System ; class GFG { static void maxModProdSubarr ( int [ ] arr , int n , int M ) { int ans = 0 ; int length = n ; for ( int i = 0 ; i < n ; i ++ ) { int product = 1 ; for ( int j = i ; j < n ; j ++ ) { product = ( product * arr [ i ] ) % M ; if ( product > ans ) { ans = product ; if ( length > j - i + 1 ) { length = j - i + 1 ; } } } } Console . WriteLine ( " Maximum ▁ subarray ▁ product ▁ is ▁ " + ans ) ; Console . WriteLine ( " Minimum ▁ length ▁ of ▁ the ▁ maximum ▁ " + " product ▁ subarray ▁ is ▁ " + length ) ; } static void Main ( ) { int [ ] arr = { 2 , 3 , 4 , 2 } ; int N = arr . Length ; int M = 5 ; maxModProdSubarr ( arr , N , M ) ; } }


"Maximum subarray sum in O ( n ) using prefix sum | C # program to find out maximum subarray sum in linear time using prefix sum . ; Function to compute maximum subarray sum in linear time . ; Initialize minimum prefix sum to 0. ; Initialize maximum subarray sum so far to - infinity . ; Initialize and compute the prefix sum array . ; loop through the array , keep track of minimum prefix sum so far and maximum subarray sum . ; Driver Program ; Test case 1 ; Test case 2"
Solution in C#:

using System ; class GFG { static int maximumSumSubarray ( int [ ] arr , int n ) { int min_prefix_sum = 0 ; int res = int . MinValue ; int [ ] prefix_sum = new int [ n ] ; prefix_sum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prefix_sum [ i ] = prefix_sum [ i - 1 ] + arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) { res = Math . Max ( res , prefix_sum [ i ] - min_prefix_sum ) ; min_prefix_sum = Math . Min ( min_prefix_sum , prefix_sum [ i ] ) ; } return res ; } public static void Main ( ) { int [ ] arr1 = { - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 } ; int n1 = arr1 . Length ; Console . WriteLine ( maximumSumSubarray ( arr1 , n1 ) ) ; int [ ] arr2 = { 4 , - 8 , 9 , - 4 , 1 , - 8 , - 1 , 6 } ; int n2 = arr2 . Length ; Console . WriteLine ( maximumSumSubarray ( arr2 , n2 ) ) ; } }


"Maximum subarray sum possible after removing at most K array elements | C # program to implement the above approach ; Function to find the maximum subarray sum greater than or equal to 0 by removing K array elements ; Base case ; If overlapping subproblems already occurred ; Include current element in the subarray ; If K elements already removed from the subarray ; Remove current element from the subarray ; Utility function to find the maximum subarray sum by removing at most K array elements ; Stores overlapping subproblems of the recurrence relation ; Initialize dp [ , ] to - 1 ; Stores maximum subarray sum by removing at most K elements ; Calculate maximum element in dp [ , ] ; Update res ; If all array elements are negative ; Update res ; Driver Code"
Solution in C#:

using System ; using System . Collections ; class GFG { static int M = 100 ; static int mxSubSum ( int i , int [ ] arr , int j , int [ , ] dp ) { if ( i == 0 ) { return dp [ i , j ] = Math . Max ( 0 , arr [ i ] ) ; } if ( dp [ i , j ] != - 1 ) { return dp [ i , j ] ; } int X = Math . Max ( 0 , arr [ i ] + mxSubSum ( i - 1 , arr , j , dp ) ) ; if ( j == 0 ) { return dp [ i , j ] = X ; } int Y = mxSubSum ( i - 1 , arr , j - 1 , dp ) ; return dp [ i , j ] = Math . Max ( X , Y ) ; } static int MaximumSubarraySum ( int n , int [ ] arr , int k ) { int [ , ] dp = new int [ M , M ] ; for ( int i = 0 ; i < M ; i ++ ) for ( int j = 0 ; j < M ; j ++ ) dp [ i , j ] = - 1 ; mxSubSum ( n - 1 , arr , k , dp ) ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j <= k ; j ++ ) { res = Math . Max ( res , dp [ i , j ] ) ; } } Array . Sort ( arr ) ; if ( arr [ n - 1 ] < 0 ) { res = arr [ n - 1 ] ; } return res ; } public static void Main ( String [ ] args ) { int [ ] arr = { - 2 , 1 , 3 , - 2 , 4 , - 7 , 20 } ; int K = 1 ; int N = arr . Length ; Console . WriteLine ( MaximumSubarraySum ( N , arr , K ) ) ; } }


"Maximum subarray sum possible after removing at most one subarray | C # program for the above approach ; Function to find maximum subarray sum possible after removing at most one subarray ; Calculate the preSum [ ] array ; Update the value of sum ; Update the value of maxSum ; Update the value of preSum [ i ] ; Calculate the postSum [ ] array ; Update the value of sum ; Update the value of maxSum ; Update the value of postSum [ i ] ; Stores the resultant maximum sum of subarray ; Update the value of ans ; Print the resultant sum ; Driver code"
Solution in C#:

using System ; class GFG { public static void maximumSum ( int [ ] arr , int n ) { long [ ] preSum = new long [ n ] ; long sum = 0 ; long maxSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = Math . Max ( arr [ i ] , sum + arr [ i ] ) ; maxSum = Math . Max ( maxSum , sum ) ; preSum [ i ] = maxSum ; } sum = 0 ; maxSum = 0 ; long [ ] postSum = new long [ n + 1 ] ; for ( int i = n - 1 ; i >= 0 ; i -- ) { sum = Math . Max ( arr [ i ] , sum + arr [ i ] ) ; maxSum = Math . Max ( maxSum , sum ) ; postSum [ i ] = maxSum ; } long ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans = Math . Max ( ans , preSum [ i ] + postSum [ i + 1 ] ) ; } Console . WriteLine ( ans ) ; } static void Main ( ) { int [ ] arr = { 7 , 6 , - 1 , - 4 , - 5 , 7 , 1 } ; maximumSum ( arr , arr . Length ) ; } }


"Maximum subsequence sum from a given array which is a perfect square | C # program to implement the above approach ; If sum is 0 , then answer is true ; If sum is not 0 and [ ] arr is empty , then answer is false ; Fill the subset table in bottom up manner ; Function to find the sum ; Find sum of all values ; return the value ; ; Driver code"
Solution in C#:

using System ; class GFG { static bool isSubsetSum ( int [ ] arr , int n , int sum ) { bool [ , ] subset = new bool [ n + 1 , sum + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) subset [ i , 0 ] = true ; for ( int i = 1 ; i <= sum ; i ++ ) subset [ 0 , i ] = false ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= sum ; j ++ ) { if ( j < arr [ i - 1 ] ) subset [ i , j ] = subset [ i - 1 , j ] ; if ( j >= arr [ i - 1 ] ) subset [ i , j ] = subset [ i - 1 , j ] || subset [ i - 1 , j - arr [ i - 1 ] ] ; } } return subset [ n , sum ] ; } static int findSum ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } int val = ( int ) Math . Sqrt ( sum ) ; for ( int i = val ; i >= 0 ; i -- ) { if ( isSubsetSum ( arr , n , i * i ) ) { return i * i ; } } return 0 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; int n = arr . Length ; Console . WriteLine ( findSum ( arr , n ) ) ; } }


"Maximum subsequence sum obtained by concatenating disjoint subarrays whose lengths are prime | C # program for the above approach ; Function to return all prime numbers smaller than N ; Create a bool array " prime [ 0 . . n ] " ; Initialize all its entries as true ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it ; Stores all prime numbers smaller than MAX ; Store all prime numbers ; If p is prime ; Function to build the auxiliary DP array from the start ; Base Case ; Stores all prime numbers < N ; Stores prefix sum ; Update prefix sum ; Iterate over range ; Update each state i . e . . when current element is excluded ; Find start & end index of subarrays when prime [ i ] is taken ; Check if starting point lies in the array ; Include the elements al al + 1 . . . ar ; Check if element lies before start of selected subarray ; Update value of dp [ i ] ; Function to find the maximum sum subsequence with prime length ; Auxiliary DP array ; Build DP array ; Print the result ; Driver Code ; Given [ ] arr ; Size of array ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int MAX = 100005 ; static List < int > SieveOfEratosthenes ( ) { bool [ ] seive = new bool [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) seive [ i ] = true ; for ( int p = 2 ; p * p < MAX ; p ++ ) { if ( seive [ p ] == true ) { for ( int i = p * p ; i < MAX ; i += p ) { seive [ i ] = false ; } } } List < int > v = new List < int > ( ) ; for ( int p = 2 ; p < MAX ; p ++ ) { if ( seive [ p ] ) { v . Add ( p ) ; } } return v ; } static void build ( int [ ] dp , int [ ] arr , int N ) { dp [ 0 ] = 0 ; dp [ 1 ] = 0 ; List < int > prime = SieveOfEratosthenes ( ) ; int [ ] pref = new int [ N + 1 ] ; pref [ 0 ] = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { pref [ i ] = pref [ i - 1 ] + arr [ i - 1 ] ; } for ( int i = 2 ; i <= N ; i ++ ) { dp [ i ] = dp [ i - 1 ] ; for ( int j = 0 ; j <= prime . Count ; j ++ ) { int r = i - 1 ; int l = r - prime [ j ] + 1 ; if ( l < 0 ) break ; int temp = 0 ; temp = pref [ r + 1 ] - pref [ l ] ; if ( l - 2 >= 0 ) temp += dp [ l - 2 + 1 ] ; dp [ i ] = Math . Max ( dp [ i ] , temp ) ; } } } static void maxSumSubseq ( int [ ] arr , int N ) { int [ ] dp = new int [ N + 1 ] ; build ( dp , arr , N ) ; Console . Write ( dp [ N ] ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 10 , 10 , 7 , 10 , 10 , 10 } ; int N = arr . Length ; maxSumSubseq ( arr , N ) ; } }


"Maximum subsequence sum possible by multiplying each element by its index | C # program for the above approach ; Initialize dp array ; Function to find the maximum sum of the subsequence formed ; Base Case ; If already calculated state occurs ; Include the current element ; Exclude the current element ; Update the maximum ans ; Function to calculate maximum sum of the subsequence obtained ; Initialise the dp array with - 1 ; Print the maximum sum possible ; Driver Code ; Given array ; Size of the array ; Function Call"
Solution in C#:

using System ; class GFG { static int [ , ] dp = new int [ 1005 , 1005 ] ; static int maximumSumUtil ( int [ ] a , int index , int count , int n ) { if ( index > n count > n + 1 ) { return 0 ; } if ( dp [ index , count ] != - 1 ) return dp [ index , count ] ; int ans1 = maximumSumUtil ( a , index + 1 , count + 1 , n ) + a [ index ] * count ; int ans2 = maximumSumUtil ( a , index + 1 , count , n ) ; return ( dp [ index , count ] = Math . Max ( ans1 , ans2 ) ) ; } static void maximumSum ( int [ ] arr , int N ) { for ( int i = 0 ; i < 1005 ; i ++ ) { for ( int j = 0 ; j < 1005 ; j ++ ) { dp [ i , j ] = - 1 ; } } Console . Write ( maximumSumUtil ( arr , 0 , 1 , N - 1 ) ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { - 1 , 2 , - 10 , 4 , - 20 } ; int N = arr . Length ; maximumSum ( arr , N ) ; } }


"Maximum subsequence sum such that all elements are K distance apart | C # implementation of the approach ; Function to return the maximum subarray sum for the array { a [ i ] , a [ i + k ] , a [ i + 2 k ] , ... } ; Function to return the sum of the maximum required subsequence ; To store the result ; Run a loop from 0 to k ; Find the maximum subarray sum for the array { a [ i ] , a [ i + k ] , a [ i + 2 k ] , ... } ; Return the maximum value ; Driver code"
Solution in C#:

using System ; class GFG { static int maxSubArraySum ( int [ ] a , int n , int k , int i ) { int max_so_far = int . MinValue , max_ending_here = 0 ; while ( i < n ) { 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 ; i += k ; } return max_so_far ; } static int find ( int [ ] arr , int n , int k ) { int maxSum = 0 ; for ( int i = 0 ; i <= Math . Min ( n , k ) ; i ++ ) { maxSum = Math . Max ( maxSum , maxSubArraySum ( arr , n , k , i ) ) ; } return maxSum ; } public static void Main ( ) { int [ ] arr = { 2 , - 3 , - 1 , - 1 , 2 } ; int n = arr . Length ; int k = 2 ; Console . WriteLine ( find ( arr , n , k ) ) ; } }


"Maximum subsequence sum such that no K elements are consecutive | C # program for the above approach ; Function to find the maximum sum of a subsequence consisting of no K consecutive array elements ; Stores states of dp ; Initialise dp state ; Stores the prefix sum ; Update the prefix sum ; Base case for i < K ; For indices less than k take all the elements ; For i >= K case ; Skip each element from i to ( i - K + 1 ) to ensure that no K elements are consecutive ; Update the current dp state ; dp [ N ] stores the maximum sum ; Driver Code ; Given array arr [ ] ; Function Call"
Solution in C#:

using System ; class GFG { static int Max_Sum ( int [ ] arr , int K , int N ) { int [ ] dp = new int [ N + 1 ] ; Array . Fill ( dp , 0 ) ; int [ ] prefix = new int [ N + 1 ] ; prefix [ 0 ] = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { prefix [ i ] = prefix [ i - 1 ] + arr [ i - 1 ] ; } dp [ 0 ] = 0 ; for ( int i = 1 ; i <= K - 1 ; i ++ ) { dp [ i ] = prefix [ i ] ; } for ( int i = K ; i <= N ; ++ i ) { for ( int j = i ; j >= ( i - K + 1 ) ; j -- ) { dp [ i ] = Math . Max ( dp [ i ] , dp [ j - 1 ] + prefix [ i ] - prefix [ j ] ) ; } } return dp [ N ] ; } static public void Main ( ) { int [ ] arr = { 4 , 12 , 22 , 18 , 34 , 12 , 25 } ; int N = arr . Length ; int K = 5 ; Console . WriteLine ( Max_Sum ( arr , K , N ) ) ; } }


"Maximum subsequence sum such that no three are consecutive in O ( 1 ) space | C # program for the above approach ; Function to calculate the maximum subsequence sum such that no three elements are consecutive ; when N is 1 , answer would be the only element present ; when N is 2 , answer would be sum of elements ; variable to store sum up to i - 3 ; variable to store sum up to i - 2 ; variable to store sum up to i - 1 ; variable to store the final sum of the subsequence ; find the maximum subsequence sum up to index i ; update first , second and third ; return ans ; ; Driver Code ; Input ; Function call"
Solution in C#:

using System ; class GFG { public static int maxSumWO3Consec ( int [ ] A , int N ) { if ( N == 1 ) return A [ 0 ] ; if ( N == 2 ) return A [ 0 ] + A [ 1 ] ; int third = A [ 0 ] ; int second = third + A [ 1 ] ; int first = Math . Max ( second , A [ 1 ] + A [ 2 ] ) ; int sum = Math . Max ( Math . Max ( third , second ) , first ) ; for ( int i = 3 ; i < N ; i ++ ) { sum = Math . Max ( Math . Max ( first , second + A [ i ] ) , third + A [ i ] + A [ i - 1 ] ) ; third = second ; second = first ; first = sum ; } return sum ; } public static void Main ( ) { int [ ] A = { 3000 , 2000 , 1000 , 3 , 10 } ; int N = A . Length ; int res = maxSumWO3Consec ( A , N ) ; Console . Write ( res ) ; } }


"Maximum subsequence sum such that no three are consecutive | C # program to find the maximum sum such that no three are consecutive ; Returns maximum subsequence sum such that no three elements are consecutive ; Stores result for subarray arr [ 0. . i ] , i . e . , maximum possible sum in subarray arr [ 0. . i ] such that no three elements are consecutive . ; Base cases ( process first three elements ) ; Process rest of the elements We have three cases 1 ) Exclude arr [ i ] , i . e . , sum [ i ] = sum [ i - 1 ] 2 ) Exclude arr [ i - 1 ] , i . e . , sum [ i ] = sum [ i - 2 ] + arr [ i ] 3 ) Exclude arr [ i - 2 ] , i . e . , sum [ i - 3 ] + arr [ i ] + arr [ i - 1 ] ; Driver code"
Solution in C#:

using System ; class GFG { static int maxSumWO3Consec ( int [ ] arr , int n ) { int [ ] sum = new int [ n ] ; if ( n >= 1 ) sum [ 0 ] = arr [ 0 ] ; if ( n >= 2 ) sum [ 1 ] = arr [ 0 ] + arr [ 1 ] ; if ( n > 2 ) sum [ 2 ] = Math . Max ( sum [ 1 ] , Math . Max ( arr [ 1 ] + arr [ 2 ] , arr [ 0 ] + arr [ 2 ] ) ) ; for ( int i = 3 ; i < n ; i ++ ) sum [ i ] = Math . Max ( Math . Max ( sum [ i - 1 ] , sum [ i - 2 ] + arr [ i ] ) , arr [ i ] + arr [ i - 1 ] + sum [ i - 3 ] ) ; return sum [ n - 1 ] ; } public static void Main ( ) { int [ ] arr = { 100 , 1000 , 100 , 1000 , 1 } ; int n = arr . Length ; Console . Write ( maxSumWO3Consec ( arr , n ) ) ; } }


"Maximum subsequence sum such that no three are consecutive | C # program to find the maximum sum such that no three are consecutive using recursion . ; Returns maximum subsequence sum such that no three elements are consecutive ; Base cases ( process first three elements ) ; Process rest of the elements We have three cases ; Driver code"
Solution in C#:

using System ; class GFG { static int [ ] arr = { 100 , 1000 , 100 , 1000 , 1 } ; static int [ ] sum = new int [ 10000 ] ; static int maxSumWO3Consec ( int n ) { if ( sum [ n ] != - 1 ) return sum [ n ] ; if ( n == 0 ) return sum [ n ] = 0 ; if ( n == 1 ) return sum [ n ] = arr [ 0 ] ; if ( n == 2 ) return sum [ n ] = arr [ 1 ] + arr [ 0 ] ; return sum [ n ] = Math . Max ( Math . Max ( maxSumWO3Consec ( n - 1 ) , maxSumWO3Consec ( n - 2 ) + arr [ n ] ) , arr [ n ] + arr [ n - 1 ] + maxSumWO3Consec ( n - 3 ) ) ; } public static void Main ( String [ ] args ) { int n = arr . Length ; for ( int i = 0 ; i < sum . Length ; i ++ ) sum [ i ] = - 1 ; Console . WriteLine ( maxSumWO3Consec ( n ) ) ; } }


"Maximum subsequence sum with adjacent elements having atleast K difference in index | C # implementation to find the maximum sum subsequence such that two adjacent element have atleast difference of K in their indices ; Function to find the maximum sum subsequence such that two adjacent element have atleast difference of K in their indices ; DP Array to store the maximum sum obtained till now ; Either select the first element or Nothing ; Either Select the ( i - 1 ) element or let the previous best answer be the current best answer ; Either select the best sum till previous_index or select the current element + best_sum till index - k ; Driver Code"
Solution in C#:

using System ; class GFG { static int max_sum ( int [ ] arr , int n , int k ) { int [ ] dp = new int [ n ] ; dp [ 0 ] = Math . Max ( 0 , arr [ 0 ] ) ; int i = 1 ; while ( i < k ) { dp [ i ] = Math . Max ( dp [ i - 1 ] , arr [ i ] ) ; i ++ ; } i = k ; while ( i < n ) { dp [ i ] = Math . Max ( dp [ i - 1 ] , arr [ i ] + dp [ i - k ] ) ; i ++ ; } return dp [ n - 1 ] ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , - 2 , 4 , 3 , 1 } ; int n = arr . Length ; int k = 4 ; Console . WriteLine ( max_sum ( arr , n , k ) ) ; } }


"Maximum subset sum such that no two elements in set have same digit in them | C # implementation of above approach ; Function to create mask for every number ; Recursion for Filling DP array ; Base Condition ; Recurrence Relation ; Function to find Maximum Subset Sum ; Initialize DP array ; Iterate over all possible masks of 10 bit number ; Driver Code"
Solution in C#:

using System ; class GFG { static int [ ] dp = new int [ 1024 ] ; static int get_binary ( int u ) { int ans = 0 ; while ( u > 0 ) { int rem = u % 10 ; ans |= ( 1 << rem ) ; u /= 10 ; } return ans ; } static int recur ( int u , int [ ] array , int n ) { if ( u == 0 ) return 0 ; if ( dp [ u ] != - 1 ) return dp [ u ] ; for ( int i = 0 ; i < n ; i ++ ) { int mask = get_binary ( array [ i ] ) ; if ( ( mask u ) == u ) { dp [ u ] = Math . Max ( Math . Max ( 0 , dp [ u ^ mask ] ) + array [ i ] , dp [ u ] ) ; } } return dp [ u ] ; } static int solve ( int [ ] array , int n ) { for ( int i = 0 ; i < ( 1 << 10 ) ; i ++ ) { dp [ i ] = - 1 ; } int ans = 0 ; for ( int i = 0 ; i < ( 1 << 10 ) ; i ++ ) { ans = Math . Max ( ans , recur ( i , array , n ) ) ; } return ans ; } static public void Main ( ) { int [ ] array = { 22 , 132 , 4 , 45 , 12 , 223 } ; int n = array . Length ; Console . WriteLine ( solve ( array , n ) ) ; } }


"Maximum subset with bitwise OR equal to k | C # Program to find the maximum subset with bitwise OR equal to k ; function to find the maximum subset with bitwise OR equal to k ; If the bitwise OR of k and element is equal to k , then include that element in the subset ; Store the bitwise OR of elements in v ; If ans is not equal to k , subset doesn 't exist ; main function"
Solution in C#:

using System ; using System . Collections ; class GFG { static void subsetBitwiseORk ( int [ ] arr , int n , int k ) { ArrayList v = new ArrayList ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] k ) == k ) { v . Add ( arr [ i ] ) ; } } int ans = 0 ; for ( int i = 0 ; i < v . Count ; i ++ ) ans = ans | ( int ) v [ i ] ; if ( ans != k ) { Console . WriteLine ( " Subset ▁ does " + " ▁ not ▁ exist " ) ; return ; } for ( int i = 0 ; i < v . Count ; i ++ ) Console . Write ( ( int ) v [ i ] + " ▁ " ) ; } static public void Main ( String [ ] args ) { int k = 3 ; int [ ] arr = { 1 , 4 , 2 } ; int n = arr . Length ; subsetBitwiseORk ( arr , n , k ) ; } }


"Maximum sum after rearranging the array for K queries | C # program to find the maximum sum after rearranging the array for K queries ; Function to find maximum sum after rearranging array elements ; Auxiliary array to find the count of each selected elements ; Finding count of every element to be selected ; Making it to 0 - indexing ; Prefix sum array concept is used to obtain the count array ; Iterating over the count array to get the readonly array ; Variable to store the maximum sum ; Sorting both the arrays ; Loop to find the maximum sum ; Driver code"
Solution in C#:

using System ; class GFG { static int maxSumArrangement ( int [ ] A , int [ , ] R , int N , int M ) { int [ ] count = new int [ N ] ; int i ; for ( i = 0 ; i < M ; ++ i ) { int l = R [ i , 0 ] , r = R [ i , 1 ] + 1 ; l -- ; r -- ; count [ l ] ++ ; if ( r < N ) count [ r ] -- ; } for ( i = 1 ; i < N ; ++ i ) { count [ i ] += count [ i - 1 ] ; } int ans = 0 ; Array . Sort ( count ) ; Array . Sort ( A ) ; for ( i = N - 1 ; i >= 0 ; -- i ) { ans += A [ i ] * count [ i ] ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] A = { 2 , 6 , 10 , 1 , 5 , 6 } ; int [ , ] R = { { 1 , 3 } , { 4 , 6 } , { 3 , 4 } } ; int N = A . Length ; int M = R . GetLength ( 0 ) ; Console . Write ( maxSumArrangement ( A , R , N , M ) ) ; } }


"Maximum sum after repeatedly dividing N by a divisor | C # implementation of the above approach ; Function to find the smallest divisor ; Function to find the maximum sum ; Driver Code"
Solution in C#:

using System ; class GFG { static double smallestDivisor ( int n ) { double mx = Math . Sqrt ( n ) ; for ( int i = 2 ; i <= mx ; i ++ ) if ( n % i == 0 ) return i ; return n ; } static double maxSum ( int n ) { long res = n ; while ( n > 1 ) { double divi = smallestDivisor ( n ) ; n /= ( int ) divi ; res += n ; } return res ; } public static void Main ( ) { int n = 34 ; Console . WriteLine ( maxSum ( n ) ) ; } }


"Maximum sum alternating subsequence | C # program to find sum of maximum sum alternating sequence starting with first element . ; Return sum of maximum sum alternating sequence starting with arr [ 0 ] and is first decreasing . ; handling the edge case ; stores sum of decreasing and increasing sub - sequence ; store sum of increasing and decreasing sun - sequence ; As per question , first element must be part of solution . ; Traverse remaining elements of array ; IF current sub - sequence is decreasing the update dec [ j ] if needed . dec [ i ] by current inc [ j ] + arr [ i ] ; Revert the flag , if first decreasing is found ; If next element is greater but flag should be 1 i . e . this element should be counted after the first decreasing element gets counted ; If current sub - sequence is increasing then update inc [ i ] ; find maximum sum in b / w inc [ ] and dec [ ] ; return maximum sum alternate sun - sequence ; Driver Method"
Solution in C#:

using System ; class GFG { static int maxAlternateSum ( int [ ] arr , int n ) { if ( n == 1 ) return arr [ 0 ] ; int min = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( min > arr [ i ] ) min = arr [ i ] ; } if ( min == arr [ 0 ] ) { return arr [ 0 ] ; } int [ ] dec = new int [ n ] ; int [ ] inc = new int [ n ] ; dec [ 0 ] = inc [ 0 ] = arr [ 0 ] ; int flag = 0 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( arr [ j ] > arr [ i ] ) { dec [ i ] = Math . Max ( dec [ i ] , inc [ j ] + arr [ i ] ) ; flag = 1 ; } else if ( arr [ j ] < arr [ i ] && flag == 1 ) inc [ i ] = Math . Max ( inc [ i ] , dec [ j ] + arr [ i ] ) ; } } int result = int . MinValue ; for ( int i = 0 ; i < n ; i ++ ) { if ( result < inc [ i ] ) result = inc [ i ] ; if ( result < dec [ i ] ) result = dec [ i ] ; } return result ; } public static void Main ( ) { int [ ] arr = { 8 , 2 , 3 , 5 , 7 , 9 , 10 } ; Console . Write ( " Maximum ▁ sum ▁ = ▁ " + maxAlternateSum ( arr , arr . Length ) ) ; } }


"Maximum sum among all ( a x b ) submatrices for given Q queries | Function to preprcess input mat [ N ] [ M ] . This function mainly fills dp [ N ] [ M ] such that dp [ i ] [ j ] stores sum of elements from ( 0 , 0 ) to ( i , j ) ; Copy first row of mat [ ] [ ] to dp [ ] [ ] ; Do column wise sum ; Do row wise sum ; A O ( 1 ) time function to compute sum of submatrix between ( tli , tlj ) and ( rbi , rbj ) using dp [ ] [ ] which is built by the preprocess function ; Result is now sum of elements between ( 0 , 0 ) and ( rbi , rbj ) ; Remove elements between ( 0 , 0 ) and ( tli - 1 , rbj ) ; Remove elements between ( 0 , 0 ) and ( rbi , tlj - 1 ) ; Add dp [ tli - 1 ] [ tlj - 1 ] as elements between ( 0 , 0 ) and ( tli - 1 , tlj - 1 ) are subtracted twice ; Function to find the maximum sum among all ( a x b ) sub - matrices of the matrix ; Function call ; Run a loop for finding answer for all queries ; Driver Code ; Given input ; Function call ; Print answer for all queries"
Solution in C#:

using System ; public class GFG { static void preProcess ( int [ , ] mat , int [ , ] dp , int n , int m ) { for ( int i = 0 ; i < m ; i ++ ) { dp [ 0 , i ] = mat [ 0 , i ] ; } for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { dp [ i , j ] = dp [ i - 1 , j ] + mat [ i , j ] ; } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 1 ; j < m ; j ++ ) { dp [ i , j ] += dp [ i , j - 1 ] ; } } } static int sumQuery ( int [ , ] dp , int tli , int tlj , int rbi , int rbj ) { int res = dp [ rbi , rbj ] ; if ( tli > 0 ) res = res - dp [ tli - 1 , rbj ] ; if ( tlj > 0 ) res = res - dp [ rbi , tlj - 1 ] ; if ( tli > 0 && tlj > 0 ) res = res + dp [ tli - 1 , tlj - 1 ] ; return res ; } static int [ ] maxSubMatrixSumQueries ( int [ , ] mat , int n , int m , int [ , ] queries , int q ) { int [ , ] dp = new int [ n , m ] ; preProcess ( mat , dp , n , m ) ; int [ ] maxSum = new int [ queries . GetLength ( 0 ) ] ; for ( int qi = 0 ; qi < q ; qi ++ ) { for ( int i = 0 ; i < n - queries [ qi , 0 ] + 1 ; i ++ ) { for ( int j = 0 ; j < m - queries [ qi , 1 ] + 1 ; j ++ ) { maxSum [ qi ] = Math . Max ( maxSum [ qi ] , sumQuery ( dp , i , j , i + queries [ qi , 0 ] - 1 , j + queries [ qi , 1 ] - 1 ) ) ; } } } return maxSum ; } static public void Main ( ) { int n = 3 , m = 4 ; int [ , ] mat = { { 1 , 2 , 3 , 9 } , { 4 , 5 , 6 , 2 } , { 8 , 3 , 2 , 6 } } ; int Q = 3 ; int [ , ] Queries = { { 1 , 1 } , { 2 , 2 } , { 3 , 3 } } ; int [ ] maxSum = maxSubMatrixSumQueries ( mat , n , m , Queries , Q ) ; for ( int i = 0 ; i < Q ; i ++ ) { Console . Write ( maxSum [ i ] + " ▁ " ) ; } } }


"Maximum sum and product of the M consecutive digits in a number | C # implementation of the above approach ; Function to find the maximum product ; Driver code"
Solution in C#:

 using System ; class GFG { static void maxProductSum ( string str , int m ) { int n = str . Length ; int maxProd = int . MinValue , maxSum = int . MinValue ; for ( int i = 0 ; i <= n - m ; i ++ ) { int product = 1 , sum = 0 ; for ( int j = i ; j < m + i ; j ++ ) { product = product * ( str [ j ] - '0' ) ; sum = sum + ( str [ j ] - '0' ) ; } maxProd = Math . Max ( maxProd , product ) ; maxSum = Math . Max ( maxSum , sum ) ; } Console . WriteLine ( " Maximum ▁ Product ▁ = ▁ " + maxProd ) ; Console . Write ( "
 Maximum ▁ Sum ▁ = ▁ " + maxSum ) ; } public static void Main ( ) { string str = "3605356297" ; int m = 3 ; maxProductSum ( str , m ) ; } }


"Maximum sum bitonic subarray | C # implementation to find the maximum sum bitonic subarray ; Function to find the maximum sum bitonic subarray . ; To store the maximum sum bitonic subarray ; Find the longest increasing subarray starting at i . ; Now we know that a [ i . . j ] is an increasing subarray . Remove non - positive elements from the left side as much as possible . ; Find the longest decreasing subarray starting at j . ; Now we know that a [ j . . k ] is a decreasing subarray . Remove non - positive elements from the right side as much as possible . last is needed to keep the last seen element . ; Compute the max sum of the increasing part . ; Compute the max sum of the decreasing part . ; The overall max sum is the sum of both parts minus the peak element , because it was counted twice . ; If the next element is equal to the current , i . e . arr [ i + 1 ] == arr [ i ] , last == i . To ensure the algorithm has progress , get the max of last and i + 1. ; Required maximum sum ; Driver code ; The example from the article , the answer is 19. ; Always increasing , the answer is 15. ; Always decreasing , the answer is 15. ; All are equal , the answer is 5. ; The whole array is bitonic , but the answer is 7. ; The answer is 4 ( the tail ) ."
Solution in C#:

using System ; class GFG { static int find_partial_sum ( int [ ] arr , int start , int end ) { int sum = 0 ; for ( int i = start ; i < end ; i ++ ) sum += arr [ i ] ; return sum ; } static int maxSumBitonicSubArr ( int [ ] arr , int n ) { int max_sum = - 1000000 ; int i = 0 ; while ( i < n ) { int j = i ; while ( j + 1 < n && arr [ j ] < arr [ j + 1 ] ) j ++ ; while ( i < j && arr [ i ] <= 0 ) i ++ ; int k = j ; while ( k + 1 < n && arr [ k ] > arr [ k + 1 ] ) k ++ ; int last = k ; while ( k > j && arr [ k ] <= 0 ) k -- ; int sum_inc = find_partial_sum ( arr , i , j + 1 ) ; int sum_dec = find_partial_sum ( arr , j , k + 1 ) ; int sum_all = sum_inc + sum_dec - arr [ j ] ; max_sum = Math . Max ( Math . Max ( max_sum , sum_inc ) , Math . Max ( sum_dec , sum_all ) ) ; i = Math . Max ( last , i + 1 ) ; } return max_sum ; } public static void Main ( ) { int [ ] arr = { 5 , 3 , 9 , 2 , 7 , 6 , 4 } ; int n = arr . Length ; Console . WriteLine ( " Maximum ▁ sum ▁ = ▁ " + maxSumBitonicSubArr ( arr , n ) ) ; int [ ] arr2 = { 1 , 2 , 3 , 4 , 5 } ; int n2 = arr2 . Length ; Console . WriteLine ( " Maximum ▁ sum ▁ = ▁ " + maxSumBitonicSubArr ( arr2 , n2 ) ) ; int [ ] arr3 = { 5 , 4 , 3 , 2 , 1 } ; int n3 = arr3 . Length ; Console . WriteLine ( " Maximum ▁ sum ▁ = ▁ " + maxSumBitonicSubArr ( arr3 , n3 ) ) ; int [ ] arr4 = { 5 , 5 , 5 , 5 } ; int n4 = arr4 . Length ; Console . WriteLine ( " Maximum ▁ sum ▁ = ▁ " + maxSumBitonicSubArr ( arr4 , n4 ) ) ; int [ ] arr5 = { - 1 , 0 , 1 , 2 , 3 , 1 , 0 , - 1 , - 10 } ; int n5 = arr5 . Length ; Console . WriteLine ( " Maximum ▁ sum ▁ = ▁ " + maxSumBitonicSubArr ( arr5 , n5 ) ) ; int [ ] arr6 = { - 1 , 0 , 1 , 2 , 0 , - 1 , - 2 , 0 , 1 , 3 } ; int n6 = arr6 . Length ; Console . WriteLine ( " Maximum ▁ sum ▁ = ▁ " + maxSumBitonicSubArr ( arr6 , n6 ) ) ; } }


"Maximum sum bitonic subarray | C # implementation to find the maximum sum bitonic subarray ; function to find the maximum sum bitonic subarray ; ' msis [ ] ' to store the maximum sum increasing subarray up to each index of ' arr ' from the beginning ' msds [ ] ' to store the maximum sum decreasing subarray from each index of ' arr ' up to the end ; to store the maximum sum bitonic subarray ; building up the maximum sum increasing subarray for each array index ; building up the maximum sum decreasing subarray for each array index ; for each array index , calculating the maximum sum of bitonic subarray of which it is a part of ; if true , then update ' max ' bitonic subarray sum ; required maximum sum ; Driver Code"
Solution in C#:

using System ; class GFG { static int maxSumBitonicSubArr ( int [ ] arr , int n ) { int [ ] msis = new int [ n ] ; int [ ] msds = new int [ n ] ; int max_sum = int . MinValue ; msis [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i ] > arr [ i - 1 ] ) msis [ i ] = msis [ i - 1 ] + arr [ i ] ; else msis [ i ] = arr [ i ] ; msds [ n - 1 ] = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) if ( arr [ i ] > arr [ i + 1 ] ) msds [ i ] = msds [ i + 1 ] + arr [ i ] ; else msds [ i ] = arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) if ( max_sum < ( msis [ i ] + msds [ i ] - arr [ i ] ) ) max_sum = msis [ i ] + msds [ i ] - arr [ i ] ; return max_sum ; } public static void Main ( ) { int [ ] arr = { 5 , 3 , 9 , 2 , 7 , 6 , 4 } ; int n = arr . Length ; Console . Write ( " Maximum ▁ Sum ▁ = ▁ " + maxSumBitonicSubArr ( arr , n ) ) ; } }


"Maximum sum by adding numbers with same number of set bits | C # program to find maximum sum by adding numbers with same number of set bits ; count the number of bits for each element of array ; Count the number of set bits ; Function to return the the maximum sum ; Calculate the ; Assuming the number to be a maximum of 32 bits ; Add the number to the number of set bits ; Find the maximum sum ; Driver code"
Solution in C#:

using System ; class GFG { static int bit_count ( int n ) { int count = 0 ; while ( n > 0 ) { count ++ ; n = n & ( n - 1 ) ; } return count ; } static int maxsum ( int [ ] arr , int n ) { int [ ] bits = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { bits [ i ] = bit_count ( arr [ i ] ) ; } int [ ] sum = new int [ 32 ] ; for ( int i = 0 ; i < n ; i ++ ) { sum [ bits [ i ] ] += arr [ i ] ; } int maximum = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) { maximum = Math . Max ( sum [ i ] , maximum ) ; } return maximum ; } static void Main ( ) { int [ ] arr = { 2 , 3 , 8 , 5 , 6 , 7 } ; int n = arr . Length ; Console . WriteLine ( maxsum ( arr , n ) ) ; } }


"Maximum sum by picking elements from two arrays in order | Set 2 | C # program to find maximum sum possible by selecting an element from one of two arrays for every index ; Function to calculate maximum sum ; Maximum elements that can be chosen from array A ; Maximum elements that can be chosen from array B ; Stores the maximum sum possible ; Fill the [ , ] dp for base case when all elements are selected from [ ] A ; Fill the [ , ] dp for base case when all elements are selected from [ ] B ; Return the readonly answer ; Driver code"
Solution in C#:

using System ; class GFG { static int maximumSum ( int [ ] A , int [ ] B , int length , int X , int Y ) { int l = length ; int l1 = Math . Min ( length , X ) ; int l2 = Math . Min ( length , Y ) ; int [ , ] dp = new int [ l1 + 1 , l2 + 1 ] ; int max_sum = int . MinValue ; for ( int i = 1 ; i <= l1 ; i ++ ) { dp [ i , 0 ] = dp [ i - 1 , 0 ] + A [ i - 1 ] ; max_sum = Math . Max ( max_sum , dp [ i , 0 ] ) ; } for ( int i = 1 ; i <= l2 ; i ++ ) { dp [ 0 , i ] = dp [ 0 , i - 1 ] + B [ i - 1 ] ; max_sum = Math . Max ( max_sum , dp [ 0 , i ] ) ; } for ( int i = 1 ; i <= l1 ; i ++ ) { for ( int j = 1 ; j <= l2 ; j ++ ) { if ( i + j <= l ) dp [ i , j ] = Math . Max ( dp [ i - 1 , j ] + A [ i + j - 1 ] , dp [ i , j - 1 ] + B [ i + j - 1 ] ) ; max_sum = Math . Max ( dp [ i , j ] , max_sum ) ; } } return max_sum ; } public static void Main ( String [ ] args ) { int [ ] A = new int [ ] { 1 , 2 , 3 , 4 , 5 } ; int [ ] B = new int [ ] { 5 , 4 , 3 , 2 , 1 } ; int X = 3 , Y = 2 ; int N = A . Length ; Console . WriteLine ( maximumSum ( A , B , N , X , Y ) ) ; } }


"Maximum sum combination from two arrays | C # program to maximum sum combination from two arrays ; Function to maximum sum combination from two arrays ; To store dp value ; For loop to calculate the value of dp ; Return the required answer ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { static int Max_Sum ( int [ ] arr1 , int [ ] arr2 , int n ) { int [ , ] dp = new int [ n , 2 ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( i == 0 ) { dp [ i , 0 ] = arr1 [ i ] ; dp [ i , 1 ] = arr2 [ i ] ; continue ; } dp [ i , 0 ] = Math . Max ( dp [ i - 1 , 0 ] , dp [ i - 1 , 1 ] + arr1 [ i ] ) ; dp [ i , 1 ] = Math . Max ( dp [ i - 1 , 1 ] , dp [ i - 1 , 0 ] + arr2 [ i ] ) ; } return Math . Max ( dp [ n - 1 , 0 ] , dp [ n - 1 , 1 ] ) ; } public static void Main ( ) { int [ ] arr1 = { 9 , 3 , 5 , 7 , 3 } ; int [ ] arr2 = { 5 , 8 , 1 , 4 , 5 } ; int n = arr1 . Length ; Console . WriteLine ( Max_Sum ( arr1 , arr2 , n ) ) ; } }


"Maximum sum in a 2 x n grid such that no two elements are adjacent | C # program Code for Maximum sum in a 2 x n grid such that no two elements are adjacent ; Function to find max sum without adjacent ; Sum including maximum element of first column ; Not including first column 's  element ; Traverse for further elements ; Update max_sum on including or excluding of previous column ; Include current column . Add maximum element from both row of current column ; If current column doesn 't  to be included ; Return maximum of excl and incl As that will be the maximum sum ; Driver Code"
Solution in C#:

using System ; class GFG { public static int maxSum ( int [ , ] grid , int n ) { int incl = Math . Max ( grid [ 0 , 0 ] , grid [ 1 , 0 ] ) ; int excl = 0 , excl_new ; for ( int i = 1 ; i < n ; i ++ ) { excl_new = Math . Max ( excl , incl ) ; incl = excl + Math . Max ( grid [ 0 , i ] , grid [ 1 , i ] ) ; excl = excl_new ; } return Math . Max ( excl , incl ) ; } public static void Main ( String [ ] args ) { int [ , ] grid = { { 1 , 2 , 3 , 4 , 5 } , { 6 , 7 , 8 , 9 , 10 } } ; int n = 5 ; Console . Write ( maxSum ( grid , n ) ) ; } }


"Maximum sum in an array such that every element has exactly one adjacent element to it | C # implementation of the approach ; To store the states of dp ; Function to return the maximized sum ; Base case ; Checks if a state is already solved ; Recurrence relation ; Return the result ; Driver code"
Solution in C#:

using System ; class GFG { static int arrSize = 51 ; static int [ ] dp = new int [ arrSize ] ; static bool [ ] v = new bool [ arrSize ] ; static int sumMax ( int i , int [ ] arr , int n ) { if ( i >= n - 1 ) return 0 ; if ( v [ i ] ) return dp [ i ] ; v [ i ] = true ; dp [ i ] = Math . Max ( arr [ i ] + arr [ i + 1 ] + sumMax ( i + 3 , arr , n ) , sumMax ( i + 1 , arr , n ) ) ; return dp [ i ] ; } public static void Main ( ) { int [ ] arr = { 1 , 1 , 1 , 1 } ; int n = arr . Length ; Console . WriteLine ( sumMax ( 0 , arr , n ) ) ; } }


"Maximum sum increasing subsequence from a prefix and a given element after prefix is must | C # program to find maximum sum increasing subsequence till i - th index and including k - th index . ; Initializing the first row of the dp [ ] [ ] . ; Creating the dp [ ] [ ] matrix . ; To calculate for i = 4 and k = 6. ; Driver code"
Solution in C#:

using System ; class GFG { static int pre_compute ( int [ ] a , int n , int index , int k ) { int [ , ] dp = new int [ n , n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > a [ 0 ] ) dp [ 0 , i ] = a [ i ] + a [ 0 ] ; else dp [ 0 , i ] = a [ i ] ; } for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( a [ j ] > a [ i ] && j > i ) { if ( dp [ i - 1 , i ] + a [ j ] > dp [ i - 1 , j ] ) dp [ i , j ] = dp [ i - 1 , i ] + a [ j ] ; else dp [ i , j ] = dp [ i - 1 , j ] ; } else dp [ i , j ] = dp [ i - 1 , j ] ; } } return dp [ index , k ] ; } static public void Main ( ) { int [ ] a = { 1 , 101 , 2 , 3 , 100 , 4 , 5 } ; int n = a . Length ; int index = 4 , k = 6 ; Console . WriteLine ( pre_compute ( a , n , index , k ) ) ; } }


"Maximum sum not exceeding K possible for any rectangle of a Matrix | C # program for the above approach ; Function to find the maximum possible sum of arectangle which is less than K ; Stores the values ( cum_sum - K ) ; Insert 0 into the set sumSet ; Traverse over the rows ; Get cumulative sum from [ 0 to i ] ; Search for upperbound of ( cSum - K ) in the hashmap ; If upper_bound of ( cSum - K ) exists , then update max sum ; Insert cummulative value in the hashmap ; Return the maximum sum which is less than K ; Function to find the maximum sum of rectangle such that its sum is no larger than K ; Stores the number of rows and columns ; Store the required result ; Set the left column ; Set the right column for the left column set by outer loop ; Calculate sum between the current left and right for every row ; Stores the sum of rectangle ; Update the overall maximum sum ; Print the result ; Driver Code ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int maxSubarraySum ( int [ ] sum , int k , int row ) { int curSum = 0 , curMax = Int32 . MinValue ; HashSet < int > sumSet = new HashSet < int > ( ) ; sumSet . Add ( 0 ) ; for ( int r = 0 ; r < row ; ++ r ) { curSum += sum [ r ] ; List < int > list = new List < int > ( ) ; list . AddRange ( sumSet ) ; int it = list . LastIndexOf ( curSum - k ) ; if ( it > - 1 ) { curMax = Math . Max ( curMax , curSum - it ) ; } sumSet . Add ( curSum ) ; } return curMax ; } static void maxSumSubmatrix ( int [ , ] matrix , int k ) { int row = matrix . GetLength ( 0 ) ; int col = matrix . GetLength ( 1 ) ; int ret = Int32 . MinValue ; for ( int i = 0 ; i < col ; ++ i ) { int [ ] sum = new int [ row ] ; for ( int j = i ; j < col ; ++ j ) { for ( int r = 0 ; r < row ; ++ r ) { sum [ r ] += matrix [ r , j ] ; } int curMax = maxSubarraySum ( sum , k , row ) ; ret = Math . Max ( ret , curMax ) ; } } Console . Write ( ret ) ; } static public void Main ( ) { int [ , ] matrix = { { 1 , 0 , 1 } , { 0 , - 2 , 3 } } ; int K = 2 ; maxSumSubmatrix ( matrix , K ) ; } }


"Maximum sum obtained by dividing Array into several subarrays as per given conditions | C # program for the above approach ; Function to find the required answer ; Stores maximum sum ; Adding the difference of elements at ends of increasing subarray to the answer ; Driver Code ; Input ; Functio calling"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int maximumSum ( int [ ] arr , int N ) { int Sum = 0 ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] ) Sum += ( arr [ i ] - arr [ i - 1 ] ) ; } return Sum ; } public static void Main ( ) { int [ ] arr = { 1 , 5 , 3 } ; int N = arr . Length ; Console . Write ( maximumSum ( arr , N ) ) ; } }


"Maximum sum of Bitwise XOR of all elements of two equal length subsets | C # program for the above approach ; Function that finds the maximum Bitwise XOR sum of the two subset ; Check if the current state is already computed ; Initialize answer to minimum value ; Iterate through all possible pairs ; Check whether ith bit and jth bit of mask is not set then pick the pair ; For all possible pairs find maximum value pick current a [ i ] , a [ j ] and set i , j th bits in mask ; Store the maximum value and return the answer ; Driver Code ; Given array [ ] arr ; Declare Initialize the dp states ; Function call"
Solution in C#:

using System ; class GFG { public static int xorSum ( int [ ] a , int n , int mask , int [ ] dp ) { if ( dp [ mask ] != - 1 ) { return dp [ mask ] ; } int max_value = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( i != j && ( mask & ( 1 << i ) ) == 0 && ( mask & ( 1 << j ) ) == 0 ) { max_value = Math . Max ( max_value , ( a [ i ] ^ a [ j ] ) + xorSum ( a , n , ( mask | ( 1 << i ) | ( 1 << j ) ) , dp ) ) ; } } } return dp [ mask ] = max_value ; } public static void Main ( String [ ] args ) { int n = 4 ; int [ ] arr = { 1 , 2 , 3 , 4 } ; int [ ] dp = new int [ ( 1 << n ) + 5 ] ; for ( int i = 0 ; i < dp . Length ; i ++ ) dp [ i ] = - 1 ; Console . WriteLine ( xorSum ( arr , n , 0 , dp ) ) ; } }


"Maximum sum of Bitwise XOR of elements with their respective positions in a permutation of size N | C # program for the above approach ; Function to calculate the score ; Stores the possible score for the current permutation ; Traverse the permutation array ; Return the readonly score ; Function to generate all the possible permutation and get the max score ; If [ ] arr length is equal to N process the permutation ; Generating the permutations ; If the current element is chosen ; Mark the current element as true ; Recursively call for next possible permutation ; Backtracking ; Return the ans ; Driver Code ; Stores the permutation ; To display the result"
Solution in C#:

 using System ; using System . Collections . Generic ; public class GFG { static int calcScr ( List < int > arr ) { int ans = 0 ; for ( int i = 0 ; i < arr . Count ; i ++ ) ans += ( i ^ arr [ i ] ) ; return ans ; } static int getMax ( List < int > arr , int ans , List < Boolean > chosen , int N ) { if ( arr . Count == N ) { ans = Math . Max ( ans , calcScr ( arr ) ) ; return ans ; } for ( int i = 0 ; i < N ; i ++ ) { if ( chosen [ i ] ) continue ; chosen [ i ] = true ; arr . Add ( i ) ; ans = getMax ( arr , ans , chosen , N ) ; chosen [ i ] = false ; arr . Remove ( arr . Count - 1 ) ; } return ans ; } public static void Main ( String [ ] args ) { int N = 2 ; List < int > arr = new List < int > ( ) ; int ans = - 1 ; List < bool > chosen = new List < bool > ( N ) ; for ( int i = 0 ; i < N ; i ++ ) chosen . Add ( false ) ; ans = getMax ( arr , ans , chosen , N ) ; Console . Write ( ans + "
 " ) ; } }


"Maximum sum of K | C # Program to implement the above approach ; Function to count the number of distinct elements present in the array ; Insert all elements into the Set ; Return the size of set ; Function that finds the maximum sum of K - length subarray having same unique elements as arr [ ] ; Not possible to find a subarray of size K ; Initialize Set ; Calculate sum of the distinct elements ; If the set size is same as the count of distinct elements ; Update the maximum value ; Driver Code ; Stores the count of distinct elements"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int distinct ( int [ ] arr , int n ) { HashSet < int > set = new HashSet < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { set . Add ( arr [ i ] ) ; } return set . Count ; } static int maxSubSum ( int [ ] arr , int n , int k , int totalDistinct ) { if ( k > n ) return 0 ; int max = 0 , sum = 0 ; for ( int i = 0 ; i < n - k + 1 ; i ++ ) { sum = 0 ; HashSet < int > set = new HashSet < int > ( ) ; for ( int j = i ; j < i + k ; j ++ ) { sum += arr [ j ] ; set . Add ( arr [ j ] ) ; } if ( set . Count == totalDistinct ) max = Math . Max ( sum , max ) ; } return max ; } public static void Main ( String [ ] args ) { int [ ] arr = { 7 , 7 , 2 , 4 , 2 , 7 , 4 , 6 , 6 , 6 } ; int K = 6 ; int N = arr . Length ; int totalDistinct = distinct ( arr , N ) ; Console . WriteLine ( maxSubSum ( arr , N , K , totalDistinct ) ) ; } }


"Maximum sum of K | C # Program to implement the above approach ; Function to print the sum of subarray of length K having maximum distinct prime factors ; If K exceeds N ; Stores the count of distinct primes ; True , if index ' i ' is a prime ; Initialize the count of factors to 0 and set all indices as prime ; If i is prime ; Number is prime ; Count of factors of a prime number is 1 ; Increment CountDistinct as the factors of i ; Mark its multiple non - prime ; Compute sum of first K - length subarray ; Compute sums of remaining windows by removing first element of the previous window and adding last element of the current window ; Print the maximum sum ; Driver Code ; Given array ; Given size of subarray ; Size of the array"
Solution in C#:

using System ; public class GFG { static int MAX = 100001 ; static void maxSum ( int [ ] arr , int N , int K ) { if ( N < K ) { Console . WriteLine ( " Invalid " ) ; return ; } 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 Maxarr_sum = 0 , DistPrimeSum = 0 ; for ( int i = 0 ; i < K ; i ++ ) { Maxarr_sum += arr [ i ] ; DistPrimeSum += CountDistinct [ arr [ i ] ] ; } int curr_sum = DistPrimeSum ; int curr_arrSum = Maxarr_sum ; for ( int i = K ; i < N ; i ++ ) { curr_sum += CountDistinct [ arr [ i ] ] - CountDistinct [ arr [ i - K ] ] ; curr_arrSum += arr [ i ] - arr [ i - K ] ; if ( curr_sum > DistPrimeSum ) { DistPrimeSum = curr_sum ; Maxarr_sum = curr_arrSum ; } else if ( curr_sum == DistPrimeSum ) { Maxarr_sum = Math . Max ( curr_arrSum , Maxarr_sum ) ; } } Console . WriteLine ( Maxarr_sum ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 4 , 2 , 10 , 3 } ; int K = 3 ; int N = arr . Length ; maxSum ( arr , N , K ) ; } }


"Maximum sum of K | C # program for the above approach ; Function to count the number of distinct elements present in the array ; Insert array elements into set ; Return the st size ; Function to calculate maximum sum of K - length subarray having same unique elements as arr [ ] ; Not possible to find an subarray of length K from an N - sized array , if K > N ; Traverse the array ; Update the mp ; If i >= K , then decrement arr [ i - K ] element 's one  occurence ; If frequency of any element is 0 then remove the element ; If mp size is same as the count of distinct elements of array arr [ ] then update maximum sum ; Function that finds the maximum sum of K - length subarray having same number of distinct elements as the original array ; Size of array ; Stores count of distinct elements ; Print maximum subarray sum ; Driver Code ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int distinct ( List < int > arr , int N ) { HashSet < int > st = new HashSet < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { st . Add ( arr [ i ] ) ; } return st . Count ; } static int maxSubarraySumUtil ( List < int > arr , int N , int K , int totalDistinct ) { if ( K > N ) return 0 ; int mx = 0 ; int sum = 0 ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) mp [ arr [ i ] ] += 1 ; else mp [ arr [ i ] ] = 1 ; sum += arr [ i ] ; if ( i >= K ) { if ( mp . ContainsKey ( arr [ i - K ] ) ) mp [ arr [ i - K ] ] -= 1 ; else mp [ arr [ i - K ] ] = 1 ; sum -= arr [ i - K ] ; if ( mp [ arr [ i - K ] ] == 0 ) mp . Remove ( arr [ i - K ] ) ; } if ( mp . Count == totalDistinct ) mx = Math . Max ( mx , sum ) ; } return mx ; } static void maxSubarraySum ( List < int > arr , int K ) { int N = arr . Count ; int totalDistinct = distinct ( arr , N ) ; Console . WriteLine ( maxSubarraySumUtil ( arr , N , K , totalDistinct ) ) ; } public static void Main ( ) { List < int > arr = new List < int > { 7 , 7 , 2 , 4 , 2 , 7 , 4 , 6 , 6 , 6 } ; int K = 6 ; maxSubarraySum ( arr , K ) ; } }


"Maximum sum of a path in a Right Number Triangle | C # program to print maximum sum in a right triangle of numbers ; function to find maximum sum path ; Adding the element of row 1 to both the elements of row 2 to reduce a step from the loop ; Traverse remaining rows ; Loop to traverse columns ; tri [ i ] would store the possible combinations of sum of the paths ; array at n - 1 index ( tri [ i ] ) stores all possible adding combination , finding the maximum one out of them ; Driver Code"
Solution in C#:

using System ; class GFG { static int maxSum ( int [ , ] tri , int n ) { if ( n > 1 ) tri [ 1 , 1 ] = tri [ 1 , 1 ] + tri [ 0 , 0 ] ; tri [ 1 , 0 ] = tri [ 1 , 0 ] + tri [ 0 , 0 ] ; for ( int i = 2 ; i < n ; i ++ ) { tri [ i , 0 ] = tri [ i , 0 ] + tri [ i - 1 , 0 ] ; tri [ i , i ] = tri [ i , i ] + tri [ i - 1 , i - 1 ] ; for ( int j = 1 ; j < i ; j ++ ) { if ( tri [ i , j ] + tri [ i - 1 , j - 1 ] >= tri [ i , j ] + tri [ i - 1 , j ] ) tri [ i , j ] = tri [ i , j ] + tri [ i - 1 , j - 1 ] ; else tri [ i , j ] = tri [ i , j ] + tri [ i - 1 , j ] ; } } int max = tri [ n - 1 , 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( max < tri [ n - 1 , i ] ) max = tri [ n - 1 , i ] ; } return max ; } public static void Main ( ) { int [ , ] tri = { { 1 , 0 , 0 } , { 2 , 1 , 0 } , { 3 , 3 , 2 } } ; Console . Write ( maxSum ( tri , 3 ) ) ; } }


"Maximum sum of a subsequence having difference between their indices equal to the difference between their values | C # program for the above approach ; Function to find the maximum sum of a subsequence having difference between indices equal to difference in their values ; Stores the maximum sum ; Stores the value for each A [ i ] - i ; Traverse the array ; Update the value in map ; Update the answer ; Finally , print the answer ; Driver code ; Given Input ; Function Call"
Solution in C#:

using System . Collections . Generic ; using System ; class GFG { static void maximumSubsequenceSum ( int [ ] A , int N ) { int ans = 0 ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . ContainsKey ( A [ i ] - i ) ) mp [ A [ i ] - i ] += A [ i ] ; else mp [ A [ i ] - i ] = A [ i ] ; ans = Math . Max ( ans , mp [ A [ i ] - i ] ) ; } Console . Write ( ans ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 10 , 7 , 1 , 9 , 10 , 1 } ; int N = A . Length ; maximumSubsequenceSum ( A , N ) ; } }


"Maximum sum of a subsequence whose Bitwise AND is non | C # program for the above approach ; Function to find the maximum sum of a subsequence whose Bitwise AND is non - zero ; Stores the resultant maximum sum of the subsequence ; Iterate over all the bits ; Stores the sum of array elements whose i - th bit is set ; Traverse the array elements ; If the bit is set , then add its value to the sum ; Update the resultant maximum sum ; Return the maximum sum ; Driver code"
Solution in C#:

using System ; class GFG { static int maximumSum ( int [ ] arr , int N ) { int ans = 0 ; for ( int bit = 0 ; bit < 32 ; bit ++ ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] & ( 1 << bit ) ) != 0 ) { sum += arr [ i ] ; } } ans = Math . Max ( ans , sum ) ; } return ans ; } static public void Main ( ) { int [ ] arr = { 5 , 4 , 1 , 7 , 11 } ; int N = arr . Length ; Console . Write ( maximumSum ( arr , N ) ) ; } }


"Maximum sum of absolute difference of any permutation | C # implementation of above algorithm ; final sequence stored in the vector ; sort the original array so that we can retrieve the large elements from the end of array elements ; In this loop first we will insert one smallest element not entered till that time in final sequence and then enter a highest element ( not entered till that time ) in final sequence so that we have large difference value . This process is repeated till all array has completely entered in sequence . Here , we have loop till n / 2 because we are inserting two elements at a time in loop . ; If there are odd elements , push the middle element at the end . ; variable to store the maximum sum of absolute difference ; In this loop absolute difference of elements for the final sequence is calculated . ; absolute difference of last element and 1 st element ; return the value ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int MaxSumDifference ( int [ ] a , int n ) { List < int > finalSequence = new List < int > ( ) ; Array . Sort ( a ) ; for ( int i = 0 ; i < n / 2 ; ++ i ) { finalSequence . Add ( a [ i ] ) ; finalSequence . Add ( a [ n - i - 1 ] ) ; } if ( n % 2 != 0 ) finalSequence . Add ( a [ n / 2 ] ) ; int MaximumSum = 0 ; for ( int i = 0 ; i < n - 1 ; ++ i ) { MaximumSum = MaximumSum + Math . Abs ( finalSequence [ i ] - finalSequence [ i + 1 ] ) ; } MaximumSum = MaximumSum + Math . Abs ( finalSequence [ n - 1 ] - finalSequence [ 0 ] ) ; return MaximumSum ; } public static void Main ( ) { int [ ] a = { 1 , 2 , 4 , 8 } ; int n = a . Length ; Console . WriteLine ( MaxSumDifference ( a , n ) ) ; } }


"Maximum sum of absolute differences between distinct pairs of a triplet from an array | C # program for the above approach ; Function to find the maximum sum of absolute differences between distinct pairs of triplet in array ; Stores the maximum sum ; Sort the array in ascending order ; Sum of differences between pairs of the triplet ; Print the sum ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void maximumSum ( int [ ] arr , int N ) { int sum ; Array . Sort ( arr ) ; sum = ( arr [ N - 1 ] - arr [ 0 ] ) + ( arr [ N - 2 ] - arr [ 0 ] ) + ( arr [ N - 1 ] - arr [ N - 2 ] ) ; Console . Write ( sum ) ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 4 , 2 } ; int N = arr . Length ; maximumSum ( arr , N ) ; } }


"Maximum sum of all elements of array after performing given operations | C # program to find the maximum sum after given operations ; Function to calculate Maximum Subarray Sum or Kadane 's Algorithm ; Function to find the maximum sum after given operations ; To store sum of all elements ; Maximum sum of a subarray ; Calculate the sum of all elements ; Driver Code ; size of an array"
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 maxSum ( int [ ] a , int n ) { int S = 0 ; int S1 = maxSubArraySum ( a , n ) ; for ( int i = 0 ; i < n ; i ++ ) S += a [ i ] ; return ( 2 * S1 - S ) ; } public static void Main ( ) { int [ ] a = { - 35 , 32 , - 24 , 0 , 27 , - 10 , 0 , - 19 } ; int n = a . Length ; Console . WriteLine ( maxSum ( a , n ) ) ; } }


"Maximum sum of any submatrix of a Matrix which is sorted row | C # program for the above approach ; Function that finds the maximum Sub - Matrix Sum ; Number of rows in the matrix ; Number of columns in the matrix ; [ , ] dp matrix to store the results of each iteration ; Base Case - The largest element in the matrix ; To stores the readonly result ; Find the max sub matrix sum for the last row ; Check whether the current sub - array yields maximum sum ; Calculate the max sub matrix sum for the last column ; Check whether the current sub - array yields maximum sum ; Build the [ , ] dp matrix from bottom to the top row ; Update sum at each cell in [ , ] dp ; Update the maximum sum ; Return the maximum sum ; Driver Code ; Given matrix [ , ] mat ; Function Call"
Solution in C#:

using System ; class GFG { static int maxSubMatSum ( int [ , ] mat ) { int n = mat . GetLength ( 0 ) ; int m = mat . GetLength ( 1 ) ; int i , j ; int [ , ] dp = new int [ n , m ] ; dp [ n - 1 , m - 1 ] = mat [ n - 1 , m - 1 ] ; int res = dp [ n - 1 , m - 1 ] ; for ( i = m - 2 ; i >= 0 ; i -- ) { dp [ n - 1 , i ] = mat [ n - 1 , i ] + dp [ n - 1 , i + 1 ] ; res = Math . Max ( res , dp [ n - 1 , i ] ) ; } for ( i = n - 2 ; i >= 0 ; i -- ) { dp [ i , m - 1 ] = mat [ i , m - 1 ] + dp [ i + 1 , m - 1 ] ; res = Math . Max ( res , dp [ i , m - 1 ] ) ; } for ( i = n - 2 ; i >= 0 ; i -- ) { for ( j = m - 2 ; j >= 0 ; j -- ) { dp [ i , j ] = mat [ i , j ] + dp [ i , j + 1 ] + dp [ i + 1 , j ] - dp [ i + 1 , j + 1 ] ; res = Math . Max ( res , dp [ i , j ] ) ; } } return res ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { - 6 , - 4 , - 1 } , { - 3 , 2 , 4 } , { 2 , 5 , 8 } } ; Console . Write ( maxSubMatSum ( mat ) ) ; } }


"Maximum sum of array after removing a positive or negative subarray | C # program for the above approach ; Function to find the maximum sum of array after removing either the contiguous positive or negative elements ; Store the total sum of array ; Store the maximum contiguous negative sum ; Store the sum of current contiguous negative elements ; Store the minimum element of array ; Traverse the array , arr [ ] ; Update the overall sum ; Store minimum element of array ; If arr [ i ] is positive ; Update temp_sum to 0 ; Add arr [ i ] to temp_sum ; Update max_neg ; If no negative element in array then remove smallest positive element ; Print the required sum ; Driver Code ; Given Input ; Function Call"
Solution in C#:

using System ; class GFG { static void maxSum ( int [ ] arr , int n ) { int sum = 0 ; int max_neg = Int32 . MaxValue ; int tempsum = 0 ; int small = Int32 . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; small = Math . Min ( small , arr [ i ] ) ; if ( arr [ i ] > 0 ) { tempsum = 0 ; } else { tempsum += arr [ i ] ; } max_neg = Math . Min ( max_neg , tempsum ) ; } if ( max_neg == 0 ) { max_neg = small ; } Console . Write ( sum - max_neg ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 } ; int n = arr . Length ; maxSum ( arr , n ) ; } }


"Maximum sum of cocktail glass in a 2D matrix | C # implementation of the approach ; Function to return the maximum sum of a cocktail glass ; If no cocktail glass is possible ; Initialize max_sum with the mini ; Here loop runs ( R - 2 ) * ( C - 2 ) times considering different top left cells of cocktail glasses ; Considering mat [ i ] [ j ] as the top left cell of the cocktail glass ; Update the max_sum ; Driver code"
Solution in C#:

using System ; class GFG { static int R = 5 ; static int C = 5 ; static int findMaxCock ( int [ , ] ar ) { if ( R < 3 C < 3 ) return - 1 ; int max_sum = int . MinValue ; for ( int i = 0 ; i < R - 2 ; i ++ ) { for ( int j = 0 ; j < C - 2 ; j ++ ) { int sum = ( ar [ i , j ] + ar [ i , j + 2 ] ) + ( ar [ i + 1 , j + 1 ] ) + ( ar [ i + 2 , j ] + ar [ i + 2 , j + 1 ] + ar [ i + 2 , j + 2 ] ) ; max_sum = Math . Max ( max_sum , sum ) ; } } return max_sum ; } public static void Main ( ) { int [ , ] ar = { { 0 , 3 , 0 , 6 , 0 } , { 0 , 1 , 1 , 0 , 0 } , { 1 , 1 , 1 , 0 , 0 } , { 0 , 0 , 2 , 0 , 1 } , { 0 , 2 , 0 , 1 , 3 } } ; Console . WriteLine ( findMaxCock ( ar ) ) ; } }


"Maximum sum of difference of adjacent elements | C # program to find maximum sum of adjacent elements of permutation of n ; To find max sum of permutation ; Base case ; Otherwise max sum will be ( n * ( n - 1 ) / 2 ) - 1 + n / 2 ; Driver program to test maxSum ( )"
Solution in C#:

using System ; public class main { static int maxSum ( int n ) { if ( n == 1 ) return 1 ; else return ( n * ( n - 1 ) / 2 ) - 1 + n / 2 ; } public static void Main ( ) { int n = 3 ; Console . WriteLine ( maxSum ( n ) ) ; } }


"Maximum sum of distinct numbers such that LCM of these numbers is N | C # program to find the max sum of numbers whose lcm is n ; Returns maximum sum of numbers with LCM as N ; Initialize result ; Finding a divisor of n and adding it to max_sum ; Driver Code"
Solution in C#:

using System ; class MaxSum { static int maxSumLCM ( int n ) { int max_sum = 0 ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { max_sum += i ; if ( n / i != i ) max_sum += ( n / i ) ; } } return max_sum ; } public static void Main ( String [ ] args ) { int n = 2 ; Console . Write ( maxSumLCM ( n ) ) ; } }


"Maximum sum of distinct numbers with LCM as N | C # program to get maximum sum of Numbers with condition that their LCM should be N ; Method returns maximum sum f distinct number whose LCM is N ; Find all divisors which divides ' N ' ; if ' i ' is divisor of ' N ' ; if both divisors are same then add it only once else add both ; Driver code"
Solution in C#:

using System ; class GFG { static int getMaximumSumWithLCMN ( int N ) { int sum = 0 ; int LIM = ( int ) Math . Sqrt ( N ) ; for ( int i = 1 ; i <= LIM ; i ++ ) { if ( N % i == 0 ) { if ( i == ( N / i ) ) sum += i ; else sum += ( i + N / i ) ; } } return sum ; } public static void Main ( ) { int N = 12 ; Console . Write ( getMaximumSumWithLCMN ( N ) ) ; } }


"Maximum sum of elements divisible by K from the given array |  ; Function to return the maximum sum divisible by k from elements of v ; check if sum of elements excluding the current one is divisible by k ; check if sum of elements including the current one is divisible by k ; Store the maximum ; Driver code"
Solution in C#:

using System ; class GFG { static int [ , ] dp = new int [ 1001 , 1001 ] ; static int find_max ( int i , int sum , int [ ] v , int k ) { if ( i == v . Length ) return 0 ; if ( dp [ i , sum ] != - 1 ) return dp [ i , sum ] ; int ans = 0 ; if ( ( sum + find_max ( i + 1 , sum , v , k ) ) % k == 0 ) ans = find_max ( i + 1 , sum , v , k ) ; if ( ( sum + v [ i ] + find_max ( i + 1 , ( sum + v [ i ] ) % k , v , k ) ) % k == 0 ) ans = Math . Max ( ans , v [ i ] + find_max ( i + 1 , ( sum + v [ i ] ) % k , v , k ) ) ; return dp [ i , sum ] = ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 43 , 1 , 17 , 26 , 15 } ; int k = 16 ; for ( int i = 0 ; i < 1001 ; i ++ ) for ( int j = 0 ; j < 1001 ; j ++ ) dp [ i , j ] = - 1 ; Console . Write ( find_max ( 0 , 0 , arr , k ) ) ; } }