"Lucky alive person in a circle | Set | Here is a O ( 1 ) solution for this problemit will work for 32 bit integers ] ; function to find the highest power of 2 which is less than n ; Suppose n is 273 ( binary is 100010001 ) . It does following 100010001 | 010001000 = 110011001 ; This makes sure 4 bits ( From MSB and including MSB ) are set . It does following 110011001 | 001100110 = 111111111 ; Increment n by 1 so that there is only one set bit which is just before original MSB . n now becomes 1000000000 ; Return original MSB after shifting . n now becomes 100000000 ; to calculate the highest number which is power of 2 and less than n ; return the correct answer as per the approach in above article ; Driver code"
Solution in C#:

using System ; class GFG { static int setBitNumber ( int n ) { n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; n = n + 1 ; return ( n >> 1 ) ; } static int luckiest_person ( int n ) { int nearestPower = setBitNumber ( n ) ; return 2 * ( n - nearestPower ) + 1 ; } public static void Main ( ) { int N = 8 ; int lucky_p = luckiest_person ( N ) ; Console . Write ( lucky_p ) ; } }


"Ludic Numbers | C # code to print Lucid number smaller than or equal to n . ; Returns a list containing all Ludic numbers smaller than or equal to n . ; ludics list contain all the ludic numbers ; Here we have to start with index 1 and will remove nothing from the list ; Here first item should be included in the list and the deletion is referred by this first item in the loop . ; remove_index variable is used to store the next index which we want to delete ; Removing the next item ; remove_index is updated so that we get the next index for deletion ; Driver code"
Solution in C#:

using System ; using System . Collections ; class GFG { public static ArrayList getLudic ( int n ) { ArrayList ludics = new ArrayList ( ) ; for ( int i = 1 ; i <= n ; i ++ ) ludics . Add ( i ) ; for ( int index = 1 ; index < ludics . Count ; index ++ ) { int first_ludic = ( int ) ludics [ index ] ; int remove_index = index + first_ludic ; while ( remove_index < ludics . Count ) { ludics . Remove ( ludics [ remove_index ] ) ; remove_index = remove_index + first_ludic - 1 ; } } return ludics ; } public static void Main ( string [ ] srgs ) { int n = 25 ; ArrayList tmp = getLudic ( n ) ; Console . Write ( " [ " ) ; foreach ( int x in tmp ) { Console . Write ( x + " , ▁ " ) ; } Console . Write ( " ] " ) ; } }


"Luhn algorithm | C # program to implement Luhn algorithm ; Returns true if given card number is valid ; We add two digits to handle cases that make two digits after doubling ; Driver code"
Solution in C#:

using System ; class GFG { static bool checkLuhn ( String cardNo ) { int nDigits = cardNo . Length ; int nSum = 0 ; bool isSecond = false ; for ( int i = nDigits - 1 ; i >= 0 ; i -- ) { int d = cardNo [ i ] - '0' ; if ( isSecond == true ) d = d * 2 ; nSum += d / 10 ; nSum += d % 10 ; isSecond = ! isSecond ; } return ( nSum % 10 == 0 ) ; } static public void Main ( ) { String cardNo = "79927398713" ; if ( checkLuhn ( cardNo ) ) Console . WriteLine ( " This ▁ is ▁ a ▁ valid ▁ card " ) ; else Console . WriteLine ( " This ▁ is ▁ not ▁ a ▁ valid ▁ card " ) ; } }


"Lychrel Number Implementation | C # Program to check whether the given number is Lychrel Number or not with given limit on number of iterations . ; Max Iterations ; Function to check whether number is Lychrel Number ; Function to check whether the number is Palindrome ; Function to reverse the number ; Driver program"
Solution in C#:

using System ; class GFG { private static int MAX_ITERATIONS = 20 ; private static bool isLychrel ( long number ) { for ( int i = 0 ; i < MAX_ITERATIONS ; i ++ ) { number = number + reverse ( number ) ; if ( isPalindrome ( number ) ) return false ; } return true ; } private static bool isPalindrome ( long number ) { return number == reverse ( number ) ; } private static long reverse ( long number ) { long reverse = 0 ; while ( number > 0 ) { long remainder = number % 10 ; reverse = ( reverse * 10 ) + remainder ; number = number / 10 ; } return reverse ; } public static void Main ( ) { long number = 295 ; Console . Write ( number + " ▁ is ▁ lychrel ? ▁ " + isLychrel ( number ) ) ; } }


"Lynch | C # program for above approach ; Function to check the divisibility of the number by its digit . ; If the digit divides the number then return true else return false . ; Function to check if all digits of n divide it or not ; Taking the digit of the number into digit var . ; Function to check if N has all distinct digits ; Create an array of size 10 and initialize all elements as false . This array is used to check if a digit is already seen or not . ; Traverse through all digits of given number ; Find the last digit ; If digit is already seen , return false ; Mark this digit as seen ; REmove the last digit from number ; Function to check Lynch - Bell numbers ; Driver Code ; Given Number N ; Function Call"
Solution in C#:

using System ; class GFG { static bool checkDivisibility ( int n , int digit ) { return ( digit != 0 && n % digit == 0 ) ; } static bool isAllDigitsDivide ( int n ) { int temp = n ; while ( temp > 0 ) { int digit = temp % 10 ; if ( ! ( checkDivisibility ( n , digit ) ) ) return false ; temp /= 10 ; } return true ; } static bool isAllDigitsDistinct ( int n ) { bool [ ] arr = new bool [ 10 ] ; while ( n > 0 ) { int digit = n % 10 ; if ( arr [ digit ] ) return false ; arr [ digit ] = true ; n = n / 10 ; } return true ; } static bool isLynchBell ( int n ) { return isAllDigitsDivide ( n ) && isAllDigitsDistinct ( n ) ; } public static void Main ( ) { int N = 12 ; if ( isLynchBell ( N ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"MO 's Algorithm (Query Square Root Decomposition) | Set 1 (Introduction) | C # program to compute sum of ranges for different range queries ; Class to represent a query range ; Prints sum of all query ranges . m is number of queries n is the size of the array . ; One by one compute sum of all queries ; Left and right boundaries of current range ; Compute sum of current query range ; Print sum of current query range ; Driver code"
Solution in C#:

 using System ; using System . Collections ; public class Query { public int L ; public int R ; public Query ( int L , int R ) { this . L = L ; this . R = R ; } } class GFG { static void printQuerySums ( int [ ] a , int n , ArrayList q , int m ) { for ( int i = 0 ; i < m ; i ++ ) { int L = ( ( Query ) q [ i ] ) . L , R = ( ( Query ) q [ i ] ) . R ; int sum = 0 ; for ( int j = L ; j <= R ; j ++ ) sum += a [ j ] ; Console . Write ( " Sum ▁ of ▁ [ " + L + " , ▁ " + R + " ] ▁ is ▁ " + sum + "
 " ) ; } } public static void Main ( string [ ] argv ) { int [ ] a = { 1 , 1 , 2 , 1 , 3 , 4 , 5 , 2 , 8 } ; int n = a . Length ; ArrayList q = new ArrayList ( ) ; q . Add ( new Query ( 0 , 4 ) ) ; q . Add ( new Query ( 1 , 3 ) ) ; q . Add ( new Query ( 2 , 4 ) ) ; int m = q . Count ; printQuerySums ( a , n , q , m ) ; } }


"Magical Indices in an array | C # program to find number of magical indices in the given array . ; Function to count number of magical indices . ; Array to store parent node of traversal . ; Array to determine whether current node is already counted in the cycle . ; Initialize the arrays . ; Check if current node is already traversed or not . If node is not traversed yet then parent value will be - 1. ; Traverse the graph until an already visited node is not found . ; Check parent value to ensure a cycle is present . ; Count number of nodes in the cycle . ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static int solve ( int [ ] A , int n ) { int i , cnt = 0 , j ; int [ ] parent = new int [ n + 1 ] ; int [ ] vis = new int [ n + 1 ] ; for ( i = 0 ; i < n + 1 ; i ++ ) { parent [ i ] = - 1 ; vis [ i ] = 0 ; } for ( i = 0 ; i < n ; i ++ ) { j = i ; if ( parent [ j ] == - 1 ) { while ( parent [ j ] == - 1 ) { parent [ j ] = i ; j = ( j + A [ j ] + 1 ) % n ; } if ( parent [ j ] == i ) { while ( vis [ j ] == 0 ) { vis [ j ] = 1 ; cnt ++ ; j = ( j + A [ j ] + 1 ) % n ; } } } } return cnt ; } public static void Main ( ) { int [ ] A = { 0 , 0 , 0 , 2 } ; int n = A . Length ; Console . WriteLine ( solve ( A , n ) ) ; } }


"Magnanimous Numbers | C # implementation to check if a number is Magnanimous ; Function to check if n is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to check if the number is Magnanimous or not ; Converting the number to string ; Finding length of string ; Number should not be of single digit ; Loop to find all left and right part of the string ; Driver code"
Solution in C#:

 using System ; class GFG { static bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static bool isMagnanimous ( int N ) { String s = N . ToString ( ) ; int l = s . Length ; if ( l < 2 ) return false ; for ( int i = 0 ; i < l - 1 ; i ++ ) { String left = s . Substring ( 0 , i + 1 ) ; String right = s . Substring ( i + 1 ) ; int x = int . Parse ( left ) ; int y = int . Parse ( right ) ; if ( ! isPrime ( x + y ) ) return false ; } return true ; } public static void Main ( String [ ] args ) { int N = 12 ; if ( isMagnanimous ( N ) ) Console . Write ( " Yes
 " ) ; else Console . Write ( " No
 " ) ; } }


"Make A , B and C equal by adding total value N to them | C # program to distribute integer N among A , B , C such that they become equal ; Find maximum among the three elements ; Summation of three elements ; Check if difference is divisible by 3 ; Driver code"
Solution in C#:

using System ; class GFG { static void distributeN ( int A , int B , int C , int n ) { int max1 = Math . Max ( A , B ) ; int max2 = Math . Max ( B , C ) ; int maximum = Math . Max ( max1 , max2 ) ; int sum = A + B + C ; int p = ( 3 * maximum ) - sum ; int diff = n - p ; if ( diff < 0 diff % 3 == 0 ) Console . Write ( " No " ) ; else Console . Write ( " Yes " ) ; } public static void Main ( String [ ] args ) { int A = 10 , B = 20 ; int C = 15 , n = 14 ; distributeN ( A , B , C , n ) ; } }


"Make Array elements equal by replacing adjacent elements with their XOR | C # Program of the above approach ; Function to check if it is possible to make all the array elements equal using the given operation ; Stores the XOR of all elements of array A [ ] ; Case 1 , check if the XOR of the array A [ ] is 0 ; Maintains the XOR till the current element ; Iterate over the array ; If the current XOR is equal to the total XOR increment the count and initialize current XOR as 0 ; Print Answer ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static void possibleEqualArray ( int [ ] A , int N ) { int tot_XOR = 0 ; for ( int i = 0 ; i < N ; i ++ ) { tot_XOR ^= A [ i ] ; } if ( tot_XOR == 0 ) { Console . Write ( " YES " ) ; return ; } int cur_XOR = 0 ; int cnt = 0 ; for ( int i = 0 ; i < N ; i ++ ) { cur_XOR ^= A [ i ] ; if ( cur_XOR == tot_XOR ) { cnt ++ ; cur_XOR = 0 ; } } if ( cnt > 2 ) { Console . Write ( " YES " ) ; } else { Console . Write ( " NO " ) ; } } public static void Main ( String [ ] args ) { int [ ] A = { 0 , 2 , 2 } ; int N = ( A . Length ) ; possibleEqualArray ( A , N ) ; } }


"Make Array elements equal by replacing adjacent elements with their XOR | C # program for the above approach ; Function to check if it is possible to make all the array elements equal using the given operation ; Stores the prefix XOR array ; Calculate prefix [ i ] ; Case 1 , check if the XOR of the input array is 0 ; Case 2 Iterate over all the ways to divide the array into three non empty subarrays ; XOR of Middle Block ; XOR of Left Block ; XOR of Right Block ; Not Possible ; Driver code ; Function Call"
Solution in C#:

using System ; public class GFG { static void possibleEqualArray ( int [ ] A , int N ) { int [ ] pref = new int [ N ] ; pref [ 0 ] = A [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { pref [ i ] = pref [ i - 1 ] ^ A [ i ] ; } if ( pref [ N - 1 ] == 0 ) { Console . WriteLine ( " YES " ) ; return ; } int cur_xor = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { cur_xor ^= A [ i ] ; for ( int j = 0 ; j < i ; j ++ ) { if ( j != 0 ) { int middle_xor = pref [ j - 1 ] ^ pref [ i - 1 ] ; int left_xor = pref [ j - 1 ] ; int right_xor = cur_xor ; if ( left_xor == middle_xor && middle_xor == right_xor ) { Console . WriteLine ( " YES " ) ; return ; } } } } Console . WriteLine ( " NO " ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 0 , 2 , 2 } ; int N = A . Length ; possibleEqualArray ( A , N ) ; } }


"Make N pairs from Array as ( X , Y ) coordinate point that are enclosed inside a minimum area rectangle | C # program for the above approach ; Function to make N pairs of coordinates such that they are enclosed in a minimum area rectangle with sides parallel to the X and Y axes ; A variable to store the answer ; For the case where the maximum and minimum are in different partitions ; For the case where the maximum and minimum are in the same partition ; Return the answer ; Driver code ; Given Input ; Function call"
Solution in C#:

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


"Make a given Binary String non | C # program for the above approach ; Function to return the length of smallest subsequence required to be removed to make the given string non - decreasing ; Length of the string ; Count of zeros and ones ; Traverse the string ; Count minimum removals to obtain strings of the form "00000 . . . . " or "11111 . . . " ; Increment count ; Remove 1 s and remaining 0 s ; Driver code"
Solution in C#:

using System ; class GFG { public static void min_length ( string str ) { int n = str . Length ; int total_zeros = 0 ; int total_ones = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == '0' ) { total_zeros ++ ; } else { total_ones ++ ; } } int ans = Math . Min ( total_zeros , total_ones ) ; int cur_zeros = 0 , cur_ones = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { char x = str [ i ] ; if ( x == '0' ) { cur_zeros ++ ; } else { cur_ones ++ ; } ans = Math . Min ( ans , cur_ones + ( total_zeros - cur_zeros ) ) ; } Console . WriteLine ( ans ) ; } static public void Main ( ) { string str = "10011" ; min_length ( str ) ; } }


"Make a palindromic string from given string | C # implementation to find which player can form a palindromic string first in a game ; Function to find winner of the game ; Array to maintain frequency of the characters in S ; Maintain count of all distinct characters ; Finding frequency of each character ; Count unique duplicate characters ; Loop to count the unique duplicate characters ; Condition for Player - 1 to be winner ; Else Player - 2 is always winner ; Driver Code ; Function call"
Solution in C#:

using System ; class GFG { static int palindromeWinner ( string S ) { int [ ] freq = new int [ 26 ] ; int count = 0 ; for ( int i = 0 ; i < ( int ) S . Length ; ++ i ) { if ( freq [ S [ i ] - ' a ' ] == 0 ) count ++ ; freq [ S [ i ] - ' a ' ] ++ ; } int unique = 0 ; int duplicate = 0 ; for ( int i = 0 ; i < 26 ; ++ i ) { if ( freq [ i ] == 1 ) unique ++ ; else if ( freq [ i ] >= 2 ) duplicate ++ ; } if ( unique == 1 && ( unique + duplicate ) == count ) return 1 ; return 2 ; } public static void Main ( string [ ] s ) { string S = " abcbc " ; Console . Write ( " Player - " + palindromeWinner ( S ) ) ; } }


"Make a string from another by deletion and rearrangement of characters | C # program to find if it is possible to make a string from characters present in other string . ; Returns true if it is possible to make s1 from characters present in s2 . ; Count occurrences of all characters present in s2 . . ; For every character , present in s1 , reduce its count if count is more than 0. ; Driver code"
Solution in C#:

using System ; class GFG { static int MAX_CHAR = 256 ; static bool isPossible ( String s1 , String s2 ) { int [ ] count = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < s2 . Length ; i ++ ) count [ ( int ) s2 [ i ] ] ++ ; for ( int i = 0 ; i < s1 . Length ; i ++ ) { if ( count [ ( int ) s1 [ i ] ] == 0 ) return false ; count [ ( int ) s1 [ i ] ] -- ; } return true ; } public static void Main ( ) { string s1 = " GeeksforGeeks " , s2 = " rteksfoGrdsskGeggehes " ; if ( isPossible ( s1 , s2 ) ) Console . WriteLine ( " Possible " ) ; else Console . WriteLine ( " Not ▁ Possible " ) ; } }


"Make all array elements equal by reducing array elements to half minimum number of times | C # program for the above approach ; Function to find minimum number of operations ; Initialize map ; Traverse the array ; Divide current array element until it reduces to 1 ; Traverse the map ; Find the maximum element having frequency equal to N ; Stores the minimum number of operations required ; Count operations required to convert current element to mx ; Print the answer ; Driver code ; Given array ; Size of the array"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void minOperations ( int [ ] arr , int N ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { int res = arr [ i ] ; while ( res > 0 ) { if ( mp . ContainsKey ( res ) ) { mp [ res ] ++ ; } else { mp [ res ] = 1 ; } res /= 2 ; } } int mx = 1 ; foreach ( KeyValuePair < int , int > it in mp ) { if ( it . Value == N ) { mx = it . Key ; } } int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int res = arr [ i ] ; while ( res != mx ) { ans ++ ; res /= 2 ; } } Console . Write ( ans ) ; } static void Main ( ) { int [ ] arr = { 3 , 1 , 1 , 3 } ; int N = arr . Length ; minOperations ( arr , N ) ; } }


"Make all array elements equal by repeated subtraction of absolute difference of pairs from their maximum | C # program for the above approach ; Function to return gcd of a and b ; Base Case ; Recursive call ; Function to find gcd of array ; Initialise the result ; Traverse the array arr [ ] ; Update result as gcd of the result and arr [ i ] ; Return the resultant GCD ; Driver Code ; Given array arr [ ] ; Function call"
Solution in C#:

using System ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int findGCD ( int [ ] arr , int N ) { int result = 0 ; foreach ( int element in arr ) { result = gcd ( result , element ) ; if ( result == 1 ) { return 1 ; } } return result ; } public static void Main ( ) { int [ ] arr = { 2 , 3 , 4 } ; int N = arr . Length ; Console . WriteLine ( findGCD ( arr , N ) ) ; } }


"Make all array elements equal to 0 by replacing minimum subsequences consisting of equal elements | C # program for the above approach ; Function to find minimum count of operations required to convert all array elements to zero br replacing subsequence of equal elements to 0 ; Store distinct elements present in the array ; Traverse the array ; If arr [ i ] is already present in the Set or arr [ i ] is equal to 0 ; Otherwise , increment ans by 1 and insert current element ; Driver Code ; Given array ; Size of the given array"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void minOpsToTurnArrToZero ( int [ ] arr , int N ) { HashSet < int > st = new HashSet < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( st . Contains ( arr [ i ] ) arr [ i ] == 0 ) { continue ; } else { st . Add ( arr [ i ] ) ; } } Console . WriteLine ( st . Count ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 7 , 3 } ; int N = arr . Length ; minOpsToTurnArrToZero ( arr , N ) ; } }


"Make all array elements equal to K by repeatedly incrementing subsequences | C # program for the above approach ; Function to find the minimum number of operations required to make all elements equal to k ; Initialize a hashmap ; Store frequency of array elements ; Store the minimum number of operations required ; Iterate until all array elements becomes equal to K ; Iterate through range [ 1 , k - 1 ] since only one element can be increased from each group ; Check if the current number has frequency > 0 , i . e . , it is a part of a group ; If true , decrease the frequency of current group of element by 1 ; Increase the frequency of the next group of elements by 1 ; If the next element is not the part of any group , then skip it ; Increment count of operations ; Print the count of operations ; Driver Code ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { static void minOperations ( int [ ] arr , int n , int k ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1 ; } else { mp . Add ( arr [ i ] , 1 ) ; } } int ans = 0 ; while ( mp . ContainsKey ( k ) == false mp [ k ] < n ) { for ( int i = 1 ; i <= k - 1 ; i ++ ) { if ( mp . ContainsKey ( i ) && mp [ i ] > 0 ) { mp [ i ] = mp [ i ] - 1 ; if ( mp . ContainsKey ( i + 1 ) ) mp [ i + 1 ] = mp [ i + 1 ] + 1 ; else mp . Add ( i + 1 , 1 ) ; if ( mp . ContainsKey ( i + 1 ) && mp [ i + 1 ] == 1 ) { i ++ ; } } } ans ++ ; } Console . Write ( ans ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 3 , 4 } ; int K = 5 ; int N = arr . Length ; minOperations ( arr , N , K ) ; } }


"Make all array elements equal with minimum cost | C # Code to Make all array elements equal with minimum cost ; Utility method to compute cost , when all values of array are made equal to X ; Method to find minimum cost to make all elements equal ; setting limits for ternary search by smallest and largest element ; loop until difference between low and high become less than 3 , because after that mid1 and mid2 will start repeating ; mid1 and mid2 are representative array equal values of search space ; if mid2 point gives more total cost , skip third part ; if mid1 point gives more total cost , skip first part ; computeCost gets optimum cost by sending average of low and high as X ; Driver program to test above function"
Solution in C#:

using System ; class GFG { public static int computeCost ( int [ ] arr , int N , int X ) { int cost = 0 ; for ( int i = 0 ; i < N ; i ++ ) cost += Math . Abs ( arr [ i ] - X ) ; return cost ; } public static int minCostToMakeElementEqual ( int [ ] arr , int N ) { int low , high ; low = high = arr [ 0 ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( low > arr [ i ] ) low = arr [ i ] ; if ( high < arr [ i ] ) high = arr [ i ] ; } while ( ( high - low ) > 2 ) { int mid1 = low + ( high - low ) / 3 ; int mid2 = high - ( high - low ) / 3 ; int cost1 = computeCost ( arr , N , mid1 ) ; int cost2 = computeCost ( arr , N , mid2 ) ; if ( cost1 < cost2 ) high = mid2 ; else low = mid1 ; } return computeCost ( arr , N , ( low + high ) / 2 ) ; } public static void Main ( ) { int [ ] arr = { 1 , 100 , 101 } ; int N = arr . Length ; Console . Write ( minCostToMakeElementEqual ( arr , N ) ) ; } }


"Make all array elements equal with minimum cost | This function assumes that a [ ] is sorted . If a [ ] is not sorted , we need to sort it first . ; If there are odd elements , we choose middle element ; If there are even elements , then we choose the average of middle two . ; After deciding the final value , find the result . ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int minCostToMakeElementEqual ( int [ ] a , int n ) { int y ; if ( n % 2 == 1 ) y = a [ n / 2 ] ; else y = ( a [ n / 2 ] + a [ ( n - 2 ) / 2 ] ) / 2 ; int s = 0 ; for ( int i = 0 ; i < n ; i ++ ) s += Math . Abs ( a [ i ] - y ) ; return s ; } static void Main ( ) { int [ ] a = { 1 , 100 , 101 } ; int n = a . Length ; Console . WriteLine ( minCostToMakeElementEqual ( a , n ) ) ; } }


"Make all characters of a string same by minimum number of increments or decrements of ASCII values of characters | C # program for the above approach ; Function to check if all characters of the string can be made the same ; Sort the string ; Calculate ASCII value of the median character ; Stores the minimum number of operations required to make all characters equal ; Traverse the string ; Calculate absolute value of current character and median character ; Print the minimum number of operations required ; Driver Code"
Solution in C#:

using System ; public class GFG { static void sameChar ( String S , int N ) { char [ ] temp = S . ToCharArray ( ) ; Array . Sort ( temp ) ; String s = new String ( temp ) ; int mid = s [ N / 2 ] ; int total_operations = 0 ; for ( int i = 0 ; i < N ; i ++ ) { total_operations += Math . Abs ( ( s [ i ] - 0 ) - mid ) ; } Console . Write ( total_operations ) ; } static public void Main ( ) { String S = " geeks " ; int N = S . Length ; sameChar ( S , N ) ; } }


"Make all elements of an array equal with the given operation | C # implementation of the approach ; Function that returns true if all the elements of the array can be made equal with the given operation ; To store the sum of the array elements and the maximum element from the array ; Driver code"
Solution in C#:

using System ; class GFG { static bool isPossible ( int n , int k , int [ ] arr ) { int sum = arr [ 0 ] ; int maxVal = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { sum += arr [ i ] ; maxVal = Math . Max ( maxVal , arr [ i ] ) ; } if ( ( float ) maxVal > ( float ) ( sum + k ) / n ) return false ; return true ; } public static void Main ( ) { int k = 8 ; int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . Length ; if ( isPossible ( n , k , arr ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Make all elements zero by decreasing any two elements by one at a time | C # implementation of the above approach ; Function that returns true if all the array elements can be made 0 with the given operation ; Find the maximum element and the sum ; Check the required condition ; Driver code"
Solution in C#:

using System ; class GFG { static bool checkZeroArray ( int [ ] arr , int n ) { int sum = 0 , maximum = int . MinValue ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + arr [ i ] ; maximum = Math . Max ( maximum , arr [ i ] ) ; } if ( sum % 2 == 0 && maximum <= sum / 2 ) return true ; return false ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 1 , 2 , 2 } ; int n = arr . Length ; if ( checkZeroArray ( arr , n ) == true ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Make all numbers of an array equal | C # implementation of above approach ; Function that returns true if all the array elements can be made equal with the given operation ; Divide number by 2 ; Divide number by 3 ; Driver code"
Solution in C#:

using System ; class GFG { static bool EqualNumbers ( int [ ] a , int n ) { for ( int i = 0 ; i < n ; i ++ ) { while ( a [ i ] % 2 == 0 ) { a [ i ] /= 2 ; } while ( a [ i ] % 3 == 0 ) { a [ i ] /= 3 ; } if ( a [ i ] != a [ 0 ] ) { return false ; } } return true ; } public static void Main ( ) { int [ ] a = { 50 , 75 , 150 } ; int n = a . Length ; if ( EqualNumbers ( a , n ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }


"Make all strings from a given array equal by replacing minimum number of characters | C # program to implement the above approach ; Function to find the minimum count of operations required to make all Strings equal by replacing characters of Strings ; Stores minimum count of operations required to make all Strings equal ; Stores length of the String ; hash [ i , j ] : Stores frequency of character i present at j - th index of all Strings ; Traverse the array [ ] arr ; Iterate over characters of current String ; Update frequency of arr [ i , j ] ; Traverse hash [ , ] array ; Stores sum of i - th column ; Stores the largest element of i - th column ; Iterate over all possible characters ; Update Sum ; Update Max ; Update cntMinOP ; Driver Code ; Function call"
Solution in C#:

 using System ; class GFG { static int minOperation ( String [ ] arr , int N ) { int cntMinOP = 0 ; int M = arr [ 0 ] . Length ; int [ , ] hash = new int [ 256 , M ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { hash [ arr [ i ] [ j ] , j ] ++ ; } } for ( int i = 0 ; i < M ; i ++ ) { int Sum = 0 ; int Max = 0 ; for ( int j = 0 ; j < 256 ; j ++ ) { Sum += hash [ j , i ] ; Max = Math . Max ( Max , hash [ j , i ] ) ; } cntMinOP += ( Sum - Max ) ; } return cntMinOP ; } public static void Main ( String [ ] args ) { String [ ] arr = { " abcd " , " bcde " , " cdef " } ; int N = arr . Length ; Console . Write ( minOperation ( arr , N ) + "
 " ) ; } }


"Make all the elements of array even with given operations | C # program to make all array element even ; Function to count the total number of operations needed to make all array element even ; Traverse the given array ; If an odd element occurs then increment that element and next adjacent element by 1 ; Traverse the array if any odd element occurs then return - 1 ; Returns the count of operations ; Driver code"
Solution in C#:

using System ; class GFG { static int countOperations ( int [ ] arr , int n ) { int count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] % 2 == 1 ) { arr [ i ] ++ ; arr [ i + 1 ] ++ ; count += 2 ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 1 ) return - 1 ; } return count ; } public static void Main ( ) { int [ ] arr = { 2 , 3 , 4 , 5 , 6 } ; int n = arr . Length ; Console . Write ( countOperations ( arr , n ) ) ; } }


"Make all the elements of array odd by incrementing odd | C # program for the above approach ; Function to count minimum subarrays whose odd - indexed elements need to be incremented to make array odd ; Stores the minimum number of operations required ; Iterate over even - indices ; Check if the current element is odd ; If true , continue ; Otherwise , mark the starting of the subarray and iterate until i < n and arr [ i ] is even ; Increment number of operations ; Iterate over odd indexed positions of [ ] arr ; Check if the current element is odd ; If true , continue ; Otherwise , mark the starting of the subarray and iterate until i < n and arr [ i ] is even ; Increment the number of operations ; Print the number of operations ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static void minOperations ( int [ ] arr , int n ) { int flips = 0 ; for ( int i = 0 ; i < n ; i += 2 ) { if ( arr [ i ] % 2 == 1 ) { continue ; } while ( i < n && arr [ i ] % 2 == 0 ) { i += 2 ; } flips ++ ; } for ( int i = 1 ; i < n ; i += 2 ) { if ( arr [ i ] % 2 == 1 ) { continue ; } while ( i < n && arr [ i ] % 2 == 0 ) { i += 2 ; } flips ++ ; } Console . WriteLine ( flips ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 4 , 3 , 5 , 3 , 2 } ; int N = arr . Length ; minOperations ( arr , N ) ; } }


"Make an array strictly increasing by repeatedly subtracting and adding arr [ i | C # program for the above approach ; Function to check if an array can be made strictly increasing ; Traverse the given array arr [ ] ; Update the value of p , arr [ i ] , and arr [ i - 1 ] ; Traverse the given array ; Check if the array arr [ ] is strictly increasing or not ; Otherwise , array is increasing order , print Yes ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void check ( int [ ] arr , int n ) { for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i - 1 ] >= ( i - 1 ) ) { int p = arr [ i - 1 ] - ( i - 1 ) ; arr [ i ] += p ; arr [ i - 1 ] -= p ; } } for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] <= arr [ i - 1 ] ) { Console . Write ( " No " ) ; return ; } } Console . Write ( " Yes " ) ; } public static void Main ( ) { int [ ] arr = { 1 , 5 , 2 , 7 , 6 } ; int N = arr . Length ; check ( arr , N ) ; } }


"Make array elements equal in Minimum Steps | C # program to make the array elements equal in minimum steps ; Returns the minimum steps required to make an array of N elements equal , where the first array element equals M ; Corner Case 1 : When N = 1 ; Corner Case 2 : When N = 2 else if ( N == 2 ) corner case 2 ; Driver Code"
Solution in C#:

using System ; class GFG { static int steps ( int N , int M ) { if ( N == 1 ) return 0 ; return M ; return 2 * M + ( N - 3 ) ; } public static void Main ( ) { int N = 4 , M = 4 ; Console . WriteLine ( steps ( N , M ) ) ; } }


"Make array elements equal with minimum cost | C # implementation to make array elements equal with minimum cost ; Function to find the minimum cost required to make array elements equal ; Loop to find the count of odd elements ; Driver Code"
Solution in C#:

using System ; class GFG { static void makearrayequal ( int [ ] arr , int n ) { int x = 0 ; for ( int i = 0 ; i < n ; i ++ ) { x += ( arr [ i ] & 1 ) ; } Console . WriteLine ( Math . Min ( x , n - x ) ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 4 , 3 , 2 , 1 } ; int n = arr . Length ; makearrayequal ( arr , n ) ; } }


"Make lexicographically smallest palindrome by substituting missing characters | C # implementation of the approach ; Function to return the lexicographically smallest palindrome that can be made from the given string after replacing the required characters ; If characters are missing at both the positions then substitute it with ' a ' ; If only str [ j ] = ' * ' then update it with the value at str [ i ] ; If only str [ i ] = ' * ' then update it with the value at str [ j ] ; If characters at both positions are not equal and != ' * ' then the string cannot be made palindrome ; Return the required palindrome ; Driver code"
Solution in C#:

using System ; class GFG { static String makePalindrome ( char [ ] str ) { int i = 0 , j = str . Length - 1 ; while ( i <= j ) { if ( str [ i ] == ' * ' && str [ j ] == ' * ' ) { str [ i ] = ' a ' ; str [ j ] = ' a ' ; } else if ( str [ j ] == ' * ' ) str [ j ] = str [ i ] ; else if ( str [ i ] == ' * ' ) str [ i ] = str [ j ] ; else if ( str [ i ] != str [ j ] ) return " - 1" ; i ++ ; j -- ; } return String . Join ( " " , str ) ; } public static void Main ( String [ ] args ) { char [ ] str = " na * an " . ToCharArray ( ) ; Console . WriteLine ( makePalindrome ( str ) ) ; } }


"Make max elements in B [ ] equal to that of A [ ] by adding / subtracting integers in range [ 0 , K ] | C # program for the above approach ; Function that count the number of integers from array [ ] B such that subtracting element in the range [ 0 , K ] given any element in [ ] A ; To store the count of element ; Traverse the array [ ] B ; Traverse the array [ ] A ; Find the difference ; If difference is atmost K then increment the cnt ; Print the count ; Driver Code ; Given array [ ] A and [ ] B ; Given K ; Function call"
Solution in C#:

using System ; class GFG { static void countElement ( int [ ] A , int N , int [ ] B , int M , int K ) { int cnt = 0 ; for ( int i = 0 ; i < M ; i ++ ) { int currentElement = B [ i ] ; for ( int j = 0 ; j < N ; j ++ ) { int diff = Math . Abs ( currentElement - A [ j ] ) ; if ( diff <= K ) { cnt ++ ; break ; } } } Console . Write ( cnt ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 100 , 65 , 35 , 85 , 55 } ; int [ ] B = { 30 , 60 , 75 , 95 } ; int K = 5 ; int N = A . Length ; int M = B . Length ; countElement ( A , N , B , M , K ) ; } }


"Make middle node head in a linked list | C # program to make middle node as head of Linked list ; Link list node ; Function to get the middle and set at beginning of the linked list ; To traverse list nodes one by one ; To traverse list nodes by skipping one . ; To keep track of previous of middle ; for previous node of middle node ; move one node each time ; move two node each time ; set middle node at head ; To insert a node at the beginning of linked list . ; allocate node ; link the old list off the new node ; move the head to point to the new node ; A function to print a given linked list ; Driver code ; Create a list of 5 nodes"
Solution in C#:

using System ; public class GFG { class Node { public int data ; public Node next ; public Node ( int data ) { this . data = data ; next = null ; } } static Node head ; static void setMiddleHead ( ) { if ( head == null ) return ; Node one_node = head ; Node two_node = head ; Node prev = null ; while ( two_node != null && two_node . next != null ) { prev = one_node ; two_node = two_node . next . next ; one_node = one_node . next ; } prev . next = prev . next . next ; one_node . next = head ; head = one_node ; } static void push ( int new_data ) { Node new_node = new Node ( new_data ) ; new_node . next = head ; head = new_node ; } static void printList ( Node ptr ) { while ( ptr != null ) { Console . Write ( ptr . data + " ▁ " ) ; ptr = ptr . next ; } Console . WriteLine ( ) ; } public static void Main ( String [ ] args ) { head = null ; int i ; for ( i = 5 ; i > 0 ; i -- ) push ( i ) ; Console . Write ( " ▁ list ▁ before : ▁ " ) ; printList ( head ) ; setMiddleHead ( ) ; Console . Write ( " ▁ list ▁ After : ▁ " ) ; printList ( head ) ; } }


"Make palindromic string non | C # Program to rearrange letters of string to find a non - palindromic string if it exists ; Function to print the non - palindromic string if it exists , otherwise prints - 1 ; If all characters are not same , set flag to 1 ; Update frequency of the current character ; If all characters are same ; Print characters in sorted manner ; Driver Code"
Solution in C#:

using System ; class GfG { static void findNonPalinString ( char [ ] s ) { int [ ] freq = new int [ 26 ] ; int flag = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] != s [ 0 ] ) flag = 1 ; freq [ s [ i ] - ' a ' ] ++ ; } if ( flag == 0 ) Console . WriteLine ( " - 1" ) ; else { for ( int i = 0 ; i < 26 ; i ++ ) for ( int j = 0 ; j < freq [ i ] ; j ++ ) Console . Write ( ( char ) ( ' a ' + i ) ) ; } } public static void Main ( ) { string s = " abba " ; findNonPalinString ( s . ToCharArray ( ) ) ; } }


"Make sum of all subarrays of length K equal by only inserting elements | C # implementation of above approach ; Function that prints another array whose all subarray of length k have an equal sum ; Store all distinct elements in the unordered map ; Condition if the number of distinct elements is greater than k ; Push all distinct elements in a vector ; Push 1 while the size of vector not equal to k ; Print the vector 2 times ; Driver Code"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static void MakeArray ( int [ ] a , int n , int k ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! mp . ContainsKey ( a [ i ] ) ) mp . Add ( a [ i ] , 1 ) ; } if ( mp . Count > k ) { Console . Write ( " Not ▁ possible
 " ) ; return ; } List < int > ans = new List < int > ( ) ; foreach ( KeyValuePair < int , int > i in mp ) { ans . Add ( i . Key ) ; } while ( ans . Count < k ) { ans . Add ( 1 ) ; } for ( int i = 0 ; i < 2 ; i ++ ) { for ( int j = 0 ; j < k ; j ++ ) Console . Write ( ans [ j ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 2 , 1 } ; int K = 2 ; int size = arr . Length ; MakeArray ( arr , size , K ) ; } }


"Make the Array sum 0 by using either ceil or floor on each element | C # program for the above approach ; Function to modify the array element such that the sum is close to 0 ; Stores the modified elements ; Stores the sum of array element ; Stores minimum size of the array ; Traverse the array and find the sum ; If sum is positive ; Find the minimum number of elements that must be changed ; Iterate until M elements are modified or the array end ; Update the current array elements to its floor ; Print the resultant array ; Driver Code"
Solution in C#:

using System ; public class GFG { static void setSumtoZero ( double [ ] arr , int N ) { int [ ] A = new int [ N ] ; int sum = 0 ; int m = int . MinValue ; for ( int i = 0 ; i < N ; i ++ ) { sum += ( int ) Math . Ceiling ( arr [ i ] ) ; A [ i ] = ( int ) Math . Ceiling ( arr [ i ] ) ; } if ( sum > 0 ) { m = Math . Min ( sum , N ) ; for ( int i = 0 ; i < N && m > 0 ; i ++ ) { A [ i ] = ( int ) Math . Floor ( arr [ i ] ) ; if ( A [ i ] != Math . Floor ( arr [ i ] ) ) m -- ; } } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( A [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { double [ ] arr = { - 2 , - 2 , 4.5 } ; int N = arr . Length ; setSumtoZero ( arr , N ) ; } }


"Make the array elements equal by performing given operations minimum number of times | C # Program for the above approach ; Function to calculate the minimum operations of given type required to make the array elements equal ; Stores the total count of operations ; Traverse the array ; Update difference between pairs of adjacent elements ; Store the maximum count of operations ; Rest of the elements ; Total Operation - Maximum Operation ; Driver Code ; Given array ; Size of the array"
Solution in C#:

using System ; public class GFG { static void minOperation ( int [ ] a , int N ) { int totOps = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { totOps += Math . Abs ( a [ i ] - a [ i + 1 ] ) ; } int maxOps = Math . Max ( Math . Abs ( a [ 0 ] - a [ 1 ] ) , Math . Abs ( a [ N - 1 ] - a [ N - 2 ] ) ) ; for ( int i = 1 ; i < N - 1 ; i ++ ) { maxOps = Math . Max ( maxOps , Math . Abs ( a [ i ] - a [ i - 1 ] ) + Math . Abs ( a [ i ] - a [ i + 1 ] ) - Math . Abs ( a [ i - 1 ] - a [ i + 1 ] ) ) ; } Console . WriteLine ( totOps - maxOps ) ; } static public void Main ( ) { int [ ] arr = { 1 , - 1 , 0 , 1 , 1 } ; int N = arr . Length ; minOperation ( arr , N ) ; } }


"Make the array non | C # implementation of the approach ; Function to make array non - decreasing ; Take the first element ; Perform the operation ; Traverse the array ; Next element ; If next element is greater than the current element then decrease it to increase the possibilities ; It is not possible to make the array non - decreasing with the given operation ; Next element is now the current ; The array can be made non - decreasing with the given operation ; Driver code"
Solution in C#:

using System ; class GFG { static Boolean isPossible ( int [ ] a , int n ) { int cur = a [ 0 ] ; cur -- ; for ( int i = 1 ; i < n ; i ++ ) { int nxt = a [ i ] ; if ( nxt > cur ) nxt -- ; else if ( nxt < cur ) return false ; cur = nxt ; } return true ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 2 , 1 , 2 , 3 } ; int n = a . Length ; if ( isPossible ( a , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Make the list non | C # implementation of the approach ; Function to return the minimum element from the range [ prev , MAX ] such that it differs in at most 1 digit with cur ; To start with the value we have achieved in the last step ; Store the value with which the current will be compared ; Current value ; There are at most 4 digits ; If the current digit differs in both the numbers ; Eliminate last digits in both the numbers ; If the number of different digits is at most 1 ; If we can 't find any number for which  the number of change is less than or  equal to 1 then return -1 ; Function to get the non - decreasing list ; Creating a vector for the updated list ; Let 's assume that it is possible to  make the list non-decreasing ; Element of the original array ; Can 't make the list non-decreasing ; If possible then print the list ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int DIGITS = 4 , MIN = 1000 , MAX = 9999 ; static int getBest ( int prev , int cur ) { int maximum = Math . Max ( MIN , prev ) ; for ( int i = maximum ; i <= MAX ; i ++ ) { int cnt = 0 ; int a = i ; int b = cur ; for ( int k = 0 ; k < DIGITS ; k ++ ) { if ( a % 10 != b % 10 ) cnt += 1 ; a /= 10 ; b /= 10 ; } if ( cnt <= 1 ) return i ; } return - 1 ; } static void getList ( int [ ] arr , int n ) { List < int > myList = new List < int > ( ) ; int i , cur ; bool possible = true ; myList . Add ( 0 ) ; for ( i = 0 ; i < n ; i ++ ) { cur = arr [ i ] ; myList . Add ( getBest ( myList [ myList . Count - 1 ] , cur ) ) ; if ( myList [ myList . Count - 1 ] == - 1 ) { possible = false ; break ; } } if ( possible ) { for ( i = 1 ; i < myList . Count ; i ++ ) Console . Write ( myList [ i ] + " ▁ " ) ; } else Console . Write ( " - 1" ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1095 , 1094 , 1095 } ; int n = arr . Length ; getList ( arr , n ) ; } }


"Make the string in AP by changing a character | C # program for the above problem ; Function to modify the given String and find the index where modification is needed ; Array to store the ASCII values of alphabets ; loop to compute the ASCII values of characters a - z ; Set to store all the possible differences between consecutive elements ; Loop to find out the differences between consecutive elements and storing them in the set ; Checks if any character of the String disobeys the pattern ; Constructing the Strings with all possible values of consecutive difference and comparing them with staring String S . ; Driver Code"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static void string_modify ( char [ ] s ) { char [ ] alphabets = new char [ 26 ] ; int flag = 0 , hold_i = 0 ; char hold_l = ( char ) 0 ; int i ; for ( i = 0 ; i < 26 ; i ++ ) { alphabets [ i ] = ( char ) ( i + ' a ' ) ; } HashSet < int > difference = new HashSet < int > ( ) ; String reconstruct = " " ; for ( i = 1 ; i < s . Length ; i ++ ) { difference . Add ( s [ i ] - s [ i - 1 ] ) ; } if ( difference . Count == 1 ) { Console . Write ( " No ▁ modifications ▁ required " ) ; return ; } foreach ( int it in difference ) { int index = s [ 0 ] - ' a ' ; reconstruct = " " ; flag = 0 ; for ( i = 0 ; i < s . Length && flag <= 1 ; i ++ ) { reconstruct += alphabets [ index ] ; index += it ; if ( index < 0 ) { index += 26 ; } index %= 26 ; if ( reconstruct [ i ] != s [ i ] ) { flag ++ ; hold_i = i ; hold_l = s [ i ] ; } } if ( flag == 1 ) { s [ hold_i ] = reconstruct [ hold_i ] ; break ; } } if ( flag < 1 ) { hold_i = 0 ; hold_l = s [ 0 ] ; int temp = ( s [ 1 ] - ' a ' - ( s [ 2 ] - s [ 1 ] ) ) % 26 ; if ( temp < 0 ) { temp += 26 ; } s [ 0 ] = alphabets [ temp ] ; } Console . Write ( hold_i + " ▁ - > ▁ " + hold_l + "
 " + String . Join ( " " , s ) + "
 " ) ; } public static void Main ( String [ ] args ) { String s = " aeimqux " ; string_modify ( s . ToCharArray ( ) ) ; } }


"Make the string lexicographically smallest and non palindromic by replacing exactly one character | C # program to make the string lexicographically smallest non palindromic string by replacing exactly one character ; Function to find the required string ; Length of the string ; Iterate till half of the string ; Replacing a non ' a ' char with ' a ' ; Check if there is no ' a ' in string we replace last char of string by ' b ' ; If the input is a single character ; Driver code"
Solution in C#:

using System ; class GFG { static String findStr ( char [ ] S ) { int n = S . Length ; for ( int i = 0 ; i < n / 2 ; ++ i ) { if ( S [ i ] != ' a ' ) { S [ i ] = ' a ' ; return new String ( S ) ; } } S [ n - 1 ] = ' b ' ; return n < 2 ? " ▁ - 1 ▁ " : new String ( S ) ; } public static void Main ( String [ ] args ) { String str = " a " ; Console . WriteLine ( findStr ( str . ToCharArray ( ) ) ) ; String str1 = " abccba " ; Console . WriteLine ( findStr ( str1 . ToCharArray ( ) ) ) ; } }


"Making elements distinct in a sorted array by minimum increments | C # program to make sorted array elements distinct by incrementing elements and keeping sum to minimum . ; To find minimum sum of unique elements . ; While current element is same as previous or has become smaller than previous . ; Driver code"
Solution in C#:

using System ; class GFG { static int minSum ( int [ ] arr , int n ) { int sum = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] ) { int j = i ; while ( j < n && arr [ j ] <= arr [ j - 1 ] ) { arr [ j ] = arr [ j ] + 1 ; j ++ ; } } sum = sum + arr [ i ] ; } return sum ; } public static void Main ( ) { int [ ] arr = { 2 , 2 , 3 , 5 , 6 } ; int n = arr . Length ; Console . WriteLine ( minSum ( arr , n ) ) ; } }


"Making elements distinct in a sorted array by minimum increments | Efficient C # program to make sorted array elements distinct by incrementing elements and keeping sum to minimum . ; To find minimum sum of unique elements . ; If violation happens , make current value as 1 plus previous value and add to sum . ; No violation . ; Drivers code"
Solution in C#:

using System ; class GFG { static int minSum ( int [ ] arr , int n ) { int sum = arr [ 0 ] , prev = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] <= prev ) { prev = prev + 1 ; sum = sum + prev ; } else { sum = sum + arr [ i ] ; prev = arr [ i ] ; } } return sum ; } public static void Main ( ) { int [ ] arr = { 2 , 2 , 3 , 5 , 6 } ; int n = arr . Length ; Console . WriteLine ( minSum ( arr , n ) ) ; } }


"Making elements of two arrays same with minimum increment / decrement | C # program to find minimum increment / decrement operations to make array elements same . ; sorting both arrays in ascending order ; variable to store the final result ; After sorting both arrays Now each array is in non - decreasing order . Thus , we will now compare each element of the array and do the increment or decrement operation depending upon the value of array b [ ] . ; Driver code"
Solution in C#:

using System ; public class GFG { static int MinOperation ( int [ ] a , int [ ] b , int n ) { Array . Sort ( a ) ; Array . Sort ( b ) ; int result = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( a [ i ] > b [ i ] ) result = result + Math . Abs ( a [ i ] - b [ i ] ) ; else if ( a [ i ] < b [ i ] ) result = result + Math . Abs ( a [ i ] - b [ i ] ) ; } return result ; } public static void Main ( ) { int [ ] a = { 3 , 1 , 1 } ; int [ ] b = { 1 , 2 , 2 } ; int n = a . Length ; Console . WriteLine ( MinOperation ( a , b , n ) ) ; } }


"Making three numbers equal with the given operations | C # implementation of the approach ; Function that returns true if a , b and c can be made equal with the given operations ; Sort the three numbers ; Find the sum of difference of the 3 rd and 2 nd element and the 3 rd and 1 st element ; Subtract the difference from k ; Check the required condition ; Driver code"
Solution in C#:

using System ; class GFG { static bool canBeEqual ( int a , int b , int c , int k ) { int [ ] arr = new int [ 3 ] ; arr [ 0 ] = a ; arr [ 1 ] = b ; arr [ 2 ] = c ; Array . Sort ( arr ) ; int diff = 2 * arr [ 2 ] - arr [ 1 ] - arr [ 0 ] ; k = k - diff ; if ( k < 0 k % 3 != 0 ) return false ; return true ; } public static void Main ( String [ ] args ) { int a1 = 6 , b1 = 3 , c1 = 2 , k1 = 7 ; if ( canBeEqual ( a1 , b1 , c1 , k1 ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Making zero array by decrementing pairs of adjacent | C # implementation of the above approach ; converting array element into number ; Check if divisible by 11 ; Driver Code"
Solution in C#:

using System ; class GFG { static bool isPossibleToZero ( int [ ] a , int n ) { int num = 0 ; for ( int i = 0 ; i < n ; i ++ ) num = num * 10 + a [ i ] ; return ( num % 11 == 0 ) ; } public static void Main ( ) { int [ ] arr = { 0 , 1 , 1 , 0 } ; int n = arr . Length ; if ( isPossibleToZero ( arr , n ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }


"Making zero array by decrementing pairs of adjacent | C # program to find if it is possible to make all array elements 0 by decrement operations . ; used for storing the sum of even and odd position element in array . ; if position is odd , store sum value of odd position in odd ; if position is even , store sum value of even position in even ; Driver Code"
Solution in C#:

using System ; class GFG { static bool isPossibleToZero ( int [ ] a , int n ) { int even = 0 , odd = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( i & 1 ) == 0 ) odd += a [ i ] ; else even += a [ i ] ; } return ( odd == even ) ; } static public void Main ( ) { int [ ] arr = { 0 , 1 , 1 , 0 } ; int n = arr . Length ; if ( isPossibleToZero ( arr , n ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }


"Manacher 's Algorithm | C # program to implement Manacher 's Algorithm ; Position count ; LPS Length Array ; centerPosition ; centerRightPosition ; currentRightPosition ; currentLeftPosition ; Uncomment it to print LPS Length array printf ( " % d ▁ % d ▁ " , L [ 0 ] , L [ 1 ] ) ; ; get currentLeftPosition iMirror for currentRightPosition i ; If currentRightPosition i is within centerRightPosition R ; Attempt to expand palindrome centered at currentRightPosition i . Here for odd positions , we compare characters and if match then increment LPS Length by ONE . If even position , we just increment LPS by ONE without any character comparison ; Track maxLPSLength ; If palindrome centered at currentRightPosition i expand beyond centerRightPosition R , adjust centerPosition C based on expanded palindrome . ; Uncomment it to print LPS Length array printf ( " % d ▁ " , L [ i ] ) ; ; Driver Code"
Solution in C#:

using System ; class GFG { static void findLongestPalindromicString ( String text ) { int N = text . Length ; if ( N == 0 ) return ; N = 2 * N + 1 ; int [ ] L = new int [ N + 1 ] ; L [ 0 ] = 0 ; L [ 1 ] = 1 ; int C = 1 ; int R = 2 ; int i = 0 ; int iMirror ; int maxLPSLength = 0 ; int maxLPSCenterPosition = 0 ; int start = - 1 ; int end = - 1 ; int diff = - 1 ; for ( i = 2 ; i < N ; i ++ ) { iMirror = 2 * C - i ; L [ i ] = 0 ; diff = R - i ; if ( diff > 0 ) L [ i ] = Math . Min ( L [ iMirror ] , diff ) ; while ( ( ( i + L [ i ] ) + 1 < N && ( i - L [ i ] ) > 0 ) && ( ( ( i + L [ i ] + 1 ) % 2 == 0 ) || ( text [ ( i + L [ i ] + 1 ) / 2 ] == text [ ( i - L [ i ] - 1 ) / 2 ] ) ) ) { L [ i ] ++ ; } if ( L [ i ] > maxLPSLength ) { maxLPSLength = L [ i ] ; maxLPSCenterPosition = i ; } if ( i + L [ i ] > R ) { C = i ; R = i + L [ i ] ; } } start = ( maxLPSCenterPosition - maxLPSLength ) / 2 ; end = start + maxLPSLength - 1 ; Console . Write ( " LPS ▁ of ▁ string ▁ is ▁ { 0 } ▁ : ▁ " , text ) ; for ( i = start ; i <= end ; i ++ ) Console . Write ( text [ i ] ) ; Console . WriteLine ( ) ; } public static void Main ( String [ ] args ) { String text = " babcbabcbaccba " ; findLongestPalindromicString ( text ) ; text = " abaaba " ; findLongestPalindromicString ( text ) ; text = " abababa " ; findLongestPalindromicString ( text ) ; text = " abcbabcbabcba " ; findLongestPalindromicString ( text ) ; text = " forgeeksskeegfor " ; findLongestPalindromicString ( text ) ; text = " caba " ; findLongestPalindromicString ( text ) ; text = " abacdfgdcaba " ; findLongestPalindromicString ( text ) ; text = " abacdfgdcabba " ; findLongestPalindromicString ( text ) ; text = " abacdedcaba " ; findLongestPalindromicString ( text ) ; } }


"Map every character of one string to another such that all occurrences are mapped to the same character | C # implementation of the approach ; Function that returns true if the mapping is possible ; Both the strings are of un - equal lengths ; To store the frequencies of the characters in both the string ; Update frequencies of the characters ; For every character of s1 ; If current character is not present in s1 ; Find a character in s2 that has frequency equal to the current character 's  frequency in s1 ; If such character is found ; Set the frequency to - 1 so that it doesn 't get picked again ; Set found to true ; If there is no character in s2 that could be mapped to the current character in s1 ; Driver code"
Solution in C#:

using System ; class GFG { static int MAX = 26 ; public static Boolean canBeMapped ( String s1 , int l1 , String s2 , int l2 ) { if ( l1 != l2 ) return false ; int [ ] freq1 = new int [ MAX ] ; int [ ] freq2 = new int [ MAX ] ; for ( int i = 0 ; i < l1 ; i ++ ) freq1 [ s1 [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < l2 ; i ++ ) freq2 [ s2 [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( freq1 [ i ] == 0 ) continue ; Boolean found = false ; for ( int j = 0 ; j < MAX ; j ++ ) { if ( freq1 [ i ] == freq2 [ j ] ) { freq2 [ j ] = - 1 ; found = true ; break ; } } if ( ! found ) return false ; } return true ; } public static void Main ( String [ ] args ) { String s1 = " axx " ; String s2 = " cbc " ; int l1 = s1 . Length ; int l2 = s2 . Length ; if ( canBeMapped ( s1 , l1 , s2 , l2 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Master Theorem For Subtract and Conquer Recurrences | C # code for above the approach . ; Driver Code"
Solution in C#:

using System ; class GFG { static int fib ( int n ) { if ( n <= 1 ) return n ; return fib ( n - 1 ) + fib ( n - 2 ) ; } public static void Main ( String [ ] args ) { int n = 9 ; Console . WriteLine ( fib ( n ) ) ; } }


"Match Expression where a single special character in pattern can match one or more characters | C # program for pattern matching where a single special character can match one more characters ; Returns true if pat matches with text . ; i is used as an index in pattern and j as an index in text . ; Traverse through pattern ; If current character of pattern is not ' # ' ; If does not match with text . ; If matches , increment i and j ; Current character is ' # ' ; At least one character must match with # ; Match characters with # until a matching character is found . ; Matching with # is over , move ahead in pattern ; Driver code"
Solution in C#:

using System ; class GFG { public static bool regexMatch ( String text , String pat ) { int lenText = text . Length ; int lenPat = pat . Length ; char [ ] Text = text . ToCharArray ( ) ; char [ ] Pat = pat . ToCharArray ( ) ; int i = 0 , j = 0 ; while ( i < lenPat ) { if ( Pat [ i ] != ' # ' ) { if ( Pat [ i ] != Text [ j ] ) return false ; i ++ ; j ++ ; } else { j ++ ; while ( Text [ j ] != Pat [ i + 1 ] ) j ++ ; i ++ ; } } return ( j == lenText ) ; } public static void Main ( ) { String str = " ABABABA " ; String pat = " A # B # A " ; if ( regexMatch ( str , pat ) ) Console . Write ( " yes " ) ; else Console . Write ( " no " ) ; } }


"Matrix Chain Multiplication ( A O ( N ^ 2 ) Solution ) | C # program to implement optimized matrix chain multiplication problem . ; Matrix Mi has dimension p [ i - 1 ] x p [ i ] for i = 1. . n ; For simplicity of the program , one extra row and one extra column are allocated in dp [ ] [ ] . 0 th row and 0 th column of dp [ ] [ ] are not used ; cost is zero when multiplying one matrix . ; Simply following above recursive formula . ; Driver code"
Solution in C#:

using System ; class GFG { static int MatrixChainOrder ( int [ ] p , int n ) { int [ , ] dp = new int [ n , n ] ; for ( int i = 1 ; i < n ; i ++ ) dp [ i , i ] = 0 ; for ( int L = 1 ; L < n - 1 ; L ++ ) for ( int i = 1 ; i < n - L ; i ++ ) dp [ i , i + L ] = Math . Min ( dp [ i + 1 , i + L ] + p [ i - 1 ] * p [ i ] * p [ i + L ] , dp [ i , i + L - 1 ] + p [ i - 1 ] * p [ i + L - 1 ] * p [ i + L ] ) ; return dp [ 1 , n - 1 ] ; } public static void Main ( ) { int [ ] arr = { 10 , 20 , 30 , 40 , 30 } ; int size = arr . Length ; Console . WriteLine ( " Minimum ▁ number ▁ of ▁ multiplications ▁ is ▁ " + MatrixChainOrder ( arr , size ) ) ; } }


"Matrix Exponentiation | C # program to find value of f ( n ) where f ( n ) is defined as F ( n ) = F ( n - 1 ) + F ( n - 2 ) + F ( n - 3 ) , n >= 3 Base Cases : F ( 0 ) = 0 , F ( 1 ) = 1 , F ( 2 ) = 1 ; A utility function to multiply two matrices a [ ] [ ] and b [ ] [ ] . Multiplication result is stored back in b [ ] [ ] ; Creating an auxiliary matrix to store elements of the multiplication matrix ; storing the multiplication result in a [ ] [ ] ; Updating our matrix ; Function to compute F raise to power n - 2. ; Multiply it with initial values i . e with F ( 0 ) = 0 , F ( 1 ) = 1 , F ( 2 ) = 1 ; Multiply it with initial values i . e with F ( 0 ) = 0 , F ( 1 ) = 1 , F ( 2 ) = 1 ; Return n 'th term of a series defined  using below recurrence relation.  f(n) is defined as  f(n) = f(n-1) + f(n-2) + f(n-3), n>=3  Base Cases :  f(0) = 0, f(1) = 1, f(2) = 1 ; Driver code"
Solution in C#:

using System ; class GFG { static void multiply ( int [ , ] a , int [ , ] b ) { int [ , ] mul = new int [ 3 , 3 ] ; for ( int i = 0 ; i < 3 ; i ++ ) { for ( int j = 0 ; j < 3 ; j ++ ) { mul [ i , j ] = 0 ; for ( int k = 0 ; k < 3 ; k ++ ) mul [ i , j ] += a [ i , k ] * b [ k , j ] ; } } for ( int i = 0 ; i < 3 ; i ++ ) for ( int j = 0 ; j < 3 ; j ++ ) a [ i , j ] = mul [ i , j ] ; } static int power ( int [ , ] F , int n ) { int [ , ] M = { { 1 , 1 , 1 } , { 1 , 0 , 0 } , { 0 , 1 , 0 } } ; if ( n == 1 ) return F [ 0 , 0 ] + F [ 0 , 1 ] ; power ( F , n / 2 ) ; multiply ( F , F ) ; if ( n % 2 != 0 ) multiply ( F , M ) ; return F [ 0 , 0 ] + F [ 0 , 1 ] ; } static int findNthTerm ( int n ) { int [ , ] F = { { 1 , 1 , 1 } , { 1 , 0 , 0 } , { 0 , 1 , 0 } } ; return power ( F , n - 2 ) ; } public static void Main ( ) { int n = 5 ; Console . WriteLine ( " F ( 5 ) ▁ is ▁ " + findNthTerm ( n ) ) ; } }


"Max occurring divisor in an interval | Efficient C # program to find maximum occurring factor in an interval ; function to find max occurring divisor interval [ x , y ] ; if there is only one number in the in the interval , return that number ; otherwise , 2 is the max occurring divisor ; Driver Code"
Solution in C#:

using System ; class GFG { static int findDivisor ( int x , int y ) { if ( x == y ) return y ; return 2 ; } public static void Main ( ) { int x = 3 , y = 16 ; Console . Write ( findDivisor ( x , y ) ) ; } }


"Maximise array sum after taking non | C # implementation of the approach ; To store the states of dp ; To check if a given state has been solved ; To store the prefix - sum ; Function to fill the prefix_sum [ ] with the prefix sum of the given array ; Function to find the maximum sum subsequence such that no two elements are adjacent ; Base case ; To check if a state has been solved ; Required recurrence relation ; Returning the value ; Driver code ; Finding prefix - sum ; Finding the maximum possible sum"
Solution in C#:

using System ; class GFG { static int maxLen = 10 ; static int [ ] dp = new int [ maxLen ] ; static bool [ ] v = new bool [ maxLen ] ; static int [ ] prefix_sum = new int [ maxLen ] ; static void findPrefixSum ( int [ ] arr , int n ) { prefix_sum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { prefix_sum [ i ] = arr [ i ] + prefix_sum [ i - 1 ] ; } } static int maxSum ( int [ ] arr , int i , int n , int k ) { if ( i + k > n ) { return 0 ; } if ( v [ i ] ) { return dp [ i ] ; } v [ i ] = true ; int x ; if ( i == 0 ) { x = prefix_sum [ k - 1 ] ; } else { x = prefix_sum [ i + k - 1 ] - prefix_sum [ i - 1 ] ; } dp [ i ] = Math . Max ( maxSum ( arr , i + 1 , n , k ) , x + maxSum ( arr , i + k + 1 , n , k ) ) ; return dp [ i ] ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 7 , 6 } ; int n = arr . Length ; int k = 1 ; findPrefixSum ( arr , n ) ; Console . Write ( maxSum ( arr , 0 , n , k ) ) ; } }


"Maximise matrix sum by following the given Path | C # implementation of the approach ; To store the states of the DP ; Function to return the maximum of the three integers ; Function to return the maximum score ; Base cases ; If the state has already been solved then return it ; Marking the state as solved ; Growing phase ; } Shrinking phase ; Returning the solved state ; Driver code"
Solution in C#:

using System ; class GFG { static int n = 3 ; static int [ , , ] dp = new int [ n , n , 2 ] ; static bool [ , , ] v = new bool [ n , n , 2 ] ; static int max ( int a , int b , int c ) { int m = a ; if ( m < b ) { m = b ; } if ( m < c ) { m = c ; } return m ; } static int maxScore ( int [ , ] arr , int i , int j , int s ) { if ( i > n - 1 i < 0 j > n - 1 ) { return 0 ; } if ( ( i == 0 ) && ( j == ( n - 1 ) ) ) { return arr [ i , j ] ; } if ( v [ i , j , s ] ) { return dp [ i , j , s ] ; } v [ i , j , s ] = true ; if ( s != 1 ) { dp [ i , j , s ] = arr [ i , j ] + Math . Max ( maxScore ( arr , i + 1 , j , s ) , Math . Max ( maxScore ( arr , i , j + 1 , s ) , maxScore ( arr , i - 1 , j , ( s == 1 ) ? 0 : 1 ) ) ) ; else { dp [ i , j , s ] = arr [ i , j ] + Math . Max ( maxScore ( arr , i - 1 , j , s ) , maxScore ( arr , i , j + 1 , s ) ) ; } return dp [ i , j , s ] ; } static public void Main ( ) { int [ , ] arr = { { 1 , 1 , 1 } , { 1 , 5 , 1 } , { 1 , 1 , 1 } } ; Console . WriteLine ( maxScore ( arr , 0 , 0 , 0 ) ) ; } }


"Maximise the number of toys that can be purchased with amount K | C # Program to maximize the number of toys with K amount ; This functions returns the required number of toys ; sort the cost array ; Check if we can buy ith toy or not ; Increment count ; Driver Code"
Solution in C#:

using System ; class GFG { static int maximum_toys ( int [ ] cost , int N , int K ) { int count = 0 , sum = 0 ; Array . Sort ( cost ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( sum + cost [ i ] <= K ) { sum = sum + cost [ i ] ; count ++ ; } } return count ; } public static void Main ( ) { int K = 50 ; int [ ] cost = { 1 , 12 , 5 , 111 , 200 , 1000 , 10 , 9 , 12 , 15 } ; int N = cost . Length ; Console . Write ( maximum_toys ( cost , N , K ) ) ; } }


"Maximise the sum of two Numbers using at most one swap between them | C # program to maximise the sum of two Numbers using at most one swap between them ; Function to maximize the sum by swapping only one digit ; Store digits of max ( n1 , n2 ) ; Store digits of min ( n1 , n2 ) ; If length of the two numbers are unequal ; Find the most significant number and the most significant index for swapping ; If both numbers are of equal length ; Fetch the index of current maximum digit present in both the arrays ; Compute the difference ; Find the most significant index and the most significant digit to be swapped ; Restore the new numbers ; Display the maximized sum ; Driver code"
Solution in C#:

using System ; class GFG { static int MAX = 100 ; static int max_element ( int [ ] arr , int pos ) { int tmp = arr [ 0 ] ; int ind = 0 ; for ( int i = 1 ; i < pos ; i ++ ) { if ( tmp < arr [ i ] ) { tmp = arr [ i ] ; ind = i ; } } return ind ; } static void findMaxSum ( int n1 , int n2 ) { int [ ] arr1 = new int [ MAX ] ; int [ ] arr2 = new int [ MAX ] ; int l1 = 0 , l2 = 0 ; int max1 = Math . Max ( n1 , n2 ) ; int min1 = Math . Min ( n1 , n2 ) ; for ( int i = max1 ; i > 0 ; i /= 10 ) arr1 [ l1 ++ ] = ( i % 10 ) ; for ( int i = min1 ; i > 0 ; i /= 10 ) arr2 [ l2 ++ ] = ( i % 10 ) ; int f = 0 ; if ( l1 != l2 ) { int index = ( max_element ( arr2 , l2 ) ) ; for ( int i = l1 - 1 ; i > ( l2 - 1 ) ; i -- ) { if ( arr1 [ i ] < arr2 [ index ] ) { int tmp = arr1 [ i ] ; arr1 [ i ] = arr2 [ index ] ; arr2 [ index ] = tmp ; f = 1 ; break ; } } } if ( f != 1 ) { int index1 = 0 , index2 = 0 ; int diff1 = 0 , diff2 = 0 ; for ( int i = l2 - 1 ; i >= 0 ; i -- ) { index1 = ( max_element ( arr1 , i ) ) ; index2 = ( max_element ( arr2 , i ) ) ; diff1 = ( arr2 [ index2 ] - arr1 [ i ] ) ; diff2 = ( arr1 [ index1 ] - arr2 [ i ] ) ; if ( diff1 > 0 diff2 > 0 ) { if ( diff1 > diff2 ) { int tmp = arr1 [ i ] ; arr1 [ i ] = arr2 [ index2 ] ; arr2 [ index2 ] = tmp ; break ; } else if ( diff2 > diff1 ) { int tmp = arr1 [ index1 ] ; arr1 [ index1 ] = arr2 [ i ] ; arr2 [ i ] = tmp ; break ; } else if ( diff1 == diff2 ) { if ( index1 <= index2 ) { int tmp = arr1 [ index1 ] ; arr1 [ index1 ] = arr2 [ i ] ; arr2 [ i ] = tmp ; break ; } else if ( index2 <= index1 ) { int tmp = arr1 [ i ] ; arr1 [ i ] = arr2 [ index2 ] ; arr2 [ index2 ] = tmp ; break ; } } } } } int f_n1 = 0 , f_n2 = 0 ; for ( int i = l1 - 1 ; i >= 0 ; i -- ) { f_n1 = ( f_n1 * 10 ) + arr1 [ i ] ; f_n2 = ( f_n2 * 10 ) + arr2 [ i ] ; } Console . Write ( f_n1 + f_n2 ) ; } public static void Main ( string [ ] args ) { int N1 = 9987 ; int N2 = 123 ; findMaxSum ( N1 , N2 ) ; } }


"Maximize 3 rd element sum in quadruplet sets formed from given Array | C # code to maximize 3 rd element sum in quadruplet sets formed from given Array ; Function to find the maximum possible value of Y ; Pairs contain count of minimum elements that will be utilized at place of Z . It is equal to count of possible pairs that is size of array divided by 4 ; Sorting the array in descending order so as to bring values with minimal difference closer to arr [ i ] ; Here , i + 2 acts as a pointer that points to the third value of every possible quadruplet ; Returning the optimally maximum possible value ; Driver code ; Array declaration ; Size of array"
Solution in C#:

 using System ; class GFG { static int formQuadruplets ( int [ ] arr , int n ) { int ans = 0 , pairs = 0 ; pairs = n / 4 ; Array . Sort ( arr ) ; Array . Reverse ( arr ) ; for ( int i = 0 ; i < n - pairs ; i += 3 ) { ans += arr [ i + 2 ] ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 1 , 7 , 5 , 5 , 4 , 1 , 1 , 3 , 3 , 2 , 2 } ; int n = arr . Length ; Console . Write ( formQuadruplets ( arr , n ) + "
 " ) ; } }


"Maximize Array sum after changing sign of any elements for exactly M times | C # implementation of the above approach ; Function to find the maximum sum with M flips ; Declare a priority queue i . e . min heap ; Declare the sum as zero ; Push all elements of the array in it ; Iterate for M times ; Get the top element ; Flip the sign of the top element ; Remove the top element ; Update the sum ; Push the temp into the queue ; Driver Code ; Given input"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { static void findMaximumSumWithMflips ( int [ ] arr , int N , int M ) { List < int > minHeap = new List < int > ( ) ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { minHeap . Add ( arr [ i ] ) ; sum += arr [ i ] ; } minHeap . Sort ( ) ; while ( M -- > 0 ) { sum -= minHeap [ 0 ] ; int temp = - 1 * minHeap [ 0 ] ; minHeap . RemoveAt ( 0 ) ; sum += temp ; minHeap . Add ( temp ) ; minHeap . Sort ( ) ; } Console . WriteLine ( sum ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { - 3 , 7 , - 1 , - 5 , - 3 } ; int M = 4 , N = 5 ; findMaximumSumWithMflips ( arr , N , M ) ; } }


"Maximize Array sum by swapping at most K elements with another array | C # implementation to find maximum sum of array A by swapping at most K elements with array B ; Function to find the maximum sum ; If element of array a is smaller than that of array b , swap them . ; Find sum of resultant array ; Driver Code"
Solution in C#:

 using System ; class GFG { static void maximumSum ( int [ ] a , int [ ] b , int k , int n ) { int i , j ; Array . Sort ( a ) ; Array . Sort ( b ) ; for ( i = 0 , j = n - 1 ; i < k ; i ++ , j -- ) { if ( a [ i ] < b [ j ] ) { int temp = a [ i ] ; a [ i ] = b [ j ] ; b [ j ] = temp ; } else break ; } int sum = 0 ; for ( i = 0 ; i < n ; i ++ ) sum += a [ i ] ; Console . Write ( sum + "
 " ) ; } public static void Main ( ) { int K = 1 ; int [ ] A = { 2 , 3 , 4 } ; int [ ] B = { 6 , 8 , 5 } ; int N = A . Length ; maximumSum ( A , B , K , N ) ; } }


"Maximize GCD of all possible pairs from 1 to N | C # Program to implement the approach ; Function to obtain the maximum gcd of all pairs from 1 to n ; Print the answer ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { static void find ( int n ) { Console . Write ( n / 2 ) ; } public static void Main ( string [ ] args ) { int n = 5 ; find ( n ) ; } }


"Maximize K to make given array Palindrome when each element is replaced by its remainder with K | c # program for the above approach ; utility function to calculate the GCD of two numbers ; Function to calculate the largest K , replacing all elements of an array A by their modulus with K , makes A a palindromic array ; check if A is palindrome ; A is not palindromic ; K can be infitely large in this case ; variable to store the largest K that makes A palindromic ; return the required answer ; Driver code ; Input ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; else return gcd ( b , a % b ) ; } static int largestK ( int [ ] A , int N ) { int l = 0 , r = N - 1 , flag = 0 ; while ( l < r ) { if ( A [ l ] != A [ r ] ) { flag = 1 ; break ; } l ++ ; r -- ; } if ( flag == 0 ) return - 1 ; int K = Math . Abs ( A [ 0 ] - A [ N - 1 ] ) ; for ( int i = 1 ; i < N / 2 ; i ++ ) K = gcd ( K , Math . Abs ( A [ i ] - A [ N - i - 1 ] ) ) ; return K ; } public static void Main ( ) { int [ ] A = { 1 , 2 , 3 , 2 , 1 } ; int N = A . Length ; Console . Write ( largestK ( A , N ) ) ; } }


"Maximize Kth largest element after splitting the given Array at most C times | C # program for the above approach ; Function to find the K - th maximum element after upto C operations ; Check for the base case ; Stores the count iterations of BS ; Create the left and right bounds of binary search ; Perform binary search ; Find the value of mid ; Traverse the array ; Update the ranges ; Return the maximum value obtained ; Driver Code"
Solution in C#:

using System ; class GFG { static double maxKth ( int [ ] arr , int N , int C , int K ) { if ( N + C < K ) { return - 1 ; } int iter = 300 ; double l = 0 , r = 1000000000.0 ; while ( iter -- > 0 ) { double mid = ( l + r ) * 0.5 ; double a = 0 ; double b = 0 ; for ( int i = 0 ; i < N ; i ++ ) { a += ( int ) ( ( double ) arr [ i ] / mid ) ; if ( ( double ) arr [ i ] >= mid ) { b ++ ; } } if ( a >= K && b + C >= K ) { l = mid ; } else { r = mid ; } } return l ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 8 } ; int K = 1 , C = 1 ; int N = arr . Length ; Console . Write ( maxKth ( arr , N , C , K ) ) ; } }


"Maximize Profit by trading stocks based on given rate per day | C # program for the above approach ; Function to find the maximum profit ; Start from the last day ; Traverse and keep adding the profit until a day with price of stock higher than currentDay is obtained ; Set this day as currentDay with maximum cost of stock currently ; Return the profit ; Driver Code ; Given array of prices ; Function call"
Solution in C#:

using System ; class GFG { static int maxProfit ( int [ ] prices , int n ) { int profit = 0 , currentDay = n - 1 ; while ( currentDay > 0 ) { int day = currentDay - 1 ; while ( day >= 0 && ( prices [ currentDay ] > prices [ day ] ) ) { profit += ( prices [ currentDay ] - prices [ day ] ) ; day -- ; } currentDay = day ; } return profit ; } public static void Main ( String [ ] args ) { int [ ] prices = { 2 , 3 , 5 } ; int N = prices . Length ; Console . Write ( maxProfit ( prices , N ) ) ; } }


"Maximize Sum possible from an Array by the given moves | C # Program to implement the above approach ; Function to find the maximum sum possible by given moves from the array ; Checking for boundary ; If previously computed subproblem occurs ; If element can be moved left ; Calculate maximum possible sum by moving left from current index ; If element can be moved right ; Calculate maximum possible sum by moving right from current index and update the maximum sum ; Store the maximum sum ; Driver Code ; Function call"
Solution in C#:

using System ; class GFG { public static int maxValue ( int [ ] a , int n , int pos , int moves , int left , int [ , ] dp ) { if ( moves == 0 || ( pos > n - 1 pos < 0 ) ) return 0 ; if ( dp [ pos , left ] != - 1 ) return dp [ pos , left ] ; int value = 0 ; if ( left > 0 && pos >= 1 ) value = Math . Max ( value , a [ pos ] + maxValue ( a , n , pos - 1 , moves - 1 , left - 1 , dp ) ) ; if ( pos <= n - 1 ) value = Math . Max ( value , a [ pos ] + maxValue ( a , n , pos + 1 , moves - 1 , left , dp ) ) ; return dp [ pos , left ] = value ; } public static void Main ( String [ ] args ) { int n = 5 ; int [ ] a = { 1 , 5 , 4 , 3 , 2 } ; int k = 1 ; int m = 4 ; int [ , ] dp = new int [ n + 1 , k + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= k ; j ++ ) dp [ i , j ] = - 1 ; Console . WriteLine ( ( a [ 0 ] + maxValue ( a , n , 1 , m , k , dp ) ) ) ; } }


"Maximize X such that sum of numbers in range [ 1 , X ] is at most K | C # program for above approach ; Function to count the elements with sum of the first that many natural numbers less than or equal to K ; If K equals to 0 ; Stores the result ; Iterate until low is less than or equal to high ; Stores the sum of first mid natural numbers ; If sum is less than or equal to K ; Update res and low ; Otherwise , ; Update ; Return res ; Driver Code ; Input ; Function call"
Solution in C#:

using System ; class GFG { static int Count ( int N , int K ) { if ( K == 0 ) return 0 ; int res = 0 ; int low = 1 , high = N ; while ( low <= high ) { int mid = ( low + high ) / 2 ; int sum = ( mid * mid + mid ) / 2 ; if ( sum <= K ) { res = Math . Max ( res , mid ) ; low = mid + 1 ; } else { high = mid - 1 ; } } return res ; } public static void Main ( String [ ] args ) { int N = 6 , K = 14 ; Console . Write ( Count ( N , K ) ) ; } }


"Maximize X such that sum of numbers in range [ 1 , X ] is at most K | C # program for the above approach ; Function to count the elements with sum of the first that many natural numbers less than or equal to K ; If K equals to 0 ; Stores sum of first i natural numbers ; Stores the result ; Iterate over the range [ 1 , N ] ; Increment sum by i ; Is sum is less than or equal to K ; Otherwise , ; Return res ; Driver Code ; Input ; Function call"
Solution in C#:

using System ; class GFG { static int Count ( int N , int K ) { if ( K == 0 ) return 0 ; int sum = 0 ; int res = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { sum += i ; if ( sum <= K ) res ++ ; else break ; } return res ; } public static void Main ( ) { int N = 6 , K = 14 ; Console . WriteLine ( Count ( N , K ) ) ; } }


"Maximize XOR by selecting 3 numbers in range [ 0 , A ] , [ 0 , B ] , and [ 0 , C ] respectively | C # Program to implement the above approach ; Function to calculate maximum triplet XOR form 3 ranges ; Initialize a variable to store the answer ; create minimum number that have a set bit at ith position ; Check for each number and try to greedily choose the bit if possible If A >= cur then A also have a set bit at ith position ; Increment the answer ; Subtract this value from A ; Check for B ; Increment the answer ; Subtract this value from B ; Check for C ; Increment the answer ; Subtract this value from C ; If any of the above conditions is not satisfied then there is no way to turn that bit ON . ; Driver Code"
Solution in C#:

using System ; class GFG { static int maximumTripletXOR ( int A , int B , int C ) { int ans = 0 ; for ( int i = 30 ; i >= 0 ; i -- ) { int cur = 1 << i ; if ( A >= cur ) { ans += cur ; A -= cur ; } else if ( B >= cur ) { ans += cur ; B -= cur ; } else if ( C >= cur ) { ans += cur ; C -= cur ; } } return ans ; } public static void Main ( String [ ] args ) { int A = 6 ; int B = 2 ; int C = 10 ; Console . Write ( maximumTripletXOR ( A , B , C ) ) ; } }


"Maximize a value for a semicircle of given radius | C # program to find the maximum value of F ; Function to find the maximum value of F ; using the formula derived for getting the maximum value of F ; Driver code"
Solution in C#:

using System ; class GFG { static double maximumValueOfF ( int R ) { return 4 * R * R + 0.25 ; } public static void Main ( ) { int R = 3 ; Console . WriteLine ( maximumValueOfF ( R ) ) ; } }


"Maximize absolute displacement from origin by moving on X | C # program for the above approach ; Recursive function to find the maximum absolute displacement from origin by performing the given set of moves ; If i is equal to N ; If S [ i ] is equal to ' L ' ; If S [ i ] is equal to ' R ' ; If S [ i ] is equal to ' ? ' ; Function to find the maximum absolute displacement from the origin ; Return the maximum absolute displacement ; Driver Code ; Input ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int DistRecursion ( string S , int i , int dist ) { if ( i == S . Length ) return Math . Abs ( dist ) ; if ( S [ i ] == ' L ' ) return DistRecursion ( S , i + 1 , dist - 1 ) ; if ( S [ i ] == ' R ' ) return DistRecursion ( S , i + 1 , dist + 1 ) ; return Math . Max ( DistRecursion ( S , i + 1 , dist - 1 ) , DistRecursion ( S , i + 1 , dist + 1 ) ) ; } static int maxDistance ( string S ) { return DistRecursion ( S , 0 , 0 ) ; } public static void Main ( ) { string S = " ? RRR ? " ; Console . Write ( maxDistance ( S ) ) ; } }


"Maximize array elements upto given number | C # code to find maximum value of number obtained by using array elements recursively . ; Utility function to find maximum possible value ; If entire array is traversed , then compare current value in num to overall maximum obtained so far . ; Case 1 : Subtract current element from value so far if result is greater than or equal to zero . ; Case 2 : Add current element to value so far if result is less than or equal to maxLimit . ; Function to find maximum possible value that can be obtained using array elements and given number . ; variable to store maximum value that can be obtained . ; variable to store current index position . ; call to utility function to find maximum possible value that can be obtained . ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void findMaxValUtil ( int [ ] arr , int n , int num , int maxLimit , int ind , ref int ans ) { if ( ind == n ) { ans = Math . Max ( ans , num ) ; return ; } if ( num - arr [ ind ] >= 0 ) { findMaxValUtil ( arr , n , num - arr [ ind ] , maxLimit , ind + 1 , ref ans ) ; } if ( num + arr [ ind ] <= maxLimit ) { findMaxValUtil ( arr , n , num + arr [ ind ] , maxLimit , ind + 1 , ref ans ) ; } } static int findMaxVal ( int [ ] arr , int n , int num , int maxLimit ) { int ans = 0 ; int ind = 0 ; findMaxValUtil ( arr , n , num , maxLimit , ind , ref ans ) ; return ans ; } public static void Main ( ) { int num = 1 ; int [ ] arr = { 3 , 10 , 6 , 4 , 5 } ; int n = arr . Length ; int maxLimit = 15 ; Console . Write ( findMaxVal ( arr , n , num , maxLimit ) ) ; } }


"Maximize array elements upto given number | C # program to find maximum value of number obtained by using array elements by using dynamic programming . ; Function to find maximum possible value of number that can be obtained using array elements . ; Variable to represent current index . ; Variable to show value between 0 and maxLimit . ; Table to store whether a value can be obtained or not upto a certain index 1. dp [ i , j ] = 1 if value j can be obtained upto index i . 2. dp [ i , j ] = 0 if value j cannot be obtained upto index i . ; Check for index 0 if given value val can be obtained by either adding to or subtracting arr [ 0 ] from num . ; 1. If arr [ ind ] is added to obtain given val then val - arr [ ind ] should be obtainable from index ind - 1. 2. If arr [ ind ] is subtracted to obtain given val then val + arr [ ind ] should be obtainable from index ind - 1. Check for both the conditions . ; If either of one condition is true , then val is obtainable at index ind . ; Find maximum value that is obtained at index n - 1. ; If no solution exists return - 1. ; Driver Code"
Solution in C#:

using System ; class GFG { static int findMaxVal ( int [ ] arr , int n , int num , int maxLimit ) { int ind ; int val ; int [ , ] dp = new int [ n , maxLimit + 1 ] ; for ( ind = 0 ; ind < n ; ind ++ ) { for ( val = 0 ; val <= maxLimit ; val ++ ) { if ( ind == 0 ) { if ( num - arr [ ind ] == val num + arr [ ind ] == val ) { dp [ ind , val ] = 1 ; } else { dp [ ind , val ] = 0 ; } } else { if ( val - arr [ ind ] >= 0 && val + arr [ ind ] <= maxLimit ) { if ( dp [ ind - 1 , val - arr [ ind ] ] == 1 dp [ ind - 1 , val + arr [ ind ] ] == 1 ) dp [ ind , val ] = 1 ; } else if ( val - arr [ ind ] >= 0 ) { dp [ ind , val ] = dp [ ind - 1 , val - arr [ ind ] ] ; } else if ( val + arr [ ind ] <= maxLimit ) { dp [ ind , val ] = dp [ ind - 1 , val + arr [ ind ] ] ; } else { dp [ ind , val ] = 0 ; } } } } for ( val = maxLimit ; val >= 0 ; val -- ) { if ( dp [ n - 1 , val ] == 1 ) { return val ; } } return - 1 ; } static void Main ( ) { int num = 1 ; int [ ] arr = new int [ ] { 3 , 10 , 6 , 4 , 5 } ; int n = arr . Length ; int maxLimit = 15 ; Console . Write ( findMaxVal ( arr , n , num , maxLimit ) ) ; } }


"Maximize array product by changing any array element arr [ i ] to ( | C # program for the above approach ; Function to find array with maximum product by changing array elements to ( - 1 ) arr [ i ] - 1 ; Traverse the array ; Applying the operation on all the positive elements ; Stores maximum element in array ; Stores index of maximum element ; Check if current element is greater than the maximum element ; Find index of the maximum element ; Perform the operation on the maximum element ; Print the elements of the array ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static void findArrayWithMaxProduct ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) if ( arr [ i ] >= 0 ) { arr [ i ] = - arr [ i ] - 1 ; } if ( N % 2 == 1 ) { int max_element = - 1 ; int index = - 1 ; for ( int i = 0 ; i < N ; i ++ ) if ( Math . Abs ( arr [ i ] ) > max_element ) { max_element = Math . Abs ( arr [ i ] ) ; index = i ; } arr [ index ] = - arr [ index ] - 1 ; } for ( int i = 0 ; i < N ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } static public void Main ( ) { int [ ] arr = { - 3 , 0 , 1 } ; int N = arr . Length ; findArrayWithMaxProduct ( arr , N ) ; } }


"Maximize array sum after K negations | Set 1 | C # program for the above approach ; Function to calculate sum of the array ; Iterate from 0 to n - 1 ; Function to maximize sum ; Iterate from 0 to n - 1 ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static int sumArray ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } return sum ; } static int maximizeSum ( int [ ] arr , int n , int k ) { Array . Sort ( arr ) ; int i = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( k != 0 && arr [ i ] < 0 ) { arr [ i ] *= - 1 ; k -- ; continue ; } break ; } if ( i == n ) i -- ; if ( k == 0 k % 2 == 0 ) { return sumArray ( arr , n ) ; } if ( i != 0 && Math . Abs ( arr [ i ] ) >= Math . Abs ( arr [ i - 1 ] ) ) { i -- ; } arr [ i ] *= - 1 ; return sumArray ( arr , n ) ; } static public void Main ( ) { int n = 5 ; int k = 4 ; int [ ] arr = { - 3 , - 2 , - 1 , 5 , 6 } ; Console . Write ( maximizeSum ( arr , n , k ) ) ; } }


"Maximize array sum after K negations | Set 1 | C # program to find maximum array sum after at most k negations . ; Sorting given array using in - built java sort function ; If we find a 0 in our sorted array , we stop ; Calculating sum ; Driver code"
Solution in C#:

using System ; class GFG { static int sol ( int [ ] arr , int k ) { Array . Sort ( arr ) ; int sum = 0 ; int i = 0 ; while ( k > 0 ) { if ( arr [ i ] >= 0 ) k = 0 ; else { arr [ i ] = ( - 1 ) * arr [ i ] ; k = k - 1 ; } i ++ ; } for ( int j = 0 ; j < arr . Length ; j ++ ) { sum += arr [ j ] ; } return sum ; } public static void Main ( string [ ] args ) { int [ ] arr = { - 2 , 0 , 5 , - 1 , 2 } ; Console . Write ( sol ( arr , 4 ) ) ; } }


"Maximize array sum after K negations | Set 1 | C # program to maximize array sum after k operations . ; This function does k operations on array in a way that maximize the array sum . index -- > stores the index of current minimum element for j 'th operation ; Modify array K number of times ; Find minimum element in array for current operation and modify it i . e ; arr [ j ] -- > - arr [ j ] ; this the condition if we find 0 as minimum element , so it will useless to replace 0 by - ( 0 ) for remaining operations ; Modify element of array ; Calculate sum of array ; Driver code"
Solution in C#:

using System ; class GFG { static int maximumSum ( int [ ] arr , int n , int k ) { for ( int i = 1 ; i <= k ; i ++ ) { int min = + 2147483647 ; int index = - 1 ; for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ j ] < min ) { min = arr [ j ] ; index = j ; } } if ( min == 0 ) break ; arr [ index ] = - arr [ index ] ; } int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; return sum ; } public static void Main ( ) { int [ ] arr = { - 2 , 0 , 5 , - 1 , 2 } ; int k = 4 ; int n = arr . Length ; Console . Write ( maximumSum ( arr , n , k ) ) ; } }


"Maximize array sum by X increments when each element is divided by 10 | C # program for the above approach ; Initialize variables ; Add the current contribution of the element to the answer ; If the value is already maximum then we can 't change it ; Moves required to move to the next multiple of 10 ; No of times we can add 10 to this value so that its value does not exceed 1000. ; Sort the array ; Adding the values to increase the numbers to the next multiple of 10 ; If the total moves are less than X then increase the answer ; If the moves exceed X then we cannot increase numbers ; If there still remain some moves ; Remaining moves ; Add minimim of increments and remaining / 10 to the answer ; Output the readonly answer ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static void maximizeval10 ( int [ ] a , int n , int k ) { int increments = 0 ; int ans = 0 ; List < int > v = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { ans += ( a [ i ] / 10 ) ; if ( a [ i ] == 1000 ) continue ; else { v . Add ( 10 - a [ i ] % 10 ) ; increments += ( 100 - ( ( a [ i ] ) / 10 ) - 1 ) ; } } v . Sort ( ) ; int sum = 0 ; for ( int i = 0 ; i < v . Count ; i ++ ) { sum += v [ i ] ; if ( sum <= k ) { ans ++ ; } else break ; } if ( sum < k ) { int remaining = k - sum ; ans += Math . Min ( increments , remaining / 10 ) ; } Console . Write ( ans ) ; } public static void Main ( String [ ] args ) { int N = 4 ; int X = 6 ; int [ ] A = { 4 , 8 , 8 , 8 } ; maximizeval10 ( A , N , X ) ; } }


"Maximize array sum by alternating the signs of adjacent elements | C # program to implement the above approach ; Function to find the maximum sum by alternating the signs of adjacent elements of the array ; Stores count of negative elements in the array ; Stores maximum sum by alternating the signs of adjacent elements ; Stores smallest absolute value of array elements ; Stores sum of absolute value of array elements ; Traverse the array ; If arr [ i ] is a negative number ; Update cntNeg ; Update sum ; Update SmValue ; Update MaxAltSum ; If cntNeg is an odd number ; Update MaxAltSum ; Driver Code"
Solution in C#:

using System ; class GFG { static int findMaxSumByAlternatingSign ( int [ ] arr , int N ) { int cntNeg = 0 ; int MaxAltSum = 0 ; int SmValue = 0 ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < 0 ) { cntNeg += 1 ; } sum += Math . Abs ( arr [ i ] ) ; SmValue = Math . Min ( SmValue , Math . Abs ( arr [ i ] ) ) ; } MaxAltSum = sum ; if ( cntNeg % 2 == 1 ) { MaxAltSum -= 2 * SmValue ; } return MaxAltSum ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , - 2 , - 4 , 5 } ; int N = arr . Length ; Console . Write ( findMaxSumByAlternatingSign ( arr , N ) ) ; } }


"Maximize array sum by concatenating corresponding elements of given two arrays | C # implementation to find the maximum array sum by concatenating corresponding elements of given two arrays ; Function to join the two numbers ; Loop to reverse the digits of the one number ; Loop to join two numbers ; Function to find the maximum array sum ; Loop to iterate over the two elements of the array ; Find the array sum ; Return the array sum ; Driver Code"
Solution in C#:

using System ; class GFG { static int joinNumbers ( int numA , int numB ) { int revB = 0 ; while ( numB > 0 ) { revB = revB * 10 + ( numB % 10 ) ; numB = numB / 10 ; } while ( revB > 0 ) { numA = numA * 10 + ( revB % 10 ) ; revB = revB / 10 ; } return numA ; } static int findMaxSum ( int [ ] A , int [ ] B , int n ) { int [ ] maxArr = new int [ n ] ; for ( int i = 0 ; i < n ; ++ i ) { int X = joinNumbers ( A [ i ] , B [ i ] ) ; int Y = joinNumbers ( B [ i ] , A [ i ] ) ; int mx = Math . Max ( X , Y ) ; maxArr [ i ] = mx ; } int maxAns = 0 ; for ( int i = 0 ; i < n ; i ++ ) { maxAns += maxArr [ i ] ; } return maxAns ; } public static void Main ( String [ ] args ) { int N = 5 ; int [ ] A = { 11 , 23 , 38 , 43 , 59 } ; int [ ] B = { 36 , 24 , 17 , 40 , 56 } ; Console . WriteLine ( findMaxSum ( A , B , N ) ) ; } }


"Maximize array sum by replacing equal adjacent pairs by their sum and X respectively | C # program for the above approach ; Function to calculate x ^ y ; Base Case ; Find the value in temp ; If y is even ; Function that find the maximum possible sum of the array ; Print the result using the formula ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static int power ( int x , int y ) { int temp ; if ( y == 0 ) return 1 ; temp = power ( x , y / 2 ) ; if ( y % 2 == 0 ) return temp * temp ; else return x * temp * temp ; } public static void maximumPossibleSum ( int N , int X ) { Console . WriteLine ( X * ( power ( 2 , N ) - 1 ) ) ; } public static void Main ( String [ ] args ) { int N = 3 , X = 5 ; maximumPossibleSum ( N , X ) ; } }


"Maximize boxes required to keep at least one black and one white shirt | C # program for the above approach ; Function to find the maximum number of boxes such that each box contains three shirts comprising of at least one white and black shirt ; Stores the low and high pointers for binary search ; Store the required answer ; Loop while low <= high ; Store the mid value ; Check if the mid number of boxes can be used ; Update answer and recur for the right half ; Else , recur for the left half ; Print the result ; Driver Code"
Solution in C#:

using System ; class GFG { static void numberofBoxes ( int W , int B , int O ) { int low = 0 , high = Math . Min ( W , B ) ; int ans = 0 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( ( ( W >= mid ) && ( B >= mid ) ) && ( ( W - mid ) + ( B - mid ) + O ) >= mid ) { ans = mid ; low = mid + 1 ; } else high = mid - 1 ; } Console . Write ( ans ) ; } public static void Main ( ) { int W = 3 , B = 3 , O = 1 ; numberofBoxes ( W , B , O ) ; } }


"Maximize cost obtained by removal of substrings " pr " or " rp " from a given String | C # program to implement the above approach ; Function to maintain the case , X >= Y ; To maintain X >= Y ; Replace ' p ' to ' r ' ; Replace ' r ' to ' p ' . ; Function to return the maximum cost ; Stores the length of the String ; To maintain X >= Y . ; Stores the maximum cost ; Stores the count of ' p ' after removal of all " pr " subStrings up to str [ i ] ; Stores the count of ' r ' after removal of all " pr " subStrings up to str [ i ] ; Stack to maintain the order of characters after removal of subStrings ; If subString " pr " is removed ; Increase cost by X ; If any subString " rp " left in the Stack ; If any subString " rp " left in the Stack ; Driver Code"
Solution in C#:

using System ; class GFG { static bool swapXandY ( char [ ] str , int X , int Y ) { int N = str . Length ; X = X + Y ; Y = X - Y ; X = X - Y ; for ( int i = 0 ; i < N ; i ++ ) { if ( str [ i ] == ' p ' ) { str [ i ] = ' r ' ; } else if ( str [ i ] == ' r ' ) { str [ i ] = ' p ' ; } } return true ; } static int maxCost ( String str , int X , int Y ) { int N = str . Length ; if ( Y > X ) { swapXandY ( str . ToCharArray ( ) , X , Y ) ; } int res = 0 ; int countP = 0 ; int countR = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( str [ i ] == ' p ' ) { countP ++ ; } else if ( str [ i ] == ' r ' ) { if ( countP > 0 ) { countP -- ; res += X ; } else countR ++ ; } else { res += Math . Min ( countP , countR ) * Y ; countP = 0 ; countR = 0 ; } } res += Math . Min ( countP , countR ) * Y ; return res ; } public static void Main ( String [ ] args ) { String str = " abppprrr " ; int X = 5 , Y = 4 ; Console . Write ( maxCost ( str , X , Y ) ) ; } }


"Maximize cost of deletions to obtain string having no pair of similar adjacent characters | C # program for the above approach ; Function to find maximum cost to remove consecutive characters ; Maximum cost ; Traverse from 0 to len ( s ) - 2 ; If characters are identical ; Add cost [ i ] if its maximum ; Add cost [ i + 1 ] if its maximum ; Increment i ; Return the final max count ; Driver code ; Given string s ; Given cost of removal ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int Maxcost ( string s , int [ ] cost ) { int maxcost = 0 ; int i = 0 ; while ( i < s . Length - 1 ) { if ( s [ i ] == s [ i + 1 ] ) { if ( cost [ i ] > cost [ i + 1 ] ) maxcost += cost [ i ] ; else { maxcost += cost [ i + 1 ] ; cost [ i + 1 ] = cost [ i ] ; } } i ++ ; } return maxcost ; } public static void Main ( string [ ] args ) { string s = " abaac " ; int [ ] cost = { 1 , 2 , 3 , 4 , 5 } ; Console . Write ( Maxcost ( s , cost ) ) ; } }