"Minimum count of numbers needed from 1 to N that yields the sum as K | C # program for the above approach ; Function to find minimum number of elements required to obtain sum K ; Stores the maximum sum that can be obtained ; If K is greater than the Maximum sum ; If K is less than or equal to to N ; Stores the sum ; Stores the count of numbers needed ; Iterate until N is greater than or equal to 1 and sum is less than K ; Increment count by 1 ; Increment sum by N ; Update the sum ; Finally , return the count ; Driver Code ; Given Input ; Function Call"
Solution in C#:

using System ; class GFG { static int Minimum ( int N , int K ) { int sum = N * ( N + 1 ) / 2 ; if ( K > sum ) return - 1 ; if ( K <= N ) return 1 ; sum = 0 ; int count = 0 ; while ( N >= 1 && sum < K ) { count += 1 ; sum += N ; N -= 1 ; } return count ; } static public void Main ( ) { int N = 5 , K = 10 ; Console . Write ( Minimum ( N , K ) ) ; } }


"Minimum count of numbers required from given array to represent S | C # implementation to find the minimum number of sequence required from array such that their sum is equal to given S ; Function to find the count of minimum length of the sequence ; Loop to initialize the array as infinite in the row 0 ; Loop to find the solution by pre - computation for the sequence ; Minimum possible for the previous minimum value of the sequence ; Driver Code"
Solution in C#:

using System ; class GFG { static int Count ( int [ ] S , int m , int n ) { int [ , ] table = new int [ m + 1 , n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { table [ 0 , i ] = int . MaxValue - 1 ; } for ( int i = 1 ; i <= m ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( S [ i - 1 ] > j ) { table [ i , j ] = table [ i - 1 , j ] ; } else { table [ i , j ] = Math . Min ( table [ i - 1 , j ] , table [ i , j - S [ i - 1 ] ] + 1 ) ; } } } return table [ m , n ] ; } public static void Main ( String [ ] args ) { int [ ] arr = { 9 , 6 , 5 , 1 } ; int m = 4 ; Console . WriteLine ( Count ( arr , m , 11 ) ) ; } }


"Minimum count of numbers required from given array to represent S | C # implementation to find the minimum number of sequence required from array such that their sum is equal to given S ; Function to find the minimum elements required to get the sum of given value S ; Condition if the sequence is found ; Condition when no such sequence found ; Calling for without choosing the current index value ; Calling for after choosing the current index value ; Function for every array ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int printAllSubsetsRec ( int [ ] arr , int n , List < int > v , int sum ) { if ( sum == 0 ) { return v . Count ; } if ( sum < 0 ) return Int32 . MaxValue ; if ( n == 0 ) return Int32 . MaxValue ; int x = printAllSubsetsRec ( arr , n - 1 , v , sum ) ; v . Add ( arr [ n - 1 ] ) ; int y = printAllSubsetsRec ( arr , n , v , sum - arr [ n - 1 ] ) ; v . RemoveAt ( v . Count - 1 ) ; return Math . Min ( x , y ) ; } static int printAllSubsets ( int [ ] arr , int n , int sum ) { List < int > v = new List < int > ( ) ; return printAllSubsetsRec ( arr , n , v , sum ) ; } static void Main ( ) { int [ ] arr = { 2 , 1 , 4 , 3 , 5 , 6 } ; int sum = 6 ; int n = arr . Length ; Console . WriteLine ( printAllSubsets ( arr , n , sum ) ) ; } }


"Minimum cuts required to convert a palindromic string to a different palindromic string | C # program to solve the above problem ; Function to check if string is palindrome or not ; Function to check if it is possible to get result by making just one cut ; Appending last element in front ; Removing last element ; Checking whether string s2 is palindrome and different from s . ; If length is <= 3 then it is impossible ; Array to store frequency of characters ; Store count of characters in a array ; Condition for edge cases ; Return 1 if it is possible to get palindromic string in just one cut . Else we can always reached in two cuttings . ; Driver Code"
Solution in C#:

using System ; using System . Linq ; class GFG { static bool isPalindrome ( string s ) { for ( int i = 0 ; i < s . Length ; ++ i ) { if ( s [ i ] != s [ s . Length - i - 1 ] ) { return false ; } } return true ; } static bool ans ( string s ) { string s2 = s ; for ( int i = 0 ; i < s . Length ; ++ i ) { s2 = s2 [ s2 . Length - 1 ] + s2 ; s2 = s2 . Substring ( 0 , s2 . Length - 1 ) ; if ( ( s == null ? s2 != null : ! s . Equals ( s2 ) ) && isPalindrome ( s2 ) ) { return true ; } } return false ; } static int solve ( string s ) { if ( s . Length <= 3 ) { return - 1 ; } int [ ] cnt = new int [ 25 ] ; for ( int i = 0 ; i < s . Length ; i ++ ) { cnt [ s [ i ] - ' a ' ] ++ ; } if ( cnt . Max ( ) >= ( s . Length - 1 ) ) { return - 1 ; } else { return ( ans ( s ) ? 1 : 2 ) ; } } static void Main ( ) { string s = " nolon " ; Console . WriteLine ( solve ( s ) ) ; } }


"Minimum cuts required to convert a palindromic string to a different palindromic string | C # program to solve the above problem ; Recursive function to find minimum number of cuts if length of String is even ; If length is odd then return 2 ; To check if half of palindromic String is itself a palindrome ; If not then return 1 ; Else call function with half palindromic String ; Function to find minimum number of cuts If length of String is odd ; If length is <= 3 then it is impossible ; Array to store frequency of characters ; Store count of characters in a array ; Condition for edge cases ; If length is even ; If length is odd ; Driver Code"
Solution in C#:

using System ; using System . Linq ; class GFG { static int solveEven ( String s ) { if ( s . Length % 2 == 1 ) { return 2 ; } String ls = s . Substring ( 0 , s . Length / 2 ) ; String rs = s . Substring ( s . Length / 2 , s . Length ) ; if ( ls != rs ) { return 1 ; } return solveEven ( ls ) ; } static int solveOdd ( String s ) { return 2 ; } static int solve ( String s ) { if ( s . Length <= 3 ) { return - 1 ; } int [ ] cnt = new int [ 25 ] ; for ( int i = 0 ; i < s . Length ; i ++ ) { cnt [ s [ i ] - ' a ' ] ++ ; } if ( cnt . Max ( ) >= s . Length - 1 ) { return - 1 ; } if ( s . Length % 2 == 0 ) { return solveEven ( s ) ; } if ( s . Length % 2 == 1 ) { return solveOdd ( s ) ; } return int . MinValue ; } public static void Main ( ) { String s = " nolon " ; Console . WriteLine ( solve ( s ) ) ; } }


"Minimum days required to cure N persons | C # Program for the above approach ; Function to find minimum count of days required to give a cure such that the high risk person and risk person does not get a dose on same day . ; Stores count of persons whose age is less than or equal to 10 and greater than or equal to 60. ; Stores the count of persons whose age is in the range [ 11 , 59 ] ; Traverse the array arr [ ] ; If age less than or equal to 10 or greater than or equal to 60 ; Update risk ; Update normal_risk ; Calculate days to cure risk and normal_risk persons ; Print the days ; Driver code ; Given array ; Size of the array ; Given P"
Solution in C#:

using System ; class GFG { static void daysToCure ( int [ ] arr , int N , int P ) { int risk = 0 ; int normal_risk = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] >= 60 arr [ i ] <= 10 ) { risk ++ ; } else { normal_risk ++ ; } } int days = ( risk / P ) + ( normal_risk / P ) ; if ( risk % P > 0 ) { days ++ ; } if ( normal_risk % P > 0 ) { days ++ ; } Console . Write ( days ) ; } static void Main ( ) { int [ ] arr = { 9 , 80 , 27 , 72 , 79 } ; int N = arr . Length ; int P = 2 ; daysToCure ( arr , N , P ) ; } }


"Minimum days to make Array elements with value at least K sum at least X | C # program for the above approach ; Function to find the minimum number of days such that the sum of array elements >= K is at least X ; Initialize the boundaries of search space ; Perform the binary search ; Find the value of mid ; Traverse the array , arr [ ] ; Find the value of arr [ i ] after mid number of days ; Check if temp is not less than K ; Update the value of sum ; Check if the value of sum is greater than X ; Update value of high ; Update the value of low ; Print the minimum number of days ; Driver Code"
Solution in C#:

using System ; class GFG { static void findMinDays ( int [ ] arr , int [ ] R , int N , int X , int K ) { int low = 0 , high = X ; int minDays = - 1 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int temp = arr [ i ] + R [ i ] * mid ; if ( temp >= K ) { sum += temp ; } } if ( sum >= X ) { minDays = mid ; high = mid - 1 ; } else { low = mid + 1 ; } } Console . Write ( minDays ) ; } public static void Main ( string [ ] args ) { int X = 100 , K = 45 ; int [ ] arr = { 2 , 5 , 2 , 6 } ; int [ ] R = { 10 , 13 , 15 , 12 } ; int N = arr . Length ; findMinDays ( arr , R , N , X , K ) ; } }


"Minimum decrement operations to make Array elements equal by only decreasing K each time | C # implementation of the above approach ; Finding the minimum element ; Loop over all the elements and find the difference ; Solution found and returned ; Driver code"
Solution in C#:

using System ; class GFG { static int INT_MAX = int . MaxValue ; static int solve ( int [ ] arr , int n , int k ) { int minx = INT_MAX ; int i ; for ( i = 0 ; i < n ; i ++ ) { minx = Math . Min ( minx , arr [ i ] ) ; } int decrements = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] - minx ) % k != 0 ) { return - 1 ; } else { decrements += ( ( arr [ i ] - minx ) / k ) ; } } return decrements ; } public static void Main ( ) { int n , k ; n = 3 ; k = 3 ; int [ ] arr = { 12 , 9 , 15 } ; Console . WriteLine ( solve ( arr , n , k ) ) ; } }


"Minimum decrements required such that sum of all adjacent pairs in an Array does not exceed K | C # program to implement the above approach ; Function to calculate the minimum number of operations required ; Stores the total number of operations ; Iterate over the array ; If the sum of pair of adjacent elements exceed k . ; If current element exceeds k ; Reduce arr [ i ] to k ; Update arr [ i + 1 ] accordingly ; Update answer ; Driver Code"
Solution in C#:

using System ; class GFG { static int minimum_required_operations ( int [ ] arr , int n , int k ) { int answer = 0 ; long mod = 1000000007 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] + arr [ i + 1 ] > k ) { if ( arr [ i ] > k ) { answer += ( arr [ i ] - k ) ; arr [ i ] = k ; } answer += ( arr [ i ] + arr [ i + 1 ] ) - k ; arr [ i + 1 ] = ( k - arr [ i ] ) ; answer = ( int ) ( answer % mod ) ; } } return answer ; } public static void Main ( String [ ] args ) { int [ ] a = { 9 , 50 , 4 , 14 , 42 , 89 } ; int k = 10 ; int n = a . Length ; Console . Write ( minimum_required_operations ( a , n , k ) ) ; } }


"Minimum decrements required to make all pairs of adjacent matrix elements distinct | C # program for the above approach ; Function to count minimum number of operations required ; Case 1 : ; Case 2 : ; Print the minimum number of operations required ; Driver Code ; The given matrix ; Function Call to count the minimum number of decrements required"
Solution in C#:

using System ; class GFG { static void countDecrements ( long [ , ] arr ) { int n = arr . GetLength ( 0 ) ; int m = arr . GetLength ( 1 ) ; int count_1 = 0 ; int count_2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( ( i + j ) % 2 == arr [ i , j ] % 2 ) count_1 ++ ; if ( 1 - ( i + j ) % 2 == arr [ i , j ] % 2 ) count_2 ++ ; } } Console . WriteLine ( Math . Min ( count_1 , count_2 ) ) ; } public static void Main ( ) { long [ , ] arr = { { 1 , 2 , 3 } , { 1 , 2 , 3 } , { 1 , 2 , 3 } } ; countDecrements ( arr ) ; } }


"Minimum decrements to make integer A divisible by integer B | C # implementation to count total numbers moves to make integer A divisible by integer B ; Function that print number of moves required ; Calculate modulo ; Print the required answer ; Driver code ; Initialise A and B"
Solution in C#:

using System ; class GFG { static void movesRequired ( int a , int b ) { int total_moves = a % b ; Console . Write ( total_moves ) ; } public static void Main ( String [ ] args ) { int A = 10 , B = 3 ; movesRequired ( A , B ) ; } }


"Minimum delete operations to make all elements of array same | C # program to find minimum number of deletes required to make all elements same . ; Function to get minimum number of elements to be deleted from array to make array elements equal ; Create an hash map and store frequencies of all array elements in it using element as key and frequency as value ; Find maximum frequency among all frequencies . ; To minimize delete operations , we remove all elements but the most frequent element . ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int minDelete ( int [ ] arr , int n ) { Dictionary < int , int > freq = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( freq . ContainsKey ( arr [ i ] ) ) { freq [ arr [ i ] ] = freq [ arr [ i ] ] + 1 ; } else { freq . Add ( arr [ i ] , 1 ) ; } int max_freq = int . MinValue ; foreach ( KeyValuePair < int , int > entry in freq ) max_freq = Math . Max ( max_freq , entry . Value ) ; return n - max_freq + 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 3 , 4 , 4 , 2 , 4 } ; int n = arr . Length ; Console . Write ( minDelete ( arr , n ) ) ; } }


"Minimum deletions from string to reduce it to string with at most 2 unique characters | C # implementation of the above approach ; Function to find the minimum deletions ; Array to store the occurrences of each characters ; Length of the string ; ASCII of the character ; Increasing the frequency for this character ; Choosing two character ; Finding the minimum deletion ; Driver program to test above functions"
Solution in C#:

using System ; public class GFG { static int check ( string s ) { int i , j ; int [ ] fr = new int [ 26 ] ; int n = s . Length ; for ( i = 0 ; i < n ; i ++ ) { char x = s [ i ] ; fr [ x - ' a ' ] += 1 ; } int minimum = int . MaxValue ; for ( i = 0 ; i < 26 ; i ++ ) { for ( j = i + 1 ; j < 26 ; j ++ ) { int z = fr [ i ] + fr [ j ] ; minimum = Math . Min ( minimum , n - z ) ; } } return minimum ; } public static void Main ( ) { string s = " geeksforgeeks " ; Console . Write ( check ( s ) ) ; } }


"Minimum deletions in Array to make difference of adjacent elements non | C # program for the above approach ; Function for finding minimum deletions so that the array becomes non decreasing and the difference between adjacent elements also becomes non decreasing ; initialize answer to a large value ; generating all subsets ; checking the first condition ; checking the second condition ; if both conditions are satisfied consider the answer for minimum ; Driver code ; Input ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int minimumDeletions ( int [ ] A , int N ) { int ans = Int32 . MaxValue ; for ( int i = 1 ; i < ( 1 << N ) ; i ++ ) { List < int > temp = new List < int > ( ) ; for ( int j = 0 ; j < N ; j ++ ) { if ( ( i & ( 1 << j ) ) != 0 ) { temp . Add ( A [ j ] ) ; } } int flag = 0 ; for ( int j = 1 ; j < temp . Count ; j ++ ) if ( temp [ j ] < temp [ j - 1 ] ) flag = 1 ; for ( int j = 1 ; j < temp . Count - 1 ; j ++ ) if ( temp [ j ] - temp [ j - 1 ] > temp [ j + 1 ] - temp [ j ] ) flag = 1 ; if ( flag == 0 ) { ans = Math . Min ( ans , N - temp . Count ) ; } } return ans ; } public static void Main ( ) { int [ ] A = { 1 , 4 , 5 , 7 , 20 , 21 } ; int N = A . Length ; Console . Write ( minimumDeletions ( A , N ) ) ; } }


"Minimum deletions in Array to make difference of adjacent elements non | C # program for the above approach ; The maximum value of A [ i ] ; Function for finding minimum deletions so that the array becomes non - decreasing and the difference between adjacent elements is also non - decreasing ; Initializing the dp table and setting all values to 0 ; Find the maximum size valid set that can be taken and then subtract its size from N to get minimum number of deletions ; When selecting only current element and deleting all elements from 0 to i - 1 inclusive ; If this is true moving from index j to i is possible ; Iterate over all elements from 0 to diff and find the max ; Take the max set size from dp [ N - 1 ] [ 0 ] to dp [ N - 1 ] [ MAX ] ; Driver Code ; Function call"
Solution in C#:

using System ; class GFG { static int MAX = 100001 ; static int minimumDeletions ( int [ ] A , int N ) { int [ , ] dp = new int [ N , MAX ] ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < MAX ; j ++ ) dp [ i , j ] = 0 ; for ( int i = 0 ; i < N ; i ++ ) { dp [ i , 0 ] = 1 ; for ( int j = i - 1 ; j >= 0 ; j -- ) { if ( A [ i ] >= A [ j ] ) { int diff = A [ i ] - A [ j ] ; for ( int k = 0 ; k <= diff ; k ++ ) { dp [ i , diff ] = Math . Max ( dp [ i , diff ] , dp [ j , k ] + 1 ) ; } } } } int maxSetSize = - 1 ; for ( int i = 0 ; i < MAX ; i ++ ) maxSetSize = Math . Max ( maxSetSize , dp [ N - 1 , i ] ) ; return N - maxSetSize ; } public static void Main ( ) { int [ ] A = { 1 , 4 , 5 , 7 , 20 , 21 } ; int N = A . Length ; Console . Write ( minimumDeletions ( A , N ) ) ; } }


"Minimum deletions in Array to make difference of adjacent elements non | C # program for the above approach ; the maximum value of A [ i ] ; Function for finding minimum deletions so that the array becomes non - decreasing and the difference between adjacent elements is also non - decreasing ; initialize the dp table and set all values to 0 pref [ i ] [ j ] will contain min ( dp [ i ] [ 0 ] , dp [ i ] [ 1 ] , ... dp [ i ] [ j ] ) ; find the maximum sized valid set possible and then subtract its size from N to get minimum number of deletions ; when selecting only the current element and deleting all elements from 0 to i - 1 inclusive ; if this is true , moving from index j to i is possible ; we can get min ( dp [ j ] [ 0 ] , . . dp [ j ] ) from pref array ; ; construct the prefix array for this element ; take the max set size from dp [ N - 1 ] [ 0 ] to dp [ N - 1 ] [ MAX ] ; Driver Code ; Function call"
Solution in C#:

using System ; class GFG { static int MAX = 100001 ; static int minimumDeletions ( int [ ] A , int N ) { int [ , ] dp = new int [ N , MAX ] ; int [ , ] pref = new int [ N , MAX ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < MAX ; j ++ ) { dp [ i , j ] = 0 ; pref [ i , j ] = 0 ; } } for ( int i = 0 ; i < N ; i ++ ) { dp [ i , 0 ] = 1 ; for ( int j = i - 1 ; j >= 0 ; j -- ) { if ( A [ i ] >= A [ j ] ) { int diff = A [ i ] - A [ j ] ; dp [ i , diff ] = Math . Max ( dp [ i , diff ] , pref [ j , diff ] + 1 ) ; } } pref [ i , 0 ] = dp [ i , 0 ] ; for ( int j = 1 ; j < MAX ; j ++ ) pref [ i , j ] = Math . Max ( dp [ i , j ] , pref [ i , j - 1 ] ) ; } int maxSetSize = - 1 ; for ( int i = 0 ; i < MAX ; i ++ ) maxSetSize = Math . Max ( maxSetSize , dp [ N - 1 , i ] ) ; return N - maxSetSize ; } public static void Main ( String [ ] args ) { int [ ] A = { 1 , 4 , 5 , 7 , 20 , 21 } ; int N = A . Length ; Console . Write ( minimumDeletions ( A , N ) ) ; } }


"Minimum deletions to convert given integer to an odd number whose sum of digits is even | Set 2 | C # implementation of the above approach ; Function to find minimum count of digits required to be remove to make N odd and the sum of digits of N even ; Stores count of even digits ; Stores count of odd digits ; Iterate over the digits of N ; If current digit is even ; Update even ; Otherwise ; Update odd ; Base conditions ; Stores count of digits required to be removed to make N odd and the sum of digits of N even ; Iterate over the digits of N ; If current digit is even ; Update ans ; Otherwise , ; Update ans ; If count of odd digits is odd ; Update ans ; Finally print the ans ; Driver code ; Input String ; Function call"
Solution in C#:

 using System ; class GFG { static void minOperations ( String N ) { int even = 0 ; int odd = 0 ; foreach ( int it in N . ToCharArray ( ) ) { if ( ( it - '0' ) % 2 == 0 ) { even ++ ; } else { odd ++ ; } } if ( odd == 0 odd == 1 ) { Console . Write ( " Not ▁ Possible " + "
 " ) ; } else { int ans = 0 ; foreach ( int it in N . ToCharArray ( ) ) { if ( ( it - '0' ) % 2 == 0 ) { ans ++ ; } else { ans = 0 ; } } if ( odd % 2 != 0 ) { ans ++ ; } Console . Write ( ans + "
 " ) ; } } public static void Main ( String [ ] args ) { String N = "12345" ; minOperations ( N ) ; } }


"Minimum difference between adjacent elements of array which contain elements from each row of a matrix | C # program to find the minimum absolute difference between any of the adjacent elements of an array which is created by picking one element from each row of the matrix ; Return smallest element greater than or equal to the current element . ; Return the minimum absolute difference adjacent elements of array ; Sort each row of the matrix . ; For each matrix element ; Search smallest element in the next row which is greater than or equal to the current element ; largest element which is smaller than the current element in the next row must be just before smallest element which is greater than or equal to the current element because rows are sorted . ; Driver code"
Solution in C#:

using System ; class GFG { static public int R = 2 ; static public int C = 2 ; static int bsearch ( int low , int high , int n , int [ ] arr ) { int mid = ( low + high ) / 2 ; if ( low <= high ) { if ( arr [ mid ] < n ) return bsearch ( mid + 1 , high , n , arr ) ; return bsearch ( low , mid - 1 , n , arr ) ; } return low ; } public static int [ ] GetRow ( int [ , ] matrix , int row ) { var rowLength = matrix . GetLength ( 1 ) ; var rowVector = new int [ rowLength ] ; for ( var i = 0 ; i < rowLength ; i ++ ) rowVector [ i ] = matrix [ row , i ] ; return rowVector ; } static int mindiff ( int [ , ] arr , int n , int m ) { for ( int i = 0 ; i < n ; i ++ ) Array . Sort ( GetRow ( arr , i ) ) ; int ans = + 2147483647 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { int p = bsearch ( 0 , m - 1 , arr [ i , j ] , GetRow ( arr , i + 1 ) ) ; ans = Math . Min ( ans , Math . Abs ( arr [ i + 1 , p ] - arr [ i , j ] ) ) ; if ( p - 1 >= 0 ) ans = Math . Min ( ans , Math . Abs ( arr [ i + 1 , p - 1 ] - arr [ i , j ] ) ) ; } } return ans ; } public static void Main ( String [ ] args ) { int [ , ] m = { { 8 , 5 } , { 6 , 8 } } ; Console . WriteLine ( mindiff ( m , R , C ) ) ; } }


"Minimum difference between any two primes from the given range | C # implementation of the approach ; Function for Sieve of Eratosthenes ; Function to return the minimum difference between any two prime numbers from the given range [ L , R ] ; Find the first prime from the range ; Find the second prime from the range ; If the number of primes in the given range is < 2 ; To store the minimum difference between two consecutive primes from the range ; Range left to check for primes ; For every integer in the range ; If the current integer is prime ; If the difference between i and snd is minimum so far ; Driver code ; Generate primes"
Solution in C#:

using System ; class GFG { static int sz = ( int ) 1e5 ; static Boolean [ ] isPrime = new Boolean [ sz + 1 ] ; static void sieve ( ) { for ( int i = 2 ; i < sz + 1 ; i ++ ) isPrime [ i ] = true ; for ( int i = 2 ; i * i <= sz ; i ++ ) { if ( isPrime [ i ] ) { for ( int j = i * i ; j < sz ; j += i ) { isPrime [ j ] = false ; } } } } static int minDifference ( int L , int R ) { int fst = 0 ; for ( int i = L ; i <= R ; i ++ ) { if ( isPrime [ i ] ) { fst = i ; break ; } } int snd = 0 ; for ( int i = fst + 1 ; i <= R ; i ++ ) { if ( isPrime [ i ] ) { snd = i ; break ; } } if ( snd == 0 ) return - 1 ; int diff = snd - fst ; int left = snd + 1 ; int right = R ; for ( int i = left ; i <= right ; i ++ ) { if ( isPrime [ i ] ) { if ( i - snd <= diff ) { fst = snd ; snd = i ; diff = snd - fst ; } } } return diff ; } public static void Main ( String [ ] args ) { sieve ( ) ; int L = 21 , R = 50 ; Console . WriteLine ( minDifference ( L , R ) ) ; } }


"Minimum difference between groups of size two | C # program to find minimum difference between groups of highest and lowest sums . ; Sorting the whole array . ; Generating sum groups . ; Driver code"
Solution in C#:

using System ; using System . Linq ; using System . Collections . Generic ; class GFG { static long calculate ( long [ ] a , int n ) { Array . Sort ( a ) ; int i , j ; List < long > s = new List < long > ( ) ; for ( i = 0 , j = n - 1 ; i < j ; i ++ , j -- ) s . Add ( ( a [ i ] + a [ j ] ) ) ; long mini = s . Min ( ) ; long maxi = s . Max ( ) ; return Math . Abs ( maxi - mini ) ; } public static void Main ( ) { long [ ] a = { 2 , 6 , 4 , 3 } ; int n = a . Length ; Console . WriteLine ( calculate ( a , n ) ) ; } }


"Minimum difference between max and min of all K | C # program to find minimum difference between max and min of all subset of K size ; returns min difference between max and min of any K - size subset ; sort the array so that close elements come together . ; initialize result by a big integer number ; loop over first ( N - K ) elements of the array only ; get difference between max and min of current K - sized segment ; Driver code"
Solution in C#:

using System ; class GFG { static int minDifferenceAmongMaxMin ( int [ ] arr , int N , int K ) { Array . Sort ( arr ) ; int res = 2147483647 ; for ( int i = 0 ; i <= ( N - K ) ; i ++ ) { int curSeqDiff = arr [ i + K - 1 ] - arr [ i ] ; res = Math . Min ( res , curSeqDiff ) ; } return res ; } public static void Main ( ) { int [ ] arr = { 10 , 20 , 30 , 100 , 101 , 102 } ; int N = arr . Length ; int K = 3 ; Console . Write ( minDifferenceAmongMaxMin ( arr , N , K ) ) ; } }


"Minimum digits to be removed to make either all digits or alternating digits same | C # program for the above approach ; Function to find longest possible subsequence of s beginning with x and y ; Iterate over the String ; Increment count ; Swap the positions ; Return the result ; Function that finds all the possible pairs ; Update count ; Return the answer ; Driver Code ; Given String s ; Find the size of the String ; Function Call ; This value is the count of minimum element to be removed"
Solution in C#:

using System ; class GFG { static int solve ( String s , int x , int y ) { int res = 0 ; foreach ( char c in s . ToCharArray ( ) ) { if ( c - '0' == x ) { res ++ ; x = x + y ; y = x - y ; x = x - y ; } } if ( x != y && res % 2 == 1 ) -- res ; return res ; } static int find_min ( String s ) { int count = 0 ; for ( int i = 0 ; i < 10 ; i ++ ) { for ( int j = 0 ; j < 10 ; j ++ ) { count = Math . Max ( count , solve ( s , i , j ) ) ; } } return count ; } public static void Main ( String [ ] args ) { String s = "100120013" ; int n = s . Length ; int answer = find_min ( s ) ; Console . Write ( ( n - answer ) ) ; } }


"Minimum digits to remove to make a number Perfect Square | C # program to find required minimum digits need to remove to make a number perfect square ; function to check minimum number of digits should be removed to make this number a perfect square ; size of the string ; our final answer ; to store string which is perfect square . ; We make all possible subsequences ; to check jth bit is set or not . ; we do not consider a number with leading zeros ; convert our temporary string into integer ; checking temp is perfect square or not . ; taking maximum sized string ; print PerfectSquare ; Driver code"
Solution in C#:

using System ; class GFG { static int perfectSquare ( string s ) { int n = s . Length ; int ans = - 1 ; string num = " " ; for ( int i = 1 ; i < ( 1 << n ) ; i ++ ) { string str = " " ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( ( i >> j ) & 1 ) == 1 ) { str += s [ j ] ; } } if ( str [ 0 ] != '0' ) { int temp = 0 ; for ( int j = 0 ; j < str . Length ; j ++ ) temp = temp * 10 + ( int ) ( str [ j ] - '0' ) ; int k = ( int ) Math . Sqrt ( temp ) ; if ( k * k == temp ) { if ( ans < ( int ) str . Length ) { ans = ( int ) str . Length ; num = str ; } } } } if ( ans == - 1 ) return ans ; else { Console . Write ( num + " ▁ " ) ; return n - ans ; } } public static void Main ( ) { Console . WriteLine ( perfectSquare ( "8314" ) ) ; Console . WriteLine ( perfectSquare ( "753" ) ) ; } }


"Minimum distance a person has to move in order to take a picture of every racer | C # program for above approach ; Function to return the minimum distance the person has to move int order to get the pictures ; To store the minimum ending point ; To store the maximum starting point ; Find the values of minSeg and maxSeg ; Impossible ; The person doesn 't need to move ; Get closer to the left point ; Get closer to the right point ; Driver Code"
Solution in C#:

using System ; class GFG { static int minDistance ( int [ ] start , int [ ] end , int n , int d ) { int left = Int32 . MinValue ; int right = Int32 . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { left = Math . Max ( left , start [ i ] ) ; right = Math . Min ( right , end [ i ] ) ; } if ( left > right ) return - 1 ; if ( d >= left && d <= right ) return 0 ; if ( d < left ) return ( left - d ) ; if ( d > right ) return ( d - right ) ; return - 1 ; } public static void Main ( String [ ] args ) { int [ ] start = { 0 , 2 , 4 } ; int [ ] end = { 7 , 14 , 6 } ; int n = start . Length ; int d = 3 ; Console . Write ( minDistance ( start , end , n , d ) ) ; } }


"Minimum distance between any most frequent and least frequent element of an array | C # implementation of the approach ; Function to find the minimum distance between any two most and least frequent element ; Initialize sets to store the least and the most frequent elements ; Initialize variables to store max and min frequency ; Initialize HashMap to store frequency of each element ; Loop through the array ; Store the count of each element ; Store the least and most frequent elements in the respective sets ; Store count of current element ; If count is equal to max count ; Store in max set ; If count is greater then max count ; Empty max set ; Update max count ; Store in max set ; If count is equal to min count ; Store in min set ; If count is less then max count ; Empty min set ; Update min count ; Store in min set ; Initialize a variable to store the minimum distance ; Initialize a variable to store the last index of least frequent element ; Traverse array ; If least frequent element ; Update last index of least frequent element ; If most frequent element ; Update minimum distance ; Traverse array from the end ; If least frequent element ; Update last index of least frequent element ; If most frequent element ; Update minimum distance ; Print the minimum distance ; Driver Code ; Given array ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { public static void getMinimumDistance ( int [ ] a , int n ) { HashSet < int > min_set = new HashSet < int > ( ) ; HashSet < int > max_set = new HashSet < int > ( ) ; int max = 0 , min = Int32 . MaxValue ; Dictionary < int , int > frequency = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! frequency . ContainsKey ( a [ i ] ) ) frequency . Add ( a [ i ] , 0 ) ; frequency [ a [ i ] ] ++ ; } for ( int i = 0 ; i < n ; i ++ ) { int count = frequency [ a [ i ] ] ; if ( count == max ) { max_set . Add ( a [ i ] ) ; } else if ( count > max ) { max_set . Clear ( ) ; max = count ; max_set . Add ( a [ i ] ) ; } if ( count == min ) { min_set . Add ( a [ i ] ) ; } else if ( count < min ) { min_set . Clear ( ) ; min = count ; min_set . Add ( a [ i ] ) ; } } int min_dist = Int32 . MaxValue ; int last_min_found = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( min_set . Contains ( a [ i ] ) ) last_min_found = i ; if ( max_set . Contains ( a [ i ] ) && last_min_found != - 1 ) { min_dist = Math . Min ( min_dist , i - last_min_found ) ; } } last_min_found = - 1 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( min_set . Contains ( a [ i ] ) ) last_min_found = i ; if ( max_set . Contains ( a [ i ] ) && last_min_found != - 1 ) { min_dist = Math . Min ( min_dist , last_min_found - i ) ; } } Console . WriteLine ( min_dist ) ; } static public void Main ( ) { int [ ] arr = { 1 , 1 , 2 , 3 , 2 , 3 , 3 } ; int N = arr . Length ; getMinimumDistance ( arr , N ) ; } }


"Minimum distance between duplicates in a String | C # Program to find the minimum distance between two repeating characters in a String using two pointers technique ; This function is used to find minimum distance between any two repeating characters using two - pointers and hashing technique ; hash array to store character 's last index ; Traverse through the String ; If the character is present in visited array find if its forming minimum distance ; update current character 's last index ; Return minimum distance found , else - 1 ; Driver code ; Given Input ; Function Call"
Solution in C#:

using System ; public class GFG { static int shortestDistance ( string s , int n ) { int [ ] visited = new int [ 128 ] ; for ( int i = 0 ; i < 128 ; i ++ ) visited [ i ] = - 1 ; int ans = int . MaxValue ; for ( int right = 0 ; right < n ; right ++ ) { char c = s [ right ] ; int left = visited ; if ( left != - 1 ) ans = Math . Min ( ans , right - left - 1 ) ; visited = right ; } return ans == int . MaxValue ? - 1 : ans ; } public static void Main ( String [ ] args ) { string s = " geeksforgeeks " ; int n = 13 ; Console . Write ( shortestDistance ( s , n ) ) ; } }


"Minimum distance between duplicates in a String | C # implementation of above approach ; This function is used to find minimum distance between same repeating characters ; Define a hashmap and an arraylist ; Temporary variable ; Traverse through string ; If character present in dictionary ; Difference between current position and last stored value ; Updating current position ; Storing difference in list ; If character not in dictionary assign it with initial of its position ; If no element inserted in list i . e . no repeating characterss ; Return minimum distance ; Driver code ; Given input ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { static int shortestDistance ( String S , int N ) { Dictionary < char , int > dic = new Dictionary < char , int > ( ) ; List < int > dis = new List < int > ( ) ; int var ; for ( int i = 0 ; i < N ; i ++ ) { if ( dic . ContainsKey ( S [ i ] ) ) { var = i - dic [ S [ i ] ] ; dic [ S [ i ] ] = i ; dis . Add ( var ) ; } else { dic . Add ( S [ i ] , i ) ; } } dis . Sort ( ) ; if ( dis . Count == 0 ) return - 1 ; else return dis [ 0 ] - 1 ; } public static void Main ( String [ ] args ) { String S = " geeksforgeeks " ; int N = 13 ; Console . WriteLine ( shortestDistance ( S , N ) ) ; } }


"Minimum distance between duplicates in a String | C # program for the above approach ; This function is used to find minimum distance between same repeating characters ; Store minimum distance between same repeating characters ; For loop to consider each element of string ; Comparison of string characters and updating the minDis value ; As this value would be least therefore break ; If minDis value is not updated that means no repeating characters ; Minimum distance is minDis - 1 ; Driver code ; Given input ; Function call"
Solution in C#:

using System ; class GFG { static int shortestDistance ( string S , int N ) { int minDis = S . Length ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( S [ i ] == S [ j ] && ( j - i ) < minDis ) { minDis = j - i ; break ; } } } if ( minDis == S . Length ) return - 1 ; else return minDis - 1 ; } public static void Main ( String [ ] args ) { string S = " geeksforgeeks " ; int N = 13 ; Console . Write ( shortestDistance ( S , N ) ) ; } }


"Minimum distance between the maximum and minimum element of a given Array | C # program to implement the above approach ; Function to find the minimum distance between the minimum and the maximum element ; Stores the minimum and maximum array element ; Stores the most recently traversed indices of the minimum and the maximum element ; Stores the minimum distance between the minimum and the maximium ; Find the maximum and the minimum element from the given array ; Find the minimum distance ; Check if current element is equal to minimum ; Check if current element is equal to maximum ; If both the minimum and the maximum element has occurred at least once ; Driver Code"
Solution in C#:

using System ; class GFG { static int minDistance ( int [ ] a , int n ) { int max = - 1 , min = Int32 . MaxValue ; int min_index = - 1 , max_index = - 1 ; int min_dist = n + 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > max ) max = a [ i ] ; if ( a [ i ] < min ) min = a [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == min ) min_index = i ; if ( a [ i ] == max ) max_index = i ; if ( min_index != - 1 && max_index != - 1 ) min_dist = Math . Min ( min_dist , Math . Abs ( min_index - max_index ) ) ; } return min_dist ; } public static void Main ( ) { int n = 12 ; int [ ] a = { 3 , 2 , 1 , 2 , 1 , 4 , 5 , 8 , 6 , 7 , 8 , 2 } ; Console . WriteLine ( minDistance ( a , n ) ) ; } }


"Minimum distance between two occurrences of maximum | C # program to find Min distance of maximum element ; function to return min distance ; case a ; case b ; case c ; Driver code"
Solution in C#:

using System ; class GFG { static int minDistance ( int [ ] arr , int n ) { int maximum_element = arr [ 0 ] ; int min_dis = n ; int index = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( maximum_element == arr [ i ] ) { min_dis = Math . Min ( min_dis , ( i - index ) ) ; index = i ; } else if ( maximum_element < arr [ i ] ) { maximum_element = arr [ i ] ; min_dis = n ; index = i ; } else continue ; } return min_dis ; } public static void Main ( ) { int [ ] arr = { 6 , 3 , 1 , 3 , 6 , 4 , 6 } ; int n = arr . Length ; Console . WriteLine ( " Minimum ▁ distance ▁ = ▁ " + minDistance ( arr , n ) ) ; } }


"Minimum distance to travel to cover all intervals | C # program to find minimum distance to travel to cover all intervals ; Structure to store an interval ; Method returns minimum distance to travel to cover all intervals ; Looping over all intervals to get right most start and left most end ; If rightmost start > leftmost end then all intervals are not aligned and it is not possible to cover all of them ; If x is in between rightmoststart and leftmostend then no need to travel any distance ; Choose minimum according to current position x ; Driver code"
Solution in C#:

 using System ; class GFG { public class Interval { public int start , end ; public Interval ( int start , int end ) { this . start = start ; this . end = end ; } } ; static int minDistanceToCoverIntervals ( Interval [ ] intervals , int N , int x ) { int rightMostStart = int . MinValue ; int leftMostEnd = int . MaxValue ; for ( int i = 0 ; i < N ; i ++ ) { if ( rightMostStart < intervals [ i ] . start ) rightMostStart = intervals [ i ] . start ; if ( leftMostEnd > intervals [ i ] . end ) leftMostEnd = intervals [ i ] . end ; } int res ; if ( rightMostStart > leftMostEnd ) res = - 1 ; else if ( rightMostStart <= x && x <= leftMostEnd ) res = 0 ; else res = ( x < rightMostStart ) ? ( rightMostStart - x ) : ( x - leftMostEnd ) ; return res ; } public static void Main ( String [ ] args ) { int x = 3 ; Interval [ ] intervals = { new Interval ( 0 , 7 ) , new Interval ( 2 , 14 ) , new Interval ( 4 , 6 ) } ; int N = intervals . Length ; int res = minDistanceToCoverIntervals ( intervals , N , x ) ; if ( res == - 1 ) Console . Write ( " Not ▁ Possible ▁ to ▁ " + " cover ▁ all ▁ intervals
 " ) ; else Console . Write ( res + "
 " ) ; } }


"Minimum distance to visit given K points on X | C # program to implement the above approach ; Function to find the minimum distance travelled to visit K point ; Stores minimum distance travelled to visit K point ; Stores distance travelled to visit points ; Traverse the array arr [ ] ; If arr [ i ] and arr [ i + K - 1 ] are positive ; Update dist ; Update dist ; Update res ; Driver Code ; Initial the array"
Solution in C#:

using System ; class GFG { static int MinDistK ( int [ ] arr , int N , int K ) { int res = Int32 . MaxValue ; int dist = 0 ; for ( int i = 0 ; i <= ( N - K ) ; i ++ ) { if ( arr [ i ] >= 0 && arr [ i + K - 1 ] >= 0 ) { dist = Math . Max ( arr [ i ] , arr [ i + K - 1 ] ) ; } else { dist = Math . Abs ( arr [ i ] ) + Math . Abs ( arr [ i + K - 1 ] ) + Math . Min ( Math . Abs ( arr [ i ] ) , Math . Abs ( arr [ i + K - 1 ] ) ) ; } res = Math . Min ( res , dist ) ; } return res ; } public static void Main ( ) { int K = 3 ; int [ ] arr = { - 30 , - 10 , 10 , 20 , 50 } ; int N = arr . Length ; Console . WriteLine ( MinDistK ( arr , N , K ) ) ; } }


"Minimum divide by 2 operations required to make GCD odd for given Array | C # program for the above approach ; Function to find the minimum number of operations to make the GCD of the array odd ; Stores the minimum operations required ; Stores the powers of two for the current array element ; Dividing by 2 ; Increment the count ; Update the minimum operation required ; Return the result required ; Driver Code"
Solution in C#:

using System ; class GFG { public static int minimumOperations ( int [ ] arr , int N ) { int mini = Int32 . MaxValue ; for ( int i = 0 ; i < N ; i ++ ) { int count = 0 ; while ( arr [ i ] % 2 == 0 ) { arr [ i ] = arr [ i ] / 2 ; count ++ ; } if ( mini > count ) { mini = count ; } } return mini ; } public static void Main ( string [ ] args ) { int [ ] arr = { 4 , 6 } ; int N = arr . Length ; Console . WriteLine ( minimumOperations ( arr , N ) ) ; } }


"Minimum division by 10 and multiplication by 2 required to reduce given number to 1 | C # program for the above approach ; Function to find the minimum number operations required to reduce N to 1 ; Stores count of powers of 2 and 5 ; Calculating the primefactors 2 ; Calculating the primefactors 5 ; If n is 1 and cnt2 <= cnt5 ; Return the minimum operations ; Otherwise , n can 't be reduced ; Driver Code ; Given Number N ; Function Call"
Solution in C#:

using System ; class GFG { static int minimumMoves ( int n ) { int cnt2 = 0 , cnt5 = 0 ; while ( n % 2 == 0 ) { n /= 2 ; cnt2 ++ ; } while ( n % 5 == 0 ) { n /= 5 ; cnt5 ++ ; } if ( n == 1 && cnt2 <= cnt5 ) { return 2 * cnt5 - cnt2 ; } else return - 1 ; } public static void Main ( ) { int N = 25 ; Console . WriteLine ( minimumMoves ( N ) ) ; } }


"Minimum divisor of a number to make the number perfect cube | C # program to find minimum number which divide n to make it a perfect cube ; Returns the minimum divisor ; Since 2 is only even prime , compute its power seprately . ; If count is not divisible by 3 , it must be removed by dividing n by prime number power . ; If count is not divisible by 3 , it must be removed by dividing n by prime number power . ; if n is a prime number ; Driver code"
Solution in C#:

 using System ; class GFG { static int findMinNumber ( int n ) { int count = 0 , ans = 1 ; while ( n % 2 == 0 ) { count ++ ; n /= 2 ; } if ( count % 3 != 0 ) ans *= ( int ) Math . Pow ( 2 , ( count % 3 ) ) ; for ( int i = 3 ; i <= Math . Sqrt ( n ) ; i += 2 ) { count = 0 ; while ( n % i == 0 ) { count ++ ; n /= i ; } if ( count % 3 != 0 ) ans *= ( int ) Math . Pow ( i , ( count % 3 ) ) ; } if ( n > 2 ) ans *= n ; return ans ; } public static void Main ( String [ ] args ) { int n = 128 ; Console . Write ( findMinNumber ( n ) + "
 " ) ; } }


"Minimum edge reversals to make a root | C # program to find min edge reversal to make every node reachable from root ; pair class ; method to dfs in tree and populates disRev values ; visit current node ; looping over all neighbors ; distance of v will be one more than distance of u ; initialize back edge count same as parent node 's count ; if there is a reverse edge from u to i , then only update ; return total reversal in subtree rooted at u ; method prints root and minimum number of edge reversal ; number of nodes are one more than number of edges ; data structure to store directed tree ; disRev stores two values - distance and back edge count from root node ; add 0 weight in direction of u to v ; add 1 weight in reverse direction ; initialize all variables ; dfs populates disRev data structure and store total reverse edge counts ; for ( int i = 0 ; i < V ; i ++ ) { cout << i << " ▁ : ▁ " << disRev [ i ] . first << " ▁ " << disRev [ i ] . second << endl ; } ; loop over all nodes to choose minimum edge reversal ; ( reversal in path to i ) + ( reversal in all other tree parts ) ; choose minimum among all values ; print the designated root and total edge reversal made ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public class pair { public int first , second ; public pair ( int a , int b ) { first = a ; second = b ; } } static int dfs ( List < List < pair > > g , pair [ ] disRev , Boolean [ ] visit , int u ) { visit [ u ] = true ; int totalRev = 0 ; for ( int i = 0 ; i < g [ u ] . Count ; i ++ ) { int v = g [ u ] [ i ] . first ; if ( ! visit [ v ] ) { disRev [ v ] . first = disRev [ u ] . first + 1 ; disRev [ v ] . second = disRev [ u ] . second ; if ( g [ u ] [ i ] . second != 0 ) { disRev [ v ] . second = disRev [ u ] . second + 1 ; totalRev ++ ; } totalRev += dfs ( g , disRev , visit , v ) ; } } return totalRev ; } static void printMinEdgeReverseForRootNode ( int [ , ] edges , int e ) { int V = e + 1 ; List < List < pair > > g = new List < List < pair > > ( ) ; for ( int i = 0 ; i < V + 1 ; i ++ ) g . Add ( new List < pair > ( ) ) ; pair [ ] disRev = new pair [ V ] ; for ( int i = 0 ; i < V ; i ++ ) disRev [ i ] = new pair ( 0 , 0 ) ; Boolean [ ] visit = new Boolean [ V ] ; int u , v ; for ( int i = 0 ; i < e ; i ++ ) { u = edges [ i , 0 ] ; v = edges [ i , 1 ] ; g [ u ] . Add ( new pair ( v , 0 ) ) ; g [ v ] . Add ( new pair ( u , 1 ) ) ; } for ( int i = 0 ; i < V ; i ++ ) { visit [ i ] = false ; disRev [ i ] . first = disRev [ i ] . second = 0 ; } int root = 0 ; int totalRev = dfs ( g , disRev , visit , root ) ; int res = int . MaxValue ; for ( int i = 0 ; i < V ; i ++ ) { int edgesToRev = ( totalRev - disRev [ i ] . second ) + ( disRev [ i ] . first - disRev [ i ] . second ) ; if ( edgesToRev < res ) { res = edgesToRev ; root = i ; } } Console . WriteLine ( root + " ▁ " + res ) ; } public static void Main ( String [ ] args ) { int [ , ] edges = { { 0 , 1 } , { 2 , 1 } , { 3 , 2 } , { 3 , 4 } , { 5 , 4 } , { 5 , 6 } , { 7 , 6 } } ; int e = edges . GetLength ( 0 ) ; printMinEdgeReverseForRootNode ( edges , e ) ; } }


"Minimum edges required to make a Directed Graph Strongly Connected | C # program to implement the above approach ; Perform DFS to count the in - degree and out - degree of the graph ; Mark the source as visited ; Traversing adjacent nodes ; Mark out - degree as 1 ; Mark in - degree as 1 ; If not visited ; DFS Traversal on adjacent vertex ; Function to return minimum number of edges required to make the graph strongly connected ; For Adjacency List ; Create the Adjacency List ; Initialize the in - degree array ; Initialize the out - degree array ; Initialize the visited array ; Perform DFS to count in - degrees and out - degreess ; To store the result ; To store total count of in - degree and out - degree ; Find total in - degree and out - degree ; Calculate the minimum edges required ; Return the minimum edges ; Driver Code ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void dfs ( int u , List < int > [ ] adj , int [ ] vis , int [ ] inDeg , int [ ] outDeg ) { vis [ u ] = 1 ; foreach ( int v in adj [ u ] ) { outDeg [ u ] = 1 ; inDeg [ v ] = 1 ; if ( vis [ v ] == 0 ) { dfs ( v , adj , vis , inDeg , outDeg ) ; } } } static int findMinimumEdges ( int [ ] source , int N , int M , int [ ] dest ) { List < int > [ ] adj = new List < int > [ N + 1 ] ; for ( int i = 0 ; i < adj . Length ; i ++ ) adj [ i ] = new List < int > ( ) ; for ( int i = 0 ; i < M ; i ++ ) { adj ] . Add ( dest [ i ] ) ; } int [ ] inDeg = new int [ N + 1 ] ; int [ ] outDeg = new int [ N + 1 ] ; int [ ] vis = new int [ N + 1 ] ; dfs ( 1 , adj , vis , inDeg , outDeg ) ; int minEdges = 0 ; int totalIndegree = 0 ; int totalOutdegree = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( inDeg [ i ] == 1 ) totalIndegree ++ ; if ( outDeg [ i ] == 1 ) totalOutdegree ++ ; } minEdges = Math . Max ( N - totalIndegree , N - totalOutdegree ) ; return minEdges ; } public static void Main ( String [ ] args ) { int N = 5 , M = 5 ; int [ ] source = { 1 , 3 , 1 , 3 , 4 } ; int [ ] destination = { 2 , 2 , 3 , 4 , 5 } ; Console . Write ( findMinimumEdges ( source , N , M , destination ) ) ; } }


"Minimum element left from the array after performing given operations | C # implementation of the above approach ; Function to return the minimum possible value of the last element left after performing the given operations ; Driver code"
Solution in C#:

using System ; using System . Linq ; class GFG { static int getMin ( int [ ] arr , int n ) { int minVal = arr . Min ( ) ; return minVal ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 3 , 1 , 6 , 9 } ; int n = arr . Length ; Console . WriteLine ( getMin ( arr , n ) ) ; } }


"Minimum element whose n | C # Code to find Minimum element whose n - th power is greater than product of an array of size n ; function to find the minimum element ; loop to traverse and store the sum of log ; computes sum ; calculates the elements according to formula . ; returns the minimal element ; Driver program to test above function ; initialised array ; computes the size of array ; prints out the minimal element"
Solution in C#:

using System ; class GFG { static int findMin ( int [ ] a , int n ) { double sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += Math . Log ( a [ i ] ) ; int x = ( int ) Math . Exp ( sum / n ) ; return x + 1 ; } public static void Main ( ) { int [ ] a = { 3 , 2 , 1 , 4 } ; int n = a . Length ; Console . WriteLine ( findMin ( a , n ) ) ; } }


"Minimum elements inserted in a sorted array to form an Arithmetic progression | C # implementation to find the minimum elements required to be inserted into an array to form an arithmetic progression ; Function to find the greatest common divisor of two numbers ; Function to find the minimum the minimum number of elements required to be inserted into array ; Difference array of consecutive elements of the array ; GCD of the difference array ; Loop to calculate the minimum number of elements required ; Driver Code ; Function calling"
Solution in C#:

using System ; class GFG { static int gcdFunc ( int a , int b ) { if ( b == 0 ) return a ; return gcdFunc ( b , a % b ) ; } static int findMinimumElements ( int [ ] a , int n ) { int [ ] b = new int [ n - 1 ] ; for ( int i = 1 ; i < n ; i ++ ) { b [ i - 1 ] = a [ i ] - a [ i - 1 ] ; } int gcd = b [ 0 ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) { gcd = gcdFunc ( gcd , b [ i ] ) ; } int ans = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { ans += ( b [ i ] / gcd ) - 1 ; } return ans ; } static public void Main ( ) { int [ ] arr1 = new int [ ] { 1 , 6 , 8 , 10 , 14 , 16 } ; int n1 = arr1 . Length ; Console . WriteLine ( findMinimumElements ( arr1 , n1 ) ) ; } }


"Minimum elements to be added in a range so that count of elements is divisible by K | C # implementation of the approach ; Total elements in the range ; If total elements are already divisible by k ; Value that must be added to count in order to make it divisible by k ; Driver Program to test above function"
Solution in C#:

using System ; class GFG { static int minimumMoves ( int k , int l , int r ) { int count = r - l + 1 ; if ( count % k == 0 ) return 0 ; return ( k - ( count % k ) ) ; } public static void Main ( ) { int k = 3 , l = 10 , r = 10 ; Console . WriteLine ( minimumMoves ( k , l , r ) ) ; } }


"Minimum elements to be inserted in Array to make adjacent differences equal | C # program for the above approach ; Function to find gcd of two numbers ; Function to calculate minimum numbers to be inserted to make equal differences between two consecutive elements ; Check if there is only one element in the array then answer will be 0 ; Calculate difference between first and second element of array ; If there is only two elements in the array then gcd of differences of consecutive elements of array will be equal to difference of first and second element of the array ; Loop to calculate the gcd of the differences between consecutive elements of the array ; Loop to calculate the elements to be inserted ; Return the answer ; 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 minimum_elements ( int n , int [ ] arr ) { if ( n < 3 ) return 0 ; int g , ans = 0 , diff , cnt ; diff = arr [ 1 ] - arr [ 0 ] ; g = diff ; for ( int i = 2 ; i < n ; i ++ ) { diff = arr [ i ] - arr [ i - 1 ] ; g = gcd ( g , diff ) ; } for ( int i = 1 ; i < n ; i ++ ) { diff = arr [ i ] - arr [ i - 1 ] ; cnt = diff / g ; ans += ( cnt - 1 ) ; } return ans ; } public static void Main ( ) { int [ ] arr = { 1 , 5 , 8 , 10 , 12 , 16 } ; int n = arr . Length ; Console . WriteLine ( minimum_elements ( n , arr ) ) ; } }


"Minimum elements to be removed from the ends to make the array sorted | C # implementation of the approach ; Function to return the minimum number of elements to be removed from the ends of the array to make it sorted ; To store the readonly answer ; Two pointer loop ; While the array is increasing increment j ; Updating the ans ; Updating the left pointer ; Returning the readonly answer ; Driver code"
Solution in C#:

using System ; class GFG { static int findMin ( int [ ] arr , int n ) { int ans = 1 ; for ( int i = 0 ; i < n ; i ++ ) { int j = i + 1 ; while ( j < n && arr [ j ] >= arr [ j - 1 ] ) j ++ ; ans = Math . Max ( ans , j - i ) ; i = j - 1 ; } return n - ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 1 } ; int n = arr . Length ; Console . WriteLine ( findMin ( arr , n ) ) ; } }


"Minimum elements to be removed such that sum of adjacent elements is always odd | C # implementation of the above approach ; Returns the minimum number of eliminations ; Stores the previous element ; Stores the new value ; Check if the previous and current values are of same parity ; Previous value is now the current value ; Return the counter variable ; Driver code"
Solution in C#:

using System ; class GFG { static int min_elimination ( int n , int [ ] arr ) { int count = 0 ; int prev_val = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { int curr_val = arr [ i ] ; if ( curr_val % 2 == prev_val % 2 ) count ++ ; prev_val = curr_val ; } return count ; } public static void Main ( ) { int [ ] arr = new int [ ] { 1 , 2 , 3 , 7 , 9 } ; int n = arr . Length ; Console . WriteLine ( min_elimination ( n , arr ) ) ; } }


"Minimum flip required to make Binary Matrix symmetric | C # Program to find minimum flip required to make Binary Matrix symmetric along main diagonal ; Return the minimum flip required to make Binary Matrix symmetric along main diagonal . ; Comparing elements across diagonal ; Driver program to test above function"
Solution in C#:

using System ; class GFG { static int minimumflip ( int [ , ] mat , int n ) { int flip = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) if ( mat [ i , j ] != mat [ j , i ] ) flip ++ ; return flip ; } public static void Main ( ) { int n = 3 ; int [ , ] mat = { { 0 , 0 , 1 } , { 1 , 1 , 1 } , { 1 , 0 , 0 } } ; Console . WriteLine ( minimumflip ( mat , n ) ) ; } }


"Minimum flip required to make Binary Matrix symmetric | C # Program to find minimum flip required to make Binary Matrix symmetric along main diagonal ; Return the minimum flip required to make Binary Matrix symmetric along main diagonal . ; finding the transpose of the matrix ; Finding the number of position where element are not same . ; Driver program to test above function"
Solution in C#:

using System ; class GFG { static int minimumflip ( int [ , ] mat , int n ) { int [ , ] transpose = new int [ n , n ] ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) transpose [ i , j ] = mat [ j , i ] ; int flip = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( transpose [ i , j ] != mat [ i , j ] ) flip ++ ; return flip / 2 ; } public static void Main ( ) { int n = 3 ; int [ , ] mat = { { 0 , 0 , 1 } , { 1 , 1 , 1 } , { 1 , 0 , 0 } } ; Console . WriteLine ( minimumflip ( mat , n ) ) ; } }


"Minimum flips in a Binary array such that XOR of consecutive subarrays of size K have different parity | C # implementation to find the minimum flips required such that alternate subarrays have different parity ; Function to find the minimum flips required in binary array ; Boolean value to indicate odd or even value of 1 's ; Loop to iterate over the subarrays of size K ; curr_index is used to iterate over all the subarrays ; Loop to iterate over the array at the jump of K to consider every parity ; Condition to check if the subarray is at even position ; The value needs to be same as the first subarray ; The value needs to be opposite of the first subarray ; Update the minimum difference ; Condition to check if the 1 s in the subarray is odd ; Driver Code"
Solution in C#:

using System ; class Count_Flips { static int count_flips ( int [ ] a , int n , int k ) { bool set = false ; int ans = 0 , min_diff = int . MaxValue ; for ( int i = 0 ; i < k ; i ++ ) { int curr_index = i , segment = 0 , count_zero = 0 , count_one = 0 ; while ( curr_index < n ) { if ( segment % 2 == 0 ) { if ( a [ curr_index ] == 1 ) count_zero ++ ; else count_one ++ ; } else { if ( a [ curr_index ] == 0 ) count_zero ++ ; else count_one ++ ; } curr_index = curr_index + k ; segment ++ ; } ans += Math . Min ( count_one , count_zero ) ; if ( count_one < count_zero ) set = ! set ; min_diff = Math . Min ( min_diff , Math . Abs ( count_zero - count_one ) ) ; } if ( set ) return ans ; else return ans + min_diff ; } public static void Main ( string [ ] args ) { int n = 6 , k = 3 ; int [ ] a = { 0 , 0 , 1 , 1 , 0 , 0 } ; Console . WriteLine ( count_flips ( a , n , k ) ) ; } }


"Minimum flips of odd indexed elements from odd length subarrays to make two given arrays equal | C # program for the above approach ; Function to find the minimum flip of subarrays required at alternate index to make binary arrays equals ; Stores count of total operations ; Stores count of consecutive unequal elements ; Loop to run on odd positions ; Incrementing the global counter ; Change count to 0 ; If all last elements are equal ; Loop to run on even positions ; Incrementing the global counter ; Change count to 0 ; Print the minimum operations ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static void minOperation ( int [ ] X , int [ ] Y , int n ) { int C = 0 ; int count = 0 ; for ( int i = 1 ; i < n ; i = i + 2 ) { if ( X [ i ] != Y [ i ] ) { count ++ ; } else { if ( count != 0 ) C ++ ; count = 0 ; } } if ( count != 0 ) C ++ ; count = 0 ; for ( int i = 0 ; i < n ; i = i + 2 ) { if ( X [ i ] != Y [ i ] ) { count ++ ; } else { if ( count != 0 ) C ++ ; count = 0 ; } } if ( count != 0 ) C ++ ; Console . Write ( C ) ; } public static void Main ( String [ ] args ) { int [ ] X = { 1 , 0 , 0 , 0 , 0 , 1 } ; int [ ] Y = { 1 , 1 , 0 , 1 , 1 , 1 } ; int N = X . Length ; minOperation ( X , Y , N ) ; } }


"Minimum flips or swapping of adjacent characters required to make a string equal to another | C # program for the above approach ; Function to find minimum operations required to convert string A to B ; Store the size of the string ; Store the required result ; Traverse the string , a ; If a [ i ] is equal to b [ i ] ; Check if swapping adjacent characters make the same - indexed characters equal or not ; Otherwise , flip the current bit ; Print the minimum number of operations ; Driver Code ; Given Input ; Function Call"
Solution in C#:

using System ; class GFG { static void minimumOperation ( string a , string b ) { int n = a . Length ; int i = 0 ; int minoperation = 0 ; while ( i < n ) { if ( a [ i ] == b [ i ] ) { i = i + 1 ; continue ; } else if ( a [ i ] == b [ i + 1 ] && a [ i + 1 ] == b [ i ] && i < n - 1 ) { minoperation ++ ; i = i + 2 ; } else if ( a [ i ] != b [ i ] ) { minoperation ++ ; i = i + 1 ; } else { ++ i ; } } Console . WriteLine ( minoperation ) ; } public static void Main ( ) { string a = "10010010" , b = "00001000" ; minimumOperation ( a , b ) ; } }


"Minimum flips required in a binary string such that all K | C # program to find the minimum numbers of flips required in a binary string such that all substrings of size K has atleast one 1 ; Function to calculate and return the minimum number of flips to make string valid ; Stores the count of required flips ; Stores the last index of '1' in the string ; Check for the first substring of length K ; If i - th character is '1' ; If the substring had no '1' ; Increase the count of required flips ; Flip the last index of the window ; Update the last index which contains 1 ; Check for remaining substrings ; If last_idx does not belong to current window make it - 1 ; If the last character of the current substring is '1' , then update last_idx to i + k - 1 ; ; If last_idx == - 1 , then the current substring has no 1 ; Increase the count of flips ; Update the last index of the current window ; Store the last index of current window as the index of last '1' in the string ; Return the number of operations ; Driver Code"
Solution in C#:

using System ; using System . Text ; class GFG { public static int minimumMoves ( String s , int K ) { StringBuilder S = new StringBuilder ( s ) ; int N = S . Length ; int ops = 0 ; int last_idx = - 1 ; for ( int i = 0 ; i < K ; i ++ ) { if ( S [ i ] == '1' ) last_idx = i ; } if ( last_idx == - 1 ) { ++ ops ; S . Insert ( K - 1 , '1' ) ; last_idx = K - 1 ; } for ( int i = 1 ; i < N - K + 1 ; i ++ ) { if ( last_idx < i ) last_idx = - 1 ; if ( S [ i + K - 1 ] == '1' ) last_idx = i + K - 1 ; if ( last_idx == - 1 ) { ++ ops ; S . Insert ( i + K - 1 , '1' ) ; last_idx = i + K - 1 ; } } return ops ; } public static void Main ( String [ ] args ) { String S = "001010000" ; int K = 3 ; Console . WriteLine ( minimumMoves ( S , K ) ) ; } }


"Minimum flips required to form given binary string where every flip changes all bits to its right as well | C # program for the above approach ; Function to return the count of minimum flips required ; If curr occurs in the readonly string ; Switch curr to '0' if '1' or vice - versa ; Driver code"
Solution in C#:

using System ; class GFG { public static int minFlips ( String target ) { char curr = '1' ; int count = 0 ; for ( int i = 0 ; i < target . Length ; i ++ ) { if ( target [ i ] == curr ) { count ++ ; curr = ( char ) ( 48 + ( curr + 1 ) % 2 ) ; } } return count ; } public static void Main ( String [ ] args ) { String S = "011000" ; Console . WriteLine ( minFlips ( S ) ) ; } }


"Minimum flips required to keep all 1 s together in a Binary string | C # implementation for Minimum number of flips required in a binary string such that all the 1 aTMs are together ; Length of the binary string ; Initial state of the dp dp [ 0 ] [ 0 ] will be 1 if the current bit is 1 and we have to flip it ; Initial state of the dp dp [ 0 ] [ 1 ] will be 1 if the current bit is 0 and we have to flip it ; dp [ i ] [ 0 ] = Flips required to make all previous bits zero + Flip required to make current bit zero ; dp [ i ] [ 1 ] = minimum flips required to make all previous states 0 or make previous states 1 satisfying the condition ; Minimum of answer and flips required to make all bits 0 ; Driver code"
Solution in C#:

using System ; class GFG { static int minFlip ( string a ) { int n = a . Length ; int [ , ] dp = new int [ n + 1 , 2 ] ; dp [ 0 , 0 ] = ( a [ 0 ] == '1' ? 1 : 0 ) ; dp [ 0 , 1 ] = ( a [ 0 ] == '0' ? 1 : 0 ) ; for ( int i = 1 ; i < n ; i ++ ) { dp [ i , 0 ] = dp [ i - 1 , 0 ] + ( a [ i ] == '1' ? 1 : 0 ) ; dp [ i , 1 ] = Math . Min ( dp [ i - 1 , 0 ] , dp [ i - 1 , 1 ] ) + ( a [ i ] == '0' ? 1 : 0 ) ; } int answer = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { answer = Math . Min ( answer , dp [ i , 1 ] + dp [ n - 1 , 0 ] - dp [ i , 0 ] ) ; } return Math . Min ( answer , dp [ n - 1 , 0 ] ) ; } public static void Main ( string [ ] args ) { string s = "1100111000101" ; Console . Write ( minFlip ( s ) ) ; } }


"Minimum flips required to maximize a number with k set bits | C # Code to find Minimum flips required to maximize a number with k set bits ; function for finding set bit ; return count of set bit ; function for finding min flip ; number of bits in n ; Find the largest number of same size with k set bits ; Count bit differences to find required flipping . ; Driver Code"
Solution in C#:

using System ; class GFG { static int setBit ( int xorValue ) { int count = 0 ; while ( xorValue >= 1 ) { if ( xorValue % 2 == 1 ) count ++ ; xorValue /= 2 ; } return count ; } static int minFlip ( int n , int k ) { int size = ( int ) ( Math . Log ( n ) / Math . Log ( 2 ) ) + 1 ; int max = ( int ) Math . Pow ( 2 , k ) - 1 ; max = max << ( size - k ) ; int xorValue = ( n ^ max ) ; return ( setBit ( xorValue ) ) ; } public static void Main ( ) { int n = 27 , k = 3 ; Console . Write ( " Min ▁ Flips ▁ = ▁ " + minFlip ( n , k ) ) ; } }


"Minimum flips to make all 1 s in left and 0 s in right | Set 1 ( Using Bitmask ) | C # program to find minimum flips to make all 1 s in left ; Function to count minimum number of flips ; This is converting string s into integer of base 2 ( if s = '100' then num = 4 ) ; initialize minXor with n that can be maximum number of flips ; right shift 1 by ( n - 1 ) bits ; calculate bitwise XOR of num and mask ; Math . Min ( a , b ) returns minimum of a and b return minimum number of flips till that digit ; Function to count number of 1 s ; Driver code"
Solution in C#:

using System ; class GFG { public static int findMiniFlip ( int [ ] nums ) { int n = nums . Length ; String s = " " ; for ( int i = 0 ; i < n ; i ++ ) s += nums [ i ] ; long num = Convert . ToInt32 ( s , 2 ) ; int minXor = n ; long mask = ( 1 << ( n - 1 ) ) ; while ( n - 1 > 0 ) { long temp = ( num ^ mask ) ; minXor = Math . Min ( minXor , countones ( temp ) ) ; n -- ; mask = ( mask | ( 1 << n - 1 ) ) ; } return minXor ; } public static int countones ( long n ) { int c = 0 ; while ( n > 0 ) { n = n & ( n - 1 ) ; c ++ ; } return c ; } public static void Main ( String [ ] args ) { int [ ] nums = { 1 , 0 , 1 , 1 , 0 , 0 , 0 } ; int n = findMiniFlip ( nums ) ; Console . WriteLine ( n ) ; } }


"Minimum flips to make all 1 s in right and 0 s in left | C # program to implement the above approach ; Function to find the minimum count of flips required to make all 1 s on the right and all 0 s on the left of the given string ; Stores length of str ; Store count of 0 s in the string ; Traverse the string ; If current character is 0 ; Update zeros ; If count of 0 s in the string is 0 or n ; Store minimum count of flips required to make all 0 s on the left and 1 s on the right ; Stores count of 1 s on the left side of each index ; Stores count of flips required to make all 0 s on the left and 1 s on the right ; Traverse the string ; If current character is 1 ; Update currOnes ; Update flips ; Update the minimum count of flips ; Driver code"
Solution in C#:

using System ; class GFG { public static int minimumCntOfFlipsRequired ( string str ) { int n = str . Length ; int zeros = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == '0' ) { zeros ++ ; } } if ( zeros == 0 zeros == n ) { return 0 ; } int minFlips = Int32 . MaxValue ; int currOnes = 0 ; int flips ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == '1' ) { currOnes ++ ; } flips = currOnes + ( zeros - ( i + 1 - currOnes ) ) ; minFlips = Math . Min ( minFlips , flips ) ; } return minFlips ; } public static void Main ( ) { string s1 = "100101" ; Console . WriteLine ( minimumCntOfFlipsRequired ( s1 ) ) ; } }


"Minimum gcd operations to make all array elements one | C # program to find minimum GCD operations to make all array elements one . ; __gcd function ; Function to count number of moves . ; Counting Number of ones . ; If there is a one ; Find smallest subarray with GCD equals to one . ; to calculate GCD ; Not Possible ; Final answer ; 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 minimumMoves ( int [ ] A , int N ) { int one = 0 ; for ( int i = 0 ; i < N ; i ++ ) if ( A [ i ] == 1 ) one ++ ; if ( one != 0 ) return N - one ; int minimum = int . MaxValue ; for ( int i = 0 ; i < N ; i ++ ) { int g = A [ i ] ; for ( int j = i + 1 ; j < N ; j ++ ) { g = __gcd ( A [ j ] , g ) ; if ( g == 1 ) { minimum = Math . Min ( minimum , j - i ) ; break ; } } } if ( minimum == int . MaxValue ) return - 1 ; else return N + minimum - 1 ; } public static void Main ( ) { int [ ] A = { 2 , 4 , 3 , 9 } ; int N = A . Length ; Console . WriteLine ( minimumMoves ( A , N ) ) ; } }


"Minimum halls required for class scheduling | C # implementation of the approach ; Function to return the minimum number of halls required ; Array to store the number of lectures ongoing at time t ; For every lecture increment start point s decrement ( end point + 1 ) ; Perform prefix sum and update the ans to maximum ; Driver code"
Solution in C#:

using System ; class GFG { static int MAX = 100001 ; static int minHalls ( int [ , ] lectures , int n ) { int [ ] prefix_sum = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { prefix_sum [ lectures [ i , 0 ] ] ++ ; prefix_sum [ lectures [ i , 1 ] + 1 ] -- ; } int ans = prefix_sum [ 0 ] ; for ( int i = 1 ; i < MAX ; i ++ ) { prefix_sum [ i ] += prefix_sum [ i - 1 ] ; ans = Math . Max ( ans , prefix_sum [ i ] ) ; } return ans ; } public static void Main ( String [ ] args ) { int [ , ] lectures = { { 0 , 5 } , { 1 , 2 } , { 1 , 10 } } ; int n = lectures . GetLength ( 0 ) ; Console . WriteLine ( minHalls ( lectures , n ) ) ; } }


"Minimum in an array which is first decreasing then increasing | C # program to find the smallest number in an array of decrease and increasing numbers ; Function to find the smallest number 's index ; Do a binary search ; Find the mid element ; Check for break point ; Return the index ; Driver Code ; Print the smallest number"
Solution in C#:

using System ; class Solution { static int minimal ( int [ ] a , int n ) { int lo = 0 , hi = n - 1 ; while ( lo < hi ) { int mid = ( lo + hi ) >> 1 ; if ( a [ mid ] < a [ mid + 1 ] ) { hi = mid ; } else { lo = mid + 1 ; } } return lo ; } public static void Main ( ) { int [ ] a = { 8 , 5 , 4 , 3 , 4 , 10 } ; int n = a . Length ; int ind = minimal ( a , n ) ; Console . WriteLine ( a [ ind ] ) ; } }


"Minimum increment / decrement operations required on Array to satisfy given conditions | C # program for the above approach ; Function to find minimum number of operations to get desired array ; For odd ' i ' , sum of elements till ' i ' is positive ; If i is even and sum is positive , make it negative by subtracting 1 + | s | from a [ i ] ; If i is odd and sum is negative , make it positive by adding 1 + | s | into a [ i ] ; For odd ' i ' , sum of elements till ' i ' is negative ; Check if ' i ' is odd and sum is positive , make it negative by subtracting 1 + | s | from a [ i ] ; Check if ' i ' is even and sum is negative , make it positive by adding 1 + | s | into a [ i ] ; Return the minimum of the two ; Driver Code ; Given array [ ] arr ; Function call"
Solution in C#:

using System ; class GFG { static int minOperations ( int [ ] a , int N ) { int num_of_ops1 , num_of_ops2 , sum ; num_of_ops1 = num_of_ops2 = sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += a [ i ] ; if ( i % 2 == 0 && sum >= 0 ) { num_of_ops1 += ( 1 + Math . Abs ( sum ) ) ; sum = - 1 ; } else if ( i % 2 == 1 && sum <= 0 ) { num_of_ops1 += ( 1 + Math . Abs ( sum ) ) ; sum = 1 ; } } sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += a [ i ] ; if ( i % 2 == 1 && sum >= 0 ) { num_of_ops2 += ( 1 + Math . Abs ( sum ) ) ; sum = - 1 ; } else if ( i % 2 == 0 && sum <= 0 ) { num_of_ops2 += ( 1 + Math . Abs ( sum ) ) ; sum = 1 ; } } return Math . Min ( num_of_ops1 , num_of_ops2 ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , - 4 , 5 , 0 , 1 } ; int N = arr . Length ; Console . Write ( minOperations ( arr , N ) ) ; } }


"Minimum increment / decrement to make array non | C # code to count the change required to convert the array into non - increasing array ; min heap ; Here in the loop we will check that whether the upcoming element of array is less than top of priority queue . If yes then we calculate the difference . After that we will remove that element and push the current element in queue . And the sum is incremented by the value of difference ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int DecreasingArray ( int [ ] a , int n ) { int sum = 0 , dif = 0 ; List < int > pq = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( pq . Count > 0 && pq [ 0 ] < a [ i ] ) { dif = a [ i ] - pq [ 0 ] ; sum += dif ; pq . RemoveAt ( 0 ) ; } pq . Add ( a [ i ] ) ; pq . Sort ( ) ; } return sum ; } static void Main ( ) { int [ ] a = { 3 , 1 , 2 , 1 } ; int n = a . Length ; Console . Write ( DecreasingArray ( a , n ) ) ; } }


"Minimum increment by k operations to make all elements equal | C # program Minimum increment by k operations to make all elements equal . ; function for calculating min operations ; max elements of array ; iterate for all elements ; check if element can make equal to max or not if not then return - 1 ; else update res for required operations ; return result ; Driver program"
Solution in C#:

using System ; class GFG { static int minOps ( int [ ] arr , int n , int k ) { Array . Sort ( arr ) ; int max = arr [ arr . Length - 1 ] ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( max - arr [ i ] ) % k != 0 ) return - 1 ; else res += ( max - arr [ i ] ) / k ; } return res ; } public static void Main ( ) { int [ ] arr = { 21 , 33 , 9 , 45 , 63 } ; int n = arr . Length ; int k = 6 ; Console . Write ( minOps ( arr , n , k ) ) ; } }


"Minimum increment in the sides required to get non | C # Program to find Minimum increment in the sides required to get non - negative area of a triangle ; push the three sides to a array ; sort the array ; check if sum is greater than third side ; Driver Code"
Solution in C#:

using System ; class GFG { static int minimumIncrease ( int a , int b , int c ) { int [ ] arr = { a , b , c } ; Array . Sort ( arr ) ; if ( arr [ 0 ] + arr [ 1 ] >= arr [ 2 ] ) return 0 ; else return arr [ 2 ] - ( arr [ 0 ] + arr [ 1 ] ) ; } public static void Main ( ) { int a = 3 , b = 5 , c = 10 ; Console . Write ( minimumIncrease ( a , b , c ) ) ; } }


"Minimum increment operations to make K elements equal | C # implementation of the approach ; Function to return the minimum number of increment operations required to make any k elements of the array equal ; Sort the array in increasing order ; Calculate the number of operations needed to make 1 st k elements equal to the kth element i . e . the 1 st window ; Answer will be the minimum of all possible k sized windows ; Find the operations needed to make k elements equal to ith element ; Slide the window to the right and subtract increments spent on leftmost element of the previous window ; Add increments needed to make the 1 st k - 1 elements of this window equal to the kth element of the current window ; Driver code"
Solution in C#:

using System ; class geeksforgeeks { static int minOperations ( int [ ] ar , int k ) { Array . Sort ( ar ) ; int opsNeeded = 0 ; for ( int i = 0 ; i < k ; i ++ ) { opsNeeded += ar [ k - 1 ] - ar [ i ] ; } int ans = opsNeeded ; for ( int i = k ; i < ar . Length ; i ++ ) { opsNeeded = opsNeeded - ( ar [ i - 1 ] - ar [ i - k ] ) ; opsNeeded += ( k - 1 ) * ( ar [ i ] - ar [ i - 1 ] ) ; ans = Math . Min ( ans , opsNeeded ) ; } return ans ; } public static void Main ( ) { int [ ] arr = { 3 , 1 , 9 , 100 } ; int n = arr . Length ; int k = 3 ; Console . Write ( minOperations ( arr , k ) ) ; } }


"Minimum increment operations to make the array in increasing order | C # program to find minimum moves required to make the array in increasing order ; function to find minimum moves required to make the array in increasing order ; to store answer ; iterate over an array ; non - increasing order ; add moves to answer ; increase the element ; return required answer ; Driver code"
Solution in C#:

using System ; class GFG { static int MinimumMoves ( int [ ] a , int n , int x ) { int ans = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] <= a [ i - 1 ] ) { int p = ( a [ i - 1 ] - a [ i ] ) / x + 1 ; ans += p ; a [ i ] += p * x ; } } return ans ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 3 , 2 } ; int x = 2 ; int n = arr . Length ; Console . Write ( MinimumMoves ( arr , n , x ) ) ; } }


"Minimum increment or decrement operations required to make the array sorted | C # implementation of the approach ; Function to return the minimum number of given operations required to sort the array ; Number of elements in the array ; Smallest element in the array ; Largest element in the array ; dp ( i , j ) represents the minimum number of operations needed to make the array [ 0 . . i ] sorted in non - decreasing order given that ith element is j ; Fill the dp [ ] , array for base cases ; Using results for the first ( i - 1 ) elements , calculate the result for the ith element ; If the ith element is j then we can have any value from small to j for the i - 1 th element We choose the one that requires the minimum operations ; If we made the ( n - 1 ) th element equal to j we required dp ( n - 1 , j ) operations We choose the minimum among all possible dp ( n - 1 , j ) where j goes from small to large ; Driver code"
Solution in C#:

using System ; using System . Linq ; using System . Collections . Generic ; class GFG { static int getMinimumOps ( List < int > ar ) { int n = ar . Count ; int small = ar . Min ( ) ; int large = ar . Max ( ) ; int [ , ] dp = new int [ n , large + 1 ] ; for ( int j = small ; j <= large ; j ++ ) { dp [ 0 , j ] = Math . Abs ( ar [ 0 ] - j ) ; } for ( int i = 1 ; i < n ; i ++ ) { int minimum = int . MaxValue ; for ( int j = small ; j <= large ; j ++ ) { minimum = Math . Min ( minimum , dp [ i - 1 , j ] ) ; dp [ i , j ] = minimum + Math . Abs ( ar [ i ] - j ) ; } } int ans = int . MaxValue ; for ( int j = small ; j <= large ; j ++ ) { ans = Math . Min ( ans , dp [ n - 1 , j ] ) ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 1 , 4 , 3 } ; List < int > ar = new List < int > ( arr ) ; Console . WriteLine ( getMinimumOps ( ar ) ) ; } }


"Minimum increment or decrement required to sort the array | Top | C # program of the above approach ; Dp array to memoized the value recursive call ; Function to find the minimum increment or decrement needed to make the array sorted ; If only one element is present , then [ ] arr is sorted ; If dp [ N , maxE ] is precalculated , then return the result ; Iterate from minE to maxE which placed at previous index ; Update the answer according to recurrence relation ; Memoized the value for dp [ N , maxE ] ; Return the readonly result ; Driver Code ; Find the minimum and maximum element from the [ ] arr ; Function call"
Solution in C#:

using System ; using System . Linq ; class GFG { static int [ , ] dp = new int [ 1000 , 1000 ] ; static int minimumIncDec ( int [ ] arr , int N , int maxE , int minE ) { if ( N == 0 ) { return 0 ; } if ( dp [ N , maxE ] != 0 ) return dp [ N , maxE ] ; int ans = int . MaxValue ; for ( int k = minE ; k <= maxE ; k ++ ) { int x = minimumIncDec ( arr , N - 1 , k , minE ) ; ans = Math . Min ( ans , x + Math . Abs ( arr [ N - 1 ] - k ) ) ; } dp [ N , maxE ] = ans ; return dp [ N , maxE ] ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 4 , 3 , 2 , 1 } ; int N = arr . Length ; int minE = arr . Min ( ) ; int maxE = arr . Max ( ) ; Console . Write ( minimumIncDec ( arr , N , maxE , minE ) ) ; } }


"Minimum increments by 1 or K required to convert a string into another given string | C # program for the above approach ; Function to count minimum increments by 1 or K required to convert X to Y ; Convert String to character array ; Traverse the string X ; Case 1 ; Case 2 ; Add the difference / K to the count ; Add the difference % K to the count ; Case 3 ; Add the difference / K to the count ; Add the difference % K to the count ; Print the answer ; Driver code"
Solution in C#:

using System ; class GFG { static void countOperations ( string X , string Y , int K ) { char [ ] ch1 = X . ToCharArray ( ) ; char [ ] ch2 = Y . ToCharArray ( ) ; int count = 0 ; for ( int i = 0 ; i < X . Length ; i ++ ) { int c = 0 ; if ( ch1 [ i ] == ch2 [ i ] ) continue ; else if ( ch1 [ i ] < ch2 [ i ] ) { if ( ( ( int ) ch2 [ i ] - ( int ) ch1 [ i ] ) >= K ) { c = ( ( int ) ch2 [ i ] - ( int ) ch1 [ i ] ) / K ; } c += ( ( int ) ch2 [ i ] - ( int ) ch1 [ i ] ) % K ; } else { int t = 90 - ( int ) ch1 [ i ] ; t += ( int ) ch2 [ i ] - 65 + 1 ; if ( t >= K ) c = t / K ; c += ( t % K ) ; } count += c ; } Console . WriteLine ( count ) ; } static void Main ( ) { string X = " ABCT " , Y = " PBDI " ; int K = 6 ; countOperations ( X , Y , K ) ; } }


"Minimum increments by index value required to obtain at least two equal Array elements | C # program to implement the above approach ; Function to calculate the minimum number of steps required ; Stores minimum difference ; Driver code"
Solution in C#:

using System ; class GFG { static void incrementCount ( int [ ] arr , int N ) { int mini = arr [ 0 ] - arr [ 1 ] ; for ( int i = 2 ; i < N ; i ++ ) { mini = Math . Min ( mini , arr [ i - 1 ] - arr [ i ] ) ; } Console . WriteLine ( mini ) ; } public static void Main ( String [ ] args ) { int N = 3 ; int [ ] arr = { 12 , 8 , 4 } ; incrementCount ( arr , N ) ; } }


"Minimum increments by index value required to obtain at least two equal Array elements | C # program to implement the above approach ; Function to update every element adding to it its index value ; Function to check if at least two elements are equal or not ; Count the frequency of arr [ i ] ; Function to calculate the number of increment operations required ; Stores the minimum number of steps ; Driver code"
Solution in C#:

using System ; class GFG { static void update ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] += ( i + 1 ) ; } } static bool check ( int [ ] arr , int N ) { int f = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < N ; j ++ ) { if ( arr [ i ] == arr [ j ] ) { count ++ ; } } if ( count >= 2 ) { f = 1 ; break ; } } if ( f == 1 ) return true ; else return false ; } static void incrementCount ( int [ ] arr , int N ) { int min = 0 ; while ( check ( arr , N ) != true ) { update ( arr , N ) ; min ++ ; } Console . WriteLine ( min ) ; } public static void Main ( String [ ] args ) { int N = 3 ; int [ ] arr = { 12 , 8 , 4 } ; incrementCount ( arr , N ) ; } }


"Minimum increments of Non | C # Program to implement the above approach ; Function to return to the minimum number of operations required to make the array non - decreasing ; Stores the count of operations ; If arr [ i ] > arr [ i + 1 ] , add arr [ i ] - arr [ i + 1 ] to the answer Otherwise , add 0 ; Driver Code"
Solution in C#:

using System ; class GFG { public static int getMinOps ( int [ ] arr ) { int ans = 0 ; for ( int i = 0 ; i < arr . Length - 1 ; i ++ ) { ans += Math . Max ( arr [ i ] - arr [ i + 1 ] , 0 ) ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 1 , 2 , 4 } ; Console . WriteLine ( getMinOps ( arr ) ) ; } }


"Minimum increments or decrements by D required to make all array elements equal | C # program for the above approach ; Function to find minimum count of operations required to make all array elements equal by incrementing or decrementing array elements by d ; Sort the array ; Traverse the array ; If difference between two consecutive elements are not divisible by D ; Store minimum count of operations required to make all array elements equal by incrementing or decrementing array elements by d ; Stores middle element of the array ; Traverse the array ; Update count ; Driver code ; Given N & D ; Given array arr [ ] ; Function Call"
Solution in C#:

using System ; class GFG { static void numOperation ( int [ ] arr , int N , int D ) { Array . Sort ( arr ) ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( ( arr [ i + 1 ] - arr [ i ] ) % D != 0 ) { Console . WriteLine ( " - 1" ) ; return ; } } int count = 0 ; int mid = arr [ N / 2 ] ; for ( int i = 0 ; i < N ; i ++ ) { count += Math . Abs ( mid - arr [ i ] ) / D ; } Console . WriteLine ( count ) ; } static public void Main ( ) { int N = 4 , D = 2 ; int [ ] arr = new int [ ] { 2 , 4 , 6 , 8 } ; numOperation ( arr , N , D ) ; } }


"Minimum increments or decrements required to convert a sorted array into a power sequence | C # program for the above approach ; Function to find the minimum number of increments or decrements required to convert array into a power sequence ; Initialize the count to f ( X ) for X = 1 ; Calculate the value of f ( X ) X ^ ( n - 1 ) <= f ( 1 ) + a [ n - 1 ] ; Calculate F ( x ) ; Check if X ^ ( n - 1 ) > f ( 1 ) + a [ n - 1 ] ; Update ans to store the minimum of ans and F ( x ) ; Return the minimum number of operations required ; Driver Code"
Solution in C#:

using System ; class GFG { static int minOperations ( int [ ] a , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans += a [ i ] ; } ans -= n ; for ( int x = 1 ; ; x ++ ) { int curPow = 1 , curCost = 0 ; for ( int i = 0 ; i < n ; i ++ ) { curCost += Math . Abs ( a [ i ] - curPow ) ; curPow *= x ; } if ( curPow / x > ans + a [ n - 1 ] ) break ; ans = Math . Min ( ans , curCost ) ; } return ans ; } public static void Main ( ) { int [ ] arr = { 1 , 5 , 7 } ; int N = arr . Length ; Console . WriteLine ( minOperations ( arr , N ) ) ; } }


"Minimum increments or decrements required to signs of prefix sum array elements alternating | C # code for above approach ; Function to find the minimum number of increments / decrements of array elements by 1 to make signs of prefix sum array elements alternating ; Case 1. neg - pos - neg ; Stores the current sign of the prefix sum of array ; Stores minimum number of operations for Case 1 ; Traverse the array ; Checking both conditions ; Update the current prefix1 to currentPrefixSum ; Case 2. pos - neg - pos ; Stores the prefix sum of array ; Stores minimum number of operations for Case 1 ; Checking both conditions ; Update the current prefix2 to currentPrefixSum ; Driver Code"
Solution in C#:

using System ; public class GFG { static int minimumOperations ( int [ ] A , int N ) { int cur_prefix_1 = 0 ; int parity = - 1 ; int minOperationsCase1 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { cur_prefix_1 += A [ i ] ; if ( cur_prefix_1 == 0 parity * cur_prefix_1 < 0 ) { minOperationsCase1 += Math . Abs ( parity - cur_prefix_1 ) ; cur_prefix_1 = parity ; } parity *= - 1 ; } int cur_prefix_2 = 0 ; parity = 1 ; int minOperationsCase2 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { cur_prefix_2 += A [ i ] ; if ( cur_prefix_2 == 0 parity * cur_prefix_2 < 0 ) { minOperationsCase2 += Math . Abs ( parity - cur_prefix_2 ) ; cur_prefix_2 = parity ; } parity *= - 1 ; } return Math . Min ( minOperationsCase1 , minOperationsCase2 ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 1 , - 3 , 1 , 0 } ; int N = A . Length ; Console . Write ( minimumOperations ( A , N ) ) ; } }


"Minimum increments required to make absolute difference of all pairwise adjacent array elements even | C # program for the above approach ; Function to find the minimum number of increments of array elements required to make difference between all pairwise adjacent elements even ; Stores the count of odd and even elements ; Traverse the array ; Increment odd count ; Increment even count ; Return the minimum number of operations required ; Driver Code"
Solution in C#:

using System ; class GFG { static int minOperations ( int [ ] arr , int n ) { int oddcount = 0 , evencount = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 1 ) oddcount ++ ; else evencount ++ ; } return Math . Min ( oddcount , evencount ) ; } public static void Main ( ) { int [ ] arr = { 2 , 4 , 3 , 1 , 8 } ; int N = ( arr . Length ) ; Console . WriteLine ( minOperations ( arr , N ) ) ; } }


"Minimum increments required to make array elements alternately even and odd | C # program for the above approach ; Function to find the minimum number of increments required to make the array even - odd alternately or vice - versa ; Store the minimum number of increments required ; Traverse the array [ ] arr ; Increment forEven if even element is present at an odd index ; Increment forEven if odd element is present at an even index ; Return the minimum number of increments ; Driver Code"
Solution in C#:

using System ; public class GFG { static int minIncr ( int [ ] arr , int n ) { int forEven = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 1 ) { if ( ( arr [ i ] % 2 ) == 0 ) forEven += 1 ; } else { if ( arr [ i ] % 2 == 1 ) forEven += 1 ; } } return Math . Min ( forEven , n - forEven ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 4 , 6 , 8 , 9 , 5 } ; int n = arr . Length ; Console . WriteLine ( minIncr ( arr , n ) ) ; } }


"Minimum increments required to make given matrix palindromic | C # program for the above approach ; Function to evaluate minimum number of operation required to convert the matrix to a palindrome matrix ; Variable to store number of operations required ; Iterate over the first quadrant of the matrix ; Store positions of all four values from four quadrants ; Store the values having unique indexes ; Largest value in the values vector ; Evaluate minimum increments required to make all vector elements equal ; Print the answer ; Driver Code ; Function Call"
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 void palindromeMatrix ( int N , int M , int [ , ] arr ) { int ans = 0 ; for ( int i = 0 ; i < ( N + 1 ) / 2 ; i ++ ) { for ( int j = 0 ; j < ( M + 1 ) / 2 ; j ++ ) { HashSet < pair > s = new HashSet < pair > ( ) ; s . Add ( new pair ( i , j ) ) ; s . Add ( new pair ( i , M - j - 1 ) ) ; s . Add ( new pair ( N - i - 1 , j ) ) ; s . Add ( new pair ( N - i - 1 , M - j - 1 ) ) ; List < int > values = new List < int > ( ) ; foreach ( pair p in s ) { values . Add ( arr [ p . first , p . second ] ) ; } values . Sort ( ) ; int max = values [ values . Count - 1 ] ; for ( int k = 1 ; k < values . Count ; k ++ ) { ans += max - values [ k ] ; } } } Console . Write ( ans ) ; } public static void Main ( String [ ] args ) { int N = 3 , M = 3 ; int [ , ] arr = { { 1 , 2 , 1 } , { 3 , 4 , 1 } , { 1 , 2 , 1 } } ; palindromeMatrix ( N , M , arr ) ; } }


"Minimum increments to convert to an array of consecutive integers | C # implementation of the approach ; Function that return true if the required array can be generated with m as the last element ; Build the desired array ; Check if the given array can be converted to the desired array with the given operation ; Function to return the minimum number of operations required to convert the given array to an increasing AP series with common difference as 1 ; Apply Binary Search ; If array can be generated with mid as the last element ; Current ans is mid ; Check whether the same can be achieved with even less operations ; Build the desired array ; Calculate the number of operations required ; Return the number of operations required ; Driver code"
Solution in C#:

using System ; using System . Linq ; class GFG { static Boolean check ( int m , int n , int [ ] arr ) { int [ ] desired = new int [ n ] ; for ( int i = n - 1 ; i >= 0 ; i -- ) { desired [ i ] = m ; m -- ; } for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > desired [ i ] desired [ i ] < 1 ) { return false ; } } return true ; } static int minOperations ( int [ ] arr , int n ) { int start = ( int ) arr [ n - 1 ] ; int end = arr . Max ( ) + n ; int max_arr = 0 ; while ( start <= end ) { int mid = ( start + end ) / 2 ; if ( check ( mid , n , arr ) ) { max_arr = mid ; end = mid - 1 ; } else { start = mid + 1 ; } } int [ ] desired = new int [ n ] ; for ( int i = n - 1 ; i >= 0 ; i -- ) { desired [ i ] = max_arr ; max_arr -- ; } int operations = 0 ; for ( int i = 0 ; i < n ; i ++ ) { operations += ( desired [ i ] - arr [ i ] ) ; } return operations ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 4 , 5 , 5 , 7 } ; int n = arr . Length ; Console . WriteLine ( minOperations ( arr , n ) ) ; } }


"Minimum increments to modify array such that value of any array element can be splitted to make all remaining elements equal | C # program for the above approach ; Function to find the minimum moves ; Stores the sum of the array ; Store the maximum element ; Base Case ; If N is 2 , the answer will always be 0 ; Traverse the array ; Calculate sum of the array ; Finding maximum element ; Calculate ceil ( sum / N - 1 ) ; If k is smaller than maxelement ; Final sum - original sum ; Print the minimum number of increments required ; Driver code ; Given array ; Size of given array ; Function Call"
Solution in C#:

using System ; class GFG { static void minimumMoves ( int [ ] arr , int N ) { int sum = 0 ; int maxelement = - 1 ; if ( N == 2 ) { Console . Write ( "0" ) ; return ; } for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; maxelement = Math . Max ( maxelement , arr [ i ] ) ; } int k = ( sum + N - 2 ) / ( N - 1 ) ; k = Math . Max ( maxelement , k ) ; int ans = k * ( N - 1 ) - sum ; Console . WriteLine ( ans ) ; } static void Main ( ) { int [ ] arr = { 2 , 3 , 7 } ; int N = arr . Length ; minimumMoves ( arr , N ) ; } }


"Minimum index i such that all the elements from index i to given index are equal | C # implementation of the approach ; Function to return the minimum required index ; Short - circuit more comparisions as found the border point ; For cases were high = low + 1 and arr [ high ] will match with arr [ pos ] but not arr [ low ] or arr [ mid ] . In such iteration the if condition will satisfy and loop will break post that low will be updated . Hence i will not point to the correct index . ; Driver code ; Console . WriteLine ( minIndex ( arr , 2 ) ) ; Should be 1 Console . WriteLine ( minIndex ( arr , 3 ) ) ; Should be 1 Console . WriteLine ( minIndex ( arr , 4 ) ) ; Should be 4"
Solution in C#:

using System ; class GFG { static int minIndex ( int [ ] arr , int pos ) { int low = 0 ; int high = pos ; int i = pos ; while ( low < high ) { int mid = ( low + high ) / 2 ; if ( arr [ mid ] != arr [ pos ] ) { low = mid + 1 ; } else { high = mid - 1 ; i = mid ; if ( mid > 0 && arr [ mid - 1 ] != arr [ pos ] ) { break ; } } } return arr [ low ] == arr [ pos ] ? low : i ; } public static void Main ( ) { int [ ] arr = { 2 , 1 , 1 , 1 , 5 , 2 } ; } }


"Minimum index i such that all the elements from index i to given index are equal | C # implementation of the approach ; Function to return the minimum required index ; Start from arr [ pos - 1 ] ; All elements are equal from arr [ i + 1 ] to arr [ pos ] ; Driver code ; Function Call"
Solution in C#:

using System ; class GFG { static int minIndex ( int [ ] arr , int n , int pos ) { int num = arr [ pos ] ; int i = pos - 1 ; while ( i >= 0 ) { if ( arr [ i ] != num ) break ; i -- ; } return i + 1 ; } public static void Main ( ) { int [ ] arr = { 2 , 1 , 1 , 1 , 5 , 2 } ; int n = arr . Length ; int pos = 4 ; Console . WriteLine ( minIndex ( arr , n , pos ) ) ; } }


"Minimum index to split array into subarrays with co | C # program for the above approach ; Function to find the GCD of 2 numbers ; Base Case ; Find the GCD recursively ; Function to find the minimum partition index K s . t . product of both subarrays around that partition are co - prime ; Stores the prefix and suffix array product ; Update the prefix array ; Update the suffix array ; Iterate the given array ; Check if prefix [ k ] and suffix [ k + 1 ] are co - prime ; If no index for partition exists , then return - 1 ; Driver code ; Function call"
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 findPartition ( int [ ] nums , int N ) { int [ ] prefix = new int [ N ] ; int [ ] suffix = new int [ N ] ; int i , k ; prefix [ 0 ] = nums [ 0 ] ; for ( i = 1 ; i < N ; i ++ ) { prefix [ i ] = prefix [ i - 1 ] * nums [ i ] ; } suffix [ N - 1 ] = nums [ N - 1 ] ; for ( i = N - 2 ; i >= 0 ; i -- ) { suffix [ i ] = suffix [ i + 1 ] * nums [ i ] ; } for ( k = 0 ; k < N - 1 ; k ++ ) { if ( GCD ( prefix [ k ] , suffix [ k + 1 ] ) == 1 ) { return k ; } } return - 1 ; } static void Main ( ) { int [ ] arr = { 2 , 3 , 4 , 5 } ; int N = arr . Length ; Console . WriteLine ( findPartition ( arr , N ) ) ; } }


"Minimum insertions to form a palindrome with permutations allowed | C # program to find minimum number of insertions to make a string palindrome ; Function will return number of characters to be added ; To store string length ; To store number of characters occurring odd number of times ; To store count of each character ; To store occurrence of each character ; To count characters with odd occurrence ; As one character can be odd return res - 1 but if string is already palindrome return 0 ; Driver program"
Solution in C#:

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


"Minimum insertions to form a palindrome | DP | A C # solution for Dynamic Programming based program to find minimum number insertions needed to make a string palindrome ; A DP function to find minimum number of insertions ; Create a table of size n * n . table [ i ] [ j ] will store minimum number of insertions needed to convert str [ i . . j ] to a palindrome . ; Fill the table ; Return minimum number of insertions for str [ 0. . n - 1 ] ; Driver code"
Solution in C#:

using System ; class GFG { static int findMinInsertionsDP ( char [ ] str , int n ) { int [ , ] table = new int [ n , n ] ; int l , h , gap ; for ( gap = 1 ; gap < n ; ++ gap ) for ( l = 0 , h = gap ; h < n ; ++ l , ++ h ) table [ l , h ] = ( str [ l ] == str [ h ] ) ? table [ l + 1 , h - 1 ] : ( Math . Min ( table [ l , h - 1 ] , table [ l + 1 , h ] ) + 1 ) ; return table [ 0 , n - 1 ] ; } public static void Main ( ) { String str = " geeks " ; Console . Write ( findMinInsertionsDP ( str . ToCharArray ( ) , str . Length ) ) ; } }