"Maximize cost of forming a set of words using given set of characters | C # program for the above approach ; Function to find the maximum cost of any valid set of words formed by using the given letters ; Stores frequency of characters ; Find the maximum cost ; Utility function to find maximum cost of generating any possible subsets of strings ; Base Case ; Stores the current cost ; Stores the cost of by the current word ; Create a copy of the letterCounts array ; Traverse the current word ; Store the current index & check if its frequency is 0 or not ; If true , then update wordScore to - 1 ; Otherwise , add the cost of the current index to wordScore ; Decrease its frequency ; If wordScore > 0 , then recursively call for next index ; Find the cost by not including current word ; Return the maximum score ; Driver Code ; Given arrays ; Function Call"
Solution in C#:

using System ; using System . Linq ; class GFG { public static int maxScoreWords ( string [ ] words , char [ ] letters , int [ ] score ) { int [ ] letterCounts = new int [ 26 ] ; foreach ( char letter in letters ) letterCounts [ letter - ' a ' ] ++ ; return helper ( words , 0 , letterCounts , score ) ; } public static int helper ( string [ ] words , int start , int [ ] letterCounts , int [ ] score ) { if ( start == words . Length ) return 0 ; int currScore = 0 ; int wordScore = 0 ; int [ ] nextCounts = letterCounts . ToArray ( ) ; for ( int i = 0 ; i < words [ start ] . Length ; ++ i ) { int idx = words [ start ] [ i ] - ' a ' ; if ( nextCounts [ idx ] == 0 ) { wordScore = - 1 ; break ; } wordScore += score [ idx ] ; nextCounts [ idx ] -- ; } if ( wordScore > 0 ) currScore = helper ( words , start + 1 , nextCounts , score ) + wordScore ; currScore = Math . Max ( currScore , helper ( words , start + 1 , letterCounts , score ) ) ; return currScore ; } public static void Main ( string [ ] args ) { string [ ] words = { " dog " , " cat " , " dad " , " good " } ; char [ ] letters = { ' a ' , ' a ' , ' c ' , ' d ' , ' d ' , ' d ' , ' g ' , ' o ' , ' o ' } ; int [ ] score = { 1 , 0 , 9 , 5 , 0 , 0 , 3 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 2 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } ; Console . WriteLine ( maxScoreWords ( words , letters , score ) ) ; } }


"Maximize cost of removing all occurrences of substrings " ab " and " ba " | C # program for the above approach : ; Function to find the maximum cost of removing substrings " ab " and " ba " from S ; MaxStr is the substring char array with larger cost ; MinStr is the substring char array with smaller cost ; ; Denotes larger point ; Denotes smaller point ; Stores cost scored ; Stack to keep track of characters ; Traverse the string ; If the substring is maxstr ; Pop from the stack ; Add maxp to cost ; Push the character to the stack ; Remaining string after removing maxstr ; Find remaining string ; Reversing the string retrieved from the stack ; Removing all occurences of minstr ; If the substring is minstr ; Pop from the stack ; Add minp to the cost ; Otherwise ; Return the maximum cost ; Driver Code ; Input String ; Costs"
Solution in C#:

using System ; using System . Collections ; class GFG { static int MaxCollection ( string S , int P , int Q ) { char [ ] maxstr = ( P >= Q ? " ab " : " ba " ) . ToCharArray ( ) ; char [ ] minstr = ( P >= Q ? " ba " : " ab " ) . ToCharArray ( ) ; int maxp = Math . Max ( P , Q ) ; int minp = Math . Min ( P , Q ) ; int cost = 0 ; Stack stack1 = new Stack ( ) ; char [ ] s = S . ToCharArray ( ) ; foreach ( char ch in s ) { if ( stack1 . Count > 0 && ( ( char ) stack1 . Peek ( ) == maxstr [ 0 ] && ch == maxstr [ 1 ] ) ) { stack1 . Pop ( ) ; cost += maxp ; } else { stack1 . Push ( ch ) ; } } string sb = " " ; while ( stack1 . Count > 0 ) { sb = sb + stack1 . Peek ( ) ; stack1 . Pop ( ) ; } char [ ] chars = sb . ToCharArray ( ) ; Array . Reverse ( chars ) ; string remstr = new string ( chars ) ; foreach ( char ch in remstr . ToCharArray ( ) ) { if ( stack1 . Count > 0 && ( ( char ) stack1 . Peek ( ) == minstr [ 0 ] && ch == minstr [ 1 ] ) ) { stack1 . Pop ( ) ; cost += minp ; } else { stack1 . Push ( ch ) ; } } return cost ; } static void Main ( ) { string S = " cbbaabbaab " ; int P = 6 ; int Q = 4 ; Console . Write ( MaxCollection ( S , P , Q ) ) ; } }


"Maximize cost to empty an array by removing contiguous subarrays of equal elements | C # program for the above approach ; Initialize dp array ; Function that removes elements from array to maximize the cost ; Base case ; Check if an answer is stored ; Deleting count + 1 i . e . including the first element and starting a new sequence ; Removing [ left + 1 , i - 1 ] elements to continue with previous sequence ; Store the result ; Return answer ; Function to remove the elements ; Function call ; Driver Code ; Given array ; Function call"
Solution in C#:

using System ; class GFG { static int [ , , ] dp = new int [ 101 , 101 , 101 ] ; static int helper ( int [ ] arr , int left , int right , int count , int m ) { if ( left > right ) return 0 ; if ( dp [ left , right , count ] != - 1 ) { return dp [ left , right , count ] ; } int ans = ( count + 1 ) * m + helper ( arr , left + 1 , right , 0 , m ) ; for ( int i = left + 1 ; i <= right ; ++ i ) { if ( arr [ i ] == arr [ left ] ) { ans = Math . Max ( ans , helper ( arr , left + 1 , i - 1 , 0 , m ) + helper ( arr , i , right , count + 1 , m ) ) ; } } dp [ left , right , count ] = ans ; return ans ; } static int maxPoints ( int [ ] arr , int n , int m ) { int len = n ; for ( int i = 0 ; i < 101 ; i ++ ) { for ( int j = 0 ; j < 101 ; j ++ ) { for ( int k = 0 ; k < 101 ; k ++ ) dp [ i , j , k ] = - 1 ; } } return helper ( arr , 0 , len - 1 , 0 , m ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 2 , 2 , 2 , 3 , 4 , 3 , 1 } ; int M = 3 ; int N = arr . Length ; Console . Write ( maxPoints ( arr , N , M ) ) ; } }


"Maximize cost to empty given array by repetitively removing K array elements | C # program for the above approach ; Function to find the maximum cost to remove all array elements ; Stores maximum cost to remove array elements ; Sort array in ascending order ; Traverse the array ; Update maxCost ; Drive Code"
Solution in C#:

using System ; class GFG { static int maxCostToRemove ( int [ ] arr , int N , int K ) { int maxCost = 0 ; Array . Sort ( arr ) ; for ( int i = 0 ; i < N ; i += K ) { maxCost += arr [ i + 1 ] ; } return maxCost ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 4 , 1 , 5 , 1 , 5 , 3 } ; int N = arr . Length ; int K = 4 ; Console . Write ( maxCostToRemove ( arr , N , K ) ) ; } }


"Maximize count of 1 s in an array by repeated division of array elements by 2 at most K times | C # program for the above approach ; Function to count the maximum number of array elements that can be reduced to 1 by repeatedly dividing array elements by 2 ; Sort the array in ascending order ; Store the count of array elements ; Traverse the array ; Store the number of operations required to reduce arr [ i ] to 1 ; Decrement k by opr ; If k becomes less than 0 , then break out of the loop ; Increment cnt by 1 ; Print the answer ; Driver Code"
Solution in C#:

using System ; public class GFG { static void findMaxNumbers ( int [ ] arr , int n , int k ) { Array . Sort ( arr ) ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int opr = ( int ) Math . Ceiling ( ( Math . Log ( arr [ i ] ) / Math . Log ( 2 ) ) ) ; k -= opr ; if ( k < 0 ) { break ; } cnt ++ ; } Console . Write ( cnt ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 8 , 4 , 7 } ; int N = arr . Length ; int K = 5 ; findMaxNumbers ( arr , N , K ) ; } }


"Maximize count of 3 | C # program to implement the above approach ; Driver Code ; Function to count the maximum number of palindrome subsequences of length 3 considering the same index only once ; Index of the string S ; Stores the frequency of every character ; Stores the pair of frequency containing same characters ; Number of subsequences having length 3 ; Counts the frequency ; Counts the pair of frequency ; Returns the minimum value"
Solution in C#:

using System ; class GFG { public static void Main ( String [ ] args ) { string S = " geeksforg " ; Console . WriteLine ( maxNumPalindrome ( S ) ) ; } static int maxNumPalindrome ( string S ) { int i = 0 ; int [ ] freq = new int [ 26 ] ; int freqPair = 0 ; int len = S . Length / 3 ; while ( i < S . Length ) { freq [ S [ i ] - ' a ' ] ++ ; i ++ ; } for ( i = 0 ; i < 26 ; i ++ ) { freqPair += ( freq [ i ] / 2 ) ; } return Math . Min ( freqPair , len ) ; } }


"Maximize count of array elements required to obtain given sum | C # program for the above approach ; Function that count the maximum number of elements to obtain sum V ; Stores the maximum number of elements required to obtain V ; Base Case ; Initialize all table values as Infinite ; Find the max arr required for all values from 1 to V ; Go through all arr smaller than i ; If current coin value is less than i ; Update table [ i ] ; Return the final count ; Driver code ; Given array ; Given sum V ; Function Call"
Solution in C#:

using System ; class GFG { static int maxCount ( int [ ] arr , int m , int V ) { int [ ] table = new int [ V + 1 ] ; table [ 0 ] = 0 ; for ( int i = 1 ; i <= V ; i ++ ) table [ i ] = - 1 ; for ( int i = 1 ; i <= V ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( arr [ j ] <= i ) { int sub_res = table [ i - arr [ j ] ] ; if ( sub_res != - 1 && sub_res + 1 > table [ i ] ) table [ i ] = sub_res + 1 ; } } } return table [ V ] ; } static void Main ( ) { int [ ] arr = { 25 , 10 , 5 } ; int m = arr . Length ; int V = 30 ; Console . WriteLine ( maxCount ( arr , m , V ) ) ; } }


"Maximize count of corresponding same elements in given permutations using cyclic rotations | C # program for the above approach ; Function to maximize the matching pairs between two permutation using left and right rotation ; Left array store distance of element from left side and right array store distance of element from right side ; Map to store index of elements ; idx1 is index of element in first permutation idx2 is index of element in second permutation ; If element if present on same index on both permutations then distance is zero ; Calculate distance from left and right side ; Calculate distance from left and right side ; Maps to store frequencies of elements present in left and right arrays ; Find maximum frequency ; Return the result ; Driver Code ; Given permutations P1 and P2 ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int maximumMatchingPairs ( int [ ] perm1 , int [ ] perm2 , int n ) { int [ ] left = new int [ n ] ; int [ ] right = new int [ n ] ; Dictionary < int , int > mp1 = new Dictionary < int , int > ( ) ; Dictionary < int , int > mp2 = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { mp1 [ perm1 [ i ] ] = i ; } for ( int j = 0 ; j < n ; j ++ ) { mp2 [ perm2 [ j ] ] = j ; } for ( int i = 0 ; i < n ; i ++ ) { int idx2 = mp2 [ perm1 [ i ] ] ; int idx1 = i ; if ( idx1 == idx2 ) { left [ i ] = 0 ; right [ i ] = 0 ; } else if ( idx1 < idx2 ) { left [ i ] = ( n - ( idx2 - idx1 ) ) ; right [ i ] = ( idx2 - idx1 ) ; } else { left [ i ] = ( idx1 - idx2 ) ; right [ i ] = ( n - ( idx1 - idx2 ) ) ; } } Dictionary < int , int > freq1 = new Dictionary < int , int > ( ) ; Dictionary < int , int > freq2 = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq1 . ContainsKey ( left [ i ] ) ) freq1 [ left [ i ] ] ++ ; else freq1 [ left [ i ] ] = 1 ; if ( freq2 . ContainsKey ( right [ i ] ) ) freq2 [ right [ i ] ] ++ ; else freq2 [ right [ i ] ] = 1 ; } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans = Math . Max ( ans , Math . Max ( freq1 [ left [ i ] ] , freq2 [ right [ i ] ] ) ) ; } return ans ; } public static void Main ( string [ ] args ) { int [ ] P1 = { 5 , 4 , 3 , 2 , 1 } ; int [ ] P2 = { 1 , 2 , 3 , 4 , 5 } ; int n = P1 . Length ; Console . Write ( maximumMatchingPairs ( P1 , P2 , n ) ) ; } }


"Maximize count of distinct elements possible in an Array from the given operation | C # program to find the maximum possible distinct elements that can be obtained from the array by performing the given operations ; Function to find the gcd of two numbers ; Function to calculate and return the count of maximum possible distinct elements in the array ; Find the maximum element ; Base Case ; Finding the gcd of first two element ; Calculate Gcd of the array ; Return the total count of distinct elements ; Driver Code"
Solution in C#:

using System ; using System . Linq ; class GFG { static int gcd ( int x , int y ) { if ( x == 0 ) return y ; return gcd ( y % x , x ) ; } static int findDistinct ( int [ ] arr , int n ) { int maximum = arr . Max ( ) ; if ( n == 1 ) return 1 ; if ( n == 2 ) { return ( maximum / gcd ( arr [ 0 ] , arr [ 1 ] ) ) ; } int k = gcd ( arr [ 0 ] , arr [ 1 ] ) ; for ( int i = 2 ; i < n ; i ++ ) { k = gcd ( k , arr [ i ] ) ; } return ( maximum / k ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 5 } ; int n = arr . Length ; Console . Write ( findDistinct ( arr , n ) ) ; } }


"Maximize count of distinct profits possible by N transactions | C # program for the above approach ; Function to count distinct profits possible ; Stores the minimum total profit ; Stores the maximum total profit ; Return count of distinct profits ; Driver Code ; Input ; Function call"
Solution in C#:

using System ; class GFG { static int numberOfWays ( int N , int X , int Y ) { int S1 = ( N - 1 ) * X + Y ; int S2 = ( N - 1 ) * Y + X ; return ( S2 - S1 + 1 ) ; } public static void Main ( ) { int N = 3 ; int X = 13 ; int Y = 15 ; Console . WriteLine ( numberOfWays ( N , X , Y ) ) ; } }


"Maximize count of distinct strings generated by replacing similar adjacent digits having sum K with K | C # program for the above approach ; Function to find the desired number of strings ; Store the count of strings ; Store the length of the string ; Initialize variable to indicate the start of the substring ; Store the starting index of the substring ; Traverse the string ; If sum of adjacent characters is K mark the starting index and set flag to 1 ; If sum of adjacent characters is not K and the flag variable is set , end the substring here ; Set flag to 0 denoting the end of substring ; Check if the length of the substring formed is odd ; Update the answer ; If flag is set and end of string is reached , mark the end of substring ; Update the answer ; Print the answer ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static void countStrings ( String s , int k ) { int ans = 1 ; int len = s . Length ; int flag = 0 ; int start_ind = 0 ; for ( int i = 0 ; i < len - 1 ; i ++ ) { if ( s [ i ] - '0' + s [ i + 1 ] - '0' == k && flag == 0 ) { flag = 1 ; start_ind = i ; } if ( flag == 1 && s [ i ] - '0' + s [ i + 1 ] - '0' != k ) { flag = 0 ; if ( ( i - start_ind + 1 ) % 2 != 0 ) ans *= ( i - start_ind + 1 - 1 ) / 2 + 1 ; } } if ( flag == 1 && ( len - start_ind ) % 2 != 0 ) ans *= ( len - start_ind ) / 2 + 1 ; Console . WriteLine ( ans ) ; } public static void Main ( String [ ] args ) { String S = "313" ; int K = 4 ; countStrings ( S , K ) ; } }


"Maximize count of empty water bottles from N filled bottles | C # program for the above approach ; Function to find the maximum bottles that can be emptied ; Iterate until a is non - zero ; Add the number of bottles that are emptied ; Update a after exchanging empty bottles ; Stores the number of bottles left after the exchange ; Return the answer ; Driver Code ; Function call"
Solution in C#:

 using System ; class GFG { static int maxBottles ( int n , int e ) { int s = 0 , b = 0 ; int a = n ; while ( a != 0 ) { s = s + a ; a = ( a + b ) / e ; b = n - ( a * e ) ; n = a + b ; } return s ; } public static void Main ( ) { int n = 9 , e = 3 ; int s = maxBottles ( n , e ) ; Console . Write ( s + "
 " ) ; } }


"Maximize count of equal numbers in Array of numbers upto N by replacing pairs with their sum | C # implementation of the above approach ; Function to count maximum number of array elements equal ; Driver Code ; Function call"
Solution in C#:

using System ; class GFG { static int countEqual ( int n ) { return ( n + 1 ) / 2 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = arr . Length ; Console . WriteLine ( countEqual ( n ) ) ; } }


"Maximize count of nodes disconnected from all other nodes in a Graph | C # implementation of the above approach ; Function which returns the maximum number of isolated nodes ; Used nodes ; Remaining edges ; Count nodes used ; If given edges are non - zero ; Driver Code ; Given N and E ; Function call"
Solution in C#:

using System ; class GFG { static int maxDisconnected ( int N , int E ) { int curr = 1 ; int rem = E ; while ( rem > 0 ) { rem = rem - Math . Min ( curr , rem ) ; curr ++ ; } if ( curr > 1 ) { return N - curr ; } else { return N ; } } public static void Main ( String [ ] args ) { int N = 5 , E = 1 ; Console . Write ( maxDisconnected ( N , E ) ) ; } }


"Maximize count of non | C # program to implement the above approach ; Function to count the maximum number of subarrays with sum K ; Stores all the distinct prefixSums obtained ; Stores the prefix sum of the current subarray ; Stores the count of subarrays with sum K ; If a subarray with sum K is already found ; Increase count ; Reset prefix sum ; Clear the set ; Insert the prefix sum ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int CtSubarr ( int [ ] arr , int N , int K ) { HashSet < int > st = new HashSet < int > ( ) ; int prefixSum = 0 ; st . Add ( prefixSum ) ; int res = 0 ; for ( int i = 0 ; i < N ; i ++ ) { prefixSum += arr [ i ] ; if ( st . Contains ( prefixSum - K ) ) { res += 1 ; prefixSum = 0 ; st . Clear ( ) ; st . Add ( 0 ) ; } st . Add ( prefixSum ) ; } return res ; } public static void Main ( String [ ] args ) { int [ ] arr = { - 2 , 6 , 6 , 3 , 5 , 4 , 1 , 2 , 8 } ; int N = arr . Length ; int K = 10 ; Console . WriteLine ( CtSubarr ( arr , N , K ) ) ; } }


"Maximize count of odd | C # implementation for the above approach ; To find maximum number of pairs in array with conversion of at most one element ; Initialize count of even elements ; Initialize count of odd elements ; If current number is even then increment x by 1 ; If current number is odd then increment y by 1 ; Initialize the answer by min ( x , y ) ; If difference in count of odd and even is more than 2 than increment answer ; Return final answer ; Driver code ; Given array"
Solution in C#:

using System ; public class GFG { static int maximumNumberofpairs ( int n , int [ ] arr ) { int x = 0 ; int y = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { x ++ ; } else { y ++ ; } } int answer = Math . Min ( x , y ) ; if ( Math . Abs ( x - y ) >= 2 ) { answer ++ ; } return answer ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 2 , 4 , 6 , 5 , 10 , 12 } ; int n = arr . Length ; Console . WriteLine ( maximumNumberofpairs ( n , arr ) ) ; } }


"Maximize count of pairs whose Bitwise AND exceeds Bitwise XOR by replacing such pairs with their Bitwise AND | C # program for the above approach ; Function to count the number of pairs whose Bitwise AND is greater than the Bitwise XOR ; Stores the frequency of MSB of array elements ; Traverse the array ; Increment count of numbers having MSB at log ( arr [ i ] ) ; Stores total number of pairs ; Traverse the Map ; Return total count of pairs ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int countPairs ( int [ ] arr , int N ) { Dictionary < int , int > freq = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( freq . ContainsKey ( ( int ) ( Math . Log ( Convert . ToDouble ( arr [ i ] ) , 2.0 ) ) ) ) freq [ ( int ) ( Math . Log ( Convert . ToDouble ( arr [ i ] ) , 2.0 ) ) ] ++ ; else freq [ ( int ) ( Math . Log ( Convert . ToDouble ( arr [ i ] ) , 2.0 ) ) ] = 1 ; } int pairs = 0 ; foreach ( var item in freq ) { pairs += item . Value - 1 ; } return pairs ; } public static void Main ( ) { int [ ] arr = { 12 , 9 , 15 , 7 } ; int N = arr . Length ; Console . WriteLine ( countPairs ( arr , N ) ) ; } }


"Maximize count of pairs whose bitwise XOR is even by replacing such pairs with their Bitwise XOR | C # program to implement the above approach ; Function to maximize the count of pairs with even XOR possible in an array by given operations ; Stores count of odd array elements ; Traverse the array ; If arr [ i ] is odd ; Stores the total number of even pairs ; Driver Code ; Input ; Function call to count the number of pairs whose XOR is even"
Solution in C#:

using System ; class GFG { static int countPairs ( int [ ] arr , int N ) { int odd = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] & 1 ) != 0 ) odd ++ ; } int ans = ( N - odd + odd / 2 - 1 ) + odd / 2 ; return ans ; } public static void Main ( ) { int [ ] arr = { 4 , 6 , 1 , 3 } ; int N = arr . Length ; Console . Write ( countPairs ( arr , N ) ) ; } }


"Maximize count of planes that can be stopped per second with help of given initial position and speed | C # program for the above approach ; Function to find maximum number of planes that can be stopped from landing ; Stores the times needed for landing for each plane ; Iterate over the arrays ; Stores the time needed for landing of current plane ; Update the value of t ; Append the t in set St ; Return the answer ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { static int maxPlanes ( int [ ] A , int [ ] B ) { HashSet < int > St = new HashSet < int > ( ) ; for ( int i = 0 ; i < A . Length ; i ++ ) { int t = ( A [ i ] % B [ i ] > 0 ) ? 1 : 0 ; t += ( A [ i ] / B [ i ] ) + t ; St . Add ( t ) ; } return St . Count ; } static public void Main ( ) { int [ ] A = { 1 , 3 , 5 , 4 , 8 } ; int [ ] B = { 1 , 2 , 2 , 1 , 2 } ; Console . WriteLine ( maxPlanes ( A , B ) ) ; } }


"Maximize count of rows consisting of equal elements by flipping columns of a Matrix | C # program to implement the above approach ; Function to find the maximum number of rows containing all equal elements ; Store each row of the matrix ; Traverse each row of the matrix ; Update frequency of current row ; Stores maximum count of rows containing all equal elements ; Traverse each row of the matrix ; Stores 1 's complement  of the current row ; Traverse current row of the given matrix ; Stores 1 s complement of mat [ i ] [ j ] ; Update cntMaxRows ; Driver code ; Stores count of rows ; Stores count of columns"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int maxEqrows ( List < List < int > > mat , int N , int M ) { Dictionary < List < int > , int > mp = new Dictionary < List < int > , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . ContainsKey ( mat [ i ] ) ) { mp [ mat [ i ] ] ++ ; } else { mp [ mat [ i ] ] = 1 ; } } int cntMaxRows = 0 ; for ( int i = 0 ; i < N ; i ++ ) { List < int > onesCompRow = new List < int > ( ) ; for ( int j = 0 ; j < M ; j ++ ) { onesCompRow . Add ( 0 ) ; } for ( int j = 0 ; j < M ; j ++ ) { onesCompRow [ j ] = ( mat [ i ] [ j ] ^ 1 ) ; } if ( ! mp . ContainsKey ( mat [ i ] ) ) { cntMaxRows = Math . Max ( cntMaxRows , mp [ onesCompRow ] + 1 ) ; } else if ( ! mp . ContainsKey ( onesCompRow ) ) { cntMaxRows = Math . Max ( cntMaxRows , mp [ mat [ i ] ] + 1 ) ; } else { cntMaxRows = Math . Max ( cntMaxRows , mp [ mat [ i ] ] + mp [ onesCompRow ] + 1 ) ; } } return cntMaxRows ; } static void Main ( ) { List < List < int > > mat = new List < List < int > > ( ) ; mat . Add ( new List < int > { 0 , 1 , 0 , 0 , 1 } ) ; mat . Add ( new List < int > { 1 , 1 , 0 , 1 , 1 } ) ; mat . Add ( new List < int > { 1 , 0 , 1 , 1 , 0 } ) ; int N = mat . Count ; int M = mat [ 0 ] . Count ; Console . WriteLine ( maxEqrows ( mat , N , M ) ) ; } }


"Maximize count of strings of length 3 that can be formed from N 1 s and M 0 s | C # program for the above approach ; Function that counts the number of Strings of length 3 that can be made with given m 0 s and n 1 s ; Print the count of Strings ; Driver Code ; Given count of 1 s and 0 s ; Function Call"
Solution in C#:

using System ; class GFG { static void number_of_Strings ( int N , int M ) { Console . Write ( Math . Min ( N , Math . Min ( M , ( N + M ) / 3 ) ) ) ; } public static void Main ( String [ ] args ) { int N = 4 , M = 19 ; number_of_Strings ( N , M ) ; } }


"Maximize count of strings of length 3 that can be formed from N 1 s and M 0 s | C # program for the above approach ; Function that counts the number of strings of length three that can be made with given m 0 s and n 1 s ; Iterate until N & M are positive ; Case 1 : ; Case 2 : ; Print the count of strings ; Driver Code ; Given count of 1 s and 0 s ; Function call"
Solution in C#:

using System ; class GFG { static void number_of_strings ( int N , int M ) { int ans = 0 ; while ( N > 0 && M > 0 ) { if ( N > M ) { if ( N >= 2 ) { N -= 2 ; -- M ; ++ ans ; } else { break ; } } else { if ( M >= 2 ) { M -= 2 ; -- N ; ++ ans ; } else { break ; } } } Console . WriteLine ( ans ) ; } public static void Main ( String [ ] args ) { int N = 4 , M = 19 ; number_of_strings ( N , M ) ; } }


"Maximize count of subsets having product of smallest element and size of the subset at least X | C # program to implement the above approach ; Function to return the maximum count of subsets possible which satisfy the above condition ; Sort the array in descending order ; Stores the count of subsets ; Stores the size of the current subset ; Check for the necessary conditions ; Driver Code"
Solution in C#:

using System ; using System . Linq ; class GFG { static int maxSubset ( int [ ] arr , int N , int X ) { Array . Sort ( arr ) ; Array . Reverse ( arr ) ; int counter = 0 ; int sz = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sz ++ ; if ( arr [ i ] * sz >= X ) { counter ++ ; sz = 0 ; } } return counter ; } public static void Main ( String [ ] args ) { int [ ] arr = { 7 , 11 , 2 , 9 , 5 } ; int N = arr . Length ; int X = 10 ; Console . Write ( maxSubset ( arr , N , X ) ) ; } }


"Maximize count of unique Squares that can be formed with N arbitrary points in coordinate plane | C # program for the above approach ; Function to find the maximum number of unique squares that can be formed from the given N points ; Stores the resultant count of squares formed ; Base Case ; Subtract the maximum possible grid size as sqrt ( N ) ; Find the total squares till now for the maximum grid ; A i * i grid contains ( i - 1 ) * ( i - 1 ) + ( i - 2 ) * ( i - 2 ) + ... + 1 * 1 squares ; When N >= len then more squares will be counted ; Return total count of squares ; Driver Code"
Solution in C#:

using System ; public class GFG { static int maximumUniqueSquares ( int N ) { int ans = 0 ; if ( N < 4 ) { return 0 ; } int len = ( int ) ( Math . Sqrt ( N ) ) ; N -= len * len ; for ( int i = 1 ; i < len ; i ++ ) { ans += i * i ; } if ( N >= len ) { N -= len ; for ( int i = 1 ; i < len ; i ++ ) { ans += i ; } } for ( int i = 1 ; i < N ; i ++ ) { ans += i ; } return ans ; } public static void Main ( string [ ] args ) { int N = 9 ; Console . WriteLine ( maximumUniqueSquares ( N ) ) ; } }


"Maximize difference between maximum and minimum array elements after K operations | C # program to implement the above approach ; Function to find the maximum difference between the maximum and minimum in the array after K operations ; Stores maximum difference between largest and smallest array element ; Sort the array in descending order ; Traverse the array arr [ ] ; Update maxDiff ; Driver code"
Solution in C#:

using System ; class GFG { static int maxDiffLargSmallOper ( int [ ] arr , int N , int K ) { int maxDiff = 0 ; Array . Sort ( arr ) ; Array . Reverse ( arr ) ; for ( int i = 0 ; i <= Math . Min ( K , N - 1 ) ; i ++ ) { maxDiff += arr [ i ] ; } return maxDiff ; } public static void Main ( ) { int [ ] arr = new int [ ] { 7 , 7 , 7 , 7 } ; int N = arr . Length ; int K = 1 ; Console . Write ( maxDiffLargSmallOper ( arr , N , K ) ) ; } }


"Maximize difference between odd and even indexed array elements by shift operations | C # program for the above approach ; Function to minimize array elements by shift operations ; For checking all the left shift operations ; Left shift ; Consider the minimum possible value ; Function to maximize array elements by shift operations ; For checking all the left shift operations ; Left shift ; Consider the maximum possible value ; Function to maximize the absolute difference between even and odd indexed array elements ; To calculate the difference of odd indexed elements and even indexed elements ; To calculate the difference between odd and even indexed array elements ; Print the maximum value ; Driver Code"
Solution in C#:

using System ; class GFG { static int minimize ( int n ) { int optEle = n ; string strEle = n . ToString ( ) ; for ( int idx = 0 ; idx < strEle . Length ; idx ++ ) { int temp = Int32 . Parse ( strEle . Substring ( idx ) + strEle . Substring ( 0 , idx ) ) ; optEle = Math . Min ( optEle , temp ) ; } return optEle ; } static int maximize ( int n ) { int optEle = n ; string strEle = n . ToString ( ) ; for ( int idx = 0 ; idx < strEle . Length ; idx ++ ) { int temp = Int32 . Parse ( strEle . Substring ( idx ) + strEle . Substring ( 0 , idx ) ) ; optEle = Math . Max ( optEle , temp ) ; } return optEle ; } static void minimumDifference ( int [ ] arr ) { int caseOne = 0 ; int minVal = 0 ; int maxVal = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( i % 2 == 0 ) minVal += minimize ( arr [ i ] ) ; else maxVal += maximize ( arr [ i ] ) ; } caseOne = Math . Abs ( maxVal - minVal ) ; int caseTwo = 0 ; minVal = 0 ; maxVal = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( i % 2 == 0 ) maxVal += maximize ( arr [ i ] ) ; else minVal += minimize ( arr [ i ] ) ; caseTwo = Math . Abs ( maxVal - minVal ) ; } Console . WriteLine ( Math . Max ( caseOne , caseTwo ) ) ; } public static void Main ( ) { int [ ] arr = { 332 , 421 , 215 , 584 , 232 } ; minimumDifference ( arr ) ; } }


"Maximize difference between odd and even indexed array elements by swapping unequal adjacent bits in their binary representations | C # program for the above approach ; Function to count total number of bits present in a number ; Function to count total set bits in a number ; Stores the count of set bits ; Right shift by 1 ; Return the final count ; Function to find maximum number by shifting two unequal bits ; Count set bits in number n ; Iterate the string bits ; Function to find minimum number by shifting two unequal bits ; Iterate the set bit ; Function to find the maximum difference ; Stores the maximum difference ; Stores the sum of elements placed at odd positions ; Stores the sum of elements placed at even positions ; Traverse the array ; Update CaseOne ; Stores the maximum difference ; Assign value O ; Traverse the array ; Update caseTwo ; Return maximum of caseOne and CaseTwo ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static int countBit ( int n ) { return ( int ) ( ( Math . Log ( n ) / Math . Log ( 2 ) ) + 1 ) ; } static int countSetBit ( int n ) { int ans = 0 ; while ( n > 0 ) { ans += ( n & 1 ) ; n >>= 1 ; } return ans ; } static int maximize ( int n ) { int bits = countBit ( n ) ; int setBits = countSetBit ( n ) ; int ans = 0 ; for ( int i = 0 ; i < bits ; i ++ ) { if ( i < setBits ) ans |= 1 ; if ( i != setBits - 1 ) ans <<= 1 ; } return ans ; } static int minimize ( int n ) { int setBits = countSetBit ( n ) ; int ans = 0 ; for ( int i = 0 ; i < setBits ; i ++ ) { ans |= 1 ; if ( i != setBits - 1 ) ans <<= 1 ; } return ans ; } static int maxDiff ( int [ ] arr ) { int caseOne = 0 ; int SumOfOdd = 0 ; int SumOfeven = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( ( i % 2 ) != 0 ) SumOfOdd += minimize ( arr [ i ] ) ; else SumOfeven += maximize ( arr [ i ] ) ; } caseOne = Math . Abs ( SumOfOdd - SumOfeven ) ; int caseTwo = 0 ; SumOfOdd = 0 ; SumOfeven = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( ( i % 2 ) != 0 ) SumOfOdd += maximize ( arr [ i ] ) ; else SumOfeven += minimize ( arr [ i ] ) ; } caseTwo = Math . Abs ( SumOfOdd - SumOfeven ) ; return Math . Max ( caseOne , caseTwo ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 54 , 32 , 11 , 23 } ; Console . Write ( maxDiff ( arr ) ) ; } }


"Maximize difference between odd and even | C # program for the above approach ; Function to find maximum and minimum value of a number that can be obtained by rotating bits ; Stores the value of N ; Stores the maximum value ; Stores the minimum value ; If temp is odd ; Update the maximum and the minimum value ; If flag is 1 , then return the maximum value ; Otherwise , return the maximum value ; Function to find the maximum difference between the sum of odd and even - indexed array elements possible by rotating bits ; Stores the maximum difference ; Stores the sum of elements present at odd indices ; Stores the sum of elements present at even indices ; Traverse the given array ; If the index is even ; Update the caseOne ; Stores the maximum difference ; Stores the sum of elements placed at odd positions ; Stores the sum of elements placed at even positions ; Traverse the array ; If the index is even ; Update the caseTwo ; Return the maximum of caseOne and caseTwo ; Driver Code"
Solution in C#:

using System ; public class GFG { static int Rotate ( int n , int f ) { int temp = n ; int maxi = n ; int mini = n ; for ( int idx = 0 ; idx < 7 ; idx ++ ) { if ( temp % 2 == 1 ) { temp >>= 1 ; temp += ( int ) Math . Pow ( 2 , 7 ) ; } else temp >>= 1 ; mini = Math . Min ( mini , temp ) ; maxi = Math . Max ( maxi , temp ) ; } if ( f == 1 ) return ( maxi ) ; else return ( mini ) ; } static int calcMinDiff ( int [ ] arr , int n ) { int caseOne = 0 ; int sumOfodd = 0 ; int sumOfeven = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) sumOfodd += Rotate ( arr [ i ] , 0 ) ; else sumOfeven += Rotate ( arr [ i ] , 1 ) ; } caseOne = Math . Abs ( sumOfodd - sumOfeven ) ; int caseTwo = 0 ; sumOfodd = 0 ; sumOfeven = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) sumOfodd += Rotate ( arr [ i ] , 1 ) ; else sumOfeven += Rotate ( arr [ i ] , 0 ) ; } caseTwo = Math . Abs ( sumOfodd - sumOfeven ) ; return Math . Max ( caseOne , caseTwo ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 123 , 86 , 234 , 189 } ; int n = arr . Length ; Console . WriteLine ( ( calcMinDiff ( arr , n ) ) ) ; } }


"Maximize difference between sum of even and odd | C # program to implement the above approach ; Function to find the maximum possible difference between sum of even and odd indices ; Convert [ ] arr into 1 - based indexing ; Reverse the array ; Convert [ ] arr into 1 based index ; Reverse the array ; Stores maximum difference between sum of even and odd indexed elements ; Traverse the array ; If arr [ i ] is local maxima ; Update maxDiff ; If arr [ i ] is local minima ; Update maxDiff ; Driver Code ; Size of array ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { static void maxPossibleDiff ( List < int > arr , int N ) { arr . Add ( - 1 ) ; arr . Reverse ( ) ; arr . Add ( - 1 ) ; arr . Reverse ( ) ; int maxDiff = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] && arr [ i ] > arr [ i + 1 ] ) { maxDiff += arr [ i ] ; } if ( arr [ i ] < arr [ i - 1 ] && arr [ i ] < arr [ i + 1 ] ) { maxDiff -= arr [ i ] ; } } Console . Write ( maxDiff ) ; } public static void Main ( String [ ] args ) { int [ ] array = { 3 , 2 , 1 , 4 , 5 , 2 , 1 , 7 , 8 , 9 } ; List < int > v = new List < int > ( ) ; foreach ( int i in array ) { v . Add ( i ) ; } int N = v . Count ; maxPossibleDiff ( v , N ) ; } }


"Maximize difference between the Sum of the two halves of the Array after removal of N elements | C # program to implement the above approach ; Function to print the maximum difference possible between the two halves of the array ; Stores n maximum values from the start ; Insert first n elements ; Update sum of largest n elements from left ; For the remaining elements ; Obtain minimum value in the set ; Insert only if it is greater than minimum value ; Update sum from left ; Remove the minimum ; Insert the current element ; Clear the set ; Store n minimum elements from the end ; Insert the last n elements ; Update sum of smallest n elements from right ; For the remaining elements ; Obtain the minimum ; Insert only if it is smaller than maximum value ; Update sum from right ; Remove the minimum ; Insert the new element ; Compare the difference and store the maximum ; Return the maximum possible difference ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static long FindMaxDif ( List < long > a , int m ) { int n = m / 3 ; long [ ] l = new long [ m + 5 ] ; long [ ] r = new long [ m + 5 ] ; List < long > s = new List < long > ( ) ; for ( int i = 1 ; i <= m ; i ++ ) { if ( i <= n ) { l [ i ] = a [ i - 1 ] + l [ i - 1 ] ; s . Add ( a [ i - 1 ] ) ; } else { l [ i ] = l [ i - 1 ] ; s . Sort ( ) ; long d = s [ 0 ] ; if ( a [ i - 1 ] > d ) { l [ i ] -= d ; l [ i ] += a [ i - 1 ] ; s . Remove ( d ) ; s . Add ( a [ i - 1 ] ) ; } } } s . Clear ( ) ; for ( int i = m ; i >= 1 ; i -- ) { if ( i >= m - n + 1 ) { r [ i ] = a [ i - 1 ] + r [ i + 1 ] ; s . Add ( a [ i - 1 ] ) ; } else { r [ i ] = r [ i + 1 ] ; s . Sort ( ) ; long d = s [ s . Count - 1 ] ; if ( a [ i - 1 ] < d ) { r [ i ] -= d ; r [ i ] += a [ i - 1 ] ; s . Remove ( d ) ; s . Add ( a [ i - 1 ] ) ; } } } long ans = ( long ) ( - 9e18 ) ; for ( int i = n ; i <= m - n ; i ++ ) { ans = Math . Max ( ans , l [ i ] - r [ i + 1 ] ) ; } return ans ; } static void Main ( ) { List < long > vtr = new List < long > ( new long [ ] { 3 , 1 , 4 , 1 , 5 , 9 } ) ; int n = vtr . Count ; Console . Write ( FindMaxDif ( vtr , n ) ) ; } }


"Maximize difference between the sum of absolute differences of each element with the remaining array | C # Program to implement the above approach ; Function to maximize difference of the sum of absolute difference of an element with the rest of the elements in the array ; Sort the array in ascending order ; Stores prefix sum at any instant ; Store the total array sum ; Initialize minimum and maximum absolute difference ; Traverse the array to find the total array sum ; Traverse the array arr [ ] ; Store the number of elements to its left ; Store the number of elements to its right ; Update the sum of elements on its left ; Store the absolute difference sum ; Update the Minimum ; Update the Maximum ; Update sum of elements on its left ; Print the result ; Driver Code"
Solution in C#:

using System ; class GFG { static void findMaxDifference ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int Leftsum = 0 ; int Totalsum = 0 ; int Minn = Int32 . MaxValue , Maxx = Int32 . MinValue ; for ( int i = 0 ; i < n ; i ++ ) Totalsum += arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) { int leftNumbers = i ; int rightNumbers = n - i - 1 ; Totalsum = Totalsum - arr [ i ] ; int sum = ( leftNumbers * arr [ i ] ) - Leftsum + Totalsum - ( rightNumbers * arr [ i ] ) ; Minn = Math . Min ( Minn , sum ) ; Maxx = Math . Max ( Maxx , sum ) ; Leftsum += arr [ i ] ; } Console . WriteLine ( Maxx - Minn ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 4 , 7 } ; int N = arr . Length ; findMaxDifference ( arr , N ) ; } }


"Maximize distance between any two consecutive 1 ' s ▁ after ▁ flipping ▁ M ▁ 0' s | C # program to Maximize distance between any two consecutive 1 ' s ▁ after ▁ flipping ▁ M ▁ 0' s ; Function to return the count ; Flipping zeros at distance " d " ; Function to implement binary search ; Check for valid distance i . e mid ; Driver code"
Solution in C#:

using System ; class GFG { static bool check ( int [ ] arr , int n , int m , int d ) { int i = 0 ; while ( i < n && m > 0 ) { m -- ; i += d ; } return m == 0 ? true : false ; } static int maximumDistance ( int [ ] arr , int n , int m ) { int low = 1 , high = n - 1 ; int ans = 0 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; bool flag = check ( arr , n , m , mid ) ; if ( flag ) { ans = mid ; low = mid + 1 ; } else { high = mid - 1 ; } } return ans ; } public static void Main ( String [ ] args ) { int n = 5 , m = 3 ; int [ ] arr = new int [ n ] ; Console . Write ( maximumDistance ( arr , n , m ) ) ; } }


"Maximize distinct elements by incrementing / decrementing an element or keeping it same | C # program to maximize the count of distinct element ; Function that maximize the count of distinct element ; Sort thr array ; Keeping track of previous change ; Decrement is possible ; Remain as it is ; Increment is possible ; Driver Code"
Solution in C#:

using System ; class GFG { static int max_dist_ele ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int ans = 0 ; int prev = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prev < ( arr [ i ] - 1 ) ) { ans ++ ; prev = arr [ i ] - 1 ; } else if ( prev < ( arr [ i ] ) ) { ans ++ ; prev = arr [ i ] ; } else if ( prev < ( arr [ i ] + 1 ) ) { ans ++ ; prev = arr [ i ] + 1 ; } } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 1 , 8 , 8 , 8 , 9 , 9 } ; int n = arr . Length ; Console . WriteLine ( max_dist_ele ( arr , n ) ) ; } }


"Maximize element at index K in an array with at most sum M and difference between adjacent elements at most 1 | C # program for the above approach ; Function to calculate the maximum possible value at index K ; Stores the sum of elements in the left and right of index K ; Stores the maximum possible value at index K ; Print the answer ; Driver Code ; Given N , K & M"
Solution in C#:

using System ; class GFG { static void maxValueAtIndexK ( int N , int K , int M ) { int S1 = 0 , S2 = 0 ; S1 = K * ( K + 1 ) / 2 ; S2 = ( N - K - 1 ) * ( N - K ) / 2 ; int X = ( M + S1 + S2 ) / N ; Console . WriteLine ( X ) ; } static public void Main ( ) { int N = 3 , K = 1 , M = 7 ; maxValueAtIndexK ( N , K , M ) ; } }


"Maximize every array element by repeatedly adding all valid i + a [ i ] th array element | C # program for the above approach ; Function to maximize value at every array index by performing given operations ; Traverse the array in reverse ; If the current index is a valid index ; Print the array ; Driver Code ; Given array ; Size of the array"
Solution in C#:

using System ; class GFG { static void maxSum ( int [ ] arr , int N ) { for ( int i = N - 1 ; i >= 0 ; i -- ) { int t = i ; if ( t + arr [ i ] < N ) { arr [ i ] += arr [ t + arr [ i ] ] ; } } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } static public void Main ( ) { int [ ] arr = { 1 , 2 , 7 , 1 , 8 } ; int N = arr . Length ; maxSum ( arr , N ) ; } }


"Maximize first array element by performing given operations at most K times | C # program for the above approach ; Function to maximize the first array element ; Traverse the array ; Initialize cur_val to a [ i ] ; If all operations are not over yet ; If current value is greater than zero ; Incrementing first element of array by 1 ; Decrementing current value of array by 1 ; Decrementing number of operations by i ; If current value is zero , then break ; Print first array element ; Driver code ; Given array ; Size of the array ; Given K ; Prints the maximum possible value of the first array element"
Solution in C#:

using System ; class GFG { static void getMax ( int [ ] arr , int N , int K ) { for ( int i = 1 ; i < N ; i ++ ) { int cur_val = arr [ i ] ; while ( K >= i ) { if ( cur_val > 0 ) { arr [ 0 ] = arr [ 0 ] + 1 ; cur_val = cur_val - 1 ; K = K - i ; } else break ; } } Console . Write ( arr [ 0 ] ) ; } static void Main ( ) { int [ ] arr = { 1 , 0 , 3 , 2 } ; int N = arr . Length ; int K = 5 ; getMax ( arr , N , K ) ; } }


"Maximize frequency of an element by at most one increment or decrement of all array elements | C # program to implement the above approach ; Function to maximize the frequency of an array element by incrementing or decrementing array elements at most once ; Stores the largest array element ; Stores the smallest array element ; freq [ i ] : Stores frequency of ( i + Min ) in the array ; Traverse the array ; Update frequency of arr [ i ] ; Stores maximum frequency of an array element by incrementing or decrementing array elements ; Iterate all the numbers over the range [ Min , Max ] ; Stores sum of three consecutive numbers ; Update maxSum ; Print maxSum ; Driver Code ; Function call"
Solution in C#:

using System ; class GFG { static void max_freq ( int [ ] arr , int N ) { Array . Sort ( arr ) ; int Max = arr [ N - 1 ] ; int Min = arr [ 0 ] ; int [ ] freq = new int [ Max - Min + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { freq [ arr [ i ] - Min ] ++ ; } int maxSum = 0 ; for ( int i = 0 ; i < ( Max - Min - 1 ) ; i ++ ) { int val = freq [ i ] + freq [ i + 1 ] + freq [ i + 2 ] ; maxSum = Math . Max ( maxSum , val ) ; } Console . WriteLine ( maxSum ) ; } public static void Main ( ) { int [ ] arr = { 3 , 1 , 4 , 1 , 5 , 9 , 2 } ; int N = arr . Length ; max_freq ( arr , N ) ; } }


"Maximize frequency sum of K chosen characters from given string | C # program for the above approach ; Function to find the maximum sum of frequencies of the exactly K chosen characters from the string S ; Stores the resultant maximum sum ; Stores the frequency of array elements ; Find the frequency of character ; Sort the frequency array in the descending order ; Iterate to choose K elements greedily ; If the freq [ i ] cards are chosen ; K cards have been picked ; Return the resultant sum ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int maximumSum ( string S , int N , int K ) { int sum = 0 ; int [ ] freq = new int [ 256 ] ; Array . Clear ( freq , 0 , 256 ) ; for ( int i = 0 ; i < N ; i ++ ) { freq [ ( int ) S [ i ] ] ++ ; } Array . Sort ( freq ) ; Array . Reverse ( freq ) ; for ( int i = 0 ; i < 256 ; i ++ ) { if ( K > freq [ i ] ) { sum += freq [ i ] * freq [ i ] ; K -= freq [ i ] ; } else { sum += freq [ i ] * K ; break ; } } return sum ; } public static void Main ( ) { string S = " GEEKSFORGEEKS " ; int K = 10 ; int N = S . Length ; Console . Write ( maximumSum ( S , N , K ) ) ; } }


"Maximize given integer by swapping pairs of unequal bits | C # implementation of the above approach ; Function to return the maximum possible value that can be obtained from the given integer by performing given operations ; Convert to equivalent binary representation ; Stores binary representation of the maximized value ; Store the count of 0 ' s ▁ and ▁ 1' s ; Stores the total Number of Bits ; If current bit is set ; Increment Count1 ; Increment Count0 ; Shift all set bits to left to maximize the value ; Shift all unset bits to right to maximize the value ; Return the maximized value ; Driver Code ; Given "Input ; Function call to find the Maximum Possible Number"
Solution in C#:

using System ; public class GFG { public static int findMaxNum ( int num ) { string binaryNumber = Convert . ToString ( num , 2 ) ; string maxBinaryNumber = " " ; int count0 = 0 , count1 = 0 ; int N = binaryNumber . Length ; for ( int i = 0 ; i < N ; i ++ ) { if ( binaryNumber [ i ] == '1' ) { count1 ++ ; } else { count0 ++ ; } } for ( int i = 0 ; i < count1 ; i ++ ) { maxBinaryNumber += '1' ; } for ( int i = 0 ; i < count0 ; i ++ ) { maxBinaryNumber += '0' ; } return Convert . ToInt32 ( maxBinaryNumber , 2 ) ; } static public void Main ( ) { int N = 11 ; Console . WriteLine ( findMaxNum ( N ) ) ; } }


"Maximize jobs that can be completed under given constraint | C # Program to implement the above approach ; Function to find maxiumum number of jobs ; Min Heap ; Sort ranges by start day ; Stores the minimum and maximum day in the ranges ; Iterating from min_day to max_day ; Insert the end day of the jobs which can be completed on i - th day in a priority queue ; Pop all jobs whose end day is less than current day ; If queue is empty , no job can be completed on the i - th day ; Increment the count of jobs completed ; Pop the job with least end day ; Return the jobs on the last day ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int find_maximum_jobs ( int N , List < Tuple < int , int > > ranges ) { List < int > queue = new List < int > ( ) ; ranges . Sort ( ) ; int min_day = ranges [ 0 ] . Item1 ; int max_day = 0 ; for ( int i = 0 ; i < N ; i ++ ) max_day = Math . Max ( max_day , ranges [ i ] . Item2 ) ; int index = 0 , count_jobs = 0 ; for ( int i = min_day ; i <= max_day ; i ++ ) { while ( index < ranges . Count && ranges [ index ] . Item1 <= i ) { queue . Add ( ranges [ index ] . Item2 ) ; index ++ ; } queue . Sort ( ) ; while ( queue . Count > 0 && queue [ 0 ] < i ) queue . RemoveAt ( 0 ) ; if ( queue . Count == 0 ) continue ; count_jobs ++ ; queue . RemoveAt ( 0 ) ; } return count_jobs ; } static void Main ( ) { int N = 5 ; List < Tuple < int , int > > ranges = new List < Tuple < int , int > > ( ) ; ranges . Add ( new Tuple < int , int > ( 1 , 5 ) ) ; ranges . Add ( new Tuple < int , int > ( 1 , 5 ) ) ; ranges . Add ( new Tuple < int , int > ( 1 , 5 ) ) ; ranges . Add ( new Tuple < int , int > ( 2 , 3 ) ) ; ranges . Add ( new Tuple < int , int > ( 2 , 3 ) ) ; Console . Write ( find_maximum_jobs ( N , ranges ) ) ; } }


"Maximize length of Non | C # program to implement the above approach ; Function to find the maximum length non decreasing subarray by reversing at most one subarray ; dp [ i , j ] be the longest subsequence of a [ 0. . . i ] with first j parts ; Maximum length sub - sequence of ( 0. . ) ; Maximum length sub - sequence of ( 0. . 1. . ) ; Maximum length sub - sequence of ( 0. . 1. .0 . . ) ; Maximum length sub - sequence of ( 0. . 1. .0 . .1 . . ) ; Find the max length subsequence ; Print the answer ; Driver code"
Solution in C#:


"Maximize length of Subarray of 1 's after removal of a pair of consecutive Array elements | C # program to find the maximum count of 1 s ; If arr [ i - 2 ] = = 1 then we increment the count of occurences of 1 's ; Else we initialise the count with 0 ; If arr [ i + 2 ] = = 1 then we increment the count of occurences of 1 's ; Else we initialise the count with 0 ; We get the maximum count by skipping the current and the next element . ; Driver code"
Solution in C#:

using System ; class GFG { static void maxLengthOf1s ( int [ ] arr , int n ) { int [ ] prefix = new int [ n ] ; for ( int i = 2 ; i < n ; i ++ ) { if ( arr [ i - 2 ] == 1 ) prefix [ i ] = prefix [ i - 1 ] + 1 ; else prefix [ i ] = 0 ; } int [ ] suffix = new int [ n ] ; for ( int i = n - 3 ; i >= 0 ; i -- ) { if ( arr [ i + 2 ] == 1 ) suffix [ i ] = suffix [ i + 1 ] + 1 ; else suffix [ i ] = 0 ; } int ans = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { ans = Math . Max ( ans , prefix [ i + 1 ] + suffix [ i ] ) ; } Console . WriteLine ( ans ) ; } static void Main ( ) { int n = 6 ; int [ ] arr = { 1 , 1 , 1 , 0 , 1 , 1 } ; maxLengthOf1s ( arr , n ) ; } }


"Maximize length of Subarray of 1 's after removal of a pair of consecutive Array elements | C # program to implement the above approach ; Function to find the maximum subarray length of ones ; Stores the length , starting index and ending index of the subarrays ; S : starting index of the sub - array ; Traverse only continuous 1 s ; Calculate length of the sub - array ; v [ i , 0 ] : Length of subarray v [ i , 1 ] : Starting Index of subarray v [ i , 2 ] : Ending Index of subarray ; If no such sub - array exists ; Traversing through the subarrays ; Update maximum length ; v [ i + 1 , 1 ] - v [ i , 2 ] - 1 : Count of zeros between the two sub - arrays ; Update length of both subarrays to the maximum ; Update length of both subarrays - 1 to the maximum ; Check if the last subarray has the maximum length ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int maxLen ( int [ ] A , int N ) { List < List < int > > v = new List < List < int > > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] == 1 ) { int s = i , len ; while ( i < N && A [ i ] == 1 ) { i ++ ; } len = i - s ; List < int > l = new List < int > { len , s , i - 1 } ; v . Add ( l ) ; } } if ( v . Count == 0 ) { return - 1 ; } int ans = 0 ; for ( int i = 0 ; i < v . Count - 1 ; i ++ ) { ans = Math . Max ( ans , v [ i ] [ 0 ] ) ; if ( v [ i + 1 ] [ 1 ] - v [ i ] [ 2 ] - 1 == 2 ) { ans = Math . Max ( ans , v [ i ] [ 0 ] + v [ i + 1 ] [ 0 ] ) ; } if ( v [ i + 1 ] [ 1 ] - v [ i ] [ 2 ] - 1 == 1 ) { ans = Math . Max ( ans , v [ i ] [ 0 ] + v [ i + 1 ] [ 0 ] - 1 ) ; } } ans = Math . Max ( v [ v . Count - 1 ] [ 0 ] , ans ) ; return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 0 , 1 , 0 , 0 , 1 } ; int N = arr . Length ; Console . WriteLine ( maxLen ( arr , N ) ) ; } }


"Maximize length of longest non | C # program to implement the above approach ; Function to find the length of the longest non - decreasing array that can be generated ; Stores the length of the longest non - decreasing array that can be generated from the array ; Stores index of start pointer ; Stores index of end pointer ; Stores previously inserted element into the new array ; Traverse the array ; If A [ start ] is less than or equal to A [ end ] ; If no element inserted into the newly generated array ; Update prev ; Update res ; Update start ; If A [ start ] is greater than or equal to prev ; Update res ; Update prev ; Update start ; If A [ end ] is greater than or equal to prev ; Update res ; Update prev ; Update end ; If A [ end ] is greater than A [ start ] ; If no element inserted into the newly generated array ; Update prev ; Update res ; Update end ; If A [ end ] is greater than or equal to prev ; Update res ; Update prev ; Update end ; If A [ start ] is greater than or equal to prev ; Update res ; Update prev ; Update start ; Driver Code ; Function call"
Solution in C#:

using System ; class GFG { static int findLongestNonDecreasing ( int [ ] A , int N ) { int res = 0 ; int start = 0 ; int end = N - 1 ; int prev = - 1 ; while ( start <= end ) { if ( A [ start ] <= A [ end ] ) { if ( prev == - 1 ) { prev = A [ start ] ; res ++ ; start ++ ; } else { if ( A [ start ] >= prev ) { res ++ ; prev = A [ start ] ; start ++ ; } else if ( A [ end ] >= prev ) { res ++ ; prev = A [ end ] ; end -- ; } else { break ; } } } else { if ( prev == - 1 ) { prev = A [ end ] ; res ++ ; end -- ; } else { if ( A [ end ] >= prev ) { res ++ ; prev = A [ end ] ; end -- ; } else if ( A [ start ] >= prev ) { res ++ ; prev = A [ start ] ; start ++ ; } else { break ; } } } } return res ; } public static void Main ( ) { int [ ] A = { 1 , 1 , 3 , 5 , 4 , 3 , 6 , 2 , 1 } ; int N = A . Length ; Console . Write ( findLongestNonDecreasing ( A , N ) ) ; } }


"Maximize length of longest subarray consisting of same elements by at most K decrements | C # program to implement the above approach ; Function to construct Segment Tree to return the minimum element in a range ; If leaf nodes of the tree are found ; Update the value in segment tree from given array ; Divide left and right subtree ; Stores smallest element in subarray { arr [ start ] , arr [ mid ] } ; Stores smallest element in subarray { arr [ mid + 1 ] , arr [ end ] } ; Stores smallest element in subarray { arr [ start ] , arr [ end ] } ; Function to find the smallest element present in a subarray ; If elements of the subarray are not in the range [ l , r ] ; If all the elements of the subarray are in the range [ l , r ] ; Divide tree into left and right subtree ; Stores smallest element in left subtree ; Stores smallest element in right subtree ; Function that find length of longest subarray with all equal elements in atmost K decrements ; Stores length of longest subarray with all equal elements in atmost K decrements . ; Store the prefix sum array ; Calculate the prefix sum array ; Build the segment tree for range min query ; Traverse the array ; Stores start index of the subarray ; Stores end index of the subarray ; Stores end index of the longest subarray ; Performing the binary search to find the endpoint for the selected range ; Find the mid for binary search ; Find the smallest element in range [ i , mid ] using Segment Tree ; Stores total sum of subarray after K decrements ; Stores sum of elements of subarray before K decrements ; If subarray found with all equal elements ; Update start ; Update max_index ; If false , it means that the selected range is invalid ; Update end ; Store the length of longest subarray ; Return result ; Driver Code"
Solution in C#:

using System ; class GFG { static int build ( int [ ] tree , int [ ] A , int start , int end , int node ) { if ( start == end ) { tree [ node ] = A [ start ] ; return tree [ node ] ; } int mid = ( start + end ) / 2 ; int X = build ( tree , A , start , mid , 2 * node + 1 ) ; int Y = build ( tree , A , mid + 1 , end , 2 * node + 2 ) ; return ( tree [ node ] = Math . Min ( X , Y ) ) ; } static int query ( int [ ] tree , int start , int end , int l , int r , int node ) { if ( start > r end < l ) return Int32 . MaxValue ; if ( start >= l && end <= r ) return tree [ node ] ; int mid = ( start + end ) / 2 ; int X = query ( tree , start , mid , l , r , 2 * node + 1 ) ; int Y = query ( tree , mid + 1 , end , l , r , 2 * node + 2 ) ; return Math . Min ( X , Y ) ; } static int longestSubArray ( int [ ] A , int N , int K ) { int res = 1 ; int [ ] preSum = new int [ N + 1 ] ; preSum [ 0 ] = A [ 0 ] ; for ( int i = 0 ; i < N ; i ++ ) preSum [ i + 1 ] = preSum [ i ] + A [ i ] ; int [ ] tree = new int [ 4 * N + 5 ] ; build ( tree , A , 0 , N - 1 , 0 ) ; for ( int i = 0 ; i < N ; i ++ ) { int start = i ; int end = N - 1 ; int mid ; int max_index = i ; while ( start <= end ) { mid = ( start + end ) / 2 ; int min_element = query ( tree , 0 , N - 1 , i , mid , 0 ) ; int expected_sum = ( mid - i + 1 ) * min_element ; int actual_sum = preSum [ mid + 1 ] - preSum [ i ] ; if ( actual_sum - expected_sum <= K ) { start = mid + 1 ; max_index = Math . Max ( max_index , mid ) ; } else { end = mid - 1 ; } } res = Math . Max ( res , max_index - i + 1 ) ; } return res ; } static void Main ( ) { int [ ] arr = { 1 , 7 , 3 , 4 , 5 , 6 } ; int k = 6 ; int n = 6 ; Console . WriteLine ( longestSubArray ( arr , n , k ) ) ; } }


"Maximize length of subarray having equal elements by adding at most K | C # program for the above approach ; Function to find the maximum number of indices having equal elements after adding at most k numbers ; Sort the array in ascending order ; Make prefix sum array ; Initialize variables ; Update mid ; Check if any subarray can be obtained of length mid having equal elements ; Decrease max to mid ; Function to check if a subarray of length len consisting of equal elements can be obtained or not ; Sliding window ; Last element of the sliding window will be having the max size in the current window ; The current number of element in all indices of the current sliding window ; If the current number of the window , added to k exceeds totalNumbers ; Driver Code ; Function call"
Solution in C#:

using System ; class GFG { public static int maxEqualIdx ( int [ ] arr , int k ) { Array . Sort ( arr ) ; int [ ] prefixSum = new int [ arr . Length + 1 ] ; prefixSum [ 1 ] = arr [ 0 ] ; for ( int i = 1 ; i < prefixSum . Length - 1 ; ++ i ) { prefixSum [ i + 1 ] = prefixSum [ i ] + arr [ i ] ; } int max = arr . Length ; int min = 1 ; int ans = 1 ; while ( min <= max ) { int mid = ( max + min ) / 2 ; if ( check ( prefixSum , mid , k , arr ) ) { ans = mid ; min = mid + 1 ; } else { max = mid - 1 ; } } return ans ; } public static bool check ( int [ ] pSum , int len , int k , int [ ] a ) { int i = 0 ; int j = len ; while ( j <= a . Length ) { int maxSize = a [ j - 1 ] ; int totalNumbers = maxSize * len ; int currNumbers = pSum [ j ] - pSum [ i ] ; if ( currNumbers + k >= totalNumbers ) { return true ; } else { i ++ ; j ++ ; } } return false ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 1 } ; int k = 7 ; Console . WriteLine ( maxEqualIdx ( arr , k ) ) ; } }


"Maximize length of subarray of equal elements by performing at most K increment operations | C # Program for the above approach ; Function to find the maximum length of subarray of equal elements after performing at most K increments ; Length of array ; Stores the size of required subarray ; Starting point of a window ; Stores the sum of window ; Iterate over array ; Current element ; Remove index of minimum elements from deque which are less than the current element ; Insert current index in deque ; Update current window sum ; Calculate required operation to make current window elements equal ; If cost is less than k ; Shift window start pointer towards right and update current window sum ; Return answer ; Driver Code ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int maxSubarray ( int [ ] a , int k ) { int n = a . Length ; int answer = 0 ; int start = 0 ; long s = 0 ; Queue < int > dq = new Queue < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int x = a [ i ] ; while ( dq . Count != 0 && a [ dq . Peek ( ) ] <= x ) dq . Dequeue ( ) ; dq . Enqueue ( i ) ; s += x ; long cost = ( long ) a [ dq . Peek ( ) ] * ( answer + 1 ) - s ; if ( cost <= ( long ) k ) answer ++ ; else { if ( dq . Peek ( ) == start ) dq . Dequeue ( ) ; s -= a [ start ++ ] ; } } return answer ; } public static void Main ( String [ ] args ) { int [ ] a = { 2 , 2 , 4 } ; int k = 10 ; Console . WriteLine ( maxSubarray ( a , k ) ) ; } }


"Maximize length of subsequence consisting of single distinct character possible by K increments in a string | C # program for the above approach ; Function to find the maximum length of a subsequence of same characters after at most K increment operations ; Store the size of s ; Sort the given string sort ( S . begin ( ) , S . end ( ) ) ; convert input string to char array ; sort tempArray ; Stores the maximum length and the sum of the sliding window ; Traverse the string S ; Add the current character to the window ; Decrease the window size ; Update the value of sum ; Increment the value of start ; Update the maximum window size ; Print the resultant maximum length of the subsequence ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void maxSubsequenceLen ( string s , int K ) { int N = s . Length ; int start = 0 , end = 0 ; char [ ] S = s . ToCharArray ( ) ; Array . Sort ( S ) ; int ans = Int32 . MinValue , sum = 0 ; for ( end = 0 ; end < N ; end ++ ) { sum = sum + ( S [ end ] - ' a ' ) ; while ( sum + K < ( S [ end ] - ' a ' ) * ( end - start + 1 ) ) { sum = sum - ( S [ start ] - ' a ' ) ; start ++ ; } ans = Math . Max ( ans , end - start + 1 ) ; } Console . WriteLine ( ans ) ; } public static void Main ( ) { string S = " acscbcca " ; int K = 1 ; maxSubsequenceLen ( S , K ) ; } }


"Maximize length of the String by concatenating characters from an Array of Strings | C # program to implement the above approach ; Function to check if all the string characters are unique ; Check for repetition in characters ; Function to generate all possible strings from the given array ; Base case ; Consider every string as a starting substring and store the generated string ; Add current string to result of other strings and check if characters are unique or not ; Function to find the maximum possible length of a string ; Return max length possible ; Return the answer ; Driver Code"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; using System . Text ; class GFG { static bool check ( string s ) { HashSet < char > a = new HashSet < char > ( ) ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( a . Contains ( s [ i ] ) ) { return false ; } a . Add ( s [ i ] ) ; } return true ; } static ArrayList helper ( ArrayList arr , int ind ) { if ( ind == arr . Count ) return new ArrayList ( ) { " " } ; ArrayList tmp = helper ( arr , ind + 1 ) ; ArrayList ret = new ArrayList ( tmp ) ; for ( int i = 0 ; i < tmp . Count ; i ++ ) { string test = ( string ) tmp [ i ] + ( string ) arr [ ind ] ; if ( check ( test ) ) ret . Add ( test ) ; } return ret ; } static int maxLength ( ArrayList arr ) { ArrayList tmp = helper ( arr , 0 ) ; int len = 0 ; for ( int i = 0 ; i < tmp . Count ; i ++ ) { len = len > ( ( string ) tmp [ i ] ) . Length ? len : ( ( string ) tmp [ i ] ) . Length ; } return len ; } public static void Main ( string [ ] args ) { ArrayList s = new ArrayList ( ) ; s . Add ( " abcdefgh " ) ; Console . Write ( maxLength ( s ) ) ; } }


"Maximize matrix sum by flipping the sign of any adjacent pairs | C # program for the above approach ; Function to find the maximum sum of matrix element after flipping the signs of adjacent matrix elements ; Initialize row and column ; Stores the sum of absolute matrix element ; Find the minimum absolute value in the matrix ; Store count of negatives ; Find the smallest absolute value in the matrix ; Increment the count of negative numbers ; Find the absolute sum ; If the count of negatives is even then print the sum ; Subtract minimum absolute element ; Driver Code"
Solution in C#:

using System ; public class GFG { static int maxSum ( int [ , ] matrix ) { int r = matrix . GetLength ( 0 ) ; int c = matrix . GetLength ( 1 ) ; int sum = 0 ; int mini = int . MaxValue ; int count = 0 ; for ( int i = 0 ; i < r ; i ++ ) { for ( int j = 0 ; j < c ; j ++ ) { int k = matrix [ i , j ] ; mini = Math . Min ( mini , Math . Abs ( k ) ) ; if ( k < 0 ) count ++ ; sum += Math . Abs ( k ) ; } } if ( count % 2 == 0 ) { return sum ; } else { return ( sum - 2 * mini ) ; } } public static void Main ( string [ ] args ) { int [ , ] matrix = { { 2 , - 2 } , { - 2 , 2 } } ; Console . Write ( maxSum ( matrix ) ) ; } }


"Maximize matrix sum by repeatedly multiplying pairs of adjacent elements with | C # program to implement the above approach ; Function to calculate maximum sum possible of a matrix by multiplying pairs of adjacent elements with - 1 any number of times ( possibly zero ) ; Store the maximum sum of matrix possible ; Stores the count of negative values in the matrix ; Store minimum absolute value present in the matrix ; Traverse the matrix row - wise ; Update sum ; If current element is negative , increment the negative count ; If current value is less than the overall minimum in A [ ] , update the overall minimum ; If there are odd number of negative values , then the answer will be sum of the absolute values of all elements - 2 * minVal ; Print maximum sum ; Driver Code ; Given matrix ; Dimensions of matrix"
Solution in C#:

using System ; class GFG { static void getMaxSum ( int [ , ] A , int M , int N ) { int sum = 0 ; int negative = 0 ; int minVal = Int32 . MaxValue ; for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { sum += Math . Abs ( A [ i , j ] ) ; if ( A [ i , j ] < 0 ) { negative ++ ; } minVal = Math . Min ( minVal , Math . Abs ( A [ i , j ] ) ) ; } } if ( negative % 2 != 0 ) { sum -= 2 * minVal ; } Console . Write ( sum ) ; } public static void Main ( ) { int [ , ] A = { { 4 , - 8 , 6 } , { 3 , 7 , 2 } } ; int M = A . GetLength ( 0 ) ; int N = A . GetLength ( 1 ) ; getMaxSum ( A , M , N ) ; } }


"Maximize maximum possible subarray sum of an array by swapping with elements from another array | C # program for the above approach ; Function to find the maximum subarray sum possible by swapping elements from array arr [ ] with that from array brr [ ] ; Stores elements from the arrays arr [ ] and brr [ ] ; Store elements of array arr [ ] and brr [ ] in the vector crr ; Sort the vector crr in descending order ; Stores maximum sum ; Calculate the sum till the last index in crr [ ] which is less than N which contains a positive element ; Print the sum ; Driver Code ; Given arrays and respective lengths ; Calculate maximum subarray sum"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void maxSum ( int [ ] arr , int [ ] brr , int N , int K ) { List < int > crr = new List < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { crr . Add ( arr [ i ] ) ; } for ( int i = 0 ; i < K ; i ++ ) { crr . Add ( brr [ i ] ) ; } crr . Sort ( ) ; crr . Reverse ( ) ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( crr [ i ] > 0 ) { sum += crr [ i ] ; } else { break ; } } Console . WriteLine ( sum ) ; } static void Main ( ) { int [ ] arr = { 7 , 2 , - 1 , 4 , 5 } ; int N = arr . Length ; int [ ] brr = { 1 , 2 , 3 , 2 } ; int K = brr . Length ; maxSum ( arr , brr , N , K ) ; } }


"Maximize median after doing K addition operation on the Array | C # program for the above approach ; Function to check operation can be perform or not ; Number of operation to perform s . t . mid is median ; If mid is median of the array ; Function to find max median of the array ; Lowest possible median ; Highest possible median ; Checking for mid is possible for the median of array after doing at most k operation ; Return the max possible ans ; Driver code ; Given array ; Given number of operation ; Size of array ; Sort the array ; Function call"
Solution in C#:

using System ; class GFG { static bool possible ( int [ ] arr , int N , int mid , int K ) { int add = 0 ; for ( int i = N / 2 - ( N + 1 ) % 2 ; i < N ; ++ i ) { if ( mid - arr [ i ] > 0 ) { add += ( mid - arr [ i ] ) ; if ( add > K ) return false ; } } if ( add <= K ) return true ; else return false ; } static int findMaxMedian ( int [ ] arr , int N , int K ) { int low = 1 ; int mx = 0 ; for ( int i = 0 ; i < N ; ++ i ) { mx = Math . Max ( mx , arr [ i ] ) ; } int high = K + mx ; while ( low <= high ) { int mid = ( high + low ) / 2 ; if ( possible ( arr , N , mid , K ) ) { low = mid + 1 ; } else { high = mid - 1 ; } } if ( N % 2 == 0 ) { if ( low - 1 < arr [ N / 2 ] ) { return ( arr [ N / 2 ] + low - 1 ) / 2 ; } } return low - 1 ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 3 , 6 } ; int K = 10 ; int N = arr . Length ; Array . Sort ( arr ) ; Console . Write ( findMaxMedian ( arr , N , K ) ) ; } }


"Maximize median of a KxK sub | C # program for the above approach ; Function to determine if a given value can be median ; Stores the prefix sum array ; Traverse the matrix arr [ ] [ ] ; Update Pre [ i + 1 ] [ j + 1 ] ; If arr [ i ] [ j ] is less than or equal to mid ; Stores the count of elements should be less than mid ; Stores if the median mid can be possible or not ; Iterate over the range [ K , N ] ; Iterate over the range [ K , N ] ; Stores count of elements less than or equal to the value mid in submatrix with bottom right vertices at ( i , j ) ; If X is less than or equal to required ; Return flag ; Function to find the maximum median of a subsquare of the given size ; Stores the range of the search space ; Iterate until low is less than high ; Stores the mid value of the range [ low , high ] ; If the current median can be possible ; Update the value of low ; Update the value of high ; Return value stored in low as answer ; Driver code"
Solution in C#:

using System ; class GFG { static bool isMaximumMedian ( int [ , ] arr , int N , int K , int mid ) { int [ , ] Pre = new int [ N + 5 , N + 5 ] ; for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < N ; ++ j ) { Pre [ i + 1 , j + 1 ] = Pre [ i + 1 , j ] + Pre [ i , j + 1 ] - Pre [ i , j ] ; if ( arr [ i , j ] <= mid ) Pre [ i + 1 , j + 1 ] ++ ; } } int required = ( K * K + 1 ) / 2 ; bool flag = false ; for ( int i = K ; i <= N ; ++ i ) { for ( int j = K ; j <= N ; ++ j ) { int X = Pre [ i , j ] - Pre [ i - K , j ] - Pre [ i , j - K ] + Pre [ i - K , j - K ] ; if ( X < required ) flag = true ; } } return flag ; } static int maximumMedian ( int [ , ] arr , int N , int K ) { int low = 0 , high = ( int ) 1e9 ; while ( low < high ) { int mid = low + ( high - low ) / 2 ; if ( isMaximumMedian ( arr , N , K , mid ) ) { low = mid + 1 ; } else { high = mid ; } } return low ; } public static void Main ( string [ ] args ) { int [ , ] arr = { { 1 , 5 , 12 } , { 6 , 7 , 11 } , { 8 , 9 , 10 } } ; int N = arr . GetLength ( 0 ) ; int K = 2 ; Console . WriteLine ( maximumMedian ( arr , N , K ) ) ; } }


"Maximize minimum array element possible by exactly K decrements | C # program for the above approach ; Function to find the maximized minimum element of the array after performing given operation exactly K times ; Stores the minimum element ; Traverse the given array ; Update the minimum element ; Stores the required operations to make all elements equal to the minimum element ; Update required operations ; If reqOperations < K ; Decrement the value of K by reqOperations ; Update minElement ; Return minimum element ; Driver Code"
Solution in C#:

using System ; class GFG { static int minimumElement ( int [ ] arr , int N , int K ) { int minElement = arr [ 0 ] ; for ( int i = 0 ; i < N ; ++ i ) { minElement = Math . Min ( minElement , arr [ i ] ) ; } int reqOperations = 0 ; for ( int i = 0 ; i < N ; ++ i ) { reqOperations += arr [ i ] - minElement ; } if ( reqOperations < K ) { K -= reqOperations ; minElement -= ( K + N - 1 ) / N ; } return minElement ; } public static void Main ( string [ ] args ) { int [ ] arr = { 10 , 10 , 10 , 10 } ; int K = 7 ; int N = arr . Length ; Console . Write ( minimumElement ( arr , N , K ) ) ; } }


"Maximize minimum of array generated by maximums of same indexed elements of two rows of a given Matrix | C # program for the above approach ; Function to find the maximum of minimum of array constructed from any two rows of the given matrix ; Initialize global max as INT_MIN ; Iterate through the rows ; Iterate through remaining rows ; Initialize row_min as INT_MAX ; Iterate through the column values of two rows ; Find max of two elements ; Update the row_min ; Update the global_max ; Print the global max ; Driver Code ; Given matrix mat [ ] [ ] ; Given number of rows and columns ; Function Call"
Solution in C#:

using System ; class GFG { static int getMaximum ( int N , int M , int [ , ] mat ) { int global_max = int . MinValue ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { int row_min = int . MaxValue ; for ( int k = 0 ; k < M ; k ++ ) { int m = Math . Max ( mat [ i , k ] , mat [ j , k ] ) ; row_min = Math . Min ( row_min , m ) ; } global_max = Math . Max ( global_max , row_min ) ; } } return global_max ; } public static void Main ( ) { int [ , ] mat = { { 5 , 0 , 3 , 1 , 2 } , { 1 , 8 , 9 , 1 , 3 } , { 1 , 2 , 3 , 4 , 5 } , { 9 , 1 , 0 , 3 , 7 } , { 2 , 3 , 0 , 6 , 3 } , { 6 , 4 , 1 , 7 , 0 } } ; int N = 6 , M = 5 ; Console . WriteLine ( getMaximum ( N , M , mat ) ) ; } }


"Maximize modulus by replacing adjacent pairs with their modulus for any permutation of given Array | C # Program to implement the above approach ; Function to find the maximum value possible of the given expression from all permutations of the array ; Stores the minimum value from the array ; Return the answer ; Driver Code"
Solution in C#:

using System ; class GFG { static int maximumModuloValue ( int [ ] A , int n ) { int mn = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { mn = Math . Min ( A [ i ] , mn ) ; } return mn ; } public static void Main ( String [ ] args ) { int [ ] A = { 7 , 10 , 12 } ; int n = A . Length ; Console . WriteLine ( maximumModuloValue ( A , n ) ) ; } }


"Maximize number of circular buildings that can be covered by L length wire | C # program for the above approach ; Function to find the maximum number of buildings covered ; Store the current sum ; Traverse the array ; Add the length of wire required for current building to cur_sum ; Add extra unit distance 1 ; If curr_sum <= length of wire increment count by 1 ; If curr_sum > length of wire increment start by 1 and decrement count by 1 and update the new curr_sum ; Update the max_count ; Return the max_count ; Driver code ; Given Input ; Size of the array ; Function Call"
Solution in C#:

using System ; class GFG { static double Pi = 3.141592 ; static int MaxBuildingsCovered ( int [ ] arr , int N , int L ) { double curr_sum = 0 ; int start = 0 , curr_count = 0 , max_count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { curr_sum = curr_sum + ( ( double ) arr [ i ] * Pi ) ; if ( i != 0 ) curr_sum += 1 ; if ( curr_sum <= L ) { curr_count ++ ; } else if ( curr_sum > L ) { curr_sum = curr_sum - ( ( double ) arr [ start ] * Pi ) ; curr_sum -= 1 ; start ++ ; curr_count -- ; } max_count = Math . Max ( curr_count , max_count ) ; } return max_count ; } static void Main ( ) { int [ ] arr = { 4 , 1 , 6 , 2 } ; int L = 24 ; int N = arr . Length ; Console . Write ( MaxBuildingsCovered ( arr , N , L ) ) ; } }


"Maximize number of elements from Array with sum at most K | C # program for the above approach ; Function to select a maximum number of elements in array whose sum is at most K ; Sort the array ; Calculate the sum and count while iterating the sorted array ; Iterate for all the elements in the array ; Add the current element to sum ; Increment the count ; Return the answer ; Driver Code ; Given array ; Given sum k ; Function call"
Solution in C#:

using System ; class GFG { static int maxSelections ( int [ ] A , int n , int k ) { Array . Sort ( A ) ; int sum = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + A [ i ] ; if ( sum > k ) { break ; } count ++ ; } return count ; } public static void Main ( String [ ] args ) { int [ ] A = { 3 , 7 , 2 , 9 , 4 } ; int k = 15 ; int n = A . Length ; Console . Write ( maxSelections ( A , n , k ) ) ; } }


"Maximize number of groups formed with size not smaller than its largest element | C # implementation of above approach ; Function that prints the number of maximum groups ; Store the number of occurrence of elements ; Make all groups of similar elements and store the left numbers ; Condition for finding first leftover element ; Condition for current leftover element ; Condition if group size is equal to or more than current element ; Printing maximum number of groups ; Driver Code"
Solution in C#:

 using System ; class GFG { static void makeGroups ( int [ ] a , int n ) { int [ ] v = new int [ n + 1 ] ; int i = 0 ; for ( i = 0 ; i < n ; i ++ ) { v [ a [ i ] ] ++ ; } int no_of_groups = 0 ; for ( i = 1 ; i <= n ; i ++ ) { no_of_groups += v [ i ] / i ; v [ i ] = v [ i ] % i ; } i = 1 ; int total = 0 ; for ( i = 1 ; i <= n ; i ++ ) { if ( v [ i ] != 0 ) { total = v [ i ] ; break ; } } i ++ ; while ( i <= n ) { if ( v [ i ] != 0 ) { total += v [ i ] ; if ( total >= i ) { int rem = total - i ; no_of_groups ++ ; total = rem ; } } i ++ ; } Console . Write ( no_of_groups + "
 " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 1 , 2 , 2 } ; int size = arr . Length ; makeGroups ( arr , size ) ; } }


"Maximize occurrences of values between L and R on sequential addition of Array elements with modulo H | C # implementation of the above approach ; Function that prints the number of times X gets a value between L and R ; Base condition ; Condition if X can be made equal to j after i additions ; Compute value of X after adding arr [ i ] ; Compute value of X after adding arr [ i ] - 1 ; Update dp as the maximum value ; Compute maximum answer from all possible cases ; Printing maximum good occurrence of X ; Driver Code"
Solution in C#:

 using System ; class GFG { static void goodint ( int [ ] arr , int n , int h , int l , int r ) { int [ , ] dp = new int [ n + 1 , h ] ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < h ; j ++ ) dp [ i , j ] = - 1 ; dp [ 0 , 0 ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < h ; j ++ ) { if ( dp [ i , j ] != - 1 ) { int h1 = ( j + arr [ i ] ) % h ; int h2 = ( j + arr [ i ] - 1 ) % h ; dp [ i + 1 , h1 ] = Math . Max ( dp [ i + 1 , h1 ] , dp [ i , j ] + ( ( h1 >= l && h1 <= r ) ? 1 : 0 ) ) ; dp [ i + 1 , h2 ] = Math . Max ( dp [ i + 1 , h2 ] , dp [ i , j ] + ( ( h2 >= l && h2 <= r ) ? 1 : 0 ) ) ; } } } int ans = 0 ; for ( int i = 0 ; i < h ; i ++ ) { if ( dp [ n , i ] != - 1 ) ans = Math . Max ( ans , dp [ n , i ] ) ; } Console . Write ( ans + "
 " ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 16 , 17 , 14 , 20 , 20 , 11 , 22 } ; int H = 24 ; int L = 21 ; int R = 23 ; int size = A . Length ; goodint ( A , size , H , L , R ) ; } }


"Maximize pair decrements required to reduce all array elements except one to 0 | C # program to implement the above approach ; Function to count maximum number of steps to make ( N - 1 ) array elements to 0 ; Stores maximum count of steps to make ( N - 1 ) elements equal to 0 ; Stores array elements ; Traverse the array ; Insert arr [ i ] into PQ ; Extract top 2 elements from the array while ( N - 1 ) array elements become 0 ; Stores top element of PQ ; Pop the top element of PQ . ; Stores top element of PQ ; Pop the top element of PQ . ; Update X ; Update Y ; If X is not equal to 0 ; Insert X into PQ ; if Y is not equal to 0 ; Insert Y into PQ ; Update cntOp ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int cntMaxOperationToMakeN_1_0 ( int [ ] arr , int N ) { int cntOp = 0 ; List < int > PQ = new List < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { PQ . Add ( arr [ i ] ) ; } PQ . Sort ( ) ; PQ . Reverse ( ) ; while ( PQ . Count > 1 ) { int X = PQ [ 0 ] ; PQ . RemoveAt ( 0 ) ; int Y = PQ [ 0 ] ; PQ . RemoveAt ( 0 ) ; X -- ; Y -- ; if ( X != 0 ) { PQ . Add ( X ) ; PQ . Sort ( ) ; PQ . Reverse ( ) ; } if ( Y != 0 ) { PQ . Add ( Y ) ; PQ . Sort ( ) ; PQ . Reverse ( ) ; } cntOp += 1 ; } return cntOp ; } static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int N = arr . Length ; Console . WriteLine ( cntMaxOperationToMakeN_1_0 ( arr , N ) ) ; } }


"Maximize palindromic strings of length 3 possible from given count of alphabets | C # program for the above approach ; Function to count maximum number of palindromic String of length 3 ; Stores the readonly count of palindromic Strings ; Traverse the array ; Increment res by arr [ i ] / 3 , i . e forming String of only i + ' a ' character ; Store remainder ; Increment c1 by one , if current frequency is 1 ; Increment c2 by one , if current frequency is 2 ; Count palindromic Strings of length 3 having the character at the ends different from that present in the middle ; Update c1 and c2 ; Increment res by 2 * c2 / 3 ; Finally print the result ; Driver Code ; Given array ; Function Call"
Solution in C#:

using System ; public class GFG { static void maximum_pallindromic ( int [ ] arr ) { int res = 0 ; int c1 = 0 , c2 = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { res += arr [ i ] / 3 ; arr [ i ] = arr [ i ] % 3 ; if ( arr [ i ] == 1 ) c1 ++ ; else if ( arr [ i ] == 2 ) c2 ++ ; } res += Math . Min ( c1 , c2 ) ; int t = Math . Min ( c1 , c2 ) ; c1 -= t ; c2 -= t ; res += 2 * ( c2 / 3 ) ; c2 %= 3 ; res += c2 / 2 ; Console . Write ( res ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 5 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } ; maximum_pallindromic ( arr ) ; } }


"Maximize partitions such that no two substrings have any common character | C # program to find the maximum number of partitions possible such that no two subStrings have common character ; Function to calculate and return the maximum number of partitions ; r : Stores the maximum number of partitions k : Stores the ending index of the partition ; Stores the last index of every unique character of the String ; Traverse the String and store the last index of every character ; Store the last index of the first character from map ; Update K to find the end of partition ; Otherwise , the end of partition is found ; Increment r ; Update k for the next partition ; Add the last partition ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int maximum_partition ( String str ) { int i = 0 , j = 0 , k = 0 ; int c = 0 , r = 0 ; Dictionary < char , int > m = new Dictionary < char , int > ( ) ; for ( i = str . Length - 1 ; i >= 0 ; i -- ) { if ( ! m . ContainsKey ( str [ i ] ) ) { m . Add ( str [ i ] , i ) ; } } i = 0 ; k = m [ str [ i ] ] ; for ( i = 0 ; i < str . Length ; i ++ ) { if ( i <= k ) { c = c + 1 ; k = Math . Max ( k , m [ str [ i ] ] ) ; } else { r = r + 1 ; c = 1 ; k = Math . Max ( k , m [ str [ i ] ] ) ; } } if ( c != 0 ) { r = r + 1 ; } return r ; } public static void Main ( String [ ] args ) { String str = " ababcbacadefegdehijhklij " ; Console . Write ( maximum_partition ( str ) ) ; } }


"Maximize path sum from top | C # program to implement the above approach ; Function to get the maximum path sum from top - left cell to all other cells of the given matrix ; Store the maximum path sum ; Base case ; Compute the value of dp [ i , j ] using the recurrence relation ; Print maximum path sum from the top - left cell ; Driver Code"
Solution in C#:

using System ; class GFG { static readonly int SZ = 100 ; static void pathSum ( int [ , ] mat , int N , int M ) { int [ , ] dp = new int [ N , M ] ; dp [ 0 , 0 ] = mat [ 0 , 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { dp [ i , 0 ] = mat [ i , 0 ] + dp [ i - 1 , 0 ] ; } for ( int j = 1 ; j < M ; j ++ ) { dp [ 0 , j ] = mat [ 0 , j ] + dp [ 0 , j - 1 ] ; } for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 1 ; j < M ; j ++ ) { dp [ i , j ] = mat [ i , j ] + Math . Max ( dp [ i - 1 , j ] , dp [ i , j - 1 ] ) ; } } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { Console . Write ( dp [ i , j ] + " ▁ " ) ; } Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { int [ , ] mat = { { 3 , 2 , 1 } , { 6 , 5 , 4 } , { 7 , 8 , 9 } } ; int N = 3 ; int M = 3 ; pathSum ( mat , N , M ) ; } }


"Maximize product obtained by taking one element from each array of a given list | C # program for the above approach ; Function to return the product of 2 numbers ; If any of the two numbers is None ; Otherwise , return the product ; Function to calculate maximum product by taking only one element from each array present in the list ; Find the maximum and minimum present in the current array ; If last index is reached , then return the highest ( positive ) and lowest ( negative ) values ; Store the positive and negative products returned by calculating for the remaining arrays ; Store highest positive product ; Store product of highest with negative ; Store product of lowest with positive ; Store lowest negative product ; Return the maximum positive and minimum negative product ; Driver Code ; Given list of N arrays ; Store the maximum positive and minimum negative product possible ; Print the maximum product"
Solution in C#:

using System ; using System . Collections . Generic ; using System . Linq ; public class GFG { static int findProduct ( int number_1 , int number_2 ) { if ( number_1 == Int32 . MinValue number_2 == Int32 . MinValue ) { return 0 ; } else return number_1 * number_2 ; } static List < int > calculateProduct ( List < List < int > > List , int index ) { int highest = List [ index ] . Max ( ) ; int lowest = List [ index ] . Min ( ) ; if ( index + 1 == List . Count ) { if ( lowest < 0 && highest >= 0 ) { return ( new List < int > ( ) { highest , lowest } ) ; } else if ( lowest <= 0 && highest <= 0 ) { return ( new List < int > ( ) { Int32 . MinValue , lowest } ) ; } else if ( lowest >= 0 && highest >= 0 ) { return ( new List < int > ( ) { highest , Int32 . MinValue } ) ; } } List < int > temp = calculateProduct ( List , index + 1 ) ; int positive = temp [ 0 ] ; int negative = temp [ 1 ] ; int highPos = findProduct ( highest , positive ) ; int highNeg = findProduct ( highest , negative ) ; int lowPos = findProduct ( lowest , positive ) ; int lowNeg = findProduct ( lowest , negative ) ; if ( lowest < 0 && highest >= 0 ) { return ( new List < int > ( ) { Math . Max ( highPos , lowNeg ) , Math . Min ( highNeg , lowPos ) } ) ; } else if ( lowest <= 0 && highest <= 0 ) { return ( new List < int > ( ) { lowNeg , lowPos } ) ; } else if ( lowest >= 0 && highest >= 0 ) { return ( new List < int > ( ) { Math . Max ( lowPos , highPos ) , Math . Min ( lowNeg , highNeg ) } ) ; } return ( new List < int > ( ) { 0 , 0 } ) ; } static public void Main ( ) { List < List < int > > arr = new List < List < int > > ( ) ; arr . Add ( new List < int > ( ) { - 3 , - 4 } ) ; arr . Add ( new List < int > ( ) { 1 , 2 , - 3 } ) ; List < int > ans = calculateProduct ( arr , 0 ) ; Console . WriteLine ( ans [ 0 ] ) ; } }


"Maximize product of a strictly increasing or decreasing subarray | C # program to implement the above approach ; Function to find the maximum product of subarray in the array , [ ] arr ; Maximum positive product ending at the i - th index ; Minimum negative product ending at the current index ; Maximum product up to i - th index ; Check if an array element is positive or not ; Traverse the array ; If current element is positive ; Update max_ending_here ; Update min_ending_here ; Update flag ; If current element is 0 , reset the start index of subarray ; Update max_ending_here ; Update min_ending_here ; If current element is negative ; Stores max_ending_here ; Update max_ending_here ; Update min_ending_here ; Update max_so_far , if needed ; If no array elements is positive and max_so_far is 0 ; Function to find the maximum product of either increasing subarray or the decreasing subarray ; Stores start index of either increasing subarray or the decreasing subarray ; Initially assume maxProd to be 1 ; Traverse the array ; Store the longest either increasing subarray or the decreasing subarray whose start index is i ; Check for increasing subarray ; Insert elements of increasing subarray ; Check for decreasing subarray ; Insert elements of decreasing subarray ; Stores maximum subarray product of current increasing or decreasing subarray ; Update maxProd ; Update i ; Finally print maxProd ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { static int maxSubarrayProduct ( List < int > arr , int n ) { int max_ending_here = 1 ; int min_ending_here = 1 ; int max_so_far = 0 ; int flag = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) { max_ending_here = max_ending_here * arr [ i ] ; min_ending_here = Math . Min ( min_ending_here * arr [ i ] , 1 ) ; flag = 1 ; } else if ( arr [ i ] == 0 ) { max_ending_here = 1 ; min_ending_here = 1 ; } else { int temp = max_ending_here ; max_ending_here = Math . Max ( min_ending_here * arr [ i ] , 1 ) ; min_ending_here = temp * arr [ i ] ; } if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; } if ( flag == 0 && max_so_far == 0 ) return 0 ; return max_so_far ; } static int findMaxProduct ( int [ ] a , int n ) { int i = 0 ; int maxProd = 1 ; while ( i < n ) { List < int > v = new List < int > ( ) ; v . Add ( a [ i ] ) ; if ( i < n - 1 && a [ i ] < a [ i + 1 ] ) { while ( i < n - 1 && a [ i ] < a [ i + 1 ] ) { v . Add ( a [ i + 1 ] ) ; i += 1 ; } } else if ( i < n - 1 && a [ i ] > a [ i + 1 ] ) { while ( i < n - 1 && a [ i ] > a [ i + 1 ] ) { v . Add ( a [ i + 1 ] ) ; i += 1 ; } } int prod = maxSubarrayProduct ( v , v . Count ) ; maxProd = Math . Max ( maxProd , prod ) ; i ++ ; } return maxProd ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 10 , 8 , 1 , 100 , 101 } ; int N = arr . Length ; Console . Write ( findMaxProduct ( arr , N ) ) ; } }


"Maximize product of absolute index difference with K | C # program to implement the above approach ; Function returns maximum possible value of k ; Pointer i make sure that A [ i ] will result in max k ; Stores maximum possible k ; Possible value of k for current pair ( A [ i ] and A [ j ] ) ; If current value exceeds k ; Update the value of k ; Update pointer i ; Return the maxm possible k ; Driver Code"
Solution in C#:

using System ; class GFG { static int solve ( int [ ] A , int N ) { int i = 0 ; int k = 0 ; for ( int j = 1 ; j < N ; j ++ ) { int tempK = Math . Min ( A [ i ] , A [ j ] ) / ( j - i ) ; if ( tempK > k ) { k = tempK ; } if ( A [ j ] >= A [ i ] / ( j - i ) ) i = j ; } return k ; } public static void Main ( string [ ] args ) { int [ ] A = { 10 , 5 , 12 , 15 , 8 } ; int N = A . Length ; Console . Write ( solve ( A , N ) ) ; } }


"Maximize product of array by replacing array elements with its sum or product with element from another array | C # program for the above approach ; Function to find the largest product of array A [ ] ; Base Case ; Store all the elements of the array A [ ] ; Sort the Array B [ ] ; Traverse the array B [ ] ; Pop minimum element ; Check which operation is producing maximum element ; Insert resultant element into the priority queue ; Evaluate the product of the elements of A [ ] ; Return the maximum product ; Driver Code ; Given arrays ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static int largeProduct ( int [ ] A , int [ ] B , int N ) { if ( N == 0 ) { return 0 ; } List < int > pq = new List < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { pq . Add ( A [ i ] ) ; } Array . Sort ( B ) ; pq . Sort ( ) ; for ( int i = 0 ; i < N ; i ++ ) { int min = pq [ 0 ] ; pq . RemoveAt ( 0 ) ; int maximized_element = Math . Max ( min * B [ i ] , min + B [ i ] ) ; pq . Add ( maximized_element ) ; pq . Sort ( ) ; } int max_product = 1 ; while ( pq . Count > 0 ) { max_product *= pq [ 0 ] ; pq . RemoveAt ( 0 ) ; } return max_product ; } static public void Main ( ) { int [ ] A = { 1 , 1 , 10 } ; int [ ] B = { 1 , 1 , 1 } ; int N = 3 ; Console . WriteLine ( largeProduct ( A , B , N ) ) ; } }


"Maximize product of digit sum of consecutive pairs in a subsequence of length K | C # implementation to find the maximum product of the digit sum of the consecutive pairs of the subsequence of the length K ; Function to find the product of two numbers digit sum in the pair ; Loop to find the digits of the number ; Loop to find the digits of other number ; Function to find the subsequence of the length K ; Base Case ; Condition when we didn 't reach  the length K, but ran out of  elements of the array ; Condition if already calculated ; If length upto this point is odd ; If length is odd , it means we need second element of this current pair , calculate the product of digit sum of current and previous element and recur by moving towards next index ; If length upto this point is even ; Exclude this current element and recur for next elements . ; Return by memoizing it , by selecting the maximum among two choices . ; Driver Code"
Solution in C#:

using System ; class GFG { static int MAX = 100 ; static int [ , , ] dp = new int [ 1000 , MAX , MAX ] ; static int productDigitSum ( int x , int y ) { int sumx = 0 ; while ( x > 0 ) { sumx += ( x % 10 ) ; x /= 10 ; } int sumy = 0 ; while ( y > 0 ) { sumy += ( y % 10 ) ; y /= 10 ; } return ( sumx * sumy ) ; } static int solve ( int [ ] arr , int i , int len , int prev , int n , int k ) { if ( len == k ) return 0 ; if ( i == n ) return Int32 . MinValue ; if ( dp [ i , len , prev ] != 0 ) return dp [ i , len , prev ] ; int inc = 0 , exc = 0 ; if ( ( len & 1 ) != 0 ) { inc = ( productDigitSum ( arr [ prev ] , arr [ i ] ) + solve ( arr , i + 1 , len + 1 , 0 , n , k ) ) ; } else { inc = solve ( arr , i + 1 , len + 1 , i , n , k ) ; } exc = solve ( arr , i + 1 , len , prev , n , k ) ; return dp [ i , len , prev ] = Math . Max ( inc , exc ) ; } public static void Main ( ) { int [ ] arr = { 10 , 5 , 9 , 101 , 24 , 2 , 20 , 14 } ; int n = arr . Length ; int k = 6 ; Console . Write ( solve ( arr , 0 , 0 , 0 , n , k ) ) ; } }


"Maximize product of lengths of strings having no common characters | C # program for the above approach ; Function to count the number of set bits in the integer n ; Stores the count of set bits in n ; Return the count ; Function to find the maximum product of pair of strings having no common characters ; Stores the integer equivalent of the strings ; Traverse the array of strings ; Traverse the current string ; Store the current bit position in bits [ i ] ; Store the required result ; Traverse the array , bits [ ] to get all unique pairs ( i , j ) ; Check whether the strings have no common characters ; Update the overall maximum product ; Print the maximum product ; Driver code"
Solution in C#:

using System ; public class GFG { public static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } public static void maximumProduct ( string [ ] words ) { int [ ] bits = new int [ words . Length ] ; for ( int i = 0 ; i < words . Length ; i ++ ) { for ( int j = 0 ; j < words [ i ] . Length ; j ++ ) { bits [ i ] = bits [ i ] | 1 << ( words [ i ] [ j ] - ' a ' ) ; } } int result = 0 ; for ( int i = 0 ; i < bits . Length ; i ++ ) { for ( int j = i + 1 ; j < bits . Length ; j ++ ) { if ( ( bits [ i ] & bits [ j ] ) == 0 ) { int L = countSetBits ( bits [ i ] ) ; int R = countSetBits ( bits [ j ] ) ; result = Math . Max ( L * R , result ) ; } } } Console . WriteLine ( result ) ; } static public void Main ( ) { string [ ] arr = { " abcw " , " baz " , " foo " , " bar " , " xtfn " , " abcdef " } ; maximumProduct ( arr ) ; } }


"Maximize product of same | C # implementation to maximize product of same - indexed elements of same size subsequences ; Utility function to find the maximum ; Utility function to find the maximum scalar product from the equal length sub - sequences taken from the two given array ; Return a very small number if index is invalid ; If the sub - problem is already evaluated , then return it ; Take the maximum of all the recursive cases ; Function to find maximum scalar product from same size sub - sequences taken from the two given array ; Initialize a 2 - D array for memoization ; Driver Code"
Solution in C#:

using System ; class GFG { static readonly int INF = 10000000 ; static int maximum ( int A , int B , int C , int D ) { return Math . Max ( Math . Max ( A , B ) , Math . Max ( C , D ) ) ; } static int maxProductUtil ( int X , int Y , int [ ] A , int [ ] B , int [ , ] dp ) { if ( X < 0 Y < 0 ) return - INF ; if ( dp [ X , Y ] != - 1 ) return dp [ X , Y ] ; dp [ X , Y ] = maximum ( A [ X ] * B [ Y ] + maxProductUtil ( X - 1 , Y - 1 , A , B , dp ) , A [ X ] * B [ Y ] , maxProductUtil ( X - 1 , Y , A , B , dp ) , maxProductUtil ( X , Y - 1 , A , B , dp ) ) ; return dp [ X , Y ] ; } static int maxProduct ( int [ ] A , int N , int [ ] B , int M ) { int [ , ] dp = new int [ N , M ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { dp [ i , j ] = - 1 ; } } return maxProductUtil ( N - 1 , M - 1 , A , B , dp ) ; } public static void Main ( String [ ] args ) { int [ ] a = { - 2 , 6 , - 2 , - 5 } ; int [ ] b = { - 3 , 4 , - 2 , 8 } ; int n = a . Length ; int m = b . Length ; Console . Write ( maxProduct ( a , n , b , m ) ) ; } }