"Maximum length of same indexed subarrays from two given arrays satisfying the given condition | C # program for the above approach ; Function to find maximum length of subarray such that sum of maximum element in subarray in brr [ ] and sum of subarray in [ ] arr * K is at most C ; Base Case ; Let maximum length be 0 ; Perform Binary search ; Find mid value ; Check if the current mid satisfy the given condition ; If yes , then store length ; Otherwise ; Return maximum length stored ; Function to check if it is possible to have such a subarray of length K ; Finds the maximum element in each window of size k ; Check for window of size K ; For all possible subarrays of length k ; Until deque is empty ; Calculate the total cost and check if less than equal to c ; Find sum of current subarray and the total cost ; Include the new element of current subarray ; Discard the element of last subarray ; Remove all the elements in the old window ; Calculate total cost and check <= c ; If current subarray length satisfies ; If it is not possible ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { public static int maxLength ( int [ ] a , int [ ] b , int n , int c ) { if ( n == 0 ) return 0 ; int max_length = 0 ; int low = 0 , high = n ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( possible ( a , b , n , c , mid ) ) { max_length = mid ; low = mid + 1 ; } else high = mid - 1 ; } return max_length ; } public static bool possible ( int [ ] a , int [ ] b , int n , int c , int k ) { List < int > dq = new List < int > ( ) ; int sum = 0 ; for ( int i = 0 ; i < k ; i ++ ) { sum += a [ i ] ; while ( dq . Count > 0 && b [ i ] > b [ dq [ dq . Count - 1 ] ] ) dq . RemoveAt ( dq . Count - 1 ) ; dq . Add ( i ) ; } int total_cost = sum * k + b [ dq [ 0 ] ] ; if ( total_cost <= c ) return true ; for ( int i = k ; i < n ; i ++ ) { sum += a [ i ] ; sum -= a [ i - k ] ; while ( dq . Count > 0 && dq [ 0 ] <= i - k ) dq . RemoveAt ( 0 ) ; while ( dq . Count > 0 && b [ i ] > b [ dq [ dq . Count - 1 ] ] ) dq . RemoveAt ( dq . Count - 1 ) ; dq . Add ( i ) ; total_cost = sum * k + b [ dq [ 0 ] ] ; if ( total_cost <= c ) return true ; } return false ; } public static void Main ( String [ ] args ) { int [ ] A = { 1 , 2 , 1 , 6 , 5 , 5 , 6 , 1 } ; int [ ] B = { 14 , 8 , 15 , 15 , 9 , 10 , 7 , 12 } ; int N = A . Length ; int C = 40 ; Console . WriteLine ( maxLength ( A , B , N , C ) ) ; } }


"Maximum length of same indexed subarrays from two given arrays satisfying the given condition | C # program for the above approach ; Stores the segment tree node values ; Function to find maximum length of subarray such that sum of maximum element in subarray in brr [ ] and sum of subarray in arr [ ] * K is at most C ; Base Case ; Let maximum length be 0 ; Perform Binary search ; Find mid value ; Check if the current mid satisfy the given condition ; If yes , then store length ; Otherwise ; Return maximum length stored ; Function to check if it is possible to have such a subarray of length K ; Check for first window of size K ; Calculate the total cost and check if less than equal to c ; If it satisfy the condition ; Find the sum of current subarray and calculate total cost ; Include the new element of current subarray ; Discard the element of last subarray ; Calculate total cost and check <= c ; If possible , then return true ; If it is not possible ; Function that builds segment Tree ; If there is only one element ; Find the value of mid ; Build left and right parts of segment tree recursively ; Update the value at current index ; Function to find maximum element in the given range ; If the query is out of bounds ; If the segment is completely inside the query range ; Calculate the mid ; Return maximum in left & right of the segment tree recursively ; Function that initializes the segment Tree ; Driver Code ; Initialize and Build the Segment Tree ; Function Call"
Solution in C#:

using System ; class GFG { static int [ ] seg ; static int maxLength ( int [ ] a , int [ ] b , int n , int c ) { if ( n == 0 ) return 0 ; int max_length = 0 ; int low = 0 , high = n ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( possible ( a , b , n , c , mid ) ) { max_length = mid ; low = mid + 1 ; } else high = mid - 1 ; } return max_length ; } static bool possible ( int [ ] a , int [ ] b , int n , int c , int k ) { int sum = 0 ; for ( int i = 0 ; i < k ; i ++ ) { sum += a [ i ] ; } int total_cost = sum * k + getMax ( b , 0 , n - 1 , 0 , k - 1 , 0 ) ; if ( total_cost <= c ) return true ; for ( int i = k ; i < n ; i ++ ) { sum += a [ i ] ; sum -= a [ i - k ] ; total_cost = sum * k + getMax ( b , 0 , n - 1 , i - k + 1 , i , 0 ) ; if ( total_cost <= c ) return true ; } return false ; } static void build ( int [ ] b , int index , int s , int e ) { if ( s == e ) { seg [ index ] = b [ s ] ; return ; } int mid = s + ( e - s ) / 2 ; build ( b , 2 * index + 1 , s , mid ) ; build ( b , 2 * index + 2 , mid + 1 , e ) ; seg [ index ] = Math . Max ( seg [ 2 * index + 1 ] , seg [ 2 * index + 2 ] ) ; } public static int getMax ( int [ ] b , int ss , int se , int qs , int qe , int index ) { if ( se < qs ss > qe ) return Int32 . MinValue / 2 ; if ( ss >= qs && se <= qe ) return seg [ index ] ; int mid = ss + ( se - ss ) / 2 ; return Math . Max ( getMax ( b , ss , mid , qs , qe , 2 * index + 1 ) , getMax ( b , mid + 1 , se , qs , qe , 2 * index + 2 ) ) ; } static void initialiseSegmentTree ( int N ) { seg = new int [ 4 * N ] ; } static void Main ( ) { int [ ] A = { 1 , 2 , 1 , 6 , 5 , 5 , 6 , 1 } ; int [ ] B = { 14 , 8 , 15 , 15 , 9 , 10 , 7 , 12 } ; int C = 40 ; int N = A . Length ; initialiseSegmentTree ( N ) ; build ( B , 0 , 0 , N - 1 ) ; Console . WriteLine ( maxLength ( A , B , N , C ) ) ; } }


"Maximum length of segments of 0 ' s ▁ and ▁ 1' s | C # implementation of above approach ; Recursive Function to find total length of the array Where 1 is greater than zero ; If reaches till end ; If dp is saved ; Finding for each length ; If the character scanned is 1 ; If one is greater than zero , add total length scanned till now ; Continue with next length ; Return the value for start index ; Driver Code ; Size of string ; Calling the function to find the value of function"
Solution in C#:

 using System ; class GFG { public static int find ( int start , string adj , int n , int [ ] dp ) { if ( start == n ) return 0 ; if ( dp [ start ] != - 1 ) return dp [ start ] ; dp [ start ] = 0 ; int one = 0 , zero = 0 , k ; for ( k = start ; k < n ; k ++ ) { if ( adj [ k ] == '1' ) one ++ ; else zero ++ ; if ( one > zero ) dp [ start ] = Math . Max ( dp [ start ] , find ( k + 1 , adj , n , dp ) + k - start + 1 ) ; else dp [ start ] = Math . Max ( dp [ start ] , find ( k + 1 , adj , n , dp ) ) ; } return dp [ start ] ; } static void Main ( ) { string adj = "100110001010001" ; int n = adj . Length ; int [ ] dp = new int [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) dp [ i ] = - 1 ; Console . Write ( find ( 0 , adj , n , dp ) + "
 " ) ; } }


"Maximum length of string formed by concatenation having even frequency of each character | C # Implementation of the above approach ; Function to check the string ; Count the frequency of the string ; Check the frequency of the string ; Store the Length of the new String ; Function to find the longest concatenated string having every character of even frequency ; Checking the string ; Dont Include the string ; Include the string ; Driver code ; Call the function ; Print the answer"
Solution in C#:

using System ; public class index { static int max = 0 ; static String ans1 = " " ; static void calculate ( String ans ) { int [ ] dp = new int [ 26 ] ; for ( int i = 0 ; i < ans . Length ; ++ i ) { dp [ ( int ) ans [ i ] - ( int ) ' A ' ] ++ ; } for ( int i = 0 ; i < dp . Length ; ++ i ) { if ( dp [ i ] % 2 == 1 ) { return ; } } if ( max < ans . Length ) { max = ans . Length ; ans1 = ans ; } } static void longestString ( String [ ] arr , int index , String str ) { if ( index == arr . Length ) { return ; } longestString ( arr , index + 1 , str ) ; str += arr [ index ] ; calculate ( str ) ; longestString ( arr , index + 1 , str ) ; } public static void Main ( ) { String [ ] A = { " ABAB " , " ABF " , " CDA " , " AD " , " CCC " } ; longestString ( A , 0 , " " ) ; Console . WriteLine ( ans1 + " ▁ " + ans1 . Length ) ; } }


"Maximum length of subarray such that all elements are equal in the subarray | C # program for the above approach ; Function to find the longest subarray with same element ; Check if the elements are same then we can increment the length ; Reinitialize j ; Compare the maximum length e with j ; Return max length ; Driver Code ; Given array [ ] arr ; Function Call"
Solution in C#:

using System ; class GFG { static int longest_subarray ( int [ ] arr , int d ) { int i = 0 , j = 1 , e = 0 ; for ( i = 0 ; i < d - 1 ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) { j = j + 1 ; } else { j = 1 ; } if ( e < j ) { e = j ; } } return e ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int N = arr . Length ; Console . Write ( longest_subarray ( arr , N ) ) ; } }


"Maximum length of subarray such that sum of the subarray is even | C # implementation of the approach ; Function to find length of the longest subarray such that sum of the subarray is even ; Check if sum of complete array is even ; if ( sum % 2 == 0 ) total sum is already even ; Find an index i such the a [ i ] is odd and compare length of both halfs excluding a [ i ] to find max length subarray ; Driver Code"
Solution in C#:

using System ; class GFG { static int maxLength ( int [ ] a , int n ) { int sum = 0 , len = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; } { return n ; } for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) { len = Math . Max ( len , Math . Max ( n - i - 1 , i ) ) ; } } return len ; } static public void Main ( ) { int [ ] a = { 1 , 2 , 3 , 2 } ; int n = a . Length ; Console . WriteLine ( maxLength ( a , n ) ) ; } }


"Maximum length palindrome that can be created with characters in range L and R | C # implementation of the approach ; Function to return the length of the longest palindrome that can be formed using the characters in the range [ l , r ] ; 0 - based indexing ; Marks if there is an odd frequency character ; Length of the longest palindrome possible from characters in range ; Traverse for all characters and count their frequencies ; Find the frequency in range 1 - r ; Exclude the frequencies in range 1 - ( l - 1 ) ; If frequency is odd , then add 1 less than the original frequency to make it even ; Else completely add if even ; If any odd frequency character is present then add 1 ; Function to pre - calculate the frequencies of the characters to reduce complexity ; Iterate and increase the count ; Create a prefix type array ; Driver code ; Pre - calculate prefix array ; Perform queries"
Solution in C#:

using System ; class GFG { static int N = 4 ; static int performQueries ( int l , int r , int [ , ] prefix ) { l -- ; r -- ; bool flag = false ; int count = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { int cnt = prefix [ r , i ] ; if ( l > 0 ) cnt -= prefix [ l - 1 , i ] ; if ( cnt % 2 == 1 ) { flag = true ; count += cnt - 1 ; } else count += cnt ; } if ( flag ) count += 1 ; return count ; } static void preCalculate ( string s , int [ , ] prefix ) { int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) { prefix [ i , s [ i ] - ' a ' ] ++ ; } for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < 26 ; j ++ ) prefix [ i , j ] += prefix [ i - 1 , j ] ; } } public static void Main ( ) { string s = " amim " ; int [ , ] prefix = new int [ N , 26 ] ; preCalculate ( s , prefix ) ; int [ , ] queries = { { 1 , 4 } , { 3 , 4 } } ; int q = queries . Length ; for ( int i = 0 ; i < q ; i ++ ) { Console . WriteLine ( performQueries ( queries [ i , 0 ] , queries [ i , 1 ] , prefix ) ) ; } } }


"Maximum length palindromic substring for every index such that it starts and ends at that index | C # program for the above approach ; Function to return true if S [ i ... j ] is a palindrome ; Iterate until i < j ; If unequal character encountered ; Otherwise ; Function to find for every index , longest palindromic substrings starting or ending at that index ; Stores the maximum palindromic substring length for each index ; Traverse the string ; Stores the maximum length of palindromic substring ; Consider that palindromic substring ends at index i ; If current character is a valid starting index ; If S [ i , j ] is palindrome , ; Update the length of the longest palindrome ; Consider that palindromic substring starts at index i ; If current character is a valid ending index ; If str [ i , j ] is palindrome ; Update the length of the longest palindrome ; Update length of the longest palindromic substring for index i ; Print the length of the longest palindromic substring ; Driver Code"
Solution in C#:

using System ; class GFG { static bool isPalindrome ( string S , int i , int j ) { while ( i < j ) { if ( S [ i ] != S [ j ] ) return false ; i ++ ; j -- ; } return true ; } static void printLongestPalindrome ( string S , int N ) { int [ ] palLength = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { int maxlength = 1 ; for ( int j = 0 ; j < i ; j ++ ) { if ( S [ j ] == S [ i ] ) { if ( ( isPalindrome ( S , j , i ) ) != false ) { maxlength = i - j + 1 ; break ; } } } for ( int j = N - 1 ; j > i ; j -- ) { if ( S [ j ] == S [ i ] ) { if ( isPalindrome ( S , i , j ) ) { maxlength = Math . Max ( j - i + 1 , maxlength ) ; break ; } } } palLength [ i ] = maxlength ; } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( palLength [ i ] + " ▁ " ) ; } } static public void Main ( ) { string S = " bababa " ; int N = S . Length ; printLongestPalindrome ( S , N ) ; } }


"Maximum length palindromic substring such that it starts and ends with given char | C # implementation of the approach ; Function that returns true if str [ i ... j ] is a palindrome ; Function to return the length of the longest palindromic sub - string such that it starts and ends with the character ch ; If current character is a valid starting index ; Instead of finding the ending index from the beginning , find the index from the end This is because if the current sub - string is a palindrome then there is no need to check the sub - strings of smaller length and we can skip to the next iteration of the outer loop ; If current character is a valid ending index ; If str [ i ... j ] is a palindrome then update the length of the maximum palindrome so far ; Driver code"
Solution in C#:

using System ; class GFG { static bool isPalindrome ( string str , int i , int j ) { while ( i < j ) { if ( str [ i ] != str [ j ] ) { return false ; } i ++ ; j -- ; } return true ; } static int maxLenPalindrome ( string str , int n , char ch ) { int maxLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ch ) { for ( int j = n - 1 ; j >= i ; j -- ) { if ( str [ j ] == ch ) { if ( isPalindrome ( str , i , j ) ) { maxLen = Math . Max ( maxLen , j - i + 1 ) ; break ; } } } } } return maxLen ; } public static void Main ( ) { string str = " lapqooqpqpl " ; int n = str . Length ; char ch = ' p ' ; Console . WriteLine ( maxLenPalindrome ( str , n , ch ) ) ; } }


"Maximum length possible by cutting N given woods into at least K pieces | C # program to implement the above approach ; Function to check if it is possible to cut woods into K pieces of length len ; Stores count of pieces having length equal to K ; Traverse wood [ ] array ; Update count ; Function to find the maximum value of L ; Stores minimum possible of L ; Stores maximum possible value of L ; Apply binary search over the range [ left , right ] ; Stores mid value of left and right ; If it is possible to cut woods into K pieces having length of each piece equal to mid ; Update left ; Update right ; Driver Code"
Solution in C#:

using System ; using System . Linq ; class GFG { static bool isValid ( int [ ] wood , int N , int len , int K ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { count += wood [ i ] / len ; } return count >= K ; } static int findMaxLen ( int [ ] wood , int N , int K ) { int left = 1 ; int right = wood . Max ( ) ; while ( left <= right ) { int mid = left + ( right - left ) / 2 ; if ( isValid ( wood , N , mid , K ) ) { left = mid + 1 ; } else { right = mid - 1 ; } } return right ; } public static void Main ( String [ ] args ) { int [ ] wood = { 5 , 9 , 7 } ; int N = wood . Length ; int K = 4 ; Console . Write ( findMaxLen ( wood , N , K ) ) ; } }


"Maximum length prefix of one string that occurs as subsequence in another | C # program to find maximum length prefix of one string occur as subsequence in another string . ; Return the maximum length prefix which is subsequence . ; Iterating string T . ; If end of string S . ; If character match , increment counter . ; Driver Code"
Solution in C#:

using System ; class GFG { static int maxPrefix ( String s , String t ) { int count = 0 ; for ( int i = 0 ; i < t . Length ; i ++ ) { if ( count == s . Length ) break ; if ( t [ i ] == s [ count ] ) count ++ ; } return count ; } public static void Main ( ) { String S = " digger " ; String T = " biggerdiagram " ; Console . Write ( maxPrefix ( S , T ) ) ; } }


"Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency | C # implementation to find the prefix of the s such that occurrence of each character is atmost the count of minimum frequency in the s ; Function to find the maximum possible prefix of the s ; Hash map to store the frequency of the characters in the s ; Iterate over the s to find the occurence of each Character ; Minimum frequency of the Characters ; Loop to find the count of minimum frequency in the hash - map ; Loop to find the maximum possible length of the prefix in the s ; Condition to check if the frequency is greater than minimum possible freq ; maxprefix s and its length . ; Driver Code ; s is initialize . ; str is passed in MaxPrefix function ."
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; using System . Linq ; class GFG { static void MaxPrefix ( string s ) { Dictionary < char , int > Dict = new Dictionary < char , int > ( ) ; foreach ( char i in s ) { if ( Dict . ContainsKey ( i ) ) { Dict [ i ] ++ ; } else { Dict [ i ] = 1 ; } } int minfrequency = Int32 . MaxValue ; foreach ( int x in Dict . Values . ToList ( ) ) { minfrequency = Math . Min ( minfrequency , x ) ; } int countminFrequency = 0 ; foreach ( char x in Dict . Keys . ToList ( ) ) { if ( Dict [ x ] == minfrequency ) countminFrequency += 1 ; } Dictionary < char , int > mapper = new Dictionary < char , int > ( ) ; int indi = 0 ; foreach ( char i in s ) { if ( mapper . ContainsKey ( i ) ) { mapper [ i ] ++ ; } else { mapper [ i ] = 1 ; } if ( mapper [ i ] > countminFrequency ) break ; indi += 1 ; } Console . Write ( s . Substring ( 0 , indi ) ) ; } public static void Main ( string [ ] args ) { string str = " aabcdaab " ; MaxPrefix ( str ) ; } }


"Maximum length sub | C # implementation of the approach ; Function to return the maximum length of the required sub - array ; For the first consecutive pair of elements ; While a consecutive pair can be selected ; If current pair forms a valid sub - array ; 2 is the length of the current sub - array ; To extend the sub - array both ways ; While elements at indices l and r are part of a valid sub - array ; Update the maximum length so far ; Select the next consecutive pair ; Return the maximum length ; Driver code"
Solution in C#:

using System ; class GFG { static int maxLength ( int [ ] arr , int n ) { int maxLen = 0 ; int i = 0 ; int j = i + 1 ; while ( j < n ) { if ( arr [ i ] != arr [ j ] ) { maxLen = Math . Max ( maxLen , 2 ) ; int l = i - 1 ; int r = j + 1 ; while ( l >= 0 && r < n && arr [ l ] == arr [ i ] && arr [ r ] == arr [ j ] ) { l -- ; r ++ ; } maxLen = Math . Max ( maxLen , 2 * ( r - j ) ) ; } i ++ ; j = i + 1 ; } return maxLen ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 1 , 0 , 0 , 1 , 1 } ; int n = arr . Length ; Console . WriteLine ( maxLength ( arr , n ) ) ; } }


"Maximum length sub | C # implementation of the approach ; Function to return the maximum length of the required sub - array ; To store the maximum length for a valid subarray ; To store the count of contiguous similar elements for previous group and the current group ; If current element is equal to the previous element then it is a part of the same group ; Else update the previous group and start counting elements for the new group ; Update the maximum possible length for a group ; Return the maximum length of the valid subarray ; Driver code"
Solution in C#:

using System ; class GFG { static int maxLength ( int [ ] a , int n ) { int maxLen = 0 ; int prev_cnt = 0 , curr_cnt = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] == a [ i - 1 ] ) curr_cnt ++ ; else { prev_cnt = curr_cnt ; curr_cnt = 1 ; } maxLen = Math . Max ( maxLen , Math . Min ( prev_cnt , curr_cnt ) ) ; } return ( 2 * maxLen ) ; } public static void Main ( ) { int [ ] arr = { 1 , 1 , 1 , 0 , 0 , 1 , 1 } ; int n = arr . Length ; Console . WriteLine ( maxLength ( arr , n ) ) ; } }


"Maximum length subsequence possible of the form R ^ N K ^ N | C # program to find the maximum length of a substring of form R ^ nK ^ n ; function to calculate the maximum length of substring of the form R ^ nK ^ n ; Count no . Of R 's before a K ; Count no . Of K 's after a K ; Update maximum length ; Driver code"
Solution in C#:

using System ; class GFG { static int find ( String s ) { int max = 0 , i , j = 0 , countk = 0 , countr = 0 ; int [ , ] table = new int [ s . Length , 2 ] ; for ( i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == ' R ' ) countr ++ ; else table [ ( j ++ ) , 0 ] = countr ; } j -- ; for ( i = s . Length - 1 ; i >= 0 ; i -- ) { if ( s [ i ] == ' K ' ) { countk ++ ; table [ j -- , 1 ] = countk ; } if ( Math . Min ( table [ j + 1 , 0 ] , table [ j + 1 , 1 ] ) > max ) max = Math . Min ( table [ j + 1 , 0 ] , table [ j + 1 , 1 ] ) ; } return max ; } static public void Main ( String [ ] srgs ) { String s = " RKRRRKKRRKKKKRR " ; int n = find ( s ) ; Console . WriteLine ( n ) ; } }


"Maximum length subsequence such that adjacent elements in the subsequence have a common factor | C # implementation of the above approach ; to compute least prime divisor of i ; Function that returns the maximum length subsequence such that adjacent elements have a common factor . ; Initialize dp array with 1. ; p has appeared at least once . ; Update latest occurrence of prime p . ; Take maximum value as the answer . ; Driver code"
Solution in C#:

using System ; using System . Collections ; class GFG { static int N = 100005 ; static int MAX = 10000002 ; static int [ ] lpd = new int [ MAX ] ; static void preCompute ( ) { lpd [ 0 ] = lpd [ 1 ] = 1 ; for ( int i = 2 ; i * i < MAX ; i ++ ) { for ( int j = i * 2 ; j < MAX ; j += i ) { if ( lpd [ j ] == 0 ) { lpd [ j ] = i ; } } } for ( int i = 2 ; i < MAX ; i ++ ) { if ( lpd [ i ] == 0 ) { lpd [ i ] = i ; } } } static int maxLengthSubsequence ( int [ ] arr , int n ) { int [ ] dp = new int [ N ] ; Hashtable pos = new Hashtable ( ) ; for ( int i = 0 ; i <= n ; i ++ ) dp [ i ] = 1 ; for ( int i = 0 ; i <= n ; i ++ ) { while ( arr [ i ] > 1 ) { int p = lpd [ arr [ i ] ] ; if ( pos . ContainsKey ( p ) ) { dp [ i ] = Math . Max ( dp [ i ] , 1 + dp [ Convert . ToInt32 ( pos [ p ] ) ] ) ; } pos [ p ] = i ; while ( arr [ i ] % p == 0 ) arr [ i ] /= p ; } } int ans = 1 ; for ( int i = 0 ; i <= n ; i ++ ) { ans = Math . Max ( ans , dp [ i ] ) ; } return ans ; } public static void Main ( ) { int [ ] arr = { 13 , 2 , 8 , 6 , 3 , 1 , 9 } ; int n = arr . Length - 1 ; preCompute ( ) ; Console . WriteLine ( maxLengthSubsequence ( arr , n ) ) ; } }


"Maximum length subsequence with difference between adjacent elements as either 0 or 1 | C # Code for Maximum length subsequence with difference between adjacent elements as either 0 or 1 ; function to find maximum length subsequence with difference between adjacent elements as either 0 or 1 ; Initialize mls [ ] values for all indexes ; Compute optimized maximum length subsequence values in bottom up manner ; Store maximum of all ' mls ' values in ' max ' ; required maximum length subsequence ; Driver program to test above function"
Solution in C#:

using System ; class GFG { public static int maxLenSub ( int [ ] arr , int n ) { int [ ] mls = new int [ n ] ; int max = 0 ; for ( int i = 0 ; i < n ; i ++ ) mls [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) if ( Math . Abs ( arr [ i ] - arr [ j ] ) <= 1 && mls [ i ] < mls [ j ] + 1 ) mls [ i ] = mls [ j ] + 1 ; for ( int i = 0 ; i < n ; i ++ ) if ( max < mls [ i ] ) max = mls [ i ] ; return max ; } public static void Main ( ) { int [ ] arr = { 2 , 5 , 6 , 3 , 7 , 6 , 5 , 8 } ; int n = arr . Length ; Console . Write ( " Maximum ▁ length ▁ subsequence ▁ = ▁ " + maxLenSub ( arr , n ) ) ; } }


"Maximum length substring having all same characters after k changes | C # program to find maximum length equal character string with k changes ; method to find the maximum length of substring having character ch ; traverse the whole string ; if character is not same as ch increase count ; While count > k traverse the string again until count becomes less than k and decrease the count when characters are not same ; length of substring will be rightIndex - leftIndex + 1. Compare this with the maximum length and return maximum length ; method which returns maximum length of substring ; Driver Method"
Solution in C#:

using System ; class GFG { public static int findLen ( string A , int n , int k , char ch ) { int maxlen = 1 ; int cnt = 0 ; int l = 0 , r = 0 ; while ( r < n ) { if ( A [ r ] != ch ) { ++ cnt ; } while ( cnt > k ) { if ( A [ l ] != ch ) { -- cnt ; } ++ l ; } maxlen = Math . Max ( maxlen , r - l + 1 ) ; ++ r ; } return maxlen ; } public static int answer ( string A , int n , int k ) { int maxlen = 1 ; for ( int i = 0 ; i < 26 ; ++ i ) { maxlen = Math . Max ( maxlen , findLen ( A , n , k , ( char ) ( i + ' A ' ) ) ) ; maxlen = Math . Max ( maxlen , findLen ( A , n , k , ( char ) ( i + ' a ' ) ) ) ; } return maxlen ; } public static void Main ( string [ ] args ) { int n = 5 , k = 2 ; string A = " ABABA " ; Console . WriteLine ( " Maximum ▁ length ▁ = ▁ " + answer ( A , n , k ) ) ; n = 6 ; k = 4 ; string B = " HHHHHH " ; Console . WriteLine ( " Maximum ▁ length ▁ = ▁ " + answer ( B , n , k ) ) ; } }


"Maximum length substring with highest frequency in a string | C # program to find maximum occurred substring of a string ; Function to return maximum occurred substring of a string ; Size of the string ; To store maximum frequency ; To store string which has maximum frequency ; Return substring which has maximum frequency ; Driver Code ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static string MaxFreq ( string str ) { int n = str . Length ; Dictionary < string , int > m = new Dictionary < string , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { string sp = " " ; for ( int j = i ; j < n ; j ++ ) { sp += str [ j ] ; if ( m . ContainsKey ( sp ) ) { m [ sp ] ++ ; } else { m [ sp ] = 1 ; } } } int maxi = 0 ; string s = " " ; foreach ( KeyValuePair < string , int > i in m ) { if ( i . Value > maxi ) { maxi = i . Value ; s = i . Key ; } else if ( i . Value == maxi ) { string ss = i . Key ; if ( ss . Length > s . Length ) s = ss ; } } return s ; } public static void Main ( string [ ] args ) { string str = " ababecdecd " ; Console . Write ( MaxFreq ( str ) ) ; } }


"Maximum litres of water that can be bought with N Rupees | C # implementation of the above approach ; if buying glass bottles is profitable ; Glass bottles that can be bought ; Change budget according the bought bottles ; Plastic bottles that can be bought ; if only plastic bottles need to be bought ; Driver Code"
Solution in C#:

using System ; class GFG { static void maxLitres ( int budget , int plastic , int glass , int refund ) { if ( glass - refund < plastic ) { int ans = Math . Max ( ( budget - refund ) / ( glass - refund ) , 0 ) ; budget -= ans * ( glass - refund ) ; ans += budget / plastic ; Console . WriteLine ( ans ) ; } else { Console . WriteLine ( ( budget / plastic ) ) ; } } public static void Main ( String [ ] args ) { int budget = 10 , plastic = 11 , glass = 9 , refund = 8 ; maxLitres ( budget , plastic , glass , refund ) ; } }


"Maximum mirrors which can transfer light from bottom to right | C # program to find how many mirror can transfer light from bottom to right ; method returns number of mirror which can transfer light from bottom to right ; To store first obstacles horizontaly ( from right ) and vertically ( from bottom ) ; initialize both array as - 1 , signifying no obstacle ; looping matrix to mark column for obstacles ; mark rightmost column with obstacle ; looping matrix to mark rows for obstacles ; mark leftmost row with obstacle ; Initialize result ; if there is not obstacle on right or below , then mirror can be placed to transfer light ; if i > vertical [ j ] then light can from bottom if j > horizontal [ i ] then light can go to right ; uncomment this code to print actual mirror position also cout << i << " ▁ " << j << endl ; ; Driver code ; B - Blank O - Obstacle"
Solution in C#:

using System ; class GFG { static int maximumMirrorInMatrix ( String [ ] mat , int N ) { int [ ] horizontal = new int [ N ] ; int [ ] vertical = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { horizontal [ i ] = - 1 ; vertical [ i ] = - 1 ; } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = N - 1 ; j >= 0 ; j -- ) { if ( mat [ i ] [ j ] == ' B ' ) { continue ; } horizontal [ i ] = j ; break ; } } for ( int j = 0 ; j < N ; j ++ ) { for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( mat [ i ] [ j ] == ' B ' ) { continue ; } vertical [ j ] = i ; break ; } } int res = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( i > vertical [ j ] && j > horizontal [ i ] ) { res ++ ; } } } return res ; } public static void Main ( String [ ] args ) { int N = 5 ; String [ ] mat = { " BBOBB " , " BBBBO " , " BBBBB " , " BOOBO " , " BBBOB " } ; Console . WriteLine ( maximumMirrorInMatrix ( mat , N ) ) ; } }


"Maximum modified Array sum possible by choosing elements as per the given conditions | C # program for the above approach ; Function that finds the maximum sum of the array elements according to the given condition ; Sort the array ; Take the max value from the array ; Iterate from the end of the array ; Check for the number had come before or not ; Take the value which is not occured already ; Change max to new value ; Return the maximum sum ; Driver Code ; Given array [ ] arr ; Function Call"
Solution in C#:

using System ; class GFG { static int findMaxValue ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int ans = arr [ n - 1 ] ; int maxPossible = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; -- i ) { if ( maxPossible > 0 ) { if ( arr [ i ] >= maxPossible ) { ans += ( maxPossible - 1 ) ; maxPossible = maxPossible - 1 ; } else { maxPossible = arr [ i ] ; ans += maxPossible ; } } } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 4 , 1 , 5 } ; int n = arr . Length ; Console . Write ( findMaxValue ( arr , n ) ) ; } }


"Maximum money that can be collected by both the players in a game of removal of coins | C # program for the above approach ; Function to find the maximum score obtained by the players A and B ; Sort the array in descending order ; Stores the maximum amount of money obtained by A ; Stores the maximum amount of money obtained by B ; If the value of N is 1 ; Update the amountA ; Print the amount of money obtained by both players ; Update the amountA ; Update the amountB ; Traverse the array arr [ ] ; If i is an odd number ; Update the amountB ; Update the amountA ; Print the amount of money obtained by both the players ; Driver Code"
Solution in C#:

using System ; class GFG { static void findAmountPlayers ( int [ ] arr , int N ) { Array . Sort ( arr ) ; Array . Reverse ( arr ) ; int amountA = 0 ; int amountB = 0 ; if ( N == 1 ) { amountA += arr [ 0 ] ; Console . WriteLine ( " ( A ▁ : ▁ " + amountA + " ) , ▁ " + " ( B ▁ : ▁ " + amountB + " ) " ) ; return ; } amountA = arr [ 0 ] ; amountB = arr [ 1 ] ; for ( int i = 2 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { amountB += arr [ i ] ; } else { amountA += arr [ i ] ; } } Console . WriteLine ( " ( A ▁ : ▁ " + amountA + " ) " ) ; Console . WriteLine ( " ( B ▁ : ▁ " + amountB + " ) " ) ; } public static void Main ( ) { int [ ] arr = { 1 , 1 , 1 } ; int N = arr . Length ; findAmountPlayers ( arr , N ) ; } }


"Maximum money that can be withdrawn in two steps | C # implementation of the approach ; Function to return the maximum coins we can get ; Update elements such that X > Y ; Take from the maximum ; Refill ; Again , take the maximum ; Driver code"
Solution in C#:

using System ; class GFG { static int maxCoins ( int X , int Y ) { if ( X < Y ) { swap ( X , Y ) ; } int coins = X ; X -- ; coins += Math . Max ( X , Y ) ; return coins ; } static void swap ( int X , int Y ) { int temp = X ; X = Y ; Y = temp ; } public static void Main ( String [ ] args ) { int X = 7 , Y = 5 ; Console . WriteLine ( maxCoins ( X , Y ) ) ; } }


"Maximum neighbor element in a matrix within distance K | C # implementation to find the maximum neighbor element within the distance of less than K ; Function to print the matrix ; Loop to iterate over the matrix ; Function to find the maximum neighbor within the distance of less than equal to K ; Loop to find the maximum element within the distance of less than K ; Driver Code"
Solution in C#:

 using System ; class GFG { static void printMatrix ( int [ , ] A ) { for ( int i = 0 ; i < A . GetLength ( 0 ) ; i ++ ) { for ( int j = 0 ; j < A . GetLength ( 1 ) ; j ++ ) Console . Write ( A [ i , j ] + " ▁ " ) ; Console . Write ( '
 ' ) ; } } static int [ , ] getMaxNeighbour ( int [ , ] A , int K ) { int [ , ] ans = A ; for ( int q = 1 ; q <= K ; q ++ ) { for ( int i = 0 ; i < A . GetLength ( 0 ) ; i ++ ) { for ( int j = 0 ; j < A . GetLength ( 1 ) ; j ++ ) { int maxi = ans [ i , j ] ; if ( i > 0 ) maxi = Math . Max ( maxi , ans [ i - 1 , j ] ) ; if ( j > 0 ) maxi = Math . Max ( maxi , ans [ i , j - 1 ] ) ; if ( i < A . GetLength ( 0 ) - 1 ) maxi = Math . Max ( maxi , ans [ i + 1 , j ] ) ; if ( j < A . GetLength ( 0 ) - 1 ) maxi = Math . Max ( maxi , ans [ i , j + 1 ] ) ; A [ i , j ] = maxi ; } } ans = A ; } return ans ; } public static void Main ( String [ ] args ) { int [ , ] B = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; printMatrix ( getMaxNeighbour ( B , 2 ) ) ; } }


"Maximum non | C # program for the above approach ; Function to find maximum distinct character after removing K character ; Freq implemented as hash table to store frequency of each character ; Store frequency of each character ; Insert each frequency in v ; Sort the freq of character in non - decreasing order ; Traverse the vector ; Update v [ i ] and k ; If K is still not 0 ; Store the readonly answer ; Count this character if freq 1 ; Return count of distinct characters ; Driver Code ; Given String ; Given k ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int maxDistinctChar ( char [ ] s , int n , int k ) { Dictionary < int , int > freq = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq . ContainsKey ( ( int ) s [ i ] ) ) { freq [ ( int ) s [ i ] ] = freq [ ( int ) s [ i ] ] + 1 ; } else { freq . Add ( ( int ) s [ i ] , 1 ) ; } } List < int > v = new List < int > ( ) ; foreach ( KeyValuePair < int , int > it in freq ) { v . Add ( it . Value ) ; } v . Sort ( ) ; for ( int i = 0 ; i < v . Count ; i ++ ) { int mn = Math . Min ( v [ i ] - 1 , k ) ; v [ i ] = v [ i ] - mn ; k -= mn ; } if ( k > 0 ) { for ( int i = 0 ; i < v . Count ; i ++ ) { int mn = Math . Min ( v [ i ] , k ) ; v [ i ] = v [ i ] - mn ; k -= mn ; } } int res = 0 ; for ( int i = 0 ; i < v . Count ; i ++ ) if ( v [ i ] == 1 ) res ++ ; return res ; } public static void Main ( String [ ] args ) { String S = " geeksforgeeks " ; int N = S . Length ; int K = 1 ; Console . Write ( maxDistinctChar ( S . ToCharArray ( ) , N , K ) ) ; } }


"Maximum non | C # program for the above approach ; Function to find the maximum product from the top left and bottom right cell of the given matrix grid [ ] [ ] ; Store dimension of grid ; Stores maximum product path ; Stores minimum product path ; Traverse the grid and update maxPath and minPath array ; Initialize to inf and - inf ; Base Case ; Calculate for row : ; Update the maximum ; Update the minimum ; Calculate for column ; Update the maximum ; Update the minimum ; Update maxPath and minPath ; If negative product ; Otherwise ; Driver Code ; Given matrix mat [ ] [ ] ; Function Call"
Solution in C#:

using System ; class GFG { static int maxProductPath ( int [ , ] grid ) { int n = grid . GetLength ( 0 ) ; int m = grid . GetLength ( 1 ) ; int [ , ] maxPath = new int [ n , m ] ; int [ , ] minPath = new int [ n , m ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { int mn = Int32 . MaxValue ; int mx = Int32 . MinValue ; if ( i == 0 && j == 0 ) { mx = grid [ i , j ] ; mn = grid [ i , j ] ; } if ( i > 0 ) { int tempmx = Math . Max ( ( maxPath [ i - 1 , j ] * grid [ i , j ] ) , ( minPath [ i - 1 , j ] * grid [ i , j ] ) ) ; mx = Math . Max ( mx , tempmx ) ; int tempmn = Math . Min ( ( maxPath [ i - 1 , j ] * grid [ i , j ] ) , ( minPath [ i - 1 , j ] * grid [ i , j ] ) ) ; mn = Math . Min ( mn , tempmn ) ; } if ( j > 0 ) { int tempmx = Math . Max ( ( maxPath [ i , j - 1 ] * grid [ i , j ] ) , ( minPath [ i , j - 1 ] * grid [ i , j ] ) ) ; mx = Math . Max ( mx , tempmx ) ; int tempmn = Math . Min ( ( maxPath [ i , j - 1 ] * grid [ i , j ] ) , ( minPath [ i , j - 1 ] * grid [ i , j ] ) ) ; mn = Math . Min ( mn , tempmn ) ; } maxPath [ i , j ] = mx ; minPath [ i , j ] = mn ; } } if ( maxPath [ n - 1 , m - 1 ] < 0 ) { return - 1 ; } else { return ( maxPath [ n - 1 , m - 1 ] ) ; } } static public void Main ( ) { int [ , ] mat = { { 1 , - 2 , 1 } , { 1 , - 2 , 1 } , { 3 , - 4 , 1 } } ; Console . WriteLine ( maxProductPath ( mat ) ) ; } }


"Maximum non | C # program for the above approach ; Function to print the maximum rooks and their positions ; Initialize row and col array ; Marking the location of already placed rooks ; Print number of non - attacking rooks that can be placed ; To store the placed rook location ; Print lexographically smallest order ; Driver Code ; Size of board ; Number of rooks already placed ; Position of rooks ; Function call"
Solution in C#:

using System ; class GFG { private static void countRooks ( int n , int k , int [ , ] pos ) { int [ ] row = new int [ n ] ; int [ ] col = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { row [ i ] = 0 ; col [ i ] = 0 ; } for ( int i = 0 ; i < k ; i ++ ) { row [ pos [ i , 0 ] - 1 ] = 1 ; col [ pos [ i , 1 ] - 1 ] = 1 ; } int res = n - k ; Console . WriteLine ( res + " ▁ " ) ; int ri = 0 , ci = 0 ; while ( res -- > 0 ) { while ( row [ ri ] == 1 ) { ri ++ ; } while ( col [ ci ] == 1 ) { ci ++ ; } Console . WriteLine ( ( ri + 1 ) + " ▁ " + ( ci + 1 ) + " ▁ " ) ; ri ++ ; ci ++ ; } } public static void Main ( String [ ] args ) { int N = 4 ; int K = 2 ; int [ , ] pos = { { 1 , 4 } , { 2 , 2 } } ; countRooks ( N , K , pos ) ; } }


"Maximum number by concatenating every element in a rotation of an array | C # program to print the Maximum number by concatenating every element in rotation of array ; Function to print the largest number ; store the index of largest left most digit of elements ; Iterate for all numbers ; check for the last digit ; check for the largest left most digit ; print the rotation of array ; print the rotation of array ; Driver Code"
Solution in C#:

using System ; class GFG { static void printLargest ( int [ ] a , int n ) { int max = - 1 ; int ind = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { int num = a [ i ] ; while ( num > 0 ) { int r = num % 10 ; num = num / 10 ; if ( num == 0 ) { if ( max < r ) { max = r ; ind = i ; } } } } for ( int i = ind ; i < n ; i ++ ) Console . Write ( a [ i ] ) ; for ( int i = 0 ; i < ind ; i ++ ) Console . Write ( a [ i ] ) ; } public static void Main ( ) { int [ ] a = { 54 , 546 , 548 , 60 } ; int n = 4 ; printLargest ( a , n ) ; } }


"Maximum number formed from the digits of given three numbers | C # program for the above approach ; Function to find the maximum number formed by taking the maximum digit at the same position from each number ; Stores the result ; Stores the position value of a digit ; Stores the digit at the unit place ; Stores the digit at the unit place ; Stores the digit at the unit place ; Update A , B and C ; Stores the maximum digit ; Increment ans cur * a ; Update cur ; Return ans ; Driver Code ; Given Input ; Function call"
Solution in C#:

using System ; class GFG { static int findkey ( int A , int B , int C ) { int ans = 0 ; int cur = 1 ; while ( A > 0 ) { int a = A % 10 ; int b = B % 10 ; int c = C % 10 ; A = A / 10 ; B = B / 10 ; C = C / 10 ; int m = Math . Max ( a , Math . Max ( c , b ) ) ; ans += cur * m ; cur = cur * 10 ; } return ans ; } static public void Main ( ) { int A = 3521 , B = 2452 , C = 1352 ; Console . Write ( findkey ( A , B , C ) ) ; } }


"Maximum number made up of distinct digits whose sum is equal to N | C # program for the above approach ; Function to find the largest positive number made up of distinct digits having the sum of its digits as N ; If given number is greater than 45 , print - 1 ; Store the required number and the digit to be considered ; Loop until N > 0 and digit > 0 ; If the current digit is at most N then , add it to number num ; Update the value of num ; Decrement N by digit ; Consider the next lower digit ; Add 0 at the end and return the number num ; Driver Code"
Solution in C#:

using System ; class GFG { static long largestNumber ( int N ) { if ( N > 45 ) return - 1 ; int num = 0 , digit = 9 ; while ( N > 0 && digit > 0 ) { if ( digit <= N ) { num *= 10 ; num += digit ; N -= digit ; } digit -= 1 ; } return num * 10 ; } public static void Main ( ) { int N = 25 ; Console . Write ( largestNumber ( N ) ) ; } }


"Maximum number of 0 s placed consecutively at the start and end in any rotation of a Binary String | C # program for the above approach ; Function to find the maximum sum of consecutive 0 s present at the start and end of a string present in any of the rotations of the given string ; Check if all the characters in the string are 0 ; Iterate over characters of the string ; If the frequency of '1' is 0 ; Print n as the result ; Concatenate the string with itself ; Stores the required result ; Generate all rotations of the string ; Store the number of consecutive 0 s at the start and end of the string ; Count 0 s present at the start ; Count 0 s present at the end ; Calculate the sum ; Update the overall maximum sum ; Print the result ; Driver Code ; Given string ; Store the size of the string"
Solution in C#:

using System ; class GFG { static void findMaximumZeros ( string str , int n ) { int c0 = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( str [ i ] == '0' ) c0 ++ ; } if ( c0 == n ) { Console . Write ( n ) ; return ; } string s = str + str ; int mx = 0 ; for ( int i = 0 ; i < n ; ++ i ) { int cs = 0 ; int ce = 0 ; for ( int j = i ; j < i + n ; ++ j ) { if ( s [ j ] == '0' ) cs ++ ; else break ; } for ( int j = i + n - 1 ; j >= i ; -- j ) { if ( s [ j ] == '0' ) ce ++ ; else break ; } int val = cs + ce ; mx = Math . Max ( val , mx ) ; } Console . Write ( mx ) ; } public static void Main ( string [ ] args ) { string s = "1001" ; int n = s . Length ; findMaximumZeros ( s , n ) ; } }


"Maximum number of 0 s placed consecutively at the start and end in any rotation of a Binary String | C # program for the above approach ; Function to find the maximum sum of consecutive 0 s present at the start and end of any rotation of the string str ; Stores the count of 0 s ; If the frequency of '1' is 0 ; Print n as the result ; Stores the required sum ; Find the maximum consecutive length of 0 s present in the string ; Update the overall maximum sum ; Find the number of 0 s present at the start and end of the string ; Update the count of 0 s at the start ; Update the count of 0 s at the end ; Update the maximum sum ; Print the result ; Driver Code ; Given string ; Store the size of the string"
Solution in C#:

using System ; class GFG { static void findMaximumZeros ( string str , int n ) { int c0 = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( str [ i ] == '0' ) c0 ++ ; } if ( c0 == n ) { Console . Write ( n ) ; return ; } int mx = 0 ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == '0' ) cnt ++ ; else { mx = Math . Max ( mx , cnt ) ; cnt = 0 ; } } mx = Math . Max ( mx , cnt ) ; int start = 0 , end = n - 1 ; cnt = 0 ; while ( str [ start ] != '1' && start < n ) { cnt ++ ; start ++ ; } while ( str [ end ] != '1' && end >= 0 ) { cnt ++ ; end -- ; } mx = Math . Max ( mx , cnt ) ; Console . Write ( mx ) ; } static public void Main ( ) { string s = "1001" ; int n = s . Length ; findMaximumZeros ( s , n ) ; } }


"Maximum number of 0 s that can be flipped such that Array has no adjacent 1 s | C # program for the above approach ; Maximum number of 0 s that can be replaced by 1 ; Check for three consecutive 0 s ; Flip the bit ; Increase the count ; Driver code"
Solution in C#:

using System ; class GFG { public static int canReplace ( int [ ] array ) { int i = 0 , count = 0 ; while ( i < array . Length ) { if ( array [ i ] == 0 && ( i == 0 array [ i - 1 ] == 0 ) && ( i == array . Length - 1 array [ i + 1 ] == 0 ) ) { array [ i ] = 1 ; count ++ ; } i ++ ; } return count ; } public static void Main ( String [ ] args ) { int [ ] array = { 1 , 0 , 0 , 0 , 1 } ; Console . WriteLine ( canReplace ( array ) ) ; } }


"Maximum number of Armstrong Numbers present in a subarray of size K | C # program for above approach ; Function to calculate the value of x raised to the power y in O ( log y ) ; Base Case ; If the power y is even ; Otherwise ; Function to calculate the order of the number , i . e . count of digits ; Stores the total count of digits ; Iterate until num is 0 ; Function to check a number is an Armstrong Number or not ; Find the order of the number ; Check for Armstrong Number ; If Armstrong number condition is satisfied ; Utility function to find the maximum sum of a subarray of size K ; If k is greater than N ; Find the sum of first subarray of size K ; Find the sum of the remaining subarray ; Return the maximum sum of subarray of size K ; Function to find all the Armstrong Numbers in the array ; Traverse the array arr [ ] ; If arr [ i ] is an Armstrong Number , then replace it by 1. Otherwise , 0 ; Return the resultant count ; Driver Code"
Solution in C#:

using System ; class GFG { static int power ( int x , int y ) { if ( y == 0 ) return 1 ; if ( y % 2 == 0 ) return power ( x , y / 2 ) * power ( x , y / 2 ) ; return x * power ( x , y / 2 ) * power ( x , y / 2 ) ; } static int order ( int num ) { int count = 0 ; while ( num > 0 ) { count ++ ; num = num / 10 ; } return count ; } static int isArmstrong ( int N ) { int r = order ( N ) ; int temp = N , sum = 0 ; while ( temp > 0 ) { int d = temp % 10 ; sum += power ( d , r ) ; temp = temp / 10 ; } if ( sum == N ) return 1 ; return 0 ; } static int maxSum ( int [ ] arr , int N , int K ) { if ( N < K ) { return - 1 ; } int res = 0 ; for ( int i = 0 ; i < K ; i ++ ) { res += arr [ i ] ; } int curr_sum = res ; for ( int i = K ; i < N ; i ++ ) { curr_sum += arr [ i ] - arr [ i - K ] ; res = Math . Max ( res , curr_sum ) ; } return res ; } static int maxArmstrong ( int [ ] arr , int N , int K ) { for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] = isArmstrong ( arr [ i ] ) ; } return maxSum ( arr , N , K ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 28 , 2 , 3 , 6 , 153 , 99 , 828 , 24 } ; int K = 6 ; int N = arr . Length ; Console . Write ( maxArmstrong ( arr , N , K ) ) ; } }


"Maximum number of Perfect Numbers present in a subarray of size K | C # program for the above approach ; Function to check a number is Perfect Number or not ; Stores sum of divisors ; Find all divisors and add them ; If sum of divisors is equal to N ; Function to return maximum sum of a subarray of size K ; If k is greater than N ; Compute sum of first window of size K ; Compute sums of remaining windows by removing first element of previous window and adding last element of current window ; return the answer ; Function to find all the perfect numbers in the array ; The given array is converted into binary array ; Driver Code"
Solution in C#:

using System ; class GFG { static int isPerfect ( int N ) { int sum = 1 ; for ( int i = 2 ; i < Math . Sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { if ( i == N / i ) { sum += i ; } else { sum += i + N / i ; } } } if ( sum == N && N != 1 ) return 1 ; return 0 ; } static int maxSum ( int [ ] arr , int N , int K ) { if ( N < K ) { Console . Write ( " Invalid " ) ; return - 1 ; } int res = 0 ; for ( int i = 0 ; i < K ; i ++ ) { res += arr [ i ] ; } int curr_sum = res ; for ( int i = K ; i < N ; i ++ ) { curr_sum += arr [ i ] - arr [ i - K ] ; res = Math . Max ( res , curr_sum ) ; } return res ; } static int max_PerfectNumbers ( int [ ] arr , int N , int K ) { for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] = isPerfect ( arr [ i ] ) == 1 ? 1 : 0 ; } return maxSum ( arr , N , K ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 28 , 2 , 3 , 6 , 496 , 99 , 8128 , 24 } ; int K = 4 ; int N = arr . Length ; Console . Write ( max_PerfectNumbers ( arr , N , K ) ) ; } }


"Maximum number of apples that can be eaten by a person | C # program of the above approach ; Function to find the maximum number of apples a person can eat such that the person eat at most one apple in a day . ; Store count of apples and number of days those apples are edible ; Stores indices of the array ; Stores count of days ; Stores maximum count of edible apples ; Traverse both the arrays ; If top element of the apple is not already expired ; Insert count of apples and their expiration date ; Remove outdated apples ; Insert all the apples produces by tree on current day ; Stores top element of pq ; Remove top element of pq ; If count of apples in curr is greater than 0 ; Insert count of apples and their expiration date ; Update total_apples ; Update index ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int cntMaxApples ( int [ ] apples , int [ ] days ) { List < Tuple < int , int > > pq = new List < Tuple < int , int > > ( ) ; int i = 0 ; int n = apples . Length ; int total_apples = 0 ; while ( i < n . Count > 0 ) { if ( i < n && apples [ i ] != 0 ) { pq . Add ( new Tuple < int , int > ( i + days [ i ] - 1 , apples [ i ] ) ) ; pq . Sort ( ) ; } while ( pq . Count > 0 && pq [ 0 ] . Item1 < i ) { pq . RemoveAt ( 0 ) ; } if ( pq . Count > 0 ) { Tuple < int , int > curr = pq [ 0 ] ; pq . RemoveAt ( 0 ) ; if ( curr . Item2 > 1 ) { pq . Add ( new Tuple < int , int > ( curr . Item1 , curr . Item2 - 1 ) ) ; pq . Sort ( ) ; } ++ total_apples ; } ++ i ; } return total_apples ; } static void Main ( ) { int [ ] apples = { 1 , 2 , 3 , 5 , 2 } ; int [ ] days = { 3 , 2 , 1 , 4 , 2 } ; Console . Write ( cntMaxApples ( apples , days ) ) ; } }


"Maximum number of bridges in a path of a given graph | C # program to find the maximum number of bridges in any path of the given graph ; Stores the nodes and their connections ; Store the tree with Bridges as the edges ; Stores the visited nodes ; For finding bridges ; for Disjoint Set Union ; For storing actual bridges ; Stores the number of nodes and edges ; For finding bridges ; Function to find root of the component in which A lies ; Doing path compression ; Function to do union between a and b ; If both are already in the same component ; If both have same rank , then increase anyone 's rank ; Function to find bridges ; Initialize in time and low value ; Update the low value of the parent ; Perform DFS on its child updating low if the child has connection with any ancestor ; Bridge found ; Otherwise ; Find union between parent and child as they are in same component ; Function to find diameter of the tree for storing max two depth child ; Finding max two depth from its children ; Update diameter with the sum of max two depths ; Return the maximum depth ; Function to find maximum bridges bwtween any two nodes ; DFS to find bridges ; If no bridges are found ; Iterate over all bridges ; Find the endpoints ; Generate the tree with bridges as the edges ; Update the head ; Return the diameter ; Driver Code ; Graph = > 1 -- -- 2 -- -- 3 -- -- 4 | | 5 -- -- 6"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int N = ( int ) 1e5 + 5 ; static List < int > [ ] v = new List < int > [ N ] ; static List < int > [ ] g = new List < int > [ N ] ; static bool [ ] vis = new bool [ N ] ; static int [ ] init = new int [ N ] ; static int [ ] low = new int [ N ] ; static int [ ] parent = new int [ N ] ; static int [ ] rnk = new int [ N ] ; static List < pair > bridges = new List < pair > ( ) ; static int n , m ; static int timer = 0 ; static int diameter ; class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void swap ( int x , int y ) { int temp = x ; x = y ; y = temp ; } static int find_set ( int a ) { if ( parent [ a ] == a ) return a ; return parent [ a ] = find_set ( parent [ a ] ) ; } static void union_set ( int a , int b ) { int x = find_set ( a ) , y = find_set ( b ) ; if ( x == y ) return ; if ( rnk [ x ] == rnk [ y ] ) rnk [ x ] ++ ; if ( rnk [ y ] > rnk [ x ] ) swap ( x , y ) ; parent [ y ] = x ; } static void dfsBridges ( int a , int par ) { vis [ a ] = true ; init [ a ] = low [ a ] = timer ++ ; foreach ( int i in v [ a ] ) { if ( i == par ) continue ; if ( vis [ i ] ) low [ a ] = Math . Min ( low [ a ] , init [ i ] ) ; else { dfsBridges ( i , a ) ; low [ a ] = Math . Min ( low [ a ] , low [ i ] ) ; if ( init [ a ] < low [ i ] ) bridges . Add ( new pair ( i , a ) ) ; else union_set ( i , a ) ; } } } static int dfsDiameter ( int a , int par ) { int x = 0 , y = 0 ; foreach ( int i in g [ a ] ) { if ( i == par ) continue ; int mx = dfsDiameter ( i , a ) ; if ( mx > x ) { y = x ; x = mx ; } else if ( mx > y ) y = mx ; } diameter = Math . Max ( diameter , x + y ) ; return x + 1 ; } static int findMaxBridges ( ) { for ( int i = 0 ; i <= n ; i ++ ) { parent [ i ] = i ; rnk [ i ] = 1 ; } dfsBridges ( 1 , 0 ) ; if ( bridges . Count == 0 ) return 0 ; int head = - 1 ; foreach ( pair i in bridges ) { int a = find_set ( i . first ) ; int b = find_set ( i . second ) ; g [...


"Maximum number of buckets that can be filled | C # program to implement the above approach ; Function to find the maximum number of buckets that can be filled with the amount of water available ; Find the total available water ; Sort the array in ascending order ; Check if bucket can be filled with available water ; Print count of buckets ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static void getBuckets ( int [ ] arr , int N ) { int availableWater = N * ( N - 1 ) / 2 ; Array . Sort ( arr ) ; int i = 0 , sum = 0 ; while ( sum <= availableWater ) { sum += arr [ i ] ; i ++ ; } Console . Write ( i - 1 ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 5 , 3 , 4 , 7 , 9 } ; int N = arr . Length ; getBuckets ( arr , N ) ; } }


"Maximum number of candies that can be bought | C # implementation of the approach ; Function to return the maximum candies that can be bought ; Buy all the candies of the last type ; Starting from second last ; Amount of candies of the current type that can be bought ; Add candies of current type that can be bought ; Update the previous bought amount ; Driver code"
Solution in C#:

using System ; class GFG { static int maxCandies ( int [ ] arr , int n ) { int prevBought = arr [ n - 1 ] ; int candies = prevBought ; for ( int i = n - 2 ; i >= 0 ; i -- ) { int x = Math . Min ( prevBought - 1 , arr [ i ] ) ; if ( x >= 0 ) { candies += x ; prevBought = x ; } } return candies ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 1 , 3 , 6 } ; int n = arr . Length ; Console . WriteLine ( maxCandies ( arr , n ) ) ; } }


"Maximum number of characters between any two same character in a string | Efficient C # program to find maximum number of characters between two occurrences of same character ; Initialize all indexes as - 1. ; If this is first occurrence ; Else find distance from previous occurrence and update result ( if required ) . ; Driver code"
Solution in C#:

using System ; public class GFG { static int MAX_CHAR = 256 ; static int maximumChars ( string str ) { int n = str . Length ; int res = - 1 ; int [ ] firstInd = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) firstInd [ i ] = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { int first_ind = firstInd [ str [ i ] ] ; if ( first_ind == - 1 ) firstInd [ str [ i ] ] = i ; else res = Math . Max ( res , Math . Abs ( i - first_ind - 1 ) ) ; } return res ; } static public void Main ( ) { string str = " abba " ; Console . WriteLine ( maximumChars ( str ) ) ; } }


"Maximum number of characters between any two same character in a string | Simple C # program to find maximum number of characters between two occurrences of same character ; Driver code"
Solution in C#:

using System ; public class GFG { static int maximumChars ( string str ) { int n = str . Length ; int res = - 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( str [ i ] == str [ j ] ) res = Math . Max ( res , Math . Abs ( j - i - 1 ) ) ; return res ; } static public void Main ( ) { string str = " abba " ; Console . WriteLine ( maximumChars ( str ) ) ; } }


"Maximum number of consecutive 1 's in binary representation of all the array elements | C # implementation of the approach ; Function to return the count of maximum consecutive 1 s in the binary representation of x ; Initialize result ; Count the number of iterations to reach x = 0. ; This operation reduces length of every sequence of 1 s by one ; Function to return the count of maximum consecutive 1 s in the binary representation among all the elements of arr [ ] ; To store the answer ; For every element of the array ; Count of maximum consecutive 1 s in the binary representation of the current element ; Update the maximum count so far ; Driver code"
Solution in C#:

using System ; class GFG { static int maxConsecutiveOnes ( int x ) { int count = 0 ; while ( x != 0 ) { x = ( x & ( x << 1 ) ) ; count ++ ; } return count ; } static int maxOnes ( int [ ] arr , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int currMax = maxConsecutiveOnes ( arr [ i ] ) ; ans = Math . Max ( ans , currMax ) ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . Length ; Console . WriteLine ( maxOnes ( arr , n ) ) ; } }


"Maximum number of consecutive 1 s after flipping all 0 s in a K length subarray | C # program for the above approach ; Function to find the maximum number of consecutive 1 's after flipping all zero in a K length subarray ; Initialize variable ; Iterate unil n - k + 1 as we have to go till i + k ; Iterate in the array in left direction till you get 1 else break ; Iterate in the array in right direction till you get 1 else break ; Compute the maximum length ; Return the length ; Driver code ; Array initialization ; Size of array"
Solution in C#:

using System ; class GFG { static int findmax ( int [ ] arr , int n , int k ) { int trav , i ; int c = 0 , maximum = 0 ; for ( i = 0 ; i < n - k + 1 ; i ++ ) { trav = i - 1 ; c = 0 ; while ( trav >= 0 && arr [ trav ] == 1 ) { trav -- ; c ++ ; } trav = i + k ; while ( trav < n && arr [ trav ] == 1 ) { trav ++ ; c ++ ; } c += k ; if ( c > maximum ) maximum = c ; } return maximum ; } public static void Main ( ) { int k = 3 ; int [ ] arr = { 0 , 0 , 1 , 1 , 0 , 0 , 0 , 0 } ; int n = arr . Length ; int ans = findmax ( arr , n , k ) ; Console . WriteLine ( ans ) ; } }


"Maximum number of distinct positive integers that can be used to represent N | C # implementation of approach ; Function to return the required count ; Driver Code"
Solution in C#:

using System ; class GFG { public static int count ( int n ) { return ( - 1 + ( int ) Math . Sqrt ( 1 + 8 * n ) ) / 2 ; } public static void Main ( ) { int n = 10 ; Console . Write ( count ( n ) ) ; } }


"Maximum number of edges in Bipartite graph | C # implementation of the approach ; Function to return the maximum number of edges possible in a Bipartite graph with N vertices ; Driver code"
Solution in C#:

using System ; class GFG { static double maxEdges ( double N ) { double edges = 0 ; edges = Math . Floor ( ( N * N ) / 4 ) ; return edges ; } static public void Main ( ) { double N = 5 ; Console . WriteLine ( maxEdges ( N ) ) ; } }


"Maximum number of edges that N | C # implementation to find the maximum number of edges for triangle free graph ; Function to find the maximum number of edges in a N - vertex graph . ; According to the Mantel 's theorem  the maximum number of edges will be  floor of [(n^2)/4] ; Driver code"
Solution in C#:

using System ; class GFG { public static int solve ( int n ) { int ans = ( n * n / 4 ) ; return ans ; } public static void Main ( ) { int n = 10 ; Console . WriteLine ( solve ( n ) ) ; } }


"Maximum number of edges to be added to a tree so that it stays a Bipartite graph | C # code to print maximum edges such that Tree remains a Bipartite graph ; To store counts of nodes with two colors ; Increment count of nodes with current color ; Traversing adjacent nodes ; Not recurring for the parent node ; Finds maximum number of edges that can be added without violating Bipartite property . ; Do a DFS to count number of nodes of each color ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static long [ ] count_color = new long [ 2 ] ; static void dfs ( List < int > [ ] adj , int node , int parent , bool color ) { count_color [ color == false ? 0 : 1 ] ++ ; for ( int i = 0 ; i < adj [ node ] . Count ; i ++ ) { if ( adj [ node ] [ i ] != parent ) dfs ( adj , adj [ node ] [ i ] , node , ! color ) ; } } static int findMaxEdges ( List < int > [ ] adj , int n ) { dfs ( adj , 1 , 0 , false ) ; return ( int ) ( count_color [ 0 ] * count_color [ 1 ] - ( n - 1 ) ) ; } public static void Main ( String [ ] args ) { int n = 5 ; List < int > [ ] adj = new List < int > [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) adj [ i ] = new List < int > ( ) ; adj [ 1 ] . Add ( 2 ) ; adj [ 1 ] . Add ( 3 ) ; adj [ 2 ] . Add ( 4 ) ; adj [ 3 ] . Add ( 5 ) ; Console . WriteLine ( findMaxEdges ( adj , n ) ) ; } }


"Maximum number of elements greater than X after equally distributing subset of array | C # implementation to find the maximum number of elements greater than X by equally distributing ; Function to find the maximum number of elements greater than X by equally distributing ; Sorting the array ; Loop to iterate over the elements of the array ; If no more elements can become larger than x ; Driver Code"
Solution in C#:

 using System ; class GFG { static void redistribute ( int [ ] arr , int n , int x ) { Array . Sort ( arr ) ; Array . Reverse ( arr ) ; int i , sum = 0 ; for ( i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( sum / ( i + 1 ) < x ) { Console . Write ( i + "
 " ) ; break ; } } if ( i == n ) Console . Write ( n + "
 " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 1 , 2 , 1 } ; int x = 3 ; redistribute ( arr , 4 , x ) ; } }


"Maximum number of elements that can be removed such that MEX of the given array remains unchanged | C # program for the above approach ; Function to find the maximum number of elements that can be removed ; Initialize hash [ ] with 0 s ; Initialize MEX ; Set hash [ i ] = 1 , if i is present in arr [ ] ; Find MEX from the hash ; Print the maximum numbers that can be removed ; Driver Code ; Given array ; Size of the array ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void countRemovableElem ( int [ ] arr , int N ) { int [ ] hash = new int [ N + 1 ] ; Array . Fill ( hash , 0 ) ; int mex = N + 1 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] <= N ) hash [ arr [ i ] ] = 1 ; } for ( int i = 1 ; i <= N ; i ++ ) { if ( hash [ i ] == 0 ) { mex = i ; break ; } } Console . WriteLine ( N - ( mex - 1 ) ) ; } public static void Main ( ) { int [ ] arr = { 2 , 3 , 5 , 1 , 6 } ; int N = arr . Length ; countRemovableElem ( arr , N ) ; } }


"Maximum number of given operations to remove the entire string | C # implementation of the approach ; Function to return the maximum number of given operations required to remove the given string entirely ; If length of the string is zero ; Single operation can delete the entire string ; To store the prefix of the string which is to be deleted ; Prefix s [ 0. . i ] ; To store the substring s [ i + 1. . .2 * i + 1 ] ; If the prefix s [ 0. . . i ] can be deleted ; 1 operation to remove the current prefix and then recursively find the count of operations for the substring s [ i + 1. . . n - 1 ] ; Entire string has to be deleted ; Driver code"
Solution in C#:

using System ; class GFG { static int find ( string s ) { if ( s . Length == 0 ) return 0 ; int c = 1 ; string d = " " ; for ( int i = 0 ; i < s . Length ; i ++ ) { d += s [ i ] ; string s2 = " " ; if ( i + d . Length < s . Length ) { s2 = s . Substring ( i + 1 , d . Length ) ; } if ( s2 == d ) { c = 1 + find ( s . Substring ( i + 1 ) ) ; break ; } } return c ; } public static void Main ( string [ ] args ) { string s = " abababab " ; Console . Write ( find ( s ) ) ; } }


"Maximum number of groups that can receive fresh donuts distributed in batches of size K | C # program for the above approach ; Recursive function to find the maximum number of groups that will receive fresh donuts ; Store the result for the current state ; Check if the leftover donuts from the previous batch is 0 ; If true , then one by one give the fresh donuts to each group ; Decrement arr [ i ] ; Update the maximum number of groups ; Increment arr [ i ] ; Otherwise , traverse the given array , arr [ ] ; Decrement arr [ i ] ; Update the maximum number of groups ; Increment arr [ i ] ; Return the value of q ; Function to find the maximum number of groups that will receive fresh donuts ; Stores count of remainder by K ; Traverse the array arr [ ] ; Stores maximum number of groups ; Return the answer ; Driver code"
Solution in C#:

using System ; class GFG { public static int dfs ( int [ ] arr , int left , int K ) { int q = 0 ; if ( left == 0 ) { for ( int i = 1 ; i < K ; ++ i ) { if ( arr [ i ] > 0 ) { arr [ i ] -- ; q = Math . Max ( q , 1 + dfs ( arr , K - i , K ) ) ; arr [ i ] ++ ; } } } else { for ( int i = 1 ; i < K ; ++ i ) { if ( arr [ i ] > 0 ) { arr [ i ] -- ; int nleft = ( i <= left ? left - i : K + left - i ) ; q = Math . Max ( q , dfs ( arr , nleft , K ) ) ; arr [ i ] ++ ; } } } return q ; } public static int maxGroups ( int K , int [ ] arr ) { int [ ] V = new int [ K ] ; foreach ( int x in arr ) V [ x % K ] ++ ; int ans = V [ 0 ] + dfs ( V , 0 , K ) ; return ans ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 } ; int K = 3 ; Console . WriteLine ( maxGroups ( K , arr ) ) ; } }


"Maximum number of groups that can receive fresh donuts distributed in batches of size K | C # program for the above approach ; Stores the result of the same recursive calls ; Recursive function to find the maximum number of groups that will receive fresh donuts ; Store the result for the current state ; Store the key and check if it is present in the hashmap ; If already calculated ; If left is 0 ; Traverse the array [ ] arr ; Decrement arr [ i ] ; Update the maximum number of groups ; Increment arr [ i ] by 1 ; Otherwise , traverse the given array [ ] arr ; Decrement arr [ i ] ; Update the maximum number of groups ; Increment arr [ i ] by 1 ; Memoize the result and return it ; Function to find the maximum number of groups that will receive fresh donuts ; Stores count of remainder by K ; Traverse the array [ ] arr ; Hashmap to memoize the results ; Store the maximum number of groups ; Return the answer ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { static Dictionary < String , int > memo ; public static int dfs ( int [ ] V , int left , int K ) { int q = 0 ; String key = string . Join ( " , " , V ) ; key += left . ToString ( ) ; if ( memo . ContainsKey ( key ) ) return memo [ key ] ; else if ( left == 0 ) { for ( int i = 1 ; i < K ; ++ i ) if ( V [ i ] > 0 ) { V [ i ] -- ; q = Math . Max ( q , 1 + dfs ( V , K - i , K ) ) ; V [ i ] ++ ; } } else { for ( int i = 1 ; i < K ; ++ i ) { if ( V [ i ] > 0 ) { V [ i ] -- ; int nleft = i <= left ? left - i : K + left - i ; q = Math . Max ( q , dfs ( V , nleft , K ) ) ; V [ i ] ++ ; } } } if ( memo . ContainsKey ( key ) ) memo [ key ] = q ; else memo . Add ( key , q ) ; return q ; } public static int maxGroups ( int K , int [ ] arr ) { int [ ] V = new int [ K ] ; foreach ( int x in arr ) V [ x % K ] ++ ; memo = new Dictionary < String , int > ( ) ; int ans = V [ 0 ] + dfs ( V , 0 , K ) ; return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 } ; int K = 3 ; Console . WriteLine ( maxGroups ( K , arr ) ) ; } }


"Maximum number of intersections possible for any of the N given segments | C # program for the above approach ; Function to find the maximum number of intersections one segment has with all the other given segments ; Stores the resultant maximum count ; Stores the starting and the ending points ; Sort arrays points in the ascending order ; Traverse the array arr [ ] ; Find the count of segments on left of ith segment ; Find the count of segments on right of ith segment ; Find the total segments not intersecting with the current segment ; Store the count of segments that intersect with the ith segment ; Update the value of count ; Return the resultant count ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int lower_bound ( int [ ] a , int low , int high , long element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } static int maximumIntersections ( int [ , ] arr , int N ) { int count = 0 ; int [ ] L = new int [ N ] ; int [ ] R = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { L [ i ] = arr [ i , 0 ] ; R [ i ] = arr [ i , 1 ] ; } Array . Sort ( L ) ; Array . Sort ( R ) ; for ( int i = 0 ; i < N ; i ++ ) { int l = arr [ i , 0 ] ; int r = arr [ i , 1 ] ; int x = lower_bound ( L , 0 , N , l ) ; int y = N - lower_bound ( R , 0 , N , r + 1 ) ; int cnt = x + y ; cnt = N - cnt - 1 ; count = Math . Max ( count , cnt ) ; } return count ; } public static void Main ( ) { int [ , ] arr = new int [ 3 , 2 ] { { 1 , 6 } , { 5 , 5 } , { 2 , 3 } } ; int N = 3 ; Console . Write ( maximumIntersections ( arr , N ) ) ; } }


"Maximum number of intervals that an interval can intersect | C # program for the above approach ; Function to count the maximum number of intervals that an interval can intersect ; Store the required answer ; Traverse all the intervals ; Store the number of intersecting intervals ; Iterate in the range [ 0 , n - 1 ] ; Check if jth interval lies outside the ith interval ; Update the overall maximum ; Print the answer ; Driver code ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void findMaxIntervals ( List < Tuple < int , int > > v , int n ) { int maxi = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int c = n ; for ( int j = 0 ; j < n ; j ++ ) { if ( v [ i ] . Item2 < v [ j ] . Item1 v [ i ] . Item1 > v [ j ] . Item2 ) { c -- ; } } maxi = Math . Max ( c , maxi ) ; } Console . Write ( maxi ) ; } static void Main ( ) { List < Tuple < int , int > > arr = new List < Tuple < int , int > > ( ) ; arr . Add ( new Tuple < int , int > ( 1 , 2 ) ) ; arr . Add ( new Tuple < int , int > ( 3 , 4 ) ) ; arr . Add ( new Tuple < int , int > ( 2 , 5 ) ) ; int N = arr . Count ; findMaxIntervals ( arr , N ) ; } }


"Maximum number of mangoes that can be bought | C # program for the above approach ; Function to check if mid number of mangoes can be bought ; Store the coins ; If watermelons needed are greater than given watermelons ; Store remaining watermelons if vl watermelons are used to buy mangoes ; Store the value of coins if these watermelon get sold ; Increment coins by ex ; Number of mangoes that can be buyed if only x coins needed for one mango ; If the condition is satisfied , return true ; Otherwise return false ; Function to find the maximum number of mangoes that can be bought by selling watermelons ; Initialize the boundary values ; Store the required result ; Binary Search ; Store the mid value ; Check if it is possible to buy mid number of mangoes ; Otherwise , update r to mid - 1 ; Return the result ; Driver Code ; Given Input ; Function Call"
Solution in C#:

using System ; class GFG { static bool check ( int n , int m , int x , int y , int vl ) { int temp = m ; if ( vl > n ) return false ; int ex = n - vl ; ex *= y ; temp += ex ; int cr = temp / x ; if ( cr >= vl ) return true ; return false ; } static int maximizeMangoes ( int n , int m , int x , int y ) { int l = 0 , r = n ; int ans = 0 ; while ( l <= r ) { int mid = l + ( r - l ) / 2 ; if ( check ( n , m , x , y , mid ) ) { ans = mid ; l = mid + 1 ; } else r = mid - 1 ; } return ans ; } public static void Main ( ) { int W = 4 , C = 8 , x = 4 , y = 4 ; Console . Write ( maximizeMangoes ( W , C , x , y ) ) ; } }


"Maximum number of multiplication by 3 or division by 2 operations possible on an array | C # program for the above approach ; Function to count maximum number of multiplication by 3 or division by 2 operations that can be performed ; Stores the maximum number of operations possible ; Traverse the array arr [ ] ; Iterate until arr [ i ] is even ; Increment count by 1 ; Update arr [ i ] ; Return the value of Count as the answer ; Driver Code ; Given Input ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int maximumTurns ( int [ ] arr , int N ) { int Count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { while ( arr [ i ] % 2 == 0 ) { Count ++ ; arr [ i ] = arr [ i ] / 2 ; } } return Count ; } public static void Main ( ) { int [ ] arr = { 5 , 2 , 4 } ; int N = arr . Length ; Console . Write ( maximumTurns ( arr , N ) ) ; } }


"Maximum number of objects that can be created as per given conditions | C # program for the above problem ; Function for finding the maximum number of objects from N type - 1 and M type - 2 items ; Storing minimum of N and M ; Storing maximum number of objects from given items ; Driver Code"
Solution in C#:

using System ; class GFG { static int numberOfObjects ( int N , int M ) { int initial = Math . Min ( N , M ) ; int last = ( N + M ) / 3 ; return Math . Min ( initial , last ) ; } public static void Main ( string [ ] args ) { int N = 8 ; int M = 7 ; Console . Write ( numberOfObjects ( N , M ) ) ; } }


"Maximum number of ones in a N * N matrix with given constraints | C # program to get Maximum Number of ones in a matrix with given constraints ; Function that returns the maximum number of ones ; Minimum number of zeroes ; Totol cells = square of the size of the matrices ; Initialising the answer ; Driver code ; Initialising the variables"
Solution in C#:

using System ; class GFG { static int getMaxOnes ( int n , int x ) { int zeroes = ( n / x ) ; zeroes = zeroes * zeroes ; int total = n * n ; int ans = total - zeroes ; return ans ; } static public void Main ( ) { int n = 5 ; int x = 2 ; Console . WriteLine ( getMaxOnes ( n , x ) ) ; } }


"Maximum number of overlapping rectangles with at least one common point | C # program for the above approach ; Function to find the maximum number of overlapping rectangles ; Stores the maximum count of overlapping rectangles ; Stores the X and Y coordinates ; Iterate over all pairs of Xs and Ys ; Store the count for the current X and Y ; Update the maximum count of rectangles ; Returns the total count ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void maxOverlappingRectangles ( int [ ] x1 , int [ ] y1 , int [ ] x2 , int [ ] y2 , int N ) { int max_rectangles = 0 ; List < int > X = new List < int > ( ) ; List < int > Y = new List < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { X . Add ( x1 [ i ] ) ; X . Add ( x2 [ i ] - 1 ) ; Y . Add ( y1 [ i ] ) ; Y . Add ( y2 [ i ] - 1 ) ; } for ( int i = 0 ; i < X . Count ; i ++ ) { for ( int j = 0 ; j < Y . Count ; j ++ ) { int cnt = 0 ; for ( int k = 0 ; k < N ; k ++ ) { if ( X [ i ] >= x1 [ k ] && X [ i ] + 1 <= x2 [ k ] && Y [ j ] >= y1 [ k ] && Y [ j ] + 1 <= y2 [ k ] ) { cnt ++ ; } } max_rectangles = Math . Max ( max_rectangles , cnt ) ; } } Console . WriteLine ( max_rectangles ) ; } public static void Main ( ) { int [ ] x1 = { 0 , 50 } ; int [ ] y1 = { 0 , 50 } ; int [ ] x2 = { 100 , 60 } ; int [ ] y2 = { 100 , 60 } ; int N = x1 . Length ; maxOverlappingRectangles ( x1 , y1 , x2 , y2 , N ) ; } }


"Maximum number of overlapping string | C # implementation to find the maximum number of occurrence of the overlapping count ; Function to find the maximum overlapping Strings ; Get the current character ; Condition to check if the current character is the first character of the String T then increment the overlapping count ; Condition to check previous character is also occurred ; Update count of previous and current character ; Condition to check the current character is the last character of the String T ; Condition to check the every subsequence is a valid String T ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { static int maxOverlap ( String S , String T ) { String str = T ; int [ ] count = new int [ T . Length ] ; int overlap = 0 ; int max_overlap = 0 ; for ( int i = 0 ; i < S . Length ; i ++ ) { int index = str . IndexOf ( S [ i ] ) ; if ( index == 0 ) { overlap ++ ; if ( overlap >= 2 ) { max_overlap = Math . Max ( overlap , max_overlap ) ; } count [ index ] ++ ; } else { if ( count [ index - 1 ] <= 0 ) return - 1 ; count [ index ] ++ ; count [ index - 1 ] -- ; } if ( index == 4 ) overlap -- ; } if ( overlap == 0 ) return max_overlap ; else return - 1 ; } public static void Main ( String [ ] args ) { String S = " chcirphirp " ; String T = " chirp " ; Console . Write ( maxOverlap ( S , T ) ) ; } }


"Maximum number of parallelograms that can be made using the given length of line segments | Function to find the maximum number of parallelograms can be made ; Finding the length of the frequency array ; Increasing the occurrence of each segment ; To store the count of parallelograms ; Counting parallelograms that can be made using 4 similar sides ; counting segments which have occurrence left >= 2 ; Adding parallelograms that can be made using 2 similar sides ; Driver code"
Solution in C#:

using System ; class GFG { static void convert ( int n , int [ ] a ) { int z = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] > z ) z = a [ i ] ; } z = z + 1 ; int [ ] ff = new int [ z ] ; for ( int i = 0 ; i < n ; i ++ ) { ff [ a [ i ] ] += 1 ; } int cc = 0 ; for ( int i = 0 ; i < z ; i ++ ) { cc += ( ff [ i ] / 4 ) ; ff [ i ] = ff [ i ] % 4 ; } int vv = 0 ; for ( int i = 0 ; i < z ; i ++ ) { if ( ff [ i ] >= 2 ) vv += 1 ; } cc += ( vv / 2 ) ; Console . WriteLine ( cc ) ; } static public void Main ( ) { int n = 4 ; int [ ] a = { 1 , 2 , 1 , 2 } ; convert ( n , a ) ; } }


"Maximum number of partitions that can be sorted individually to make sorted | C # program to find Maximum number of partitions such that we can get a sorted array ; Function to find maximum partitions . ; Find maximum in prefix arr [ 0. . i ] ; If maximum so far is equal to index , we can make a new partition ending at index i . ; Driver code"
Solution in C#:

using System ; class GFG { static int maxPartitions ( int [ ] arr , int n ) { int ans = 0 , max_so_far = 0 ; for ( int i = 0 ; i < n ; ++ i ) { max_so_far = Math . Max ( max_so_far , arr [ i ] ) ; if ( max_so_far == i ) ans ++ ; } return ans ; } public static void Main ( ) { int [ ] arr = { 1 , 0 , 2 , 3 , 4 } ; int n = arr . Length ; Console . Write ( maxPartitions ( arr , n ) ) ; } }


"Maximum number of people that can be killed with strength P | C # implementation of the approach ; Function to return the maximum number of people that can be killed ; Loop will break when the ith person cannot be killed ; Driver code"
Solution in C#:

using System ; class GFG { static int maxPeople ( int p ) { int tmp = 0 , count = 0 ; for ( int i = 1 ; i * i <= p ; i ++ ) { tmp = tmp + ( i * i ) ; if ( tmp <= p ) count ++ ; else break ; } return count ; } public static void Main ( ) { int p = 14 ; Console . WriteLine ( maxPeople ( p ) ) ; } }


"Maximum number of people that can be killed with strength P | C # implementation of the approach ; Function to return the maximum number of people that can be killed ; Storing the sum beforehand so that it can be used in each query ; lower_bound returns an iterator pointing to the first element greater than or equal to your val ; Previous value ; Returns the index in array upto which killing is possible with strength P ; Driver code"
Solution in C#:

using System ; public class GFG { static int kN = 1000000 ; static int maxPeople ( int p ) { long [ ] sums = new long [ kN ] ; sums [ 0 ] = 0 ; for ( int i = 1 ; i < kN ; i ++ ) sums [ i ] = ( long ) ( i * i ) + sums [ i - 1 ] ; int it = lower_bound ( sums , 0 , kN , p ) ; if ( it > p ) { -- it ; } return it ; } private static int lower_bound ( long [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } public static void Main ( String [ ] args ) { int p = 14 ; Console . WriteLine ( maxPeople ( p ) ) ; } }


"Maximum number of people that can be killed with strength P | C # implementation of the approach ; Helper function which returns the sum of series ( 1 ^ 2 + 2 ^ 2 + ... + n ^ 2 ) ; maxPeople function which returns appropriate value using Binary Search in O ( logn ) ; Set the lower and higher values ; Calculate the mid using low and high ; Compare value with n ; Return the ans ; Driver code"
Solution in C#:

using System ; class GFG { static long squareSeries ( long n ) { return ( n * ( n + 1 ) * ( 2 * n + 1 ) ) / 6 ; } static long maxPeople ( long n ) { long low = 0 ; long high = 1000000L ; long ans = 0L ; while ( low <= high ) { long mid = low + ( ( high - low ) / 2 ) ; long value = squareSeries ( mid ) ; if ( value <= n ) { ans = mid ; low = mid + 1 ; } else { high = mid - 1 ; } } return ans ; } public static void Main ( String [ ] args ) { long p = 14 ; Console . Write ( maxPeople ( p ) ) ; } }


"Maximum number of pieces in N cuts | C # program to find maximum no of pieces by given number of cuts ; Function for finding maximum pieces with n cuts . ; to maximize number of pieces x is the horizontal cuts ; Now ( x ) is the horizontal cuts and ( n - x ) is vertical cuts , then maximum number of pieces = ( x + 1 ) * ( n - x + 1 ) ; Driver code ; Taking the maximum number of cuts allowed as 3 ; Finding and printing the max number of pieces"
Solution in C#:

using System ; class GFG { public static int findMaximumPieces ( int n ) { int x = n / 2 ; return ( ( x + 1 ) * ( n - x + 1 ) ) ; } static public void Main ( ) { int n = 3 ; Console . Write ( " Max ▁ number ▁ of ▁ pieces ▁ for ▁ n ▁ = ▁ " + n + " ▁ is ▁ " + findMaximumPieces ( 3 ) ) ; } }


"Maximum number of prime factors a number can have with exactly x factors | C # implementation to find the maximum count of the prime factors by the count of factors of number ; Function to count number of prime factors of x ; Count variable is incremented form every prime factor of x ; Loop to count the number of the prime factors of the given number ; Driver Code"
Solution in C#:

using System ; class GFG { static int countPrimeFactors ( int n ) { if ( n == 1 ) return 0 ; int cnt = 0 ; while ( n % 2 == 0 ) { cnt ++ ; n = n / 2 ; } for ( int i = 3 ; i <= Math . Sqrt ( n ) ; i += 2 ) { while ( n % i == 0 ) { cnt ++ ; n = n / i ; } } if ( n > 2 ) cnt ++ ; return cnt ; } static public void Main ( ) { int x = 8 ; int prime_factor_cnt = countPrimeFactors ( x ) ; Console . Write ( prime_factor_cnt ) ; } }


"Maximum number of removals of given subsequence from a string | C # implementation of the approach ; Function to return max possible operation of the given type that can be performed on str ; Increment count of sub - sequence ' g ' ; Increment count of sub - sequence ' gk ' if ' g ' is available ; Increment count of sub - sequence ' gks ' if sub - sequence ' gk ' appeared previously ; Return the count of sub - sequence ' gks ' ; Driver code"
Solution in C#:

using System ; public class GFG { static int maxOperations ( string str ) { int i , g , gk , gks ; i = g = gk = gks = 0 ; for ( i = 0 ; i < str . Length ; i ++ ) { if ( str [ i ] == ' g ' ) { g ++ ; } else if ( str [ i ] == ' k ' ) { if ( g > 0 ) { g -- ; gk ++ ; } } else if ( str [ i ] == ' s ' ) { if ( gk > 0 ) { gk -- ; gks ++ ; } } } return gks ; } public static void Main ( ) { string a = " ggkssk " ; Console . WriteLine ( maxOperations ( a ) ) ; } }


"Maximum number of segments of lengths a , b and c | C # implementation to divide N into maximum number of segments of length a , b and c ; function to find the maximum number of segments ; stores the maximum number of segments each index can have ; initialize with - 1 ; 0 th index will have 0 segments base case ; traverse for all possible segments till n ; conditions if ( i + a <= n ) avoid buffer overflow ; if ( i + b <= n ) avoid buffer overflow ; if ( i + c <= n ) avoid buffer overflow ; Driver code"
Solution in C#:

using System ; class GFG { static int maximumSegments ( int n , int a , int b , int c ) { int [ ] dp = new int [ n + 10 ] ; for ( int i = 0 ; i < n + 10 ; i ++ ) dp [ i ] = - 1 ; dp [ 0 ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( dp [ i ] != - 1 ) { dp [ i + a ] = Math . Max ( dp [ i ] + 1 , dp [ i + a ] ) ; dp [ i + b ] = Math . Max ( dp [ i ] + 1 , dp [ i + b ] ) ; dp [ i + c ] = Math . Max ( dp [ i ] + 1 , dp [ i + c ] ) ; } } return dp [ n ] ; } public static void Main ( ) { int n = 7 , a = 5 , b = 2 , c = 5 ; Console . Write ( maximumSegments ( n , a , b , c ) ) ; } }


"Maximum number of segments that can contain the given points | C # implementation of the approach ; Function to return the maximum number of segments ; Sort both the vectors ; Initially pointing to the first element of b [ ] ; Try to find a match in b [ ] ; The segment ends before b [ j ] ; The point lies within the segment ; The segment starts after b [ j ] ; Return the required count ; Driver code"
Solution in C#:

using System ; class GFG { static int countPoints ( int n , int m , int [ ] a , int [ ] b , int x , int y ) { Array . Sort ( a ) ; Array . Sort ( b ) ; int j = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( j < m ) { if ( a [ i ] + y < b [ j ] ) break ; if ( b [ j ] >= a [ i ] - x && b [ j ] <= a [ i ] + y ) { count ++ ; j ++ ; break ; } else j ++ ; } } return count ; } public static void Main ( ) { int x = 1 , y = 4 ; int [ ] a = { 1 , 5 } ; int n = a . Length ; int [ ] b = { 1 , 1 , 2 } ; int m = a . Length ; Console . WriteLine ( countPoints ( n , m , a , b , x , y ) ) ; } }


"Maximum number of set bits count in a K | C # program to find the maximum set bits in a substring of size K ; Function that find Maximum number of set bit appears in a substring of size K . ; Traverse string 1 to k ; Increment count if character is set bit ; Traverse string k + 1 to length of string ; remove the contribution of the ( i - k ) th character which is no longer in the window ; add the contribution of the current character ; update maxCount at for each window of size k ; return maxCount ; Driver Program"
Solution in C#:

using System ; class GFG { static int maxSetBitCount ( string s , int k ) { int maxCount = 0 , n = s . Length ; int count = 0 ; for ( int i = 0 ; i < k ; i ++ ) { if ( s [ i ] == '1' ) count ++ ; } maxCount = count ; for ( int i = k ; i < n ; i ++ ) { if ( s [ i - k ] == '1' ) count -- ; if ( s [ i ] == '1' ) count ++ ; maxCount = Math . Max ( maxCount , count ) ; } return maxCount ; } public static void Main ( ) { string s = "100111010" ; int k = 3 ; Console . Write ( maxSetBitCount ( s , k ) ) ; } }


"Maximum number of splits of a binary number | C # implementation of the approach ; Function to return the required count ; If the splitting is not possible ; To store the final ans ; Counting the number of zeros ; Return the final answer ; Driver code"
Solution in C#:

using System ; class GFG { static int cntSplits ( String s ) { if ( s [ s . Length - 1 ] == '1' ) return - 1 ; int ans = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) ans += ( s [ i ] == '0' ) ? 1 : 0 ; return ans ; } public static void Main ( String [ ] args ) { String s = "10010" ; Console . WriteLine ( cntSplits ( s ) ) ; } }


"Maximum number of teams of size K possible with each player from different country | C # program for the above approach ; Function to find if T number of teams can be formed or not ; Store the sum of array elements ; Traverse the array teams [ ] ; Required Condition ; Function to find the maximum number of teams possible ; Lower and Upper part of the range ; Perform the Binary Search ; Find the value of mid ; Perform the Binary Search ; Otherwise , update the search range ; Otherwise , update the search range ; Driver Code"
Solution in C#:

using System ; class GFG { public static bool is_possible ( int [ ] teams , int T , int k ) { int sum = 0 ; for ( int i = 0 ; i < teams . Length ; i ++ ) { sum += Math . Min ( T , teams [ i ] ) ; } return ( sum >= ( T * k ) ) ; } public static int countOfTeams ( int [ ] teams_list , int N , int K ) { int lb = 0 ; double ub = 1e9 ; while ( lb <= ub ) { int mid = lb + ( int ) ( ub - lb ) / 2 ; if ( is_possible ( teams_list , mid , K ) ) { if ( ! is_possible ( teams_list , mid + 1 , K ) ) { return mid ; } else { lb = mid + 1 ; } } else { ub = mid - 1 ; } } return 0 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 4 } ; int K = 2 ; int N = arr . Length ; Console . WriteLine ( countOfTeams ( arr , N , K ) ) ; } }


"Maximum number of teams that can be formed with given persons | C # implementation of the approach ; Function that returns true if it possible to form a team with the given n and m ; 1 person of Type1 and 2 persons of Type2 can be chosen ; 1 person of Type2 and 2 persons of Type1 can be chosen ; Cannot from a team ; Function to return the maximum number of teams that can be formed ; To store the required count of teams formed ; Choose 2 persons of Type1 ; And 1 person of Type2 ; Choose 2 persons of Type2 ; And 1 person of Type1 ; Another team has been formed ; Driver code"
Solution in C#:

using System ; class GFG { static bool canFormTeam ( int n , int m ) { if ( n >= 1 && m >= 2 ) return true ; if ( m >= 1 && n >= 2 ) return true ; return false ; } static int maxTeams ( int n , int m ) { int count = 0 ; while ( canFormTeam ( n , m ) ) { if ( n > m ) { n -= 2 ; m -= 1 ; } else { m -= 2 ; n -= 1 ; } count ++ ; } return count ; } public static void Main ( ) { int n = 4 , m = 5 ; Console . WriteLine ( maxTeams ( n , m ) ) ; } }


"Maximum number of tiles required to cover the floor of given size using 2 x1 size tiles | C # program for the above approach ; Function to find the maximum number of tiles required to cover the floor of size m x n using 2 x 1 size tiles ; Print the answer ; Driver code ; Given M and N ; Function call"
Solution in C#:

using System ; class GFG { static void maximumTiles ( int n , int m ) { Console . WriteLine ( ( m * n ) / 2 ) ; } public static void Main ( String [ ] args ) { int M = 3 ; int N = 4 ; maximumTiles ( N , M ) ; } }


"Maximum number of times Array can be reduced in half when its all elements are even | C # code implementation for the above approach ; Function to return the number of operations possible ; counter to store the number of times the current element is divisible by 2 ; variable to store the final answer ; Initialize the counter to zero for each element ; update the counter till the number is divisible by 2 ; update the answer as the minimum of all the counts ; Driver code"
Solution in C#:

using System ; class GFG { static int arrayDivisionByTwo ( int [ ] arr , int n ) { int cnt = 0 ; int ans = Int32 . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { cnt = 0 ; while ( arr [ i ] % 2 == 0 ) { arr [ i ] = arr [ i ] / 2 ; cnt ++ ; } ans = Math . Min ( ans , cnt ) ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 8 , 12 , 40 } ; int n = arr . Length ; Console . Write ( arrayDivisionByTwo ( arr , n ) ) ; } }


"Maximum number of times a given string needs to be concatenated to form a substring of another string | C # program for the above approach ; Function to find lps [ ] for given pattern pat [ 0. . M - 1 ] ; Length of the previous longest prefix suffix ; lps [ 0 ] is always 0 ; Iterate string to calculate lps [ i ] ; If the current character of the pattern matches ; Otherwise ; Otherwise ; Function to implement KMP algorithm ; Stores the longest prefix and suffix values for pattern ; Preprocess the pattern and find the lps [ ] array ; Index for txt [ ] ; Index for pat [ ] ; If pattern is found return 1 ; Mismatch after j matches ; Don 't match lps[0, lps[j - 1]]  characters they will  match anyway ; Return 0 if the pattern is not found ; Function to find the maximum value K for which string S2 concatenated K times is a substring of string S1 ; Store the required maximum number ; Create a temporary string to store string word ; Store the maximum number of times string S2 can occur in string S1 ; Traverse in range [ 0 , numWords - 1 ] ; If curWord is found in sequence ; Concatenate word to curWord ; Increment resCount by 1 ; Otherwise break the loop ; Print the answer ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static void computeLPSArray ( String pat , int M , int [ ] lps ) { int len = 0 ; lps [ 0 ] = 0 ; int i = 1 ; while ( i < M ) { if ( pat [ i ] == pat [ len ] ) { len ++ ; lps [ i ] = len ; i ++ ; } else { if ( len != 0 ) { len = lps [ len - 1 ] ; } else { lps [ i ] = 0 ; i ++ ; } } } } static int KMPSearch ( String pat , String txt ) { int M = pat . Length ; int N = txt . Length ; int [ ] lps = new int [ M ] ; computeLPSArray ( pat , M , lps ) ; int i = 0 ; int j = 0 ; while ( i < N ) { if ( pat [ j ] == txt [ i ] ) { j ++ ; i ++ ; } if ( j == M ) { return 1 ; else if ( i < N && [ j ] != txt [ i ] ) { if ( j != 0 ) j = lps [ j - 1 ] ; else = i + 1 ; } } return 0 ; } static void ( String seq , String word ) { int resCount = 0 ; curWord = word ; int numWords = seq . Length / word . Length ; for ( int i = 0 ; i < numWords ; ++ ) { if ( KMPSearch ( curWord , seq ) == 1 ) { curWord += word ; ++ ; } else break ; } Console . Write ( resCount ) ; } public static void ( String [ ] args ) { String S1 = " ababc " , S2 = " ab " ; ( , S2 ) ; } }


"Maximum number of uncrossed lines between two given arrays | C # program for the above approach ; Function to count maximum number of uncrossed lines between the two given arrays ; Stores the length of lcs obtained upto every index ; Iterate over first array ; Iterate over second array ; Update value in dp table ; If both characters are equal ; Update the length of lcs ; If both characters are not equal ; Update the table ; Return the answer ; Driver Code ; Given array A [ ] and B [ ] ; Function call"
Solution in C#:

using System ; class GFG { static int uncrossedLines ( int [ ] a , int [ ] b , int n , int m ) { int [ , ] dp = new int [ n + 1 , m + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= m ; j ++ ) { if ( i == 0 j == 0 ) dp [ i , j ] = 0 ; else if ( a [ i - 1 ] == b [ j - 1 ] ) dp [ i , j ] = 1 + dp [ i - 1 , j - 1 ] ; else dp [ i , j ] = Math . Max ( dp [ i - 1 , j ] , dp [ i , j - 1 ] ) ; } } return dp [ n , m ] ; } public static void Main ( String [ ] args ) { int [ ] A = { 3 , 9 , 2 } ; int [ ] B = { 3 , 2 , 9 } ; int N = A . Length ; int M = B . Length ; Console . Write ( uncrossedLines ( A , B , N , M ) ) ; } } }


"Maximum number of unique Triplets such that each element is selected only once | C # program for the above approach ; Function that finds maximum number of triplets with different elements ; Map M will store the frequency of each element in the array ; Priority queue of pairs { frequency , value } ; ans will store possible triplets ; Extract top 3 elements ; Make a triplet ; Decrease frequency and push back into priority queue if non - zero frequency ; Print the triplets ; Print the triplets ; Driver code ; Given array arr [ ] ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void findTriplets ( int [ ] arr , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int x = 0 ; x < n ; x ++ ) { if ( mp . ContainsKey ( arr [ x ] ) ) { mp [ arr [ x ] ] ++ ; } else { mp [ arr [ x ] ] = 1 ; } } List < Tuple < int , int > > pq = new List < Tuple < int , int > > ( ) ; int cnt = 0 ; foreach ( KeyValuePair < int , int > pa in mp ) pq . Add ( new Tuple < int , int > ( pa . Value , pa . Key ) ) ; List < List < int > > ans = new List < List < int > > ( ) ; pq . Sort ( ) ; pq . Reverse ( ) ; while ( pq . Count >= 3 ) { Tuple < int , int > [ ] ar = new Tuple < int , int > [ 3 ] ; for ( int x = 0 ; x < 3 ; x ++ ) { ar [ x ] = pq [ 0 ] ; pq . RemoveAt ( 0 ) ; } ans . Add ( new List < int > ( ) ) ; ans [ cnt ] . Add ( ar [ 0 ] . Item2 ) ; ans [ cnt ] . Add ( ar [ 1 ] . Item2 ) ; ans [ cnt ] . Add ( ar [ 2 ] . Item2 ) ; for ( int x = 0 ; x < 3 ; x ++ ) { ar [ x ] = new Tuple < int , int > ( ar [ x ] . Item1 - 1 , ar [ x ] . Item2 ) ; if ( ar [ x ] . Item1 != 0 ) { pq . Add ( ar [ x ] ) ; pq . Sort ( ) ; pq . Reverse ( ) ; } } cnt ++ ; } Console . Write ( " Maximum ▁ number ▁ of ▁ possible ▁ triples : ▁ " ) ; Console . WriteLine ( ans . Count ) ; foreach ( List < int > pa in ans ) { foreach ( int v in pa ) Console . Write ( v + " ▁ " ) ; Console . WriteLine ( ) ; } } static void Main ( ) { int [ ] arr = { 2 , 2 , 3 , 3 , 4 , 4 , 4 , 4 , 5 } ; int n = arr . Length ; findTriplets ( arr , n ) ; } }