"Minimum number of swaps required for arranging pairs adjacent to each other | C # program to find minimum number of swaps required so that all pairs become adjacent . ; This function updates indexes of elements ' a ' and ' b ' ; This function returns minimum number of swaps required to arrange all elements of arr [ i . . n ] become arranged ; If all pairs procesed so no swapping needed return 0 ; If current pair is valid so DO NOT DISTURB this pair and move ahead . ; Swap pair of arr [ i ] with arr [ i + 1 ] and recursively compute minimum swap required if this move is made . ; Backtrack to previous configuration . Also restore the previous indices , of one and two ; Now swap arr [ i ] with pair of arr [ i + 1 ] and recursively compute minimum swaps required for the subproblem after this move ; Backtrack to previous configuration . Also restore the previous indices , of one and two ; Return minimum of two cases ; Returns minimum swaps required ; To store indices of array elements ; Store index of each element in array index ; Call the recursive function ; Driver code ; For simplicity , it is assumed that arr [ 0 ] is not used . The elements from index 1 to n are only valid elements ; if ( a , b ) is pair than we have assigned elements in array such that pairs [ a ] = b and pairs [ b ] = a ; Number of pairs n is half of total elements ; If there are n elements in array , then there are n pairs"
Solution in C#:

using System ; class GFG { public static void updateindex ( int [ ] index , int a , int ai , int b , int bi ) { index [ a ] = ai ; index [ b ] = bi ; } public static int minSwapsUtil ( int [ ] arr , int [ ] pairs , int [ ] index , int i , int n ) { if ( i > n ) { return 0 ; } if ( pairs [ arr [ i ] ] == arr [ i + 1 ] ) { return minSwapsUtil ( arr , pairs , index , i + 2 , n ) ; } int one = arr [ i + 1 ] ; int indextwo = i + 1 ; int indexone = index [ pairs [ arr [ i ] ] ] ; int two = arr [ index [ pairs [ arr [ i ] ] ] ] ; arr [ i + 1 ] = arr [ i + 1 ] ^ arr [ indexone ] ^ ( arr [ indexone ] = arr [ i + 1 ] ) ; updateindex ( index , one , indexone , two , indextwo ) ; int a = minSwapsUtil ( arr , pairs , index , i + 2 , n ) ; arr [ i + 1 ] = arr [ i + 1 ] ^ arr [ indexone ] ^ ( arr [ indexone ] = arr [ i + 1 ] ) ; updateindex ( index , one , indextwo , two , indexone ) ; one = arr [ i ] ; indexone = index [ pairs [ arr [ i + 1 ] ] ] ; two = arr [ index [ pairs [ arr [ i + 1 ] ] ] ] ; indextwo = i ; arr [ i ] = arr [ i ] ^ arr [ indexone ] ^ ( arr [ indexone ] = arr [ i ] ) ; updateindex ( index , one , indexone , two , indextwo ) ; int b = minSwapsUtil ( arr , pairs , index , i + 2 , n ) ; arr [ i ] = arr [ i ] ^ arr [ indexone ] ^ ( arr [ indexone ] = arr [ i ] ) ; updateindex ( index , one , indextwo , two , indexone ) ; return 1 + Math . Min ( a , b ) ; } public static int minSwaps ( int n , int [ ] pairs , int [ ] arr ) { int [ ] index = new int [ 2 * n + 1 ] ; for ( int i = 1 ; i <= 2 * n ; i ++ ) { index [ arr [ i ] ] = i ; } return minSwapsUtil ( arr , pairs , index , 1 , 2 * n ) ; } public static void Main ( string [ ] args ) { int [ ] arr = new int [ ] { 0 , 3 , 5 , 6 , 4 , 1 , 2 } ; int [ ] pairs = new int [ ] { 0 , 3 , 6 , 1 , 5 , 4 , 2 } ; int m = pairs . Length ; int n = m / 2 ; Console . Write ( " Min ▁ swaps ▁ required ▁ is ▁ " + minSwaps ( n , pairs , arr ) ) ; } }


"Minimum number of swaps required such that a given substring consists of exactly K 1 s | C # program to implement the above approach ; Function to find the minimum number of swaps required such that the substring { s [ l ] , . . , s [ r ] } consists of exactly k 1 s ; Store the size of the string ; Store the total number of 1 s and 0 s in the entire string ; Traverse the string S to find the frequency of 1 and 0 ; Store the number of 1 s and 0 s in the substring s [ l , r ] ; Traverse the substring S [ l , r ] to find the frequency of 1 s and 0 s in it ; Store the count of 1 s and 0 s outside substring s [ l , r ] ; Check if the sum of the substring is at most K ; Store number of 1 s required ; Check if there are enough 1 s remaining to be swapped ; If the count of 1 s in the substring exceeds k ; Store the number of 0 s required ; Check if there are enough 0 s remaining to be swapped ; In all other cases , print - 1 ; Driver Code"
Solution in C#:

using System ; class GFG { static int minimumSwaps ( string s , int l , int r , int k ) { int n = s . Length ; int tot_ones = 0 , tot_zeros = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == '1' ) tot_ones ++ ; else tot_zeros ++ ; } int ones = 0 , zeros = 0 , sum = 0 ; for ( int i = l - 1 ; i < r ; i ++ ) { if ( s [ i ] == '1' ) { ones ++ ; sum ++ ; } else zeros ++ ; } int rem_ones = tot_ones - ones ; int rem_zeros = tot_zeros - zeros ; if ( k >= sum ) { int rem = k - sum ; if ( zeros >= rem && rem_ones >= rem ) return rem ; } else if ( k < sum ) { int rem = sum - k ; if ( ones >= rem && rem_zeros >= rem ) return rem ; } return - 1 ; } public static void Main ( String [ ] args ) { string S = "110011111000101" ; int L = 5 , R = 8 , K = 2 ; Console . Write ( minimumSwaps ( S , L , R , K ) ) ; } }


"Minimum number of swaps required to make a number divisible by 60 | C # program to find minimum number of swap operations required ; Function that print minimum number of swap operations required ; Condition if more than one zero exist ; Condition if zero_exist ; Computing total sum of all digits ; Condition if zero does not exist or the sum is not divisible by 3 ; Condition to find a digit that is multiple of 2 other than one zero ; Condition if multiple of 2 do not exist ; Condition for zero swaps means the number is already is divisible by 60 ; Condition for only one swap ; Otherwise 2 swaps required ; Driver Code"
Solution in C#:

using System ; class GFG { static void MinimumSwapOperations ( string s ) { bool zero_exist = false ; bool multiple_of_2 = false ; int sum = 0 ; int index_of_zero = 0 ; bool more_zero = false ; for ( int i = 0 ; i < s . Length ; i ++ ) { int val = s [ i ] - '0' ; if ( zero_exist == true ) more_zero = true ; if ( val == 0 ) { zero_exist = true ; index_of_zero = i ; } sum += val ; } if ( zero_exist == false sum % 3 != 0 ) { Console . WriteLine ( " - 1" ) ; return ; } for ( int i = 0 ; i < s . Length ; i ++ ) { int val = s [ i ] - '0' ; if ( val % 2 == 0 && i != index_of_zero ) multiple_of_2 = true ; } if ( multiple_of_2 == false ) { Console . WriteLine ( " - 1" ) ; return ; } int last_val = s [ ( s . Length - 1 ) - '0' ] ; int second_last_val = s [ ( s . Length - 2 ) - '0' ] ; if ( last_val == 0 && second_last_val % 2 == 0 ) Console . WriteLine ( 0 ) ; else if ( ( last_val == 0 && second_last_val % 2 != 0 ) || ( last_val % 2 == 0 && second_last_val == 0 ) ) Console . WriteLine ( 1 ) ; else if ( more_zero == true && ( last_val == 0 && second_last_val % 2 != 0 ) ) Console . WriteLine ( 1 ) ; else Console . WriteLine ( 2 ) ; } public static void Main ( string [ ] args ) { string N = "20" ; MinimumSwapOperations ( N ) ; } }


"Minimum number of swaps required to make parity of array elements same as their indices | C # program to implement the above approach ; Function to count the minimum number of swaps required to make the parity of array elements same as their indices ; Stores count of even and odd array elements ; Traverse the array ; Check if indices and array elements are not of the same parity ; If index is even ; Update even ; Update odd ; Condition for not possible ; Otherwise ; Driver Code"
Solution in C#:

using System ; class GFG { static void minimumSwaps ( int [ ] arr , int N ) { int even = 0 , odd = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 != i % 2 ) { if ( i % 2 == 0 ) { even ++ ; } else { odd ++ ; } } } if ( even != odd ) { Console . WriteLine ( - 1 ) ; } else { Console . WriteLine ( even ) ; } } public static void Main ( ) { int [ ] arr = { 3 , 2 , 7 , 6 } ; int N = arr . Length ; minimumSwaps ( arr , N ) ; } }


"Minimum number of swaps required to make the string K periodic | C # code to find the minimum number of swaps required to make the String K periodic ; Mark all allowed characters as true ; Initialize the freq array to 0 ; Increase the frequency of each character ; Total number of periods of size K in the String ; Check if the current character is present in allowed and whether the current frequency is greater than all previous frequencies for this position ; Update the answer by subtracting the maxfrequency from total positions if there exist extra character at the end of the String apart from the n / k characters then add 1. ; Driver code"
Solution in C#:

 using System ; class GFG { static void minFlip ( String s , int n , int k , char [ ] a , int p ) { bool [ ] allowed = new bool [ 26 ] ; for ( int i = 0 ; i < p ; i ++ ) { allowed [ a [ i ] - ' a ' ] = true ; } char [ , ] freq = new char [ k , 26 ] ; for ( int i = 0 ; i < k ; i ++ ) for ( int j = 0 ; j < 26 ; j ++ ) freq [ i , j ] = ( char ) 0 ; for ( int i = 0 ; i < n ; i ++ ) { freq [ i % k , s [ i ] - ' a ' ] += ( char ) 1 ; } int ans = 0 ; int totalpositions = n / k ; for ( int i = 0 ; i < k ; i ++ ) { int maxfrequency = 0 ; for ( int j = 0 ; j < 26 ; j ++ ) { if ( freq [ i , j ] > maxfrequency && allowed [ j ] == true ) maxfrequency = freq [ i , j ] ; } ans += ( totalpositions - maxfrequency + ( ( i % k < n % k ) ? 1 : 0 ) ) ; } Console . Write ( ans + "
 " ) ; } public static void Main ( String [ ] args ) { String S = " nihsiakyt " ; int n = S . Length ; int K = 3 ; char [ ] A = { ' n ' , ' i ' , ' p ' , ' s ' , ' q ' } ; int p = A . Length ; minFlip ( S , n , K , A , p ) ; } }


"Minimum number of swaps required to sort an array | C # program to find minimum number of swaps required to sort an array ; swap function ; indexOf function ; Return the minimum number of swaps required to sort the array ; This is checking whether the current element is at the right place or not ; Swap the current element with the right index so that arr [ 0 ] to arr [ i ] is sorted ; Driver program to test the above function ; Output will be 5"
Solution in C#:

using System ; public class GFG { static void swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } static int indexOf ( int [ ] arr , int ele ) { for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( arr [ i ] == ele ) { return i ; } } return - 1 ; } static int minSwaps ( int [ ] arr , int N ) { int ans = 0 ; int [ ] temp = new int [ N ] ; Array . Copy ( arr , temp , N ) ; Array . Sort ( temp ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] != temp [ i ] ) { ans ++ ; swap ( arr , i , indexOf ( arr , temp [ i ] ) ) ; } } return ans ; } static public void Main ( ) { int [ ] a = { 101 , 758 , 315 , 730 , 472 , 619 , 460 , 479 } ; int n = a . Length ; Console . WriteLine ( minSwaps ( a , n ) ) ; } }


"Minimum number of swaps to make two binary string equal | C # program for the above approach ; Function to calculate min swaps to make binary strings equal ; Count of zero 's  ; Count of one 's  ; As discussed above ; Driver code"
Solution in C#:

using System ; class GFG { static int minSwaps ( string s1 , string s2 ) { int c0 = 0 , c1 = 0 ; for ( int i = 0 ; i < s1 . Length ; i ++ ) { if ( s1 [ i ] == '0' && s2 [ i ] == '1' ) { c0 ++ ; } else if ( s1 [ i ] == '1' && s2 [ i ] == '0' ) { c1 ++ ; } } int ans = c0 / 2 + c1 / 2 ; if ( c0 % 2 == 0 && c1 % 2 == 0 ) { return ans ; } else if ( ( c0 + c1 ) % 2 == 0 ) { return ans + 2 ; } else { return - 1 ; } } public static void Main ( ) { string s1 = "0011" , s2 = "1111" ; int ans = minSwaps ( s1 , s2 ) ; Console . WriteLine ( ans ) ; } }


"Minimum number of towers required such that every house is in the range of at least one tower | C # implementation of above approach ; Function to count the number of tower ; first we sort the house numbers ; for count number of towers ; for iterate all houses ; count number of towers ; find find the middle location ; traverse till middle location ; this is point to middle house where we insert the tower ; now find the last location ; traverse till last house of the range ; return the number of tower ; Driver code ; given elements ; print number of towers"
Solution in C#:

using System ; public class Improve { static int number_of_tower ( int [ ] house , int range , int n ) { Array . Sort ( house ) ; int numOfTower = 0 ; int i = 0 ; while ( i < n ) { numOfTower ++ ; int loc = house [ i ] + range ; while ( i < n && house [ i ] <= loc ) i ++ ; -- i ; loc = house [ i ] + range ; while ( i < n && house [ i ] <= loc ) i ++ ; } return numOfTower ; } public static void Main ( ) { int [ ] house = { 7 , 2 , 4 , 6 , 5 , 9 , 12 , 11 } ; int range = 2 ; int n = house . Length ; Console . WriteLine ( number_of_tower ( house , range , n ) ) ; } }


"Minimum number of working days required to achieve each of the given scores | C # program for the above approach ; Function to find the minimum number of days required to work to at least arr [ i ] points for every array element ; Traverse the array P [ ] ; Find the prefix sum ; Traverse the array arr [ ] ; Find the minimum index of the array having value at least arr [ i ] ; If the index is not - 1 ; Otherwise ; Function to find the lower bound of N using binary search ; Stores the lower bound ; Stores the upper bound ; Stores the minimum index having value is at least N ; Iterater while i <= j ; Stores the mid index of the range [ i , j ] ; If P [ mid ] is at least N ; Update the value of mid to index ; Update the value of j ; Update the value of i ; Return the resultant index ; Driver Code"
Solution in C#:

using System ; class GFG { static void minDays ( int [ ] P , int [ ] arr ) { for ( int i = 1 ; i < P . Length ; i ++ ) { P [ i ] += P [ i ] + P [ i - 1 ] ; } for ( int i = 0 ; i < arr . Length ; i ++ ) { int index = binarySeach ( P , arr [ i ] ) ; if ( index != - 1 ) { Console . Write ( index + 1 + " ▁ " ) ; } else { Console . Write ( - 1 + " ▁ " ) ; } } } static int binarySeach ( int [ ] P , int N ) { int i = 0 ; int j = P . Length - 1 ; int index = - 1 ; while ( i <= j ) { int mid = i + ( j - i ) / 2 ; if ( P [ mid ] >= N ) { index = mid ; j = mid - 1 ; } else { i = mid + 1 ; } } return index ; } public static void Main ( string [ ] args ) { int [ ] arr = { 400 , 200 , 700 , 900 , 1400 } ; int [ ] P = { 100 , 300 , 400 , 500 , 600 } ; minDays ( P , arr ) ; } }


"Minimum number operations required to convert n to m | Set | C # implementation of the above approach ; Function to find the minimum number of steps ; If n exceeds M ; If N reaches the target ; The minimum of both the states will be the answer ; Driver code"
Solution in C#:

using System ; class GFG { static int MAXN = 10000000 ; static int minimumSteps ( int n , int m , int a , int b ) { if ( n > m ) return MAXN ; if ( n == m ) return 0 ; return Math . Min ( 1 + minimumSteps ( n * a , m , a , b ) , 1 + minimumSteps ( n * b , m , a , b ) ) ; } public static void Main ( ) { int n = 120 , m = 51840 ; int a = 2 , b = 3 ; Console . WriteLine ( minimumSteps ( n , m , a , b ) ) ; } }


"Minimum number that can be obtained by applying ' + ' and ' * ' operations on array elements | C # program for the above approach ; Function to find the smallest number that can be obtained after applying the arithmetic operations mentioned in the String S ; Stores the count of multiplication operator in the String ; Store the required result ; Iterate in the range to create the mask ; Checking the number of bits that are set in the mask ; Check if the number of bits that are set in the mask is multiplication operation ; Storing the elements that is to be added ; Apply the multiplications operation first ; If sign is ' * ' , then multiply last element of deque with arr [ i ] ; Push last multiplied element in the deque ; If the element is to be added , then add it to the deque ; Add all the element of the deque ; Minimize the answer with the given sum ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { static int minimumSum ( int [ ] A , int N , String S ) { int mul = 0 ; for ( int i = 0 ; i < ( int ) S . Length ; i ++ ) { if ( S [ i ] == ' * ' ) mul += 1 ; } int ans = 1000000 ; for ( int i = 0 ; i < ( 1 << ( N - 1 ) ) ; i ++ ) { int cnt = 0 ; List < char > v = new List < char > ( ) ; for ( int j = 0 ; j < N - 1 ; j ++ ) { if ( ( ( 1 << j ) & ( i ) ) > 0 ) { cnt += 1 ; v . Add ( ' * ' ) ; } else { v . Add ( ' + ' ) ; } } if ( cnt == mul ) { List < int > d = new List < int > ( ) ; d . Add ( A [ 0 ] ) ; for ( int j = 0 ; j < N - 1 ; j ++ ) { if ( v [ j ] == ' * ' ) { int x = d [ d . Count - 1 ] ; d . RemoveAt ( d . Count - 1 ) ; x = x * A [ j + 1 ] ; d . Add ( x ) ; } else { d . Add ( A [ j + 1 ] ) ; } } int sum = 0 ; while ( d . Count > 0 ) { int x = d [ 0 ] ; sum += x ; d . RemoveAt ( 0 ) ; } ans = Math . Min ( ans , sum ) ; } } return ans ; } public static void Main ( String [ ] args ) { int [ ] A = { 2 , 2 , 2 , 2 } ; String S = " * * + " ; int N = A . Length ; Console . Write ( minimumSum ( A , N , S ) ) ; } }


"Minimum number to be added to all digits of X to make X > Y | C # program to find Minimum number to be . Added to all digits of X to make X > Y ; Function to check if X is lexicographically larger Y ; It is lexicographically larger ; Utility function to check minimum value of d ; If X is already larger do not need to . Add anything ; Adding d to all elements of X ; If X is larger now print d ; else print d + 1 ; Driver Code ; Taking the numbers as sequences"
Solution in C#:

using System ; class GFG { static bool IsLarger ( int [ ] X , int [ ] Y , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( X [ i ] < Y [ i ] ) { return false ; } } return true ; } static int solve ( int [ ] X , int [ ] Y , int n ) { int ans = 0 ; if ( IsLarger ( X , Y , n ) ) ans = 0 ; else { int d = Y [ 0 ] - X [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) X [ i ] += d ; if ( IsLarger ( X , Y , n ) ) ans = d ; else { ans = d + 1 ; } } return ans ; } public static void Main ( String [ ] args ) { int [ ] X = { 2 , 3 , 6 , 9 } ; int [ ] Y = { 3 , 4 , 8 , 1 } ; int n = X . Length ; Console . WriteLine ( solve ( X , Y , n ) ) ; } }


"Minimum number using set bits of a given number | An efficient C # program to find minimum number formed by bits of a given number . ; Returns minimum number formed by bits of a given number . ; _popcnt32 ( a ) gives number of 1 's present in binary   representation of a. ; Driver Code ."
Solution in C#:

using System ; using System . Linq ; using System . Collections . Generic ; class GFG { static long minimize ( long a ) { string binaryString = Convert . ToString ( a , 2 ) ; int n = binaryString . Split ( new [ ] { '0' } , StringSplitOptions . RemoveEmptyEntries ) . Length + 1 ; return ( ( long ) Math . Pow ( 2 , n ) - 1 ) ; } static void Main ( ) { long a = 11 ; Console . Write ( minimize ( a ) ) ; } }


"Minimum number with digits as 4 and 7 only and given sum | C # program to find smallest number with given sum of digits . ; Prints minimum number with given digit sum and only allowed digits as 4 and 7. ; Cases where all remaining digits are 4 or 7 ( Remaining sum of digits should be multiple of 4 or 7 ) ; If both 4 s and 7 s are there in digit sum , we subtract a 4. ; Driver code"
Solution in C#:

using System ; class GFG { static void findMin ( int sum ) { int a = 0 , b = 0 ; while ( sum > 0 ) { if ( sum % 7 == 0 ) { b ++ ; sum -= 7 ; } else if ( sum % 4 == 0 ) { a ++ ; sum -= 4 ; } else { a ++ ; sum -= 4 ; } } if ( sum < 0 ) { Console . Write ( " - 1n " ) ; return ; } for ( int i = 0 ; i < a ; i ++ ) Console . Write ( "4" ) ; for ( int i = 0 ; i < b ; i ++ ) Console . Write ( "7" ) ; Console . WriteLine ( ) ; } public static void Main ( ) { findMin ( 15 ) ; } }


"Minimum numbers ( smaller than or equal to N ) with sum S | C # program to find the minimum numbers required to get to S ; Function to find the minimum numbers required to get to S ; Driver Code"
Solution in C#:

using System ; class GFG { static int minimumNumbers ( int n , int s ) { if ( ( s % n ) > 0 ) return s / n + 1 ; else return s / n ; } public static void Main ( ) { int n = 5 ; int s = 11 ; Console . WriteLine ( minimumNumbers ( n , s ) ) ; } }


"Minimum numbers needed to express every integer below N as a sum | C # program to find count of integers needed to express all numbers from 1 to N ; function to count length of binary expression of n ; Driver code"
Solution in C#:

using System ; class GFG { static int countBits ( int n ) { int count = 0 ; while ( n > 0 ) { count ++ ; n >>= 1 ; } return count ; } static public void Main ( ) { int n = 32 ; Console . WriteLine ( " Minimum ▁ value ▁ of ▁ K ▁ is ▁ = ▁ " + countBits ( n ) ) ; } }


"Minimum numbers to be appended such that mean of Array is equal to 1 | C # program for above approach ; Function to calculate minimum Number of operations ; Storing sum of array arr [ ] ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static void minumumOperation ( int N , int [ ] arr ) { int sum_arr = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum_arr = sum_arr + arr [ i ] ; } if ( sum_arr >= N ) Console . Write ( sum_arr - N ) ; else Console . Write ( 1 ) ; } static public void Main ( ) { int N = 4 ; int [ ] arr = { 8 , 4 , 6 , 2 } ; minumumOperation ( N , arr ) ; } }


"Minimum numbers with one 's place as 9 to be added to get N | C # implementation of the approach ; Function to find minimum count of numbers ( with one 's digit 9)  that sum up to N ; Fetch one 's digit ; Apply Cases mentioned in approach ; If no possible answer exists ; Driver code"
Solution in C#:

using System ; class GFG { static int findMin ( int N ) { int digit = N % 10 ; switch ( digit ) { case 0 : if ( N >= 90 ) return 10 ; break ; case 1 : if ( N >= 81 ) return 9 ; break ; case 2 : if ( N >= 72 ) return 8 ; break ; case 3 : if ( N >= 63 ) return 7 ; break ; case 4 : if ( N >= 54 ) return 6 ; break ; case 5 : if ( N >= 45 ) return 5 ; break ; case 6 : if ( N >= 36 ) return 4 ; break ; case 7 : if ( N >= 27 ) return 3 ; break ; case 8 : if ( N >= 18 ) return 2 ; break ; case 9 : if ( N >= 9 ) return 1 ; break ; } return - 1 ; } public static void Main ( String [ ] args ) { int N = 27 ; Console . WriteLine ( findMin ( N ) ) ; } }


"Minimum odd cost path in a matrix | C # program to find minimum odd cost path in a matrix ; Function to find the minimum cost ; leftmost element ; rightmost element ; Any element except leftmost and rightmost element of a row is reachable from direct upper or left upper or right upper row 's block ; Counting the minimum cost ; Find the minimum cost ; Driver code"
Solution in C#:

using System ; public class GFG { public static int M = 100 ; public static int N = 100 ; static int find_min_odd_cost ( int [ , ] given , int m , int n ) { int [ , ] floor = new int [ M , N ] ; int min_odd_cost = 0 ; int i , j , temp ; for ( j = 0 ; j < n ; j ++ ) floor [ 0 , j ] = given [ 0 , j ] ; for ( i = 1 ; i < m ; i ++ ) for ( j = 0 ; j < n ; j ++ ) { if ( j == 0 ) { floor [ i , j ] = given [ i , j ] ; floor [ i , j ] += Math . Min ( floor [ i - 1 , j ] , floor [ i - 1 , j + 1 ] ) ; } else if ( j == n - 1 ) { floor [ i , j ] = given [ i , j ] ; floor [ i , j ] += Math . Min ( floor [ i - 1 , j ] , floor [ i - 1 , j - 1 ] ) ; } else { temp = Math . Min ( floor [ i - 1 , j ] , floor [ i - 1 , j - 1 ] ) ; temp = Math . Min ( temp , floor [ i - 1 , j + 1 ] ) ; floor [ i , j ] = given [ i , j ] + temp ; } } min_odd_cost = int . MaxValue ; for ( j = 0 ; j < n ; j ++ ) { if ( floor [ n - 1 , j ] % 2 == 1 ) { if ( min_odd_cost > floor [ n - 1 , j ] ) min_odd_cost = floor [ n - 1 , j ] ; } } if ( min_odd_cost == int . MinValue ) return - 1 ; return min_odd_cost ; } public static void Main ( ) { int m = 5 , n = 5 ; int [ , ] given = { { 1 , 2 , 3 , 4 , 6 } , { 1 , 2 , 3 , 4 , 5 } , { 1 , 2 , 3 , 4 , 5 } , { 1 , 2 , 3 , 4 , 5 } , { 100 , 2 , 3 , 4 , 5 } } ; Console . Write ( " Minimum ▁ odd ▁ cost ▁ is ▁ " + find_min_odd_cost ( given , m , n ) ) ; } }


"Minimum operation require to make first and last character same | C # program to find minimum operation require to make first and last character same ; Function to find minimum operation require to make first and last character same ; Base conditions ; If answer found ; If string is already visited ; Decrement ending index only ; Increment starting index only ; Increment starting index and decrement index ; Store the minimum value ; Driver code ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int MAX = Int32 . MaxValue ; static Dictionary < string , int > m = new Dictionary < string , int > ( ) ; static int Min = Int32 . MaxValue ; static int minOperation ( string s , int i , int j , int count ) { if ( ( i >= s . Length && j < 0 ) || ( i == j ) ) { return MAX ; } if ( s [ i ] == s [ j ] || ( count >= Min ) ) { return count ; } string str = i . ToString ( ) + " | " + j . ToString ( ) ; if ( ! m . ContainsKey ( str ) ) { if ( i >= s . Length ) { m [ str ] = minOperation ( s , i , j - 1 , count + 1 ) ; } else if ( j < 0 ) { m [ str ] = minOperation ( s , i + 1 , j , count + 1 ) ; } else { m [ str ] = Math . Min ( minOperation ( s , i , j - 1 , count + 1 ) , minOperation ( s , i + 1 , j , count + 1 ) ) ; } } if ( m [ str ] < Min ) { Min = m [ str ] ; } return m [ str ] ; } static void Main ( ) { string s = " bacdefghipalop " ; int ans = minOperation ( s , 0 , s . Length - 1 , 0 ) ; if ( ans == MAX ) { Console . WriteLine ( - 1 ) ; } else { Console . WriteLine ( ans ) ; } } }


"Minimum operation require to make first and last character same | C # program to find minimum operation require to make first and last character same ; Return the minimum operation requires to make string first and last character same . ; Store indexes of first occurrences of characters . ; Initialize all the elements to - 1 ; Initialize result ; Traverse through all characters ; Find first occurrence ; Update result for subsequent occurrences ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int MAX = 256 ; static int minimumOperation ( String s ) { int n = s . Length ; List < int > first_occ = new List < int > ( ) ; for ( int i = 0 ; i < MAX ; i ++ ) first_occ . Insert ( i , - 1 ) ; int res = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { int x = ( int ) s [ i ] ; if ( first_occ [ x ] == - 1 ) first_occ . Insert ( x , i ) ; else { int last_occ = ( n - i - 1 ) ; res = Math . Min ( res , first_occ [ x ] + last_occ ) ; } } return res ; } public static void Main ( String [ ] args ) { String s = " bacdefghipalop " ; Console . WriteLine ( minimumOperation ( s ) ) ; } }


"Minimum operation require to make first and last character same | C # program to minimum operation require to make first and last character same ; Recursive function call ; Decrement ending index only ; Increment starting index only ; Increment starting index and decrement index ; Driver Code ; Function call"
Solution in C#:

using System ; class GFG { static int MAX = Int32 . MaxValue ; static int minOperation ( string s , int i , int j , int count ) { if ( ( i >= s . Length && j < 0 ) || ( i == j ) ) return MAX ; if ( s [ i ] == s [ j ] ) return count ; if ( i >= s . Length ) return minOperation ( s , i , j - 1 , count + 1 ) ; else if ( j < 0 ) return minOperation ( s , i + 1 , j , count + 1 ) ; else return Math . Min ( minOperation ( s , i , j - 1 , count + 1 ) , minOperation ( s , i + 1 , j , count + 1 ) ) ; } public static void Main ( ) { string s = " bacdefghipalop " ; int ans = minOperation ( s , 0 , s . Length - 1 , 0 ) ; if ( ans == MAX ) Console . Write ( - 1 ) ; else Console . Write ( ans ) ; } }


"Minimum operation required to make a balanced sequence | C # implementation of the above approach ; Function to return the minimum operations required ; Condition where we got only one closing bracket instead of 2 , here we have to add one more closing bracket to make the sequence balanced ; Add closing bracket that costs us one operation ; Remove the top opening bracket because we got the 1 opening and 2 continuous closing brackets ; Inserting the opening bracket to stack ; After making the sequence balanced closing is now set to 0 ; Case when there is no opening bracket the sequence starts with a closing bracket and one opening bracket is required Now we have one opening and one closing bracket ; Add opening bracket that costs us one operation ; Assigning 1 to cntClosing because we have one closing bracket ; Case where we got two continuous closing brackets need to pop one opening bracket from stack top ; Condition where stack is not empty This is the case where we have only opening brackets ( st . Count * 2 ) will give us the total closing bracket needed cntClosing is the count of closing bracket that we already have ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int minOperations ( String s , int len ) { int operationCnt = 0 ; Stack < char > st = new Stack < char > ( ) ; int cntClosing = 0 ; for ( int i = 0 ; i < len ; i ++ ) { if ( s [ i ] == ' { ' ) { if ( cntClosing > 0 ) { operationCnt ++ ; st . Pop ( ) ; } st . Push ( s [ i ] ) ; cntClosing = 0 ; } else if ( st . Count != 0 ) { st . Push ( ' { ' ) ; operationCnt ++ ; cntClosing = 1 ; } else { cntClosing = ( cntClosing + 1 ) % 2 ; if ( cntClosing == 0 && st . Count != 0 ) { st . Pop ( ) ; } } } operationCnt += st . Count * 2 - cntClosing + 1 ; return operationCnt ; } public static void Main ( String [ ] args ) { String str = " } } { " ; int len = str . Length ; Console . Write ( minOperations ( str , len ) ) ; } }


"Minimum operation to make all elements equal in array | C # Code For Minimum operation to make all elements equal in array ; function for min operation ; Insert all elements in hash . ; find the max frequency ; return result ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static int minOperation ( int [ ] arr , int n ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . ContainsKey ( arr [ i ] ) ) { var val = m [ arr [ i ] ] ; m . Remove ( arr [ i ] ) ; m . Add ( arr [ i ] , val + 1 ) ; } else { m . Add ( arr [ i ] , 1 ) ; } } int max_count = 0 ; HashSet < int > s = new HashSet < int > ( m . Keys ) ; foreach ( int i in s ) if ( max_count < m [ i ] ) max_count = m [ i ] ; return ( n - max_count ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 5 , 2 , 1 , 3 , 2 , 1 } ; int n = arr . Length ; Console . Write ( minOperation ( arr , n ) ) ; } }


"Minimum operations of given type to make all elements of a matrix equal | C # implementation of the approach ; Function to return the minimum number of operations required ; Create another array to store the elements of matrix ; If not possible ; Sort the array to get median ; To count the minimum operations ; If there are even elements , then there are two medians . We consider the best of two as answer . ; Return minimum operations required ; Driver code"
Solution in C#:

using System ; class GFG { static int minOperations ( int n , int m , int k , int [ , ] matrix ) { int [ ] arr = new int [ n * m ] ; int mod = matrix [ 0 , 0 ] % k ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 0 ; j < m ; ++ j ) { arr [ i * m + j ] = matrix [ i , j ] ; if ( matrix [ i , j ] % k != mod ) { return - 1 ; } } } Array . Sort ( arr ) ; int median = arr [ ( n * m ) / 2 ] ; int minOperations = 0 ; for ( int i = 0 ; i < n * m ; ++ i ) minOperations += Math . Abs ( arr [ i ] - median ) / k ; if ( ( n * m ) % 2 == 0 ) { int median2 = arr [ ( ( n * m ) / 2 ) - 1 ] ; int minOperations2 = 0 ; for ( int i = 0 ; i < n * m ; ++ i ) minOperations2 += Math . Abs ( arr [ i ] - median2 ) / k ; minOperations = Math . Min ( minOperations , minOperations2 ) ; } return minOperations ; } public static void Main ( ) { int [ , ] matrix = { { 2 , 4 , 6 } , { 8 , 10 , 12 } , { 14 , 16 , 18 } , { 20 , 22 , 24 } } ; int n = matrix . GetLength ( 0 ) ; int m = matrix . GetLength ( 1 ) ; int k = 2 ; Console . WriteLine ( minOperations ( n , m , k , matrix ) ) ; } }


"Minimum operations of given type to make all elements of a matrix equal | C # implementation of the approach ; Function to return the minimum number of operations required ; Create another array to store the elements of matrix ; will not work for negative elements , so . . adding this ; adding this to handle negative elements too . ; Sort the array to get median ; To count the minimum operations ; If there are even elements , then there are two medians . We consider the best of two as answer . ; changed here as in case of even elements there will be 2 medians ; Return minimum operations required ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int minOperations ( int n , int m , int k , List < List < int > > matrix ) { List < int > arr = new List < int > ( ) ; int mod ; if ( matrix [ 0 ] [ 0 ] < 0 ) { mod = k - ( Math . Abs ( matrix [ 0 ] [ 0 ] ) % k ) ; } else { mod = matrix [ 0 ] [ 0 ] % k ; } for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 0 ; j < m ; ++ j ) { arr . Add ( matrix [ i ] [ j ] ) ; int val = matrix [ i ] [ j ] ; if ( val < 0 ) { int res = k - ( Math . Abs ( val ) % k ) ; if ( res != mod ) { return - 1 ; } } else { int foo = matrix [ i ] [ j ] ; if ( foo % k != mod ) { return - 1 ; } } } } arr . Sort ( ) ; int median = arr [ ( n * m ) / 2 ] ; int minOperations = 0 ; for ( int i = 0 ; i < n * m ; ++ i ) minOperations += Math . Abs ( arr [ i ] - median ) / k ; if ( ( n * m ) % 2 == 0 ) { int median2 = arr [ ( ( n * m ) / 2 ) - 1 ] ; int minOperations2 = 0 ; for ( int i = 0 ; i < n * m ; ++ i ) minOperations2 += Math . Abs ( arr [ i ] - median2 ) / k ; minOperations = Math . Min ( minOperations , minOperations2 ) ; } return minOperations ; } static void Main ( ) { List < List < int > > matrix = new List < List < int > > { new List < int > { 2 , 4 , 6 } , new List < int > { 8 , 10 , 12 } , new List < int > { 14 , 16 , 18 } , new List < int > { 20 , 22 , 24 } , } ; int n = matrix . Count ; int m = matrix [ 0 ] . Count ; int k = 2 ; Console . Write ( minOperations ( n , m , k , matrix ) ) ; } }


"Minimum operations of the given type required to make a complete graph | C # implementation of the approach ; Function to return the minimum number of steps required ; Driver Code"
Solution in C#:

using System ; class GFG { static int minOperations ( int N ) { double x = Math . Log ( N , 2 ) ; int ans = ( int ) ( Math . Ceiling ( x ) ) ; return ans ; } static void Main ( ) { int N = 10 ; Console . WriteLine ( minOperations ( N ) ) ; } }


"Minimum operations required to change the array such that | arr [ i ] | C # implementation of the approach ; Function to return the minimum number of operations required ; Minimum and maximum elements from the array ; To store the minimum number of operations required ; To store the number of operations required to change every element to either ( num - 1 ) , num or ( num + 1 ) ; If current element is not already num ; Add the count of operations required to change arr [ i ] ; Update the minimum operations so far ; Driver code"
Solution in C#:

using System ; using System . Linq ; class GFG { static int changeTheArray ( int [ ] arr , int n ) { int minEle = arr . Min ( ) ; int maxEle = arr . Max ( ) ; int minOperations = int . MaxValue ; for ( int num = minEle ; num <= maxEle ; num ++ ) { int operations = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != num ) { operations += ( Math . Abs ( num - arr [ i ] ) - 1 ) ; } } minOperations = Math . Min ( minOperations , operations ) ; } return minOperations ; } public static void Main ( String [ ] args ) { int [ ] arr = { 10 , 1 , 4 } ; int n = arr . Length ; Console . WriteLine ( changeTheArray ( arr , n ) ) ; } }


"Minimum operations required to convert a binary string to all 0 s or all 1 s | C # implementation of the approach ; Function to return the count of minimum operations required ; Increment count when consecutive characters are different ; Answer is rounding off the ( count / 2 ) to lower ; Driver code"
Solution in C#:

using System ; class GFG { static int minOperations ( string str , int n ) { int count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( str [ ( i ) ] != str [ ( i + 1 ) ] ) count ++ ; } return ( count + 1 ) / 2 ; } public static void Main ( ) { string str = "000111" ; int n = str . Length ; Console . WriteLine ( minOperations ( str , n ) ) ; } }


"Minimum operations required to convert all characters of a String to a given Character | C # program to implement the above approach ; Function to find the minimum number of operations required ; Maximum number of characters that can be changed in one operation ; If length of the String less than maximum number of characters that can be changed in an operation ; Set the last index as the index for the operation ; Otherwise ; If size of the String is equal to the maximum number of characters in an operation ; Find the number of operations required ; Find the starting position ; Print i - th index ; Shift to next index ; Otherwise ; Find the number of operations required ; If n % div exceeds k ; Print i - th index ; Shift to next index ; Driver Code"
Solution in C#:

 using System ; class GFG { static void countOperations ( int n , int k ) { int div = 2 * k + 1 ; if ( n / 2 <= k ) { Console . Write ( 1 + "
 " ) ; if ( n > k ) Console . Write ( k + 1 ) ; else Console . Write ( n ) ; } else { if ( n % div == 0 ) { int oprn = n / div ; Console . Write ( oprn + "
 " ) ; int pos = k + 1 ; Console . Write ( pos + " ▁ " ) ; for ( int i = 1 ; i <= oprn ; i ++ ) { Console . Write ( pos + " ▁ " ) ; pos += div ; } } else { int oprn = n / div + 1 ; Console . Write ( oprn + "
 " ) ; int pos = n % div ; if ( n % div > k ) pos -= k ; for ( int i = 1 ; i <= oprn ; i ++ ) { Console . Write ( pos + " ▁ " ) ; pos += div ; } } } } public static void Main ( String [ ] args ) { String str = " edfreqwsazxet " ; int n = str . Length ; int k = 4 ; countOperations ( n , k ) ; } }


"Minimum operations required to make all Array elements divisible by K | C # implementation to find the minimum number of moves required to update the array such that each of its element is divisible by K ; Function to find the minimum number of moves required to update the array such that each of its element is divisible by K ; Initialize Map data structure ; Iterate for all the elements of the array ; Calculate the value to be added ; Check if the value equals to 0 then simply continue ; Check if the value to be added is present in the map ; Update the answer ; Print the required result We need to add 1 to maxX because we cant ignore the first move where initially X = 0 and we need to increase it by 1 to make some changes in array ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void compute ( int [ ] a , int N , int K ) { Dictionary < long , long > eqVal = new Dictionary < long , long > ( ) ; long maxX = 0 ; for ( int i = 0 ; i < N ; i ++ ) { long val = a [ i ] % K ; val = ( val == 0 ? 0 : K - val ) ; if ( val == 0 ) continue ; if ( eqVal . ContainsKey ( val ) ) { long numVal = eqVal [ val ] ; maxX = Math . Max ( maxX , val + ( K * numVal ) ) ; eqVal [ val ] = 1 + eqVal . GetValueOrDefault ( val , 0 ) ; } else { eqVal . Add ( val , 1 ) ; maxX = Math . Max ( maxX , val ) ; } } Console . Write ( maxX == 0 ? 0 : maxX + 1 ) ; } public static void Main ( string [ ] args ) { int K = 3 ; int [ ] a = { 1 , 2 , 2 , 18 } ; int N = a . Length ; compute ( a , N , K ) ; } }


"Minimum operations required to make all elements in an array of first N odd numbers equal | C # program for the above approach ; Function to find the minimum number of operations required to make the array elements equal ; If the value of N is even ; Return the value ; Otherwise , N is odd ; Return the value ; Driver code"
Solution in C#:

using System ; class GFG { public static int minOperation ( int N ) { if ( N % 2 == 0 ) { return ( N / 2 ) * ( N / 2 ) ; } int k = ( N - 1 ) / 2 ; return k * ( k + 1 ) ; } static void Main ( ) { int N = 6 ; Console . WriteLine ( minOperation ( N ) ) ; } }


"Minimum operations required to make all elements in an array of first N odd numbers equal | C # program for the above approach ; Function to find the minimum number of operations required to make the array elements equal ; Stores the array elements ; Stores the sum of the array ; Iterate over the range [ 0 , N ] ; Update the value arr [ i ] ; Increment the sum by the value arr [ i ] ; Stores the middle element ; If N is even ; Otherwise ; Stores the result ; Traverse the range [ 0 , N / 2 ] ; Update the value of ans ; Return the result ; Driver Code"
Solution in C#:

using System ; class GFG { public static int minOperations ( int N ) { int [ ] arr = new int [ N ] ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] = ( 2 * i ) + 1 ; sum = sum + arr [ i ] ; } int mid = 0 ; if ( N % 2 == 0 ) { mid = sum / N ; } else { mid = arr [ N / 2 ] ; } int ans = 0 ; for ( int i = 0 ; i < N / 2 ; i ++ ) { ans += mid - arr [ i ] ; } return ans ; } public static void Main ( string [ ] args ) { int N = 6 ; Console . WriteLine ( minOperations ( N ) ) ; } }


"Minimum operations required to make all the array elements equal | C # implementation of the above approach ; Function to return the minimum number of given operation required to make all the array elements equal ; Check if all the elements from kth index to last are equal ; Finding the 1 st element which is not equal to the kth element ; Driver code"
Solution in C#:

using System ; class GFG { static void minOperation ( int n , int k , int [ ] a ) { for ( int i = k ; i < n ; i ++ ) { if ( a [ i ] != a [ k - 1 ] ) Console . WriteLine ( - 1 ) ; } for ( int i = k - 2 ; i > - 1 ; i -- ) { if ( a [ i ] != a [ k - 1 ] ) Console . WriteLine ( i + 1 ) ; } } static public void Main ( ) { int n = 5 ; int k = 3 ; int [ ] a = { 2 , 1 , 1 , 1 , 1 } ; minOperation ( n , k , a ) ; } }


"Minimum operations required to make all the elements distinct in an array | C # program to find Minimum number of changes to make array distinct ; Function that returns minimum number of changes ; Hash - table to store frequency ; Increase the frequency of elements ; Traverse in the map to sum up the ( occurrences - 1 ) of duplicate elements ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class geeks { public static int minimumOperations ( int [ ] a , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( a [ i ] ) ) { var val = mp [ a [ i ] ] ; mp . Remove ( a [ i ] ) ; mp . Add ( a [ i ] , val + 1 ) ; } else { mp . Add ( a [ i ] , 1 ) ; } } int count = 0 ; foreach ( KeyValuePair < int , int > entry in mp ) { if ( entry . Value > 1 ) { count += ( entry . Value - 1 ) ; } } return count ; } public static void Main ( String [ ] args ) { int [ ] a = { 2 , 1 , 2 , 3 , 3 , 4 , 3 } ; int n = a . Length ; Console . WriteLine ( minimumOperations ( a , n ) ) ; } }


"Minimum operations required to make each row and column of matrix equals | C # Program to Find minimum number of operation required such that sum of elements on each row and column becomes same ; Function to find minimum operation required to make sum of each row and column equals ; Initialize the sumRow [ ] and sumCol [ ] array to 0 ; Calculate sumRow [ ] and sumCol [ ] array ; Find maximum sum value in either row or in column ; Find minimum increment required in either row or column ; Add difference in corresponding cell , sumRow [ ] and sumCol [ ] array ; Update the count variable ; If ith row satisfied , increment ith value for next iteration ; If jth column satisfied , increment jth value for next iteration ; Utility function to print matrix ; Driver program"
Solution in C#:

using System ; class GFG { static int findMinOpeartion ( int [ , ] matrix , int n ) { int [ ] sumRow = new int [ n ] ; int [ ] sumCol = new int [ n ] ; for ( int i = 0 ; i < n ; ++ i ) for ( int j = 0 ; j < n ; ++ j ) { sumRow [ i ] += matrix [ i , j ] ; sumCol [ j ] += matrix [ i , j ] ; } int maxSum = 0 ; for ( int i = 0 ; i < n ; ++ i ) { maxSum = Math . Max ( maxSum , sumRow [ i ] ) ; maxSum = Math . Max ( maxSum , sumCol [ i ] ) ; } int count = 0 ; for ( int i = 0 , j = 0 ; i < n && j < n ; ) { int diff = Math . Min ( maxSum - sumRow [ i ] , maxSum - sumCol [ j ] ) ; matrix [ i , j ] += diff ; sumRow [ i ] += diff ; sumCol [ j ] += diff ; count += diff ; if ( sumRow [ i ] == maxSum ) ++ i ; if ( sumCol [ j ] == maxSum ) ++ j ; } return count ; } static void printMatrix ( int [ , ] matrix , int n ) { for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 0 ; j < n ; ++ j ) Console . Write ( matrix [ i , j ] + " ▁ " ) ; Console . WriteLine ( ) ; } } public static void Main ( ) { int [ , ] matrix = { { 1 , 2 } , { 3 , 4 } } ; Console . WriteLine ( findMinOpeartion ( matrix , 2 ) ) ; printMatrix ( matrix , 2 ) ; } }


"Minimum operations required to make every element greater than or equal to K | C # implementation of above approach ; Function to calculate gcd of two numbers ; C # function to get minimum operation needed ; The priority queue holds a minimum element in the top position ; push value one by one from the given array ; store count of minimum operation needed ; All elements are now >= k ; It is impossible to make as there are no sufficient elements available ; Take two smallest elements and replace them by their LCM first smallest element ; Second smallest element ; Increment the count ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int FindMinOperation ( int [ ] a , int n , int k ) { List < int > Q = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) Q . Add ( a [ i ] ) ; Q . Sort ( ) ; int ans = 0 ; while ( true ) { if ( Q [ 0 ] >= k ) break ; if ( Q . Count < 2 ) return - 1 ; int x = Q [ 0 ] ; Q . RemoveAt ( 0 ) ; int y = Q [ 0 ] ; Q . RemoveAt ( 0 ) ; int z = ( x * y ) / gcd ( x , y ) ; Q . Add ( z ) ; Q . Sort ( ) ; ans ++ ; } return ans ; } static void Main ( ) { int [ ] a = { 3 , 5 , 7 , 6 , 8 } ; int k = 8 ; int n = a . Length ; Console . WriteLine ( FindMinOperation ( a , n , k ) ) ; } }


"Minimum operations required to make the string satisfy the given condition | C # implementation of the approach ; Function to return the minimum operations required ; To store the first and the last occurrence of all the characters ; Set the first and the last occurrence of all the characters to - 1 ; Update the occurrences of the characters ; Only set the first occurrence if it hasn 't already been set ; To store the minimum operations ; If the frequency of the current character in the string is less than 2 ; Count of characters to be removed so that the string starts and ends at the current character ; Driver code"
Solution in C#:

using System ; class GFG { readonly static int MAX = 26 ; static int minOperation ( String str , int len ) { int [ ] first = new int [ MAX ] ; int [ ] last = new int [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) { first [ i ] = - 1 ; last [ i ] = - 1 ; } for ( int i = 0 ; i < len ; i ++ ) { int index = ( str [ i ] - ' a ' ) ; if ( first [ index ] == - 1 ) first [ index ] = i ; last [ index ] = i ; } int minOp = - 1 ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( first [ i ] == - 1 first [ i ] == last [ i ] ) continue ; int cnt = len - ( last [ i ] - first [ i ] + 1 ) ; if ( minOp == - 1 cnt < minOp ) minOp = cnt ; } return minOp ; } public static void Main ( String [ ] args ) { String str = " abcda " ; int len = str . Length ; Console . WriteLine ( minOperation ( str , len ) ) ; } }


"Minimum operations required to make two numbers equal | C # program to find minimum operations required to make two numbers equal ; Function to return the minimum operations required ; Keeping B always greater ; Reduce B such that gcd ( A , B ) becomes 1. ; Driver code"
Solution in C#:

 using System ; class GFG { static int minOperations ( int A , int B ) { if ( A > B ) { A = A + B ; B = A - B ; A = A - B ; } B = B / __gcd ( A , B ) ; return B - 1 ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void Main ( String [ ] args ) { int A = 7 , B = 15 ; Console . Write ( minOperations ( A , B ) + "
 " ) ; } }


"Minimum operations required to modify the array such that parity of adjacent elements is different | C # implementation of the approach ; Function to return the parity of a number ; Function to return the minimum number of operations required ; Operation needs to be performed ; Parity of previous element ; Parity of next element ; Update parity of current element to be other than the parities of the previous and the next number ; Driver Code"
Solution in C#:

using System ; class GFG { static int parity ( int a ) { return a % 3 ; } static int solve ( int [ ] array , int size ) { int operations = 0 ; for ( int i = 0 ; i < size - 1 ; i ++ ) { if ( parity ( array [ i ] ) == parity ( array [ i + 1 ] ) ) { operations ++ ; if ( i + 2 < size ) { int pari1 = parity ( array [ i ] ) ; int pari2 = parity ( array [ i + 2 ] ) ; if ( pari1 == pari2 ) { if ( pari1 == 0 ) array [ i + 1 ] = 1 ; else if ( pari1 == 1 ) array [ i + 1 ] = 0 ; else array [ i + 1 ] = 1 ; } else { if ( ( pari1 == 0 && pari2 == 1 ) || ( pari1 == 1 && pari2 == 0 ) ) array [ i + 1 ] = 2 ; if ( ( pari1 == 1 && pari2 == 2 ) || ( pari1 == 2 && pari2 == 1 ) ) array [ i + 1 ] = 0 ; if ( ( pari1 == 2 && pari2 == 0 ) || ( pari1 == 0 && pari2 == 2 ) ) array [ i + 1 ] = 1 ; } } } } return operations ; } public static void Main ( ) { int [ ] array = { 2 , 1 , 3 , 0 } ; int size = array . Length ; Console . WriteLine ( solve ( array , size ) ) ; } }


"Minimum operations required to set all elements of binary matrix | C # program to find minimum operations required to set all the element of binary matrix ; Return minimum operation required to make all 1 s . ; check if this cell equals 0 ; increase the number of moves ; flip from this cell to the start point ; flip the cell ; Driven Program"
Solution in C#:

using System ; public class GFG { public const int N = 5 ; public const int M = 5 ; public static int minOperation ( bool [ ] [ ] arr ) { int ans = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { for ( int j = M - 1 ; j >= 0 ; j -- ) { if ( arr [ i ] [ j ] == false ) { ans ++ ; for ( int k = 0 ; k <= i ; k ++ ) { for ( int h = 0 ; h <= j ; h ++ ) { if ( arr [ k ] [ h ] == true ) { arr [ k ] [ h ] = false ; } else { arr [ k ] [ h ] = true ; } } } } } } return ans ; } public static void Main ( string [ ] args ) { bool [ ] [ ] mat = new bool [ ] [ ] { new bool [ ] { false , false , true , true , true } , new bool [ ] { false , false , false , true , true } , new bool [ ] { false , false , false , true , true } , new bool [ ] { true , true , true , true , true } , new bool [ ] { true , true , true , true , true } } ; Console . WriteLine ( minOperation ( mat ) ) ; } }


"Minimum operations to convert Binary Matrix A to B by flipping submatrix of size K | C # program for the above approach ; Function to count the operations required to convert matrix A to B ; Store the sizes of matrix ; Stores count of flips required ; Traverse the iven matrix ; Check if the matrix values are not equal ; Increment the count of moves ; Check if the current square sized exists or not ; Flip all the bits in this square sized submatrix ; Count of operations required ; Driver Code"
Solution in C#:

using System ; class GFG { static int minMoves ( char [ , ] a , char [ , ] b , int K ) { int n = a . Length ; int m = a . GetLength ( 0 ) ; int cntOperations = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( a [ i , j ] != b [ i , j ] ) { cntOperations ++ ; if ( i + K - 1 >= n j + K - 1 >= m ) { return - 1 ; } for ( int p = 0 ; p <= K - 1 ; p ++ ) { for ( int q = 0 ; q <= K - 1 ; q ++ ) { if ( a [ i + p , j + q ] == '0' ) { a [ i + p , j + q ] = '1' ; } else { a [ i + p , j + q ] = '0' ; } } } } } } return cntOperations ; } public static void Main ( ) { char [ , ] A = new char [ , ] { { '1' , '0' , '0' } , { '0' , '0' , '0' } , { '0' , '0' , '0' } } ; char [ , ] B = new char [ , ] { { '0' , '0' , '0' } , { '0' , '0' , '0' } , { '0' , '0' , '0' } } ; int K = 3 ; Console . WriteLine ( minMoves ( A , B , K ) ) ; } }


"Minimum operations to make Array equal by repeatedly adding K from an element and subtracting K from other | C # program for the above approach ; Function to find the minimum number of operations to make all the elements of the array equal ; Store the sum of the array arr [ ] ; Traverse through the array ; If it is not possible to make all array element equal ; Store the minimum number of operations needed ; Traverse through the array ; Finally , print the minimum number operation to make array elements equal ; Driver Code ; Given Input ; Function Call"
Solution in C#:

using System ; class GFG { static void miniOperToMakeAllEleEqual ( int [ ] arr , int n , int k ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } if ( sum % n != 0 ) { Console . WriteLine ( - 1 ) ; return ; } int valueAfterDivision = sum / n ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( Math . Abs ( valueAfterDivision - arr [ i ] ) % k != 0 ) { Console . WriteLine ( - 1 ) ; return ; } count += Math . Abs ( valueAfterDivision - arr [ i ] ) / k ; } Console . WriteLine ( ( int ) count / 2 ) ; } static void Main ( ) { int n = 3 , k = 3 ; int [ ] arr = { 5 , 8 , 11 } ; miniOperToMakeAllEleEqual ( arr , n , k ) ; } }


"Minimum operations to make Array equal by repeatedly adding K from an element and subtracting K from other | C # program for the above approach ; Function to find the minimum number of operations to make all the elements of the array equal ; Store the sum of the array arr [ ] ; Traverse through the array ; If it is not possible to make all array element equal ; Store the minimum number of operations needed ; Traverse through the array ; Finally , print the minimum number operation to make array elements equal ; Given Input ; Function Call"
Solution in C#:


"Minimum operations to make GCD of array a multiple of k | C # program to make GCD of array a multiple of k . ; If array value is not 1 and it is greater than k then we can increase the or decrease the remainder obtained by dividing k from the ith value of array so that we get the number which is either closer to k or its multiple ; Else we only have one choice which is to increment the value to make equal to k ; Driver code"
Solution in C#:

using System ; public class GFG { static int MinOperation ( int [ ] a , int n , int k ) { int result = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( a [ i ] != 1 && a [ i ] > k ) { result = result + Math . Min ( a [ i ] % k , k - a [ i ] % k ) ; } else { result = result + k - a [ i ] ; } } return result ; } static public void Main ( ) { int [ ] arr = { 4 , 5 , 6 } ; int n = arr . Length ; int k = 5 ; Console . WriteLine ( MinOperation ( arr , n , k ) ) ; } }


"Minimum operations to make counts of remainders same in an array | C # implementation of the above approach ; Function to return the minimum number of operations required ; To store modulos values ; If it 's size greater than k  it needed to be decreased ; If it 's size is less than k  it needed to be increased ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int minOperations ( int n , int [ ] a , int m ) { int k = n / m ; List < int > [ ] val = new List < int > [ m ] ; for ( int i = 0 ; i < val . Length ; i ++ ) val [ i ] = new List < int > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { val [ a [ i ] % m ] . Add ( i ) ; } long ans = 0 ; List < pair > extra = new List < pair > ( ) ; for ( int i = 0 ; i < 2 * m ; ++ i ) { int cur = i % m ; while ( ( val [ cur ] . Count ) > k ) { int elem = val [ cur ] [ val [ cur ] . Count - 1 ] ; val [ cur ] . RemoveAt ( val [ cur ] . Count - 1 ) ; extra . Add ( new pair ( elem , i ) ) ; } while ( val [ cur ] . Count < k && extra . Count != 0 ) { int elem = extra [ extra . Count - 1 ] . first ; int mmod = extra [ extra . Count - 1 ] . second ; extra . RemoveAt ( extra . Count - 1 ) ; val [ cur ] . Add ( elem ) ; ans += i - mmod ; } } return ( int ) ans ; } public static void Main ( String [ ] args ) { int m = 3 ; int [ ] a = { 3 , 2 , 0 , 6 , 10 , 12 } ; int n = a . Length ; Console . Write ( minOperations ( n , a , m ) ) ; } }


"Minimum operations to make product of adjacent element pair of prefix sum negative | C # code for the above approach ; Function to find minimum operations needed to make the product of any two adjacent elements in prefix sum array negative ; Stores the minimum operations ; Stores the prefix sum and number of operations ; Traverse the array ; Update the value of sum ; Check if i + r is odd ; Check if prefix sum is not positive ; Update the value of ans and sum ; Check if prefix sum is not negative ; Update the value of ans and sum ; Update the value of res ; Print the value of res ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void minOperations ( List < int > a ) { int res = Int32 . MaxValue ; int N = a . Count ; for ( int r = 0 ; r < 2 ; r ++ ) { int sum = 0 , ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += a [ i ] ; if ( ( i + r ) % 2 == 1 ) { if ( sum <= 0 ) { ans += - sum + 1 ; sum = 1 ; } } else { if ( sum >= 0 ) { ans += sum + 1 ; sum = - 1 ; } } } res = Math . Min ( res , ans ) ; } Console . Write ( res ) ; } public static void Main ( ) { List < int > a = new List < int > ( ) { 1 , - 3 , 1 , 0 } ; minOperations ( a ) ; } }


"Minimum operations to make two numbers equal | C # implementation of the above approach ; Function to find the minimum no of operations ; find the maximum of two and store it in p ; increase it until it is achievable from given n and m ; Here value added to n and m will be S ( n ) = p - n + p - m ; check whether integer value of n exist by the formula n = ( - 1 + Math . sqrt ( 1 + 8 * S ( n ) ) ) / 2 ; Driver code ; Function calling"
Solution in C#:

using System ; class GFG { static int minOperations ( int n , int m ) { int a = 0 , k = 1 ; int p = Math . Max ( n , m ) ; while ( n != m ) { float s = ( float ) ( p - n + p - m ) ; float q = ( float ) ( ( - 1 + Math . Sqrt ( 8 * s + 1 ) ) / 2 ) ; if ( q - Math . Floor ( q ) == 0 ) { a = ( int ) q ; n = m ; } p = p + 1 ; } return a ; } public static void Main ( ) { int n = 1 , m = 3 ; Console . Write ( minOperations ( n , m ) ) ; } }


"Minimum operations to transform given string to another by moving characters to front or end | C # program for the above approach ; Function that finds the minimum number of steps to find the minimum characters must be moved to convert String s to t ; r = maximum value over all dp [ i , j ] computed so far ; dp [ i , j ] stores the longest contiguous suffix of T [ 0. . j ] that is subsequence of S [ 0. . i ] ; Update the maximum length ; Return the resulting length ; Driver Code ; Given String s , t ; Function Call"
Solution in C#:

using System ; class GFG { static int [ , ] dp = new int [ 1010 , 1010 ] ; static int solve ( String s , String t ) { int n = s . Length ; int r = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { dp [ i , j ] = 0 ; if ( i > 0 ) { dp [ i , j ] = Math . Max ( dp [ i - 1 , j ] , dp [ i , j ] ) ; } if ( s [ i ] == t [ j ] ) { int ans = 1 ; if ( i > 0 && j > 0 ) { ans = 1 + dp [ i - 1 , j - 1 ] ; } dp [ i , j ] = Math . Max ( dp [ i , j ] , ans ) ; r = Math . Max ( r , dp [ i , j ] ) ; } } } return ( n - r ) ; } public static void Main ( String [ ] args ) { String s = " abcde " ; String t = " edacb " ; Console . Write ( solve ( s , t ) ) ; } }


"Minimum pair merge operations required to make Array non | C # program to implement the above approach ; Function to find the minimum operations to make the array Non - increasing ; Size of the array ; Dp table initialization ; dp [ i ] : Stores minimum number of operations required to make subarray { A [ i ] , ... , A [ N ] } non - increasing ; Increment the value of j ; Add current value to sum ; Update the dp tables ; Return the answer ; Driver code"
Solution in C#:

using System ; class GFG { static int solve ( int [ ] a ) { int n = a . Length ; int [ ] dp = new int [ n + 1 ] ; int [ ] val = new int [ n + 1 ] ; for ( int i = n - 1 ; i >= 0 ; i -- ) { int sum = a [ i ] ; int j = i ; while ( j + 1 < n && sum < val [ j + 1 ] ) { j ++ ; sum += a [ j ] ; } dp [ i ] = ( j - i ) + dp [ j + 1 ] ; val [ i ] = sum ; } return dp [ 0 ] ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 5 , 3 , 9 , 1 } ; Console . Write ( solve ( arr ) ) ; } }


"Minimum pairs required to be removed such that the array does not contain any pair with sum K | C # program for the above approach ; Function to find the maximum count of pairs required to be removed such that no pairs exist whose sum equal to K ; Stores maximum count of pairs required to be removed such that no pairs exist whose sum equal to K ; Base Case ; Sort the array ; Stores index of left pointer ; Stores index of right pointer ; Stores sum of left and right pointer ; If s equal to k ; Update cntPairs ; Update left ; Update right ; If s > k ; Update right ; Update left ; Return the cntPairs ; Driver Code ; Function call"
Solution in C#:

using System ; class GFG { static int maxcntPairsSumKRemoved ( int [ ] arr , int k ) { int cntPairs = 0 ; if ( arr . Length <= 1 ) return cntPairs ; Array . Sort ( arr ) ; int left = 0 ; int right = arr . Length - 1 ; while ( left < right ) { int s = arr [ left ] + arr [ right ] ; if ( s == k ) { cntPairs += 1 ; left += 1 ; right -= 1 ; } else if ( s > k ) right -= 1 ; else left += 1 ; } return cntPairs ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int K = 5 ; Console . WriteLine ( maxcntPairsSumKRemoved ( arr , K ) ) ; } }


"Minimum partitions of maximum size 2 and sum limited by given value | C # program to count minimum number of partitions of size 2 and sum smaller than or equal to given key . ; sort the array ; if sum of ith smaller and jth larger element is less than key , then pack both numbers in a set otherwise pack the jth larger number alone in the set ; After ending of loop i will contain minimum number of sets ; Driver Code"
Solution in C#:

using System ; class GFG { static int minimumSets ( int [ ] arr , int n , int key ) { int i , j ; Array . Sort ( arr ) ; for ( i = 0 , j = n - 1 ; i <= j ; ++ i ) if ( arr [ i ] + arr [ j ] <= key ) j -- ; return i ; } public static void Main ( ) { int [ ] arr = { 3 , 5 , 3 , 4 } ; int n = arr . Length ; int key = 5 ; Console . WriteLine ( minimumSets ( arr , n , key ) ) ; } }


"Minimum peak elements from an array by their repeated removal at every iteration of the array | C # program for the above approach ; Function to return the list of minimum peak elements ; Length of original list ; Initialize resultant list ; Traverse each element of list ; Length of original list after removing the peak element ; Traverse new list after removal of previous min peak element ; Update min and index , if first element of list > next element ; Update min and index , if last element of list > previous one ; Update min and index , if list has single element ; Update min and index , if current element > adjacent elements ; Remove current min peak element from list ; Insert min peak into resultant list ; Print resultant list ; Driver Code ; Given array [ ] arr ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void minPeaks ( List < int > list ) { int n = list . Count ; List < int > result = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int min = int . MaxValue ; int index = - 1 ; int size = list . Count ; for ( int j = 0 ; j < size ; j ++ ) { if ( j == 0 && j + 1 < size ) { if ( list [ j ] > list [ j + 1 ] && min > list [ j ] ) { min = list [ j ] ; index = j ; } } else if ( j == size - 1 && j - 1 >= 0 ) { if ( list [ j ] > list [ j - 1 ] && min > list [ j ] ) { min = list [ j ] ; index = j ; } } else if ( size == 1 ) { min = list [ j ] ; index = j ; } else if ( list [ j ] > list [ j - 1 ] && list [ j ] > list [ j + 1 ] && min > list [ j ] ) { min = list [ j ] ; index = j ; } } list . RemoveAt ( index ) ; result . Add ( min ) ; } for ( int i = 0 ; i < result . Count ; i ++ ) Console . Write ( result [ i ] + " , ▁ " ) ; } public static void Main ( String [ ] args ) { List < int > arr = new List < int > { 1 , 9 , 7 , 8 , 2 , 6 } ; minPeaks ( arr ) ; } }


"Minimum positive integer divisible by C and is not in range [ A , B ] | C # implementation of the approach ; Function to return the required number ; If doesn 't belong to the range  then c is the required number ; Else get the next multiple of c starting from b + 1 ; Driver code"
Solution in C#:

using System ; class GFG { static int getMinNum ( int a , int b , int c ) { if ( c < a c > b ) { return c ; } int x = ( ( b / c ) * c ) + c ; return x ; } static public void Main ( ) { int a = 2 , b = 4 , c = 4 ; Console . WriteLine ( getMinNum ( a , b , c ) ) ; } }


"Minimum positive integer to divide a number such that the result is an odd | C # program to make a number odd ; Function to find the value ; Return 1 if already odd ; Check how many times it is divided by 2 ; Driver code"
Solution in C#:

using System ; class GFG { static int makeOdd ( int n ) { if ( n % 2 != 0 ) return 1 ; int ans = 1 ; while ( n % 2 == 0 ) { n /= 2 ; ans *= 2 ; } return ans ; } public static void Main ( ) { int n = 36 ; int res = makeOdd ( n ) ; Console . Write ( res ) ; } }


"Minimum positive integer to divide a number such that the result is an odd | C # program to make a number odd ; Return 1 if already odd ; Check on dividing with a number when the result becomes odd Return that number ; If n is divided by i and n / i is odd then return i ; Driver code"
Solution in C#:

using System ; class GFG { static int makeOdd ( int n ) { if ( n % 2 != 0 ) return 1 ; int i ; for ( i = 2 ; i <= n ; i ++ ) if ( ( n % i == 0 ) && ( ( n / i ) % 2 == 1 ) ) break ; return i ; } public static void Main ( ) { int n = 36 ; int res = makeOdd ( n ) ; Console . Write ( res ) ; } }


"Minimum positive integer value possible of X for given A and B in X = P * A + Q * B | CSHARP Program to find minimum value of X in equation X = P * A + Q * B ; function to get gcd of a and b ; Driver Code"
Solution in C#:

using System ; class GFG { public static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static public void Main ( ) { int a = 2 ; int b = 4 ; Console . WriteLine ( gcd ( a , b ) ) ; } }


"Minimum possible final health of the last monster in a game | C # implementation of the approach ; Function to return the gcd of two numbers ; Function to return the minimum possible health for the monster ; gcd of first and second element ; gcd for all subsequent elements ; Driver code"
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 solve ( int [ ] health , int n ) { int currentgcd = gcd ( health [ 0 ] , health [ 1 ] ) ; for ( int i = 2 ; i < n ; ++ i ) { currentgcd = gcd ( currentgcd , health [ i ] ) ; } return currentgcd ; } public static void Main ( String [ ] args ) { int [ ] health = { 4 , 6 , 8 , 12 } ; int n = health . Length ; Console . WriteLine ( solve ( health , n ) ) ; } }


"Minimum possible number with the given operation | C # implementation of the above approach ; Function to return the minimum possible integer that can be obtained from the given integer after performing the given operations ; For every digit ; Digits less than 5 need not to be changed as changing them will lead to a larger number ; The resulting integer cannot have leading zero ; Driver code"
Solution in C#:

using System ; class GFG { static string minInt ( char [ ] str ) { for ( int i = 0 ; i < str . Length ; i ++ ) { if ( ( int ) str [ i ] >= ( int ) ( '5' ) ) { str [ i ] = ( char ) ( ( ( int ) ( '9' ) - ( int ) ( str [ i ] ) ) + ( int ) ( '0' ) ) ; } } if ( str [ 0 ] == '0' ) str [ 0 ] = '9' ; string s = new string ( str ) ; return s ; } static public void Main ( ) { string str = "589" ; Console . WriteLine ( minInt ( str . ToCharArray ( ) ) ) ; } }


"Minimum possible sum of array B such that AiBi = AjBj for all 1 â ‰¤ i < j â ‰¤ N | C # implementation of the approach ; To store least prime factors of all the numbers ; Function to find the least prime factor of all the numbers ; Function to return the ( ( a ^ m1 ) % mod ) ; Function to return the sum of elements of array B ; Find the prime factors of all the numbers ; To store each prime count in lcm ; Current number ; Map to store the prime count of a single number ; Basic way to calculate all prime factors ; If it is the first number in the array ; Take the maximum count of prime in a number ; Calculate lcm of given array ; Calculate sum of elements of array B ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int mod = 1000000007 ; static int N = 1000005 ; static int [ ] lpf = new int [ N ] ; static void least_prime_factor ( ) { for ( int i = 1 ; i < N ; i ++ ) lpf [ i ] = i ; for ( int i = 2 ; i < N ; i ++ ) if ( lpf [ i ] == i ) for ( int j = i * 2 ; j < N ; j += i ) if ( lpf [ j ] == j ) lpf [ j ] = i ; } static long power ( long a , long m1 ) { if ( m1 == 0 ) return 1 ; else if ( m1 == 1 ) return a ; else if ( m1 == 2 ) return ( a * a ) % mod ; else if ( ( m1 & 1 ) != 0 ) return ( a * power ( power ( a , m1 / 2 ) , 2 ) ) % mod ; else return power ( power ( a , m1 / 2 ) , 2 ) % mod ; } static long sum_of_elements ( long [ ] a , int n ) { least_prime_factor ( ) ; Dictionary < long , long > prime_factor = new Dictionary < long , long > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { long temp = a [ i ] ; Dictionary < long , long > single_number = new Dictionary < long , long > ( ) ; while ( temp > 1 ) { long x = lpf [ ( int ) temp ] ; if ( single_number . ContainsKey ( x ) ) { single_number [ x ] ++ ; } else { single_number [ x ] = 1 ; } temp /= x ; } if ( i == 0 ) prime_factor = single_number ; else { foreach ( KeyValuePair < long , long > ele in single_number ) { if ( prime_factor . ContainsKey ( ele . Key ) ) { prime_factor [ ele . Key ] = Math . Max ( ele . Value , prime_factor [ ele . Key ] ) ; } else { prime_factor [ ele . Key ] = Math . Max ( ele . Value , 0 ) ; } } } } long ans = 0 , lcm = 1 ; foreach ( KeyValuePair < long , long > x in prime_factor ) { lcm = ( lcm * power ( x . Key , x . Value ) ) % mod ; } for ( int i = 0 ; i < n ; i ++ ) ans = ( ans + ( lcm * power ( a [ i ] , mod - 2 ) ) % mod ) % mod ; return ans ; } public static void Main ( string [ ] args ) { long [ ] a = { 2 , 3 , 4 } ; int n = a . Length ; Console . Write ( sum_of_elements ( a , n ) ) ; } }


"Minimum possible sum of array elements after performing the given operation | C # implementation of the approach ; Function to return the minimized sum ; To store the largest element from the array which is divisible by x ; Sum of array elements before performing any operation ; If current element is divisible by x and it is maximum so far ; Update the minimum element ; If no element can be reduced then there 's no point in performing the  operation as we will end up increasing  the sum when an element is multiplied by x ; Subtract the chosen elements from the sum and then add their updated values ; Return the minimized sum ; Driver code"
Solution in C#:

using System ; class GFG { static int minSum ( int [ ] arr , int n , int x ) { int sum = 0 ; int largestDivisible = - 1 , minimum = arr [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( arr [ i ] % x == 0 && largestDivisible < arr [ i ] ) largestDivisible = arr [ i ] ; if ( arr [ i ] < minimum ) minimum = arr [ i ] ; } if ( largestDivisible == - 1 ) return sum ; int sumAfterOperation = sum - minimum - largestDivisible + ( x * minimum ) + ( largestDivisible / x ) ; return Math . Min ( sum , sumAfterOperation ) ; } public static void Main ( ) { int [ ] arr = { 5 , 5 , 5 , 5 , 6 } ; int n = arr . Length ; int x = 3 ; Console . WriteLine ( minSum ( arr , n , x ) ) ; } }


"Minimum possible sum of array elements after performing the given operation | C # program to find the minimum possible sum of the array elements after performing the given operation ; Function to find the maximum sum of the sub array ; max_so_far represents the maximum sum found till now and max_ending_here represents the maximum sum ending at a specific index ; Iterating through the array to find the maximum sum of the subarray ; If the maximum sum ending at a specific index becomes less than 0 , then making it equal to 0. ; Function to find the minimum possible sum of the array elements after performing the given operation ; Finding the sum of the array ; Computing the minimum sum of the array ; Driver code"
Solution in C#:

 using System ; class GFG { static double maxSubArraySum ( double [ ] a , int size ) { double max_so_far = Int32 . MinValue , max_ending_here = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; if ( max_ending_here < 0 ) max_ending_here = 0 ; } return max_so_far ; } static void minPossibleSum ( double [ ] a , int n , double x ) { double mxSum = maxSubArraySum ( a , n ) ; double sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; } sum = sum - mxSum + mxSum / x ; Console . Write ( sum + "
 " ) ; } public static void Main ( ) { int N = 3 ; double X = 2 ; double [ ] A = { 1 , - 2 , 3 } ; minPossibleSum ( A , N , X ) ; } }


"Minimum possible sum of prices of a Triplet from the given Array | C # program to implement the above approach ; Function to minimize the sum of price by taking a triplet ; Initialize a [ ] dp array ; Stores the readonly result ; Iterate for all values till N ; Check if num [ j ] > num [ i ] ; Update dp [ j ] if it is greater than stored value ; Update the minimum sum as ans ; If there is no minimum sum exist then print - 1 else print the ans ; Driver Code"
Solution in C#:

using System ; class GFG { public static long minSum ( int n , int [ ] num , int [ ] price ) { long [ ] dp = new long [ n ] ; for ( int i = 0 ; i < n ; i ++ ) dp [ i ] = int . MaxValue ; long ans = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( num [ j ] > num [ i ] ) { dp [ j ] = ( long ) Math . Min ( ( long ) dp [ j ] , ( long ) price [ i ] + ( long ) price [ j ] ) ; ans = Math . Min ( ans , ( long ) dp [ i ] + ( long ) price [ j ] ) ; } } } return ans != int . MaxValue ? ans : - 1 ; } public static void Main ( String [ ] args ) { int [ ] num = { 2 , 4 , 6 , 7 , 8 } ; int [ ] price = { 10 , 20 , 100 , 20 , 40 } ; int n = price . Length ; Console . WriteLine ( minSum ( n , num , price ) ) ; } }


"Minimum possible travel cost among N cities | C # implementation of the approach ; Function to return the minimum cost to travel from the first city to the last ; To store the total cost ; Start from the first city ; If found any city with cost less than that of the previous boarded bus then change the bus ; Calculate the cost to travel from the currently boarded bus till the current city ; Update the currently boarded bus ; Finally calculate the cost for the last boarding bus till the ( N + 1 ) th city ; Driver code"
Solution in C#:

using System ; class GFG { static int minCost ( int [ ] cost , int n ) { int totalCost = 0 ; int boardingBus = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( cost [ boardingBus ] > cost [ i ] ) { totalCost += ( ( i - boardingBus ) * cost [ boardingBus ] ) ; boardingBus = i ; } } totalCost += ( ( n - boardingBus ) * cost [ boardingBus ] ) ; return totalCost ; } public static void Main ( String [ ] args ) { int [ ] cost = { 4 , 7 , 8 , 3 , 4 } ; int n = cost . Length ; Console . Write ( minCost ( cost , n ) ) ; } }


"Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible | C # program for the above approach ; Function to check if the array can be partitioned into atmost d subarray with sum atmost T ; Initial partition ; Current sum ; If current sum exceeds T ; Create a new partition ; If count of partitions exceed d ; Function to find the minimum possible value of T ; Stores the maximum and total sum of elements ; Maximum element ; Sum of all elements ; Calculate median T ; If atmost D partitions possible ; Check for smaller T ; Otherwise ; Check for larger T ; Print the minimum T required ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { public static bool possible ( int T , int [ ] arr , int n , int d ) { int partition = 1 ; int total = 0 ; for ( int i = 0 ; i < n ; i ++ ) { total = total + arr [ i ] ; if ( total > T ) { partition = partition + 1 ; total = arr [ i ] ; if ( partition > d ) { return false ; } } } return true ; } public static void calcT ( int n , int d , int [ ] arr ) { int mx = - 1 , sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { mx = Math . Max ( mx , arr [ i ] ) ; sum = sum + arr [ i ] ; } int lb = mx ; int ub = sum ; while ( lb < ub ) { int T_mid = lb + ( ub - lb ) / 2 ; if ( possible ( T_mid , arr , n , d ) == true ) { ub = T_mid ; } else { lb = T_mid + 1 ; } } Console . WriteLine ( lb ) ; } public static void Main ( String [ ] args ) { int d = 2 ; int [ ] arr = { 1 , 1 , 1 , 1 , 1 } ; int n = arr . Length ; calcT ( n , d , arr ) ; } }


"Minimum possible value of ( i * j ) % 2019 | C # implementation of the approach ; Function to return the minimum possible value of ( i * j ) % 2019 ; If we can get a number divisible by 2019 ; Find the minimum value by running nested loops ; Driver code"
Solution in C#:

using System ; class GFG { static int MOD = 2019 ; static int min_modulo ( int l , int r ) { if ( r - l >= MOD ) return 0 ; else { int ans = MOD - 1 ; for ( int i = l ; i <= r ; i ++ ) { for ( int j = i + 1 ; j <= r ; j ++ ) { ans = Math . Min ( ans , ( i * j ) % MOD ) ; } } return ans ; } } public static void Main ( String [ ] args ) { int l = 2020 , r = 2040 ; Console . WriteLine ( min_modulo ( l , r ) ) ; } }


"Minimum possible value of D which when added to or subtracted from K repeatedly obtains every array element | C # program for the above approach ; Recursive function tox previous gcd of a and b ; Function to find the maximum value of D such that every element in the array can be obtained by performing K + D or K - D ; Traverse the array arr [ ] ; Update arr [ i ] ; Stores GCD of the array ; Iterate over the range [ 1 , N ] ; Update the value of D ; Print the value of D ; Driver Code"
Solution in C#:

using System ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int findMaxD ( int [ ] arr , int N , int K ) { for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] = Math . Abs ( arr [ i ] - K ) ; } int D = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { D = gcd ( D , arr [ i ] ) ; } return D ; } public static void Main ( ) { int [ ] arr = { 1 , 7 , 11 } ; int N = arr . Length ; int K = 3 ; Console . Write ( findMaxD ( arr , N , K ) ) ; } }


"Minimum prefix increments required to make all elements of an array multiples of another array | C # program for the above approach ; Function to find minimum count of operations required to make A [ i ] multiple of B [ i ] by incrementing prefix subarray ; Stores minimum count of operations required to make A [ i ] multiple of B [ i ] by incrementing prefix subarray ; Stores the carry ; Stores minimum difference of correspoinding element in prefix subarray ; Traverse the array ; Stores the closest greater or equal number to A [ i ] which is a multiple of B [ i ] ; Stores minimum difference ; Update totalOperations ; Update carry ; Driver Code ; Input arrays A [ ] and B [ ] ; Length of arrays"
Solution in C#:

 using System ; class GFG { static int MinimumMoves ( int [ ] A , int [ ] B , int N ) { int totalOperations = 0 ; int carry = 0 ; int K = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { int nearestMultiple = ( int ) ( Math . Ceiling ( ( double ) ( A [ i ] + carry ) / ( double ) ( B [ i ] ) ) * B [ i ] ) ; K = nearestMultiple - ( A [ i ] + carry ) ; totalOperations += K ; carry += K ; } return totalOperations ; } public static void Main ( string [ ] args ) { int [ ] A = { 3 , 4 , 5 , 2 , 5 , 5 , 9 } ; int [ ] B = { 1 , 1 , 9 , 6 , 3 , 8 , 7 } ; int N = A . Length ; Console . Write ( MinimumMoves ( A , B , N ) + "
 " ) ; } }


"Minimum prefixes required to be flipped to convert a Binary String to another | C # program for the above approach ; Function to count flips required to make Strings A and B equal ; Stores the count of the number of operations ; Stores the length of the chosen prefixes ; Stores if operations are performed even or odd times ; Traverse the given String ; If current characters in the two Strings are unequal ; If A [ i ] is not flipped ; Increment count of operations ; Insert the length of the chosen prefix ; Update invert to true ; If A [ i ] is flipped ; Increment count of operations ; Insert length of the chosen prefix ; Update invert to false ; Print the number of operations required ; Print the chosen prefix length in each operation ; Driver Code ; Given binary Strings"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static void findOperations ( String A , String B , int N ) { int operations = 0 ; List < int > ops = new List < int > ( ) ; bool invert = false ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( A [ i ] != B [ i ] ) { if ( ! invert ) { operations ++ ; ops . Add ( i + 1 ) ; invert = true ; } } else { if ( invert ) { operations ++ ; ops . Add ( i + 1 ) ; invert = false ; } } } Console . Write ( operations + "
 " ) ; if ( operations != 0 ) { foreach ( int x in ops ) Console . Write ( x + " ▁ " ) ; } } public static void Main ( String [ ] args ) { String A = "001" , B = "000" ; int N = A . Length ; findOperations ( A , B , N ) ; } }


"Minimum prime numbers required to be subtracted to make all array elements equal | C # program for the above approach ; Stores the sieve of prime numbers ; Function that performs the Sieve of Eratosthenes ; Initialize all numbers as prime ; Iterate over the range [ 2 , 1000 ] ; If the current element is a prime number ; Mark all its multiples as false ; Function to find the minimum number of subtraction of primes numbers required to make all array elements the same ; Perform sieve of eratosthenes ; Find the minimum value ; Stores the value to each array element should be reduced ; If an element exists with value ( M + 1 ) ; Stores the minimum count of subtraction of prime numbers ; Traverse the array ; If D is equal to 0 ; If D is a prime number ; Increase count by 1 ; If D is an even number ; Increase count by 2 ; If D - 2 is prime ; Increase count by 2 ; Otherwise , increase count by 3 ; Driver Code"
Solution in C#:

using System ; class GFG { static int limit = 100000 ; static bool [ ] prime ; static void sieve ( ) { prime = new bool [ limit + 1 ] ; Array . Fill ( prime , true ) ; for ( int p = 2 ; p * p <= limit ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= limit ; i += p ) prime [ i ] = false ; } } } static int findOperations ( int [ ] arr , int n ) { sieve ( ) ; int minm = Int32 . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { minm = Math . Min ( minm , arr [ i ] ) ; } int val = minm ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == minm + 1 ) { val = minm - 2 ; break ; } } int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int D = arr [ i ] - val ; if ( D == 0 ) { continue ; } else if ( prime [ D ] == true ) { cnt += 1 ; } else if ( D % 2 == 0 ) { cnt += 2 ; } else { if ( prime [ D - 2 ] == true ) { cnt += 2 ; } else { cnt += 3 ; } } } return cnt ; } public static void Main ( string [ ] args ) { int [ ] arr = { 7 , 10 , 4 , 5 } ; int N = 4 ; Console . WriteLine ( findOperations ( arr , N ) ) ; } }


"Minimum product modulo N possible for any pair from a given range | C # program for the above approach ; Function to return the minimum possible value of ( i * j ) % N ; Stores the minimum remainder ; Iterate from L to R ; Iterate from L to R ; Print the minimum value of remainder ; If R - L >= N ; Driver Code"
Solution in C#:

using System ; class GFG { static void minModulo ( int L , int R , int N ) { if ( R - L < N ) { int ans = Int32 . MaxValue ; for ( int i = L ; i <= R ; i ++ ) for ( int j = L ; j <= R ; j ++ ) if ( i != j ) ans = Math . Min ( ans , ( i * j ) % N ) ; Console . WriteLine ( ans ) ; } else { Console . WriteLine ( 0 ) ; } } public static void Main ( string [ ] args ) { int L = 6 , R = 10 , N = 2019 ; minModulo ( L , R , N ) ; } }


"Minimum product pair an array of positive Integers | C # program to calculate minimum product of a pair ; Function to calculate minimum product of pair ; Initialize first and second minimums . It is assumed that the array has at least two elements . ; Traverse remaining array and keep track of two minimum elements ( Note that the two minimum elements may be same if minimum element appears more than once ) ; Driver program to test above function"
Solution in C#:

using System ; class GFG { static int printMinimumProduct ( int [ ] arr , int n ) { int first_min = Math . Min ( arr [ 0 ] , arr [ 1 ] ) ; int second_min = Math . Max ( arr [ 0 ] , arr [ 1 ] ) ; for ( int i = 2 ; i < n ; i ++ ) { if ( arr [ i ] < first_min ) { second_min = first_min ; first_min = arr [ i ] ; } else if ( arr [ i ] < second_min ) second_min = arr [ i ] ; } return first_min * second_min ; } public static void Main ( ) { int [ ] a = { 11 , 8 , 5 , 7 , 5 , 100 } ; int n = a . Length ; Console . WriteLine ( printMinimumProduct ( a , n ) ) ; } }


"Minimum product subset of an array | C # program to find maximum product of a subset . ; Find count of negative numbers , count of zeros , maximum valued negative number , minimum valued positive number and product of non - zero numbers ; if number is zero , count it but dont multiply ; count the negative numbers and find the max negative number ; find the minimum positive number ; if there are all zeroes or zero is present but no negative number is present ; If there are all positive ; If there are even number except zero of negative numbers ; Otherwise result is product of all non - zeros divided by maximum valued negative . ; main function"
Solution in C#:

using System ; public class GFG { static int minProductSubset ( int [ ] a , int n ) { if ( n == 1 ) return a [ 0 ] ; int negmax = int . MinValue ; int posmin = int . MinValue ; int count_neg = 0 , count_zero = 0 ; int product = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { count_zero ++ ; continue ; } if ( a [ i ] < 0 ) { count_neg ++ ; negmax = Math . Max ( negmax , a [ i ] ) ; } if ( a [ i ] > 0 && a [ i ] < posmin ) { posmin = a [ i ] ; } product *= a [ i ] ; } if ( count_zero == n || ( count_neg == 0 && count_zero > 0 ) ) return 0 ; if ( count_neg == 0 ) return posmin ; if ( count_neg % 2 == 0 && count_neg != 0 ) { product = product / negmax ; } return product ; } public static void Main ( ) { int [ ] a = new int [ ] { - 1 , - 1 , - 2 , 4 , 3 } ; int n = 5 ; Console . WriteLine ( minProductSubset ( a , n ) ) ; } }


"Minimum reduce operations to convert a given string into a palindrome | C # program to count minimum reduce operations to make a palindrome ; Returns count of minimum character reduce operations to make palindrome . ; Compare every character of first half with the corresponding character of second half and add difference to result . ; Driver code"
Solution in C#:

using System ; class GFG { static int countReduce ( string str ) { int n = str . Length ; int res = 0 ; for ( int i = 0 ; i < n / 2 ; i ++ ) res += Math . Abs ( str [ i ] - str [ n - i - 1 ] ) ; return res ; } public static void Main ( ) { string str = " abcd " ; Console . WriteLine ( countReduce ( str ) ) ; } }


"Minimum removal of consecutive similar characters required to empty a Binary String | C # program for the above approach ; Function to find minimum steps to make the string empty ; Stores the modified string ; Size of string ; Removing substring of same character from modified string ; Print the minimum steps required ; Driver Code ; Given string S ; Function Call"
Solution in C#:

using System ; class GFG { static void minSteps ( string S ) { string new_str = " " ; int N = S . Length ; int i = 0 ; while ( i < N ) { new_str += S [ i ] ; int j = i ; while ( i < N && S [ i ] == S [ j ] ) ++ i ; } Console . Write ( ( int ) Math . Ceiling ( ( new_str . Length + 1 ) / 2.0 ) ) ; } public static void Main ( ) { string S = "0010100" ; minSteps ( S ) ; } }


"Minimum removal of elements from end of an array required to obtain sum K | C # program for the above approach ; Function to find the smallest array that can be removed from the ends of an array to obtain sum K ; Number of elements removed from the left and right ends of the array ; Sum of left and right subarrays ; No element is taken from left initially ; Start taking elements from right side ; ( left + 1 ) : Count of elements removed from the left ( N - right ) : Count of elements removed from the right ; If sum is greater than K ; If it is not possible to obtain sum K ; Driver Code ; Given Array ; Given target sum"
Solution in C#:

using System ; class GFG { static void minSizeArr ( int [ ] A , int N , int K ) { int leftTaken = N , rightTaken = N ; int leftSum = 0 , rightSum = 0 ; for ( int left = - 1 ; left < N ; left ++ ) { if ( left != - 1 ) leftSum += A [ left ] ; rightSum = 0 ; for ( int right = N - 1 ; right > left ; right -- ) { rightSum += A [ right ] ; if ( leftSum + rightSum == K ) { if ( leftTaken + rightTaken > ( left + 1 ) + ( N - right ) ) { leftTaken = left + 1 ; rightTaken = N - right ; } break ; } if ( leftSum + rightSum > K ) break ; } } if ( leftTaken + rightTaken <= N ) { for ( int i = 0 ; i < leftTaken ; i ++ ) Console . Write ( A [ i ] + " ▁ " ) ; for ( int i = 0 ; i < rightTaken ; i ++ ) Console . Write ( A [ N - i - 1 ] + " ▁ " ) ; } else Console . Write ( - 1 ) ; } public static void Main ( ) { int N = 7 ; int [ ] A = { 3 , 2 , 1 , 1 , 1 , 1 , 3 } ; int K = 10 ; minSizeArr ( A , N , K ) ; } }


"Minimum removal of elements from end of an array required to obtain sum K | C # program for the above approach ; Function to find the smallest array that can be removed from the ends of an array to obtain sum K ; Sum of complete array ; If given number is greater than sum of the array ; If number is equal to the sum of array ; tar is sum of middle subarray ; Find the longest subarray with sum equal to tar ; If there is no subarray with sum equal to tar ; Driver code ; Given Array ; Given target sum"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void minSizeArr ( int [ ] A , int N , int K ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += A [ i ] ; if ( K > sum ) { Console . WriteLine ( - 1 ) ; return ; } if ( K == sum ) { for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( A [ i ] + " ▁ " ) ; } return ; } int tar = sum - K ; Dictionary < int , int > um = new Dictionary < int , int > ( ) ; um [ 0 ] = - 1 ; int left = 0 , right = 0 ; int cur = 0 , maxi = - 1 ; for ( int i = 0 ; i < N ; i ++ ) { cur += A [ i ] ; if ( um . ContainsKey ( cur - tar ) && i - um [ cur - tar ] > maxi ) { maxi = i - um [ cur - tar ] ; right = i ; left = um [ cur - tar ] ; } if ( ! um . ContainsKey ( cur ) ) um [ cur ] = i ; } if ( maxi == - 1 ) Console . Write ( - 1 ) ; else { for ( int i = 0 ; i <= left ; i ++ ) Console . Write ( A [ i ] + " ▁ " ) ; for ( int i = 0 ; i < right ; i ++ ) Console . Write ( A [ N - i - 1 ] + " ▁ " ) ; } } static public void Main ( ) { int N = 7 ; int [ ] A = { 3 , 2 , 1 , 1 , 1 , 1 , 3 } ; int K = 10 ; minSizeArr ( A , N , K ) ; } }


"Minimum removal of subsequences of distinct consecutive characters required to empty a given string | C # program to implement the above approach ; Function to count minimum operations required to make the string an empty string ; Stores count of 1 s by removing consecutive distinct subsequence ; Stores count of 0 s by removing consecutive distinct subsequence ; Stores length of str ; Traverse the string ; If current character is 0 ; Update cntOne ; Update cntZero ; If current character is 1 ; Update cntZero ; Update cntOne ; Driver code"
Solution in C#:

using System ; class GFG { static int findMinOperationsReqEmpStr ( String str ) { int cntOne = 0 ; int cntZero = 0 ; int N = str . Length ; for ( int i = 0 ; i < N ; i ++ ) { if ( str [ i ] == '0' ) { if ( cntOne != 0 ) { cntOne -- ; } cntZero ++ ; } else { if ( cntZero != 0 ) { cntZero -- ; } cntOne ++ ; } } return ( cntOne + cntZero ) ; } public static void Main ( String [ ] args ) { String str = "0100100111" ; Console . Write ( findMinOperationsReqEmpStr ( str ) ) ; } }


"Minimum removal to make palindrome permutation | C # Program to find minimum number of removal to make any permutation of the string a palindrome ; function to find minimum removal of characters ; hash to store frequency of each character ; to set hash array to zeros ; count frequency of each character ; count the odd frequency characters ; if count is - 1 return 0 otherwise return count ; Driver code"
Solution in C#:

using System ; class GFG { static int MAX_CHAR = 26 ; static int minRemoval ( string str ) { int [ ] hash = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) hash [ i ] = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) hash [ str [ i ] - ' a ' ] ++ ; int count = 0 ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) if ( hash [ i ] % 2 == 1 ) count ++ ; return ( count == 0 ) ? 0 : count - 1 ; } public static void Main ( ) { string str = " geeksforgeeks " ; Console . Write ( minRemoval ( str ) ) ; } }


"Minimum removals from array to make max | C # program for the above approach ; Function to find rightmost index which satisfies the condition arr [ j ] - arr [ i ] <= k ; Initialising start to i + 1 ; Initialising end to n - 1 ; Binary search implementation to find the rightmost element that satisfy the condition ; Check if the condition satisfies ; Store the position ; Make start = mid + 1 ; Make end = mid ; Return the rightmost position ; Function to calculate minimum number of elements to be removed ; Sort the given array ; Iterate from 0 to n - 1 ; Find j such that arr [ j ] - arr [ i ] <= k ; If there exist such j that satisfies the condition ; Number of elements to be removed for this particular case is ( j - i + 1 ) ; Return answer ; Driver code"
Solution in C#:

using System ; class GFG { static int findInd ( int key , int i , int n , int k , int [ ] arr ) { int start , end , mid , ind = - 1 ; start = i + 1 ; end = n - 1 ; while ( start < end ) { mid = start + ( end - start ) / 2 ; if ( arr [ mid ] - key <= k ) { ind = mid ; start = mid + 1 ; } else { end = mid ; } } return ind ; } static int removals ( int [ ] arr , int n , int k ) { int i , j , ans = n - 1 ; Array . Sort ( arr ) ; for ( i = 0 ; i < n ; i ++ ) { j = findInd ( arr [ i ] , i , n , k , arr ) ; if ( j != - 1 ) { ans = Math . Min ( ans , n - ( j - i + 1 ) ) ; } } return ans ; } static void Main ( ) { int [ ] a = { 1 , 3 , 4 , 9 , 10 , 11 , 12 , 17 , 20 } ; int n = a . Length ; int k = 4 ; Console . Write ( removals ( a , n , k ) ) ; } }


"Minimum removals from array to make max | C # program to find minimum removals to make max - min <= K ; function to check all possible combinations of removal and return the minimum one ; base case when all elements are removed ; if condition is satisfied , no more removals are required ; if the state has already been visited ; when Amax - Amin > d ; minimum is taken of the removal of minimum element or removal of the maximum element ; To sort the array and return the answer ; sort the array ; fill all stated with - 1 when only one element ; Driver code"
Solution in C#:

using System ; class GFG { static int MAX = 100 ; static int [ , ] dp = new int [ MAX , MAX ] ; static int countRemovals ( int [ ] a , int i , int j , int k ) { if ( i >= j ) return 0 ; else if ( ( a [ j ] - a [ i ] ) <= k ) return 0 ; else if ( dp [ i , j ] != - 1 ) return dp [ i , j ] ; else if ( ( a [ j ] - a [ i ] ) > k ) { dp [ i , j ] = 1 + Math . Min ( countRemovals ( a , i + 1 , j , k ) , countRemovals ( a , i , j - 1 , k ) ) ; } return dp [ i , j ] ; } static int removals ( int [ ] a , int n , int k ) { Array . Sort ( a ) ; for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j < MAX ; j ++ ) dp [ i , j ] = - 1 ; } if ( n == 1 ) return 0 ; else return countRemovals ( a , 0 , n - 1 , k ) ; } static void Main ( ) { int [ ] a = new int [ ] { 1 , 3 , 4 , 9 , 10 , 11 , 12 , 17 , 20 } ; int n = a . Length ; int k = 4 ; Console . Write ( removals ( a , n , k ) ) ; } }


"Minimum removals required such that a string can be rearranged to form a palindrome | C # program for the above approach ; Function to find the number of deletions required such that characters of the string can be rearranged to form a palindrome ; Stores frequency of characters ; Store the frequency of each character in frequency array ; Count number of characters with odd frequency ; If count is 1 or 0 , return 0 ; Otherwise , return count - 1 ; Driver Code ; Function call to find minimum number of deletions required"
Solution in C#:

using System ; public class GFG { static int minDeletions ( string str ) { int [ ] fre = new int [ 26 ] ; int n = str . Length ; for ( int i = 0 ; i < n ; i ++ ) { fre [ str [ i ] - ' a ' ] += 1 ; } int count = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( fre [ i ] % 2 == 1 ) { count += 1 ; } } if ( count == 0 count == 1 ) { return 0 ; } else { return count - 1 ; } } public static void Main ( string [ ] args ) { string str = " ababbccca " ; Console . WriteLine ( minDeletions ( str ) ) ; } }