"Minimize the cost to make all the adjacent elements distinct in an Array | C # program to find the minimum cost required to make all adjacent elements distinct ; Function that prints minimum cost required ; Dp - table ; Base case Not increasing the first element ; Increasing the first element by 1 ; Increasing the first element by 2 ; Condition if current element is not equal to previous non - increased element ; Condition if current element is not equal to previous element after being increased by 1 ; Condition if current element is not equal to previous element after being increased by 2 ; Take the minimum from all cases ; Finding the minimum cost ; Printing the minimum cost required to make all adjacent elements distinct ; Driver Code"
Solution in C#:

 using System ; class GFG { static void minimumCost ( int [ ] arr , int [ ] cost , int N ) { int [ , ] dp = new int [ N , 3 ] ; dp [ 0 , 0 ] = 0 ; dp [ 0 , 1 ] = cost [ 0 ] ; dp [ 0 , 2 ] = cost [ 0 ] * 2 ; for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 0 ; j < 3 ; j ++ ) { int minimum = ( int ) 1e6 ; if ( j + arr [ i ] != arr [ i - 1 ] ) minimum = Math . Min ( minimum , dp [ i - 1 , 0 ] ) ; if ( j + arr [ i ] != arr [ i - 1 ] + 1 ) minimum = Math . Min ( minimum , dp [ i - 1 , 1 ] ) ; if ( j + arr [ i ] != arr [ i - 1 ] + 2 ) minimum = Math . Min ( minimum , dp [ i - 1 , 2 ] ) ; dp [ i , j ] = j * cost [ i ] + minimum ; } } int ans = ( int ) 1e6 ; for ( int i = 0 ; i < 3 ; i ++ ) ans = Math . Min ( ans , dp [ N - 1 , i ] ) ; Console . Write ( ans + "
 " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 2 , 2 , 3 , 4 } ; int [ ] cost = { 3 , 2 , 5 , 4 , 2 , 1 } ; int N = arr . Length ; minimumCost ( arr , cost , N ) ; } }


"Minimize the cost to split a number | C # implementation of the approach ; check if a number is prime or not ; run a loop upto square root of x ; Function to return the minimized cost ; If n is prime ; If n is odd and can be split into ( prime + 2 ) then cost will be 1 + 1 = 2 ; Every non - prime even number can be expressed as the sum of two primes ; n is odd so n can be split into ( 3 + even ) further even part can be split into ( prime + prime ) ( 3 + prime + prime ) will cost 3 ; Driver code"
Solution in C#:

using System ; public class GFG { static bool isPrime ( int x ) { for ( int i = 2 ; i * i <= x ; i ++ ) { if ( x % i == 0 ) return false ; } return true ; } static int minimumCost ( int n ) { if ( isPrime ( n ) ) return 1 ; if ( n % 2 == 1 && isPrime ( n - 2 ) ) return 2 ; if ( n % 2 == 0 ) return 2 ; return 3 ; } public static void Main ( String [ ] args ) { int n = 6 ; Console . WriteLine ( minimumCost ( n ) ) ; } }


"Minimize the difference between minimum and maximum elements | C # program to minimize the difference between minimum and maximum elements ; Function to minimize the difference between minimum and maximum elements ; Find max and min elements of the array ; Check whether the difference between the max and min element is less than or equal to k or not ; Calculate average of max and min ; If the array element is greater than the average then decrease it by k ; If the array element is smaller than the average then increase it by k ; Find max and min of the modified array ; return the new difference ; Driver code"
Solution in C#:

using System ; using System . Linq ; class GFG { static int minimizeDiff ( int [ ] arr , int n , int k ) { int max = arr . Max ( ) ; int min = arr . Min ( ) ; if ( ( max - min ) <= k ) { return ( max - min ) ; } int avg = ( max + min ) / 2 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > avg ) { arr [ i ] -= k ; } else { arr [ i ] += k ; } } max = arr . Max ( ) ; min = arr . Min ( ) ; return ( max - min ) ; } public static void Main ( ) { int [ ] arr = { 3 , 16 , 12 , 9 , 20 } ; int n = 5 ; int k = 3 ; Console . WriteLine ( " Max ▁ height ▁ difference ▁ = ▁ " + minimizeDiff ( arr , n , k ) ) ; } }


"Minimize the maximum difference between adjacent elements in an array | C # implementation to find the minimum of the maximum difference of the adjacent elements after removing K elements from the array ; Function to find the minimum of the maximum difference of the adjacent elements after removing K elements from the array ; Initialising the minimum difference ; Iterating over all subarrays of size n - k ; Maximum difference after removing elements ; Minimum Adjacent Difference ; Driver Code"
Solution in C#:

using System ; class GFG { static int minimumAdjacentDifference ( int [ ] a , int n , int k ) { int minDiff = int . MaxValue ; for ( int i = 0 ; i <= k ; i ++ ) { int maxDiff = int . MinValue ; for ( int j = 0 ; j < n - k - 1 ; j ++ ) { for ( int p = i ; p <= i + j ; p ++ ) { maxDiff = Math . Max ( maxDiff , a [ p + 1 ] - a [ p ] ) ; } } minDiff = Math . Min ( minDiff , maxDiff ) ; } return minDiff ; } public static void Main ( string [ ] args ) { int n = 5 ; int k = 2 ; int [ ] a = { 3 , 7 , 8 , 10 , 14 } ; Console . WriteLine ( minimumAdjacentDifference ( a , n , k ) ) ; } }


"Minimize the maximum difference between adjacent elements in an array | C # implementation to find the minimum of the maximum difference of the adjacent elements after removing K elements from the array ; Function to find the minimum of the maximum difference of the adjacent elements after removing K elements from the array ; Initialising the minimum difference ; Traversing over subsets in iterative manner ; Number of elements to be taken in the subset ON bits of i represent elements not to be removed ; If the removed set is of size k ; Creating the new array after removing elements ; Maximum difference of adjacent elements of remaining array ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int minimumAdjacentDifference ( int [ ] a , int n , int k ) { int minDiff = int . MaxValue ; for ( int i = 0 ; i < ( 1 << n ) ; i ++ ) { int cnt = countSetBits ( i ) ; if ( cnt == n - k ) { List < int > temp = new List < int > ( ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( i & ( 1 << j ) ) != 0 ) temp . Add ( a [ j ] ) ; } int maxDiff = int . MinValue ; for ( int j = 0 ; j < temp . Count - 1 ; j ++ ) { maxDiff = Math . Max ( maxDiff , temp [ j + 1 ] - temp [ j ] ) ; } minDiff = Math . Min ( minDiff , maxDiff ) ; } } return minDiff ; } static int countSetBits ( int x ) { int setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } public static void Main ( String [ ] args ) { int n = 5 ; int k = 2 ; int [ ] a = { 3 , 7 , 8 , 10 , 14 } ; Console . WriteLine ( minimumAdjacentDifference ( a , n , k ) ) ; } }


"Minimize the maximum difference of any pair by doubling odd elements and reducing even elements by half | C # program for the above approach ; Function to minimize the maximum difference between any pair of elements of the array by the given operations ; Traverse the array ; If current element is even ; Insert it into even ; Otherwise ; Make it even by multiplying by 2 and insert it into set ; Calculate difference between first and the last element of the set ; Iterate until difference is minimized ; Erase the current element ; Reduce current element by half and insert it into the Set ; Update difference ; Return the resultant difference ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static int minimumMaxDiff ( int [ ] nums ) { HashSet < int > s = new HashSet < int > ( ) ; for ( int i = 0 ; i < nums . Length ; i ++ ) { if ( nums [ i ] % 2 == 0 ) s . Add ( nums [ i ] ) ; else s . Add ( nums [ i ] * 2 ) ; } int res = s . Last ( ) - s . First ( ) ; while ( s . Last ( ) % 2 == 0 ) { int x = s . Last ( ) ; s . Remove ( x ) ; s . Add ( x / 2 ) ; res = Math . Min ( res , s . Last ( ) - s . First ( ) ) ; } return res ; } static public void Main ( ) { int [ ] arr = new int [ ] { 1 , 2 , 5 , 9 } ; Console . WriteLine ( minimumMaxDiff ( arr ) ) ; } }


"Minimize the maximum distance between adjacent points after adding K points anywhere in between | C # program for the above approach ; Function to check if it is possible to add K points such that the maximum distance between adjacent points is D ; Stores the count of point used ; Iterate over all given points ; Add number of points required to be placed between ith and ( i + 1 ) th point ; Return answer ; Function to find the minimum value of maximum distance between adjacent points after adding K points any where between ; Stores the lower bound and upper bound of the given range ; Perform binary search ; Find the middle value ; Update the current range to lower half ; Update the current range to upper half ; Driver Code"
Solution in C#:

using System ; public class GFG { public static bool isPossible ( double D , int [ ] arr , int N , int K ) { int used = 0 ; for ( int i = 0 ; i < N - 1 ; ++ i ) { used += ( int ) ( ( arr [ i + 1 ] - arr [ i ] ) / D ) ; } return used <= K ; } public static double minMaxDist ( int [ ] stations , int N , int K ) { double low = 0 , high = 1e8 ; while ( high - low > 1e-6 ) { double mid = ( low + high ) / 2.0 ; if ( isPossible ( mid , stations , N , K ) ) { high = mid ; } else { low = mid ; } } return low ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; int K = 9 ; int N = arr . Length ; Console . Write ( " { 0 : F1 } " , minMaxDist ( arr , N , K ) ) ; } }


"Minimize the maximum element in constructed Array with sum divisible by K | C # program for the above approach . ; Function to find smallest maximum number in an array whose sum is divisible by K . ; Minimum possible sum possible for an array of size N such that its sum is divisible by K ; If sum is not divisible by N ; If sum is divisible by N ; Driver code ."
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int smallestMaximum ( int N , int K ) { int sum = ( ( N + K - 1 ) / K ) * K ; if ( sum % N != 0 ) return ( sum / N ) + 1 ; else return sum / N ; } public static void Main ( ) { int N = 4 ; int K = 3 ; Console . Write ( smallestMaximum ( N , K ) ) ; } }


"Minimize the maximum minimum difference after one removal from array | C # implementation of the approach ; Function to return the minimum required difference ; If current element is greater than max ; max will become secondMax ; Update the max ; If current element is greater than secondMax but smaller than max ; Update the secondMax ; If current element is smaller than min ; min will become secondMin ; Update the min ; If current element is smaller than secondMin but greater than min ; Update the secondMin ; Minimum of the two possible differences ; Driver code"
Solution in C#:

using System ; public class GFG { static int findMinDifference ( int [ ] arr , int n ) { int min , secondMin , max , secondMax ; min = secondMax = ( arr [ 0 ] < arr [ 1 ] ) ? arr [ 0 ] : arr [ 1 ] ; max = secondMin = ( arr [ 0 ] < arr [ 1 ] ) ? arr [ 1 ] : arr [ 0 ] ; for ( int i = 2 ; i < n ; i ++ ) { if ( arr [ i ] > max ) { secondMax = max ; max = arr [ i ] ; } else if ( arr [ i ] > secondMax ) { secondMax = arr [ i ] ; } else if ( arr [ i ] < min ) { secondMin = min ; min = arr [ i ] ; } else if ( arr [ i ] < secondMin ) { secondMin = arr [ i ] ; } } int diff = Math . Min ( max - secondMin , secondMax - min ) ; return diff ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 4 , 3 , 4 } ; int n = arr . Length ; Console . WriteLine ( findMinDifference ( arr , n ) ) ; } }


"Minimize the maximum minimum difference after one removal from array | C # implementation of the approach ; Function to return the minimum required difference ; Sort the given array ; When minimum element is removed ; When maximum element is removed ; Return the minimum of diff1 and diff2 ; Driver Code"
Solution in C#:

using System ; public class GFG { static int findMinDifference ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int diff1 = arr [ n - 1 ] - arr [ 1 ] ; int diff2 = arr [ n - 2 ] - arr [ 0 ] ; return Math . Min ( diff1 , diff2 ) ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 4 , 3 , 4 } ; int n = arr . Length ; Console . Write ( findMinDifference ( arr , n ) ) ; } }


"Minimize the non | C # implementation to minimize the non - zero elements in the array ; Function to minimize the non - zero elements in the given array ; To store the min pos needed ; Loop to iterate over the elements of the given array ; If current position A [ i ] is occupied the we can place A [ i ] , A [ i + 1 ] and A [ i + 2 ] elements together at A [ i + 1 ] if exists . ; Driver code ; Function Call"
Solution in C#:

using System ; class GFG { static int minOccupiedPosition ( int [ ] A , int n ) { int minPos = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( A [ i ] > 0 ) { ++ minPos ; i += 2 ; } } return minPos ; } static void Main ( ) { int [ ] A = { 8 , 0 , 7 , 0 , 0 , 6 } ; int n = A . Length ; Console . WriteLine ( minOccupiedPosition ( A , n ) ) ; } }


"Minimize the number by changing at most K digits | C # implementation of the approach ; Function to return the minimized number ; Total digits in the number ; If the string is empty or there are no operations to perform ; "0" is a valid number ; If the first digit is not already 1 then update it to 1 and decrement k ; While there are operations left and the number can still be updated ; If the current digit is not already 0 then update it to 0 and decrement k ; Return the minimised number ; Driver code"
Solution in C#:

using System ; class GFG { static String minNum ( char [ ] num , int k ) { int len = num . Length ; if ( len == 0 k == 0 ) { return String . Join ( " " , num ) ; } if ( len == 1 ) return "0" ; if ( num [ 0 ] != '1' ) { num [ 0 ] = '1' ; k -- ; } int i = 1 ; while ( k > 0 && i < len ) { if ( num [ i ] != '0' ) { num [ i ] = '0' ; k -- ; } i ++ ; } return String . Join ( " " , num ) ; } public static void Main ( String [ ] args ) { String num = "91945" ; int k = 3 ; Console . WriteLine ( minNum ( num . ToCharArray ( ) , k ) ) ; } }


"Minimize the number of replacements to get a string with same number of ' a ' , ' b ' and ' c ' in it | C # program to minimize the number of replacements to get a string with same number of a , b and c in it ; Function to count numbers ; Count the number of ' a ' , ' b ' and ' c ' in string ; If equal previously ; If not a multiple of 3 ; Increase the number of a ' s ▁ by ▁ ▁ removing ▁ extra ▁ ' b ' and ;c; ; Check if it is ' b ' and it more than n / 3 ; Check if it is ' c ' and it more than n / 3 ; Increase the number of b ' s ▁ by ▁ ▁ removing ▁ extra ▁ ' c ' ; Check if it is ' c ' and it more than n / 3 ; Increase the number of c 's from back ; Check if it is ' a ' and it more than n / 3 ; Increase the number of b 's from back ; Check if it is ' a ' and it more than n / 3 ; Increase the number of c 's from back ; Check if it is ' b ' and it more than n / 3 ; Driver Code"
Solution in C#:

using System ; using System . Text ; class GFG { static string lexoSmallest ( string str , int n ) { int ca = 0 , cb = 0 , cc = 0 ; StringBuilder s = new StringBuilder ( str ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( s [ j ] == ' a ' ) ca ++ ; else if ( s [ j ] == ' b ' ) cb ++ ; else cc ++ ; } if ( ca == cb && cb == cc ) { return s . ToString ( ) ; } int cnt = n / 3 ; if ( cnt * 3 != n ) { return " - 1" ; } int i = 0 ; while ( ca < cnt && i < n ) { if ( s [ i ] == ' b ' && cb > cnt ) { cb -- ; s [ i ] = ' a ' ; ca ++ ; } else if ( s [ i ] == ' c ' && cc > cnt ) { cc -- ; s [ i ] = ' a ' ; ca ++ ; } i ++ ; } i = 0 ; while ( cb < cnt && i < n ) { if ( s [ i ] == ' c ' && cc > cnt ) { cc -- ; s [ i ] = '1' ; cb ++ ; } i ++ ; } i = n - 1 ; while ( cc < cnt && i >= 0 ) { if ( s [ i ] == ' a ' && ca > cnt ) { ca -- ; s [ i ] = ' c ' ; cc ++ ; } i -- ; } i = n - 1 ; while ( cb < cnt && i >= 0 ) { if ( s [ i ] == ' a ' && ca > cnt ) { ca -- ; s [ i ] = ' b ' ; cb ++ ; } i -- ; } i = n - 1 ; while ( cc < cnt && i >= 0 ) { if ( s [ i ] == ' b ' && cb > cnt ) { cb -- ; s [ i ] = ' c ' ; cc ++ ; } i -- ; } return s . ToString ( ) ; } public static void Main ( string [ ] args ) { string s = " aaaaaa " ; int n = s . Length ; Console . Write ( lexoSmallest ( s , n ) ) ; } }


"Minimize the number of steps required to reach the end of the array | C # implementation of the above approach ; variable to store states of dp ; variable to check if a given state has been solved ; Function to find the minimum number of steps required to reach the end of the array ; base case ; to check if a state has been solved ; required recurrence relation ; returning the value ; Driver code"
Solution in C#:

using System ; class GFG { static int maxLen = 10 ; static int maskLen = 130 ; static int [ , ] dp = new int [ maxLen , maskLen ] ; static bool [ , ] v = new bool [ maxLen , maskLen ] ; static int minSteps ( int [ ] arr , int i , int mask , int n ) { if ( i == n - 1 ) { return 0 ; } if ( i > n - 1 i < 0 ) { return 9999999 ; } if ( ( mask >> i ) % 2 == 1 ) { return 9999999 ; } if ( v [ i , mask ] ) { return dp [ i , mask ] ; } v [ i , mask ] = true ; dp [ i , mask ] = 1 + Math . Min ( minSteps ( arr , i - arr [ i ] , ( mask | ( 1 << i ) ) , n ) , minSteps ( arr , i + arr [ i ] , ( mask | ( 1 << i ) ) , n ) ) ; return dp [ i , mask ] ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 2 , 2 , 1 , 1 } ; int n = arr . Length ; int ans = minSteps ( arr , 0 , 0 , n ) ; if ( ans >= 9999999 ) { Console . WriteLine ( - 1 ) ; } else { Console . WriteLine ( ans ) ; } } }


"Minimize the number of steps required to reach the end of the array | Set 2 | A C # implementation of the approach ; Function to return the minimum steps required to reach the end of the given array ; Array to determine whether a cell has been visited before ; Queue for bfs ; Push the source i . e . index 0 ; Variable to store the depth of search ; BFS algorithm ; Current queue size ; Top - most element of queue ; Base case ; If we reach the destination i . e . index ( n - 1 ) ; Marking the cell visited ; Pushing the adjacent nodes i . e . indices reachable from the current index ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int minSteps ( int [ ] arr , int n ) { Boolean [ ] v = new Boolean [ n ] ; Queue < int > q = new Queue < int > ( ) ; q . Enqueue ( 0 ) ; int depth = 0 ; while ( q . Count > 0 ) { int x = q . Count ; while ( x -- > 0 ) { int i = q . Peek ( ) ; q . Dequeue ( ) ; if ( v [ i ] ) continue ; if ( i == n - 1 ) return depth ; v [ i ] = true ; if ( i + arr [ i ] < n ) q . Enqueue ( i + arr [ i ] ) ; if ( i - arr [ i ] >= 0 ) q . Enqueue ( i - arr [ i ] ) ; } depth ++ ; } return - 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 1 , 1 , 1 , 1 } ; int n = arr . Length ; Console . WriteLine ( minSteps ( arr , n ) ) ; } }


"Minimize the number of strictly increasing subsequences in an array | Set 2 | C # program to implement the above approach ; Function to find the number of strictly increasing subsequences in an array ; Sort the array ; Stores readonly count of subsequences ; Traverse the array ; Stores current element ; Stores frequency of the current element ; Count frequency of the current element ; If current element frequency is greater than count ; Print the readonly count ; Driver Code ; Given array ; Size of the array ; Function call to find the number of strictly increasing subsequences"
Solution in C#:

using System ; public class GFG { static void minimumIncreasingSubsequences ( int [ ] arr , int N ) { Array . Sort ( arr ) ; int count = 0 ; int i = 0 ; while ( i < N ) { int x = arr [ i ] ; int freqX = 0 ; while ( i < N && arr [ i ] == x ) { freqX ++ ; i ++ ; } count = Math . Max ( count , freqX ) ; } Console . Write ( count ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 1 , 2 , 1 , 4 , 3 } ; int N = arr . Length ; minimumIncreasingSubsequences ( arr , N ) ; } }


"Minimize the number of weakly connected nodes | C # code to minimize the number of weakly connected nodes ; Set of nodes which are traversed in each launch of the DFS ; Function traversing the graph using DFS approach and updating the set of nodes ; Building a undirected graph ; Computes the minimum number of disconnected components when a bi - directed graph is converted to a undirected graph ; Declaring and initializing a visited array ; We check if each node is visited once or not ; We only launch DFS from a node iff it is unvisited . ; Clearing the set of nodes on every relaunch of DFS ; Relaunching DFS from an unvisited node . ; Iterating over the node set to count the number of nodes visited after making the graph directed and storing it in the variable count . If count / 2 == number of nodes - 1 , then increment count by 1. ; Driver Code ; Building graph in the form of a adjacency list"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static HashSet < int > node = new HashSet < int > ( ) ; static List < int > [ ] Graph = new List < int > [ 10001 ] ; static void dfs ( bool [ ] visit , int src ) { visit [ src ] = true ; node . Add ( src ) ; int len = Graph [ src ] . Count ; for ( int i = 0 ; i < len ; i ++ ) if ( ! visit [ Graph [ src ] [ i ] ] ) dfs ( visit , Graph [ src ] [ i ] ) ; } static void buildGraph ( int [ ] x , int [ ] y , int len ) { for ( int i = 0 ; i < len ; i ++ ) { int p = x [ i ] ; int q = y [ i ] ; Graph [ p ] . Add ( q ) ; Graph [ q ] . Add ( p ) ; } } static int compute ( int n ) { bool [ ] visit = new bool [ n + 5 ] ; Array . Fill ( visit , false ) ; int number_of_nodes = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! visit [ i ] ) { node . Clear ( ) ; dfs ( visit , i ) ; int count = 0 ; foreach ( int it in node ) count += Graph [ ( it ) ] . Count ; count /= 2 ; if ( count == node . Count - 1 ) number_of_nodes ++ ; } } return number_of_nodes ; } static void Main ( string [ ] args ) { int n = 6 ; for ( int i = 0 ; i < 10001 ; i ++ ) { Graph [ i ] = new List < int > ( ) ; } int [ ] x = { 1 , 1 , 4 , 4 , 0 , 0 , 0 , 0 } ; int [ ] y = { 2 , 3 , 5 , 6 , 0 , 0 , 0 , 0 } ; buildGraph ( x , y , n ) ; Console . Write ( compute ( n ) + " ▁ weakly ▁ connected ▁ nodes " ) ; } }


"Minimize the sum after choosing elements from the given three arrays | C # implementation of the above approach ; Function to return the minimized sum ; If all the indices have been used ; If this value is pre - calculated then return its value from dp array instead of re - computing it ; If A [ i - 1 ] was chosen previously then only B [ i ] or C [ i ] can chosen now choose the one which leads to the minimum sum ; If B [ i - 1 ] was chosen previously then only A [ i ] or C [ i ] can chosen now choose the one which leads to the minimum sum ; If C [ i - 1 ] was chosen previously then only A [ i ] or B [ i ] can chosen now choose the one which leads to the minimum sum ; Driver code ; Initialize the dp [ ] [ ] array ; min ( start with A [ 0 ] , start with B [ 0 ] , start with C [ 0 ] )"
Solution in C#:

using System ; class GFG { static int SIZE = 3 ; static int N = 3 ; static int minSum ( int [ ] A , int [ ] B , int [ ] C , int i , int n , int curr , int [ , ] dp ) { if ( n <= 0 ) return 0 ; if ( dp [ n , curr ] != - 1 ) return dp [ n , curr ] ; if ( curr == 0 ) { return dp [ n , curr ] = Math . Min ( B [ i ] + minSum ( A , B , C , i + 1 , n - 1 , 1 , dp ) , C [ i ] + minSum ( A , B , C , i + 1 , n - 1 , 2 , dp ) ) ; } if ( curr == 1 ) return dp [ n , curr ] = Math . Min ( A [ i ] + minSum ( A , B , C , i + 1 , n - 1 , 0 , dp ) , C [ i ] + minSum ( A , B , C , i + 1 , n - 1 , 2 , dp ) ) ; return dp [ n , curr ] = Math . Min ( A [ i ] + minSum ( A , B , C , i + 1 , n - 1 , 0 , dp ) , B [ i ] + minSum ( A , B , C , i + 1 , n - 1 , 1 , dp ) ) ; } public static void Main ( ) { int [ ] A = { 1 , 50 , 1 } ; int [ ] B = { 50 , 50 , 50 } ; int [ ] C = { 50 , 50 , 50 } ; int [ , ] dp = new int [ SIZE , N ] ; for ( int i = 0 ; i < SIZE ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) dp [ i , j ] = - 1 ; Console . WriteLine ( Math . Min ( A [ 0 ] + minSum ( A , B , C , 1 , SIZE - 1 , 0 , dp ) , Math . Min ( B [ 0 ] + minSum ( A , B , C , 1 , SIZE - 1 , 1 , dp ) , C [ 0 ] + minSum ( A , B , C , 1 , SIZE - 1 , 2 , dp ) ) ) ) ; } }


"Minimize the sum of differences of consecutive elements after removing exactly K elements | C # implementation of the above approach . ; function to find minimum sum ; variable to store final answer and initialising it with the values when 0 elements is removed from the left and K from the right . ; loop to simulate removal of elements ; removing i elements from the left and and K - i elements from the right and updating the answer correspondingly ; returning final answer ; Driver function ; input values ; calling the required function ;"
Solution in C#:

using System ; class GFG { static int findSum ( int [ ] arr , int n , int k ) { int ans = arr [ n - k - 1 ] - arr [ 0 ] ; for ( int i = 1 ; i <= k ; i ++ ) { ans = Math . Min ( arr [ n - 1 - ( k - i ) ] - arr [ i ] , ans ) ; } return ans ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 100 , 120 , 140 } ; int k = 2 ; int n = arr . Length ; Console . WriteLine ( findSum ( arr , n , k ) ) ; } }


"Minimize the sum of differences of consecutive elements after removing exactly K elements | C # implementation of the above approach . ; states of DP ; function to find minimum sum ; base - case ; if state is solved before , return ; marking the state as solved ; recurrence relation ; Driver function ; input values ; calling the required function ;"
Solution in C#:

using System ; class GFG { static int N = 100 ; static int [ , ] dp = new int [ N , N ] ; static int [ , ] vis = new int [ N , N ] ; static int findSum ( int [ ] arr , int n , int k , int l , int r ) { if ( ( l ) + ( n - 1 - r ) == k ) return arr [ r ] - arr [ l ] ; if ( vis [ l , r ] == 1 ) return dp [ l , r ] ; vis [ l , r ] = 1 ; dp [ l , r ] = Math . Min ( findSum ( arr , n , k , l , r - 1 ) , findSum ( arr , n , k , l + 1 , r ) ) ; return dp [ l , r ] ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 100 , 120 , 140 } ; int k = 2 ; int n = arr . Length ; Console . WriteLine ( findSum ( arr , n , k , 0 , n - 1 ) ) ; } }


"Minimize the sum of digits of A and B such that A + B = N | C # implementation of the approach ; Function to return the minimum possible sum of digits of A and B such that A + B = n ; Find the sum of digits of n ; If num is a power of 10 ; Driver code"
Solution in C#:

using System ; class GFG { static int minSum ( int n ) { int sum = 0 ; while ( n > 0 ) { sum += ( n % 10 ) ; n /= 10 ; } if ( sum == 1 ) return 10 ; return sum ; } public static void Main ( String [ ] args ) { int n = 1884 ; Console . Write ( minSum ( n ) ) ; } }


"Minimize the sum of pair which upon removing divides the Array into 3 subarrays | C # implementation of the above approach ; Function to find minimum possible sum of pair which breaks the array into 3 non - empty subarrays ; prefixMin [ i ] contains minimum element till i ; Driver Code ; Given array"
Solution in C#:

 using System ; public class GFG { static int minSumPair ( int [ ] arr , int N ) { if ( N < 5 ) { return - 1 ; } int [ ] prefixMin = new int [ N ] ; prefixMin [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < N - 1 ; i ++ ) { prefixMin [ i ] = Math . Min ( arr [ i ] , prefixMin [ i - 1 ] ) ; } int ans = int . MaxValue ; for ( int i = 3 ; i < N - 1 ; i ++ ) { ans = Math . Min ( ans , arr [ i ] + prefixMin [ i - 2 ] ) ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 2 , 4 , 6 , 3 , 7 } ; int N = arr . Length ; Console . Write ( minSumPair ( arr , N ) + "
 " ) ; } }


"Minimize the sum of product of two arrays with permutations allowed | C # program to calculate minimum sum of product of two arrays . ; Returns minimum sum of product of two arrays with permutations allowed ; Sort A and B so that minimum and maximum value can easily be fetched . ; Multiplying minimum value of A and maximum value of B ; Driven Code"
Solution in C#:

using System ; class GFG { static long minValue ( int [ ] a , int [ ] b , int n ) { Array . Sort ( a ) ; Array . Sort ( b ) ; long result = 0 ; for ( int i = 0 ; i < n ; i ++ ) result += ( a [ i ] * b [ n - i - 1 ] ) ; return result ; } public static void Main ( ) { int [ ] a = { 3 , 1 , 1 } ; int [ ] b = { 6 , 5 , 4 } ; int n = a . Length ; Console . Write ( minValue ( a , b , n ) ) ; } }


"Minimize the sum of roots of a given polynomial | C # program to find minimum sum of roots of a given polynomial ; resultant vector ; a vector that store indices of the positive elements ; a vector that store indices of the negative elements ; Case - 1 : ; Case - 2 : ; Case - 3 : ; Case - 4 : ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void getMinimumSum ( int [ ] arr , int n ) { List < int > res = new List < int > ( ) ; List < int > pos = new List < int > ( ) ; List < int > neg = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) pos . Add ( i ) ; else if ( arr [ i ] < 0 ) neg . Add ( i ) ; } if ( pos . Count >= 2 && neg . Count >= 2 ) { int posMax = Int32 . MinValue , posMaxIdx = - 1 ; int posMin = Int32 . MaxValue , posMinIdx = - 1 ; int negMax = Int32 . MinValue , negMaxIdx = - 1 ; int negMin = Int32 . MaxValue , negMinIdx = - 1 ; for ( int i = 0 ; i < pos . Count ; i ++ ) { if ( arr [ pos [ i ] ] > posMax ) { posMaxIdx = pos [ i ] ; posMax = arr [ posMaxIdx ] ; } } for ( int i = 0 ; i < pos . Count ; i ++ ) { if ( arr [ pos [ i ] ] < posMin && pos [ i ] != posMaxIdx ) { posMinIdx = pos [ i ] ; posMin = arr [ posMinIdx ] ; } } for ( int i = 0 ; i < neg . Count ; i ++ ) { if ( Math . Abs ( arr [ neg [ i ] ] ) > negMax ) { negMaxIdx = neg [ i ] ; negMax = Math . Abs ( arr [ negMaxIdx ] ) ; } } for ( int i = 0 ; i < neg . Count ; i ++ ) { if ( Math . Abs ( arr [ neg [ i ] ] ) < negMin && neg [ i ] != negMaxIdx ) { negMinIdx = neg [ i ] ; negMin = Math . Abs ( arr [ negMinIdx ] ) ; } } double posVal = - 1.0 * ( double ) posMax / ( double ) posMin ; double negVal = - 1.0 * ( double ) negMax / ( double ) negMin ; if ( posVal < negVal ) { res . Add ( arr [ posMinIdx ] ) ; res . Add ( arr [ posMaxIdx ] ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i != posMinIdx && i != posMaxIdx ) { res . Add ( arr [ i ] ) ; } } } else { res . Add ( arr [ negMinIdx ] ) ; res . Add ( arr [ negMaxIdx ] ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i != negMinIdx && i != negMaxIdx ) { res . Add ( arr [ i ] ) ; } } } for ( int i = 0 ; i < res . Count ; i ++ ) { Console . Write ( res [ i ] + " ▁ " ) ; } Console . WriteLine ( ) ; } else if ( pos . Count >= 2 ) { int posMax = Int32 . MinValue , posMaxIdx = - 1 ; int posMin = Int32 . MaxValue , posMinIdx = - 1 ; for ( int...


"Minimize the sum of the array according the given condition | C # implementation of the above approach ; Function to return the minimum sum ; sort the array to find the minimum element ; finding the number to divide ; Checking to what instance the sum has decreased ; getting the max difference ; Driver Code"
Solution in C#:

using System ; class GFG { static void findMin ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; Array . Sort ( arr ) ; int min = arr [ 0 ] ; int max = 0 ; for ( int i = n - 1 ; i >= 1 ; i -- ) { int num = arr [ i ] ; int total = num + min ; int j ; for ( j = 2 ; j <= num ; j ++ ) { if ( num % j == 0 ) { int d = j ; int now = ( num / d ) + ( min * d ) ; int reduce = total - now ; if ( reduce > max ) max = reduce ; } } } Console . WriteLine ( sum - max ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . Length ; findMin ( arr , n ) ; } }


"Minimize the sum of the squares of the sum of elements of each group the array is divided into | C # implementation of the approach ; Function to return the minimized sum ; Sort the array to pair the elements ; Variable to hold the answer ; Pair smallest with largest , second smallest with second largest , and so on ; Driver code"
Solution in C#:

using System ; class GFG { static int findAnswer ( int n , int [ ] arr ) { Array . Sort ( arr ) ; int sum = 0 ; for ( int i = 0 ; i < n / 2 ; ++ i ) { sum += ( arr [ i ] + arr [ n - i - 1 ] ) * ( arr [ i ] + arr [ n - i - 1 ] ) ; } return sum ; } static void Main ( ) { int [ ] arr = { 53 , 28 , 143 , 5 } ; int n = arr . Length ; Console . WriteLine ( findAnswer ( n , arr ) ) ; } }


"Minimize the value of N by applying the given operations | C # implementation of the above approach ; function to return the product of distinct prime factors of a number ; find distinct prime ; Driver code"
Solution in C#:

class GFG { static int minimum ( int n ) { int product = 1 ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { while ( n % i == 0 ) n = n / i ; product = product * i ; } } if ( n >= 2 ) product = product * n ; return product ; } static void Main ( ) { int n = 20 ; System . Console . WriteLine ( minimum ( n ) ) ; } }


"Minimize total cost without repeating same task in two consecutive iterations | C # implementation of the above approach ; Function to return the minimum cost for N iterations ; Construct the dp table ; 1 st row of dp table will be equal to the 1 st of cost matrix ; Iterate through all the rows ; To iterate through the columns of current row ; Initialize val as infinity ; To iterate through the columns of previous row ; Fill the dp matrix ; Returning the minimum value ; Driver code ; Number of iterations ; Number of tasks ; Cost matrix"
Solution in C#:

using System ; class GFG { static int findCost ( int [ , ] cost_mat , int N , int M ) { int [ , ] dp = new int [ N , M ] ; for ( int i = 0 ; i < M ; i ++ ) dp [ 0 , i ] = cost_mat [ 0 , i ] ; for ( int row = 1 ; row < N ; row ++ ) { for ( int curr_col = 0 ; curr_col < M ; curr_col ++ ) { int val = 999999999 ; for ( int prev_col = 0 ; prev_col < M ; prev_col ++ ) { if ( curr_col != prev_col ) val = Math . Min ( val , dp [ row - 1 , prev_col ] ) ; } dp [ row , curr_col ] = val + cost_mat [ row , curr_col ] ; } } int ans = int . MaxValue ; for ( int i = 0 ; i < M ; i ++ ) ans = Math . Min ( ans , dp [ N - 1 , i ] ) ; return ans ; } public static void Main ( string [ ] args ) { int N = 4 ; int M = 4 ; int [ , ] cost_mat = { { 4 , 5 , 3 , 2 } , { 6 , 2 , 8 , 1 } , { 6 , 2 , 2 , 1 } , { 0 , 5 , 5 , 1 } } ; Console . WriteLine ( findCost ( cost_mat , N , M ) ) ; } }


"Minimize value of | A | C # program for the above approach ; Function to find the minimum possible value of | A - X | + | B - Y | + | C - Z | such that X * Y = Z for given A , B and C ; Stores the minimum value of | A - X | + | B - Y | + | C - Z | such that X * Y = Z ; Iterate over all values of i in the range [ 1 , 2 * C ] ; Iterate over all values of j such that i * j <= 2 * c ; Update the value of ans ; Return answer ; Driver Code"
Solution in C#:

using System ; class GFG { public static int minimizeCost ( int A , int B , int C ) { int ans = A + B + C ; for ( int i = 1 ; i <= 2 * C ; i ++ ) { int j = 0 ; while ( i * j <= 2 * C ) { ans = Math . Min ( ans , Math . Abs ( A - i ) + Math . Abs ( B - j ) + Math . Abs ( i * j - C ) ) ; j ++ ; } } return ans ; } public static void Main ( String [ ] args ) { int A = 19 , B = 28 , C = 522 ; Console . Write ( minimizeCost ( A , B , C ) ) ; } }


"Minimizing array sum by applying XOR operation on all elements of the array | C # implementation of the approach ; Function to return the minimized sum ; To store the frequency of bit in every element ; Finding element X ; Taking XOR of elements and finding sum ; Driver code"
Solution in C#:

using System ; class GFG { static int MAX = 25 ; static int getMinSum ( int [ ] arr , int n ) { int [ ] bits_count = new int [ MAX ] ; int max_bit = 0 , sum = 0 , ans = 0 ; for ( int d = 0 ; d < n ; d ++ ) { int e = arr [ d ] , f = 0 ; while ( e > 0 ) { int rem = e % 2 ; e = e / 2 ; if ( rem == 1 ) { bits_count [ f ] += rem ; } f ++ ; } max_bit = Math . Max ( max_bit , f ) ; } for ( int d = 0 ; d < max_bit ; d ++ ) { int temp = ( int ) Math . Pow ( 2 , d ) ; if ( bits_count [ d ] > n / 2 ) ans = ans + temp ; } for ( int d = 0 ; d < n ; d ++ ) { arr [ d ] = arr [ d ] ^ ans ; sum = sum + arr [ d ] ; } return sum ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 5 , 7 , 11 , 15 } ; int n = arr . Length ; Console . WriteLine ( getMinSum ( arr , n ) ) ; } }


"Minimum , maximum and average price values for all the items of given type | C # implementation of the approach ; To store an item ; To store the minimum and the maximum price for the item ; To store the total number of items of the current type and the total cost of buying them ; Initializing an element ; Function to find the minimum , the maximum and the average price for every item ; To store the distinct items ; For every item ; If the current item has aready been purchased earlier from a different shop ; Get the item ; Update its minimum and maximum price so far ; Increment the total count of the current item ; Add the current price to the sum ; The item has been purchased for the first time ; Print all the items with their minimum , maximum and average prices ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public class Item { public int min , max ; public int total , sum ; public Item ( int price ) { min = price ; max = price ; total = 1 ; this . sum = price ; } } static void findPrices ( String [ ] item , int [ ] price , int n ) { Dictionary < String , Item > map = new Dictionary < String , Item > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( map . ContainsKey ( item [ i ] ) ) { Item currItem = map [ item [ i ] ] ; currItem . min = Math . Min ( currItem . min , price [ i ] ) ; currItem . max = Math . Max ( currItem . max , price [ i ] ) ; currItem . total ++ ; currItem . sum += price [ i ] ; } else { Item currItem = new Item ( price [ i ] ) ; map . Add ( item [ i ] , currItem ) ; } } Console . WriteLine ( " Item ▁ Min ▁ Max ▁ Average " ) ; foreach ( KeyValuePair < String , Item > ob in map ) { String key = ob . Key ; Item currItem = ob . Value ; Console . WriteLine ( key + " ▁ " + currItem . min + " ▁ " + currItem . max + " ▁ " + ( ( float ) currItem . sum / ( float ) currItem . total ) ) ; } } public static void Main ( String [ ] args ) { String [ ] item = { " toy " , " pen " , " notebook " , " pen " } ; int n = item . Length ; int [ ] price = { 2 , 1 , 3 , 2 } ; findPrices ( item , price , n ) ; } }


"Minimum Bipartite Groups | Function to find the height sizeof the current component with vertex s ; Visit the current Node ; Call DFS recursively to find the maximum height of current CC ; If the node is not visited then the height recursively for next element ; Function to find the minimum Groups ; Initialise with visited array ; To find the minimum groups ; Traverse all the non visited Node and calculate the height of the tree with current node as a head ; If the current is not visited therefore , we get another CC ; Return the minimum bipartite matching ; Function that adds the current edges in the given graph ; Drivers Code ; Adjacency List ; Adding edges to List"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int height ( int s , List < int > [ ] adj , int [ ] visited ) { visited [ s ] = 1 ; int h = 0 ; foreach ( int child in adj [ s ] ) { if ( visited [ child ] == 0 ) { h = Math . Max ( h , 1 + height ( child , adj , visited ) ) ; } } return h ; } static int minimumGroups ( List < int > [ ] adj , int N ) { int [ ] visited = new int [ N + 1 ] ; int groups = int . MinValue ; for ( int i = 1 ; i <= N ; i ++ ) { if ( visited [ i ] == 0 ) { int comHeight ; comHeight = height ( i , adj , visited ) ; groups = Math . Max ( groups , comHeight ) ; } } return groups ; } static void addEdge ( List < int > [ ] adj , int u , int v ) { adj [ u ] . Add ( v ) ; adj [ v ] . Add ( u ) ; } public static void Main ( String [ ] args ) { int N = 5 ; List < int > [ ] adj = new List < int > [ N + 1 ] ; for ( int i = 0 ; i < N + 1 ; i ++ ) adj [ i ] = new List < int > ( ) ; addEdge ( adj , 1 , 2 ) ; addEdge ( adj , 3 , 2 ) ; addEdge ( adj , 4 , 3 ) ; Console . Write ( minimumGroups ( adj , N ) ) ; } }


"Minimum Cost Path in a directed graph via given set of intermediate nodes | C # program to implement the above approach ; Stores minimum - cost of path from source ; Function to Perform BFS on graph g starting from vertex v ; If destination is reached ; Set flag to true ; Visit all the intermediate nodes ; If any intermediate node is not visited ; If all intermediate nodes are visited ; Update the minSum ; Mark the current node visited ; Traverse adjacent nodes ; Mark the neighbour visited ; Find minimum cost path considering the neighbour as the source ; Mark the neighbour unvisited ; Mark the source unvisited ; Driver code ; Stores the graph ; Number of nodes ; Source ; Destination ; Keeps a check on visited and unvisited nodes ; Stores intemediate nodes ; If no path is found"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class GFG { class pair { public int first , second ; public pair ( int f , int s ) { this . first = f ; this . second = s ; } } static int minSum = 100000000 ; static void getMinPathSum ( Dictionary < int , ArrayList > graph , bool [ ] visited , ArrayList necessary , int source , int dest , int currSum ) { if ( src == dest ) { bool flag = true ; foreach ( int i in necessary ) { if ( ! visited [ i ] ) { flag = false ; break ; } } if ( flag ) minSum = Math . Min ( minSum , currSum ) ; return ; } else { visited [ src ] = true ; foreach ( pair node in graph ) { if ( ! visited [ node . first ] ) { visited [ node . first ] = true ; getMinPathSum ( graph , visited , necessary , node . first , dest , currSum + node . second ) ; visited [ node . first ] = false ; } } visited [ src ] = false ; } } public static void Main ( string [ ] args ) { Dictionary < int , ArrayList > graph = new Dictionary < int , ArrayList > ( ) ; for ( int i = 0 ; i <= 6 ; i ++ ) graph [ i ] = new ArrayList ( ) ; graph [ 0 ] . Add ( new pair ( 1 , 2 ) ) ; graph [ 0 ] . Add ( new pair ( 2 , 3 ) ) ; graph [ 0 ] . Add ( new pair ( 3 , 2 ) ) ; graph [ 1 ] . Add ( new pair ( 4 , 4 ) ) ; graph [ 1 ] . Add ( new pair ( 0 , 1 ) ) ; graph [ 2 ] . Add ( new pair ( 4 , 5 ) ) ; graph [ 2 ] . Add ( new pair ( 5 , 6 ) ) ; graph [ 3 ] . Add ( new pair ( 5 , 7 ) ) ; graph [ 3 ] . Add ( new pair ( 0 , 1 ) ) ; graph [ 4 ] . Add ( new pair ( 6 , 4 ) ) ; graph [ 5 ] . Add ( new pair ( 4 , 2 ) ) ; graph [ 6 ] . Add ( new pair ( 7 , 11 ) ) ; int n = 7 ; int source = 0 ; int dest = 6 ; bool [ ] visited = new bool [ n ] ; ArrayList necessary = new ArrayList ( ) ; necessary . Add ( 2 ) ; necessary . Add ( 4 ) ; getMinPathSum ( graph , visited , necessary , source , dest , 0 ) ; if ( minSum == 100000000 ) Console . WriteLine ( - 1 ) ; else Console . WriteLine ( minSum ) ; } }


"Minimum Cost Path to visit all nodes situated at the Circumference of Circular Road | C # implementation to find the Minimum Cost Path to visit all nodes situated at the Circumference of Circular Road ; Function to find the minimum cost ; Sort the given array ; Initialise a new array of double size ; Fill the array elements ; Find the minimum path cost ; Return the readonly result ; Driver code"
Solution in C#:

using System ; class GFG { static int minCost ( int [ ] arr , int n , int circumference ) { Array . Sort ( arr ) ; int [ ] arr2 = new int [ 2 * n ] ; for ( int i = 0 ; i < n ; i ++ ) { arr2 [ i ] = arr [ i ] ; arr2 [ i + n ] = arr [ i ] + circumference ; } int res = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) res = Math . Min ( res , arr2 [ i + ( n - 1 ) ] - arr2 [ i ] ) ; return res ; } public static void Main ( String [ ] args ) { int [ ] arr = { 19 , 3 , 6 } ; int n = arr . Length ; int circumference = 20 ; Console . WriteLine ( minCost ( arr , n , circumference ) ) ; } }


"Minimum Cost To Make Two Strings Identical | C # code to find minimum cost to make two strings identical ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; Following steps build L [ m + 1 ] [ n + 1 ] in bottom up fashion . Note that L [ i ] [ j ] contains length of LCS of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] ; L [ m ] [ n ] contains length of LCS for X [ 0. . n - 1 ] and Y [ 0. . m - 1 ] ; Returns cost of making X [ ] and Y [ ] identical . costX is cost of removing a character from X [ ] and costY is cost of removing a character from Y [ ] ; Find LCS of X [ ] and Y [ ] ; Cost of making two strings identical is SUM of following two 1 ) Cost of removing extra characters from first string 2 ) Cost of removing extra characters from second string ; Driver code"
Solution in C#:

using System ; class GFG { static int lcs ( String X , String Y , int m , int n ) { int [ , ] L = new int [ m + 1 , n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) L [ i , j ] = 0 ; else if ( X [ i - 1 ] == Y [ j - 1 ] ) L [ i , j ] = L [ i - 1 , j - 1 ] + 1 ; else L [ i , j ] = Math . Max ( L [ i - 1 , j ] , L [ i , j - 1 ] ) ; } } return L [ m , n ] ; } static int findMinCost ( String X , String Y , int costX , int costY ) { int m = X . Length ; int n = Y . Length ; int len_LCS ; len_LCS = lcs ( X , Y , m , n ) ; return costX * ( m - len_LCS ) + costY * ( n - len_LCS ) ; } public static void Main ( ) { String X = " ef " ; String Y = " gh " ; Console . Write ( " Minimum ▁ Cost ▁ to ▁ make ▁ two ▁ strings ▁ " + " ▁ identical ▁ is ▁ = ▁ " + findMinCost ( X , Y , 10 , 20 ) ) ; } }


"Minimum Cost to make all array elements equal using given operations | C # implementation to find the minimum cost to make all array elements equal ; Checks if the value is less than middle element of the array ; Function that returns the cost of making all elements equal to current element ; Compute the lower bound of current element ; Calculate the requirement of add operation ; Calculate the requirement of subtract operation ; Compute minimum of left and right ; Computing the total cost of add and subtract operations ; Function that prints minimum cost of making all elements equal ; Sort the given array ; Calculate minimum from a + r and m ; Compute prefix sum and store in pref array ; Find the minimum cost from the given elements ; Finding the minimum cost from the other cases where minimum cost can occur ; Printing the minimum cost of making all elements equal ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { public static int lowerBound ( int [ ] array , int length , int value ) { int low = 0 ; int high = length ; while ( low < high ) { int mid = ( low + high ) / 2 ; if ( value <= array [ mid ] ) { high = mid ; } else { low = mid + 1 ; } } return low ; } public static int costCalculation ( int current , int [ ] arr , int n , int [ ] pref , int a , int r , int minimum ) { int index = lowerBound ( arr , arr . Length , current ) ; int left = index * current - pref [ index ] ; int right = pref [ n ] - pref [ index ] - ( n - index ) * current ; int res = Math . Min ( left , right ) ; left -= res ; right -= res ; int total = res * minimum ; total += left * a ; total += right * r ; return total ; } public static void solve ( int [ ] arr , int n , int a , int r , int m ) { Array . Sort ( arr ) ; int minimum = Math . Min ( a + r , m ) ; int [ ] pref = new int [ n + 1 ] ; Array . Fill ( pref , 0 ) ; for ( int i = 0 ; i < n ; i ++ ) pref [ i + 1 ] = pref [ i ] + arr [ i ] ; int ans = 10000 ; for ( int i = 0 ; i < n ; i ++ ) ans = Math . Min ( ans , costCalculation ( arr [ i ] , arr , n , pref , a , r , minimum ) ) ; ans = Math . Min ( ans , costCalculation ( pref [ n ] / n , arr , n , pref , a , r , minimum ) ) ; ans = Math . Min ( ans , costCalculation ( pref [ n ] / n + 1 , arr , n , pref , a , r , minimum ) ) ; Console . WriteLine ( ans ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 5 , 5 , 3 , 6 , 5 } ; int A = 1 , R = 2 , M = 4 ; int size = arr . Length ; solve ( arr , size , A , R , M ) ; } }


"Minimum Cost to make two Numeric Strings Identical | C # program to find minimum cost to make two numeric strings identical ; Function to find weight of LCS ; if this state is already calculated then return ; adding required weight for particular match ; recurse for left and right child and store the max ; Function to calculate cost of string ; Driver code ; Minimum cost needed to make two strings identical"
Solution in C#:

using System ; public class GFG { static int lcs ( int [ , ] dp , String a , String b , int m , int n ) { for ( int i = 0 ; i < 100 ; i ++ ) for ( int j = 0 ; j < 100 ; j ++ ) dp [ i , j ] = - 1 ; if ( m < 0 n < 0 ) { return 0 ; } if ( dp [ m , n ] != - 1 ) return dp [ m , n ] ; int ans = 0 ; if ( a [ m ] == b [ n ] ) { ans = ( a [ m ] - 48 ) + lcs ( dp , a , b , m - 1 , n - 1 ) ; } else ans = Math . Max ( lcs ( dp , a , b , m - 1 , n ) , lcs ( dp , a , b , m , n - 1 ) ) ; dp [ m , n ] = ans ; return ans ; } static int costOfString ( String str ) { int cost = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) cost += ( str [ i ] - 48 ) ; return cost ; } public static void Main ( ) { String a , b ; a = "9142" ; b = "1429" ; int [ , ] dp = new int [ 101 , 101 ] ; Console . Write ( ( costOfString ( a ) + costOfString ( b ) - 2 * lcs ( dp , a , b , a . Length - 1 , b . Length - 1 ) ) ) ; } }


"Minimum Count of Bit flips required to make a Binary String Palindromic | C # program to implement the above approach ; Function to calculate the length of the binary string ; Length ; Right shift of n ; Increment the length ; Return the length ; Function to check if the bit present at i - th position is a set bit or not ; Returns true if the bit is set ; Function to count the minimum number of bit flips required ; Length of the binary form ; Number of flips ; Pointer to the LSB ; Pointer to the MSB ; Check if the bits are equal ; Decrementing the left pointer ; Incrementing the right pointer ; Returns the number of bits to flip . ; Driver Code"
Solution in C#:

using System ; class GFG { static int check_length ( int n ) { int ans = 0 ; while ( n != 0 ) { n = n >> 1 ; ans ++ ; } return ans ; } static bool check_ith_bit ( int n , int i ) { return ( n & ( 1 << ( i - 1 ) ) ) != 0 ? true : false ; } static int no_of_flips ( int n ) { int len = check_length ( n ) ; int ans = 0 ; int right = 1 ; int left = len ; while ( right < left ) { if ( check_ith_bit ( n , right ) != check_ith_bit ( n , left ) ) ans ++ ; left -- ; right ++ ; } return ans ; } public static void Main ( String [ ] args ) { int n = 12 ; Console . WriteLine ( no_of_flips ( n ) ) ; } }


"Minimum Cuts can be made in the Chessboard such that it is not divided into 2 parts | C # implementation of above approach ; function that calculates the maximum no . of cuts ; Driver Code ; Calling function ."
Solution in C#:

using System ; public class GFG { static int numberOfCuts ( int M , int N ) { int result = 0 ; result = ( M - 1 ) * ( N - 1 ) ; return result ; } static public void Main ( ) { int M = 4 , N = 4 ; int Cuts = numberOfCuts ( M , N ) ; Console . WriteLine ( " Maximum ▁ cuts ▁ = ▁ " + Cuts ) ; } }


"Minimum De | C # code for counting minimum de - arrangements present in an array . ; function to count Dearrangement ; create a copy of original array ; sort the array ; traverse sorted array for counting mismatches ; reverse the sorted array ; traverse reverse sorted array for counting mismatches ; return minimum mismatch count ; Driver code"
Solution in C#:

using System ; class GFG { public static int countDe ( int [ ] arr , int n ) { int [ ] v = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) v [ i ] = arr [ i ] ; Array . Sort ( arr ) ; int count1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] != v [ i ] ) count1 ++ ; Array . Reverse ( arr ) ; int count2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] != v [ i ] ) count2 ++ ; return ( Math . Min ( count1 , count2 ) ) ; } public static void Main ( ) { int [ ] arr = new int [ ] { 5 , 9 , 21 , 17 , 13 } ; int n = 5 ; Console . WriteLine ( " Minimum ▁ Dearrangement ▁ = ▁ " + countDe ( arr , n ) ) ; } }


"Minimum Deci | C # program to implement the above approach ; Function to find the count of minimum Deci - Binary numbers required to obtain S ; Stores the minimum count ; Iterate over the string s ; Convert the char to its equivalent integer ; If current character is the maximum so far ; Update the maximum digit ; Print the required result ; Driver Code"
Solution in C#:

using System ; class GFG { static int minimum_deci_binary_number ( string s ) { int m = int . MinValue ; for ( int i = 0 ; i < s . Length ; i ++ ) { int temp = s [ i ] - '0' ; if ( temp > m ) { m = temp ; } } return m ; } public static void Main ( String [ ] args ) { string S = "31" ; Console . WriteLine ( minimum_deci_binary_number ( S ) ) ; } }


"Minimum Decrements on Subarrays required to reduce all Array elements to zero | C # program to implement the above approach ; Function to count the minimum number of subarrays that are required to be decremented by 1 ; Base Case ; Initializing ans to first element ; For A [ i ] > A [ i - 1 ] , operation ( A [ i ] - A [ i - 1 ] ) is required ; Return the count ; Driver Code"
Solution in C#:

using System ; class GFG { static int min_operations ( int [ ] A , int n ) { if ( n == 0 ) return 0 ; int ans = A [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( A [ i ] > A [ i - 1 ] ) { ans += A [ i ] - A [ i - 1 ] ; } } return ans ; } public static void Main ( ) { int n = 5 ; int [ ] A = { 1 , 2 , 3 , 2 , 1 } ; Console . WriteLine ( min_operations ( A , n ) ) ; } }


"Minimum Fibonacci terms with sum equal to K | C # code to find the minimum number of Fibonacci terms that sum to k . ; Function to calaculate Fibonacci Terms ; Calculate all Fibonacci terms which are less than or equal to k . ; If next term is greater than k do not add in arraylist and return . ; Function to find the minimum number of Fibonacci terms having sum equal to k . ; List to store Fibonacci terms . ; Subtract Fibonacci terms from sum k until sum > 0. ; Divide sum k by j - th Fibonacci term to find how many terms it contribute in sum . ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static void calcFiboTerms ( List < int > fiboterms , int k ) { int i = 3 , nextTerm = 0 ; fiboterms . Add ( 0 ) ; fiboterms . Add ( 1 ) ; fiboterms . Add ( 1 ) ; while ( true ) { nextTerm = fiboterms [ i - 1 ] + fiboterms [ i - 2 ] ; if ( nextTerm > k ) return ; fiboterms . Add ( nextTerm ) ; i ++ ; } } public static int fibMinTerms ( int k ) { List < int > fiboterms = new List < int > ( ) ; calcFiboTerms ( fiboterms , k ) ; int count = 0 , j = fiboterms . Count - 1 ; while ( k > 0 ) { count += ( k / fiboterms [ j ] ) ; k %= ( fiboterms [ j ] ) ; j -- ; } return count ; } public static void Main ( String [ ] args ) { int k = 17 ; Console . WriteLine ( fibMinTerms ( k ) ) ; } }


"Minimum Increment / decrement to make array elements equal | C # program to find minimum Increment or decrement to make array elements equal ; Function to return minimum operations need to be make each element of array equal ; Initialize cost to 0 ; Sort the array ; Middle element ; Find Cost ; If n , is even . Take minimum of the Cost obtained by considering both middle elements ; Find cost again ; Take minimum of two cost ; Return total cost ; Driver Code"
Solution in C#:

using System ; class GFG { static int minCost ( int [ ] A , int n ) { int cost = 0 ; Array . Sort ( A ) ; int K = A [ n / 2 ] ; for ( int i = 0 ; i < n ; ++ i ) cost += Math . Abs ( A [ i ] - K ) ; if ( n % 2 == 0 ) { int tempCost = 0 ; K = A [ ( n / 2 ) - 1 ] ; for ( int i = 0 ; i < n ; ++ i ) tempCost += Math . Abs ( A [ i ] - K ) ; cost = Math . Min ( cost , tempCost ) ; } return cost ; } public static void Main ( String [ ] args ) { int [ ] A = new int [ ] { 1 , 6 , 7 , 10 } ; int n = A . Length ; Console . WriteLine ( minCost ( A , n ) ) ; } }


"Minimum Index Sum for Common Elements of Two Lists |  ; Function to print common Strings with minimum index sum ; resultant list ; iterating over sum in ascending order ; iterating over one list and check index ( Corresponding to given sum ) in other list ; put common Strings in resultant list ; if common String found then break as we are considering index sums in increasing order . ; print the resultant list ; Driver code ; Creating list1 ; Creating list2"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void find ( List < String > list1 , List < String > list2 ) { List < String > res = new List < String > ( ) ; int max_possible_sum = list1 . Count + list2 . Count - 2 ; for ( int sum = 0 ; sum <= max_possible_sum ; sum ++ ) { for ( int i = 0 ; i <= sum ; i ++ ) if ( i < list1 . Count && ( sum - i ) < list2 . Count && list1 [ i ] == list2 [ sum - i ] ) res . Add ( list1 [ i ] ) ; if ( res . Count > 0 ) break ; } for ( int i = 0 ; i < res . Count ; i ++ ) Console . Write ( res [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { List < String > list1 = new List < String > ( ) ; list1 . Add ( " GeeksforGeeks " ) ; list1 . Add ( " Udemy " ) ; list1 . Add ( " Coursera " ) ; list1 . Add ( " edX " ) ; List < String > list2 = new List < String > ( ) ; list2 . Add ( " Codecademy " ) ; list2 . Add ( " Khan ▁ Academy " ) ; list2 . Add ( " GeeksforGeeks " ) ; find ( list1 , list2 ) ; } }


"Minimum Index Sum for Common Elements of Two Lists | Hashing based C # program to find common elements with minimum index sum . ; Function to print common Strings with minimum index sum ; mapping Strings to their indices ; resultant list ; If current sum is smaller than minsum ; if index sum is same then put this String in resultant list as well ; Print result ; Driver code ; Creating list1 ; Creating list2"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void find ( List < String > list1 , List < String > list2 ) { Dictionary < String , int > map = new Dictionary < String , int > ( ) ; for ( int i = 0 ; i < list1 . Count ; i ++ ) map . Add ( list1 [ i ] , i ) ; List < String > res = new List < String > ( ) ; int minsum = int . MaxValue ; for ( int j = 0 ; j < list2 . Count ; j ++ ) { if ( map . ContainsKey ( list2 [ j ] ) ) { int sum = j + map [ list2 [ j ] ] ; if ( sum < minsum ) { minsum = sum ; res . Clear ( ) ; res . Add ( list2 [ j ] ) ; } else if ( sum == minsum ) res . Add ( list2 [ j ] ) ; } } for ( int i = 0 ; i < res . Count ; i ++ ) Console . Write ( res [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { List < String > list1 = new List < String > ( ) ; list1 . Add ( " GeeksforGeeks " ) ; list1 . Add ( " Udemy " ) ; list1 . Add ( " Coursera " ) ; list1 . Add ( " edX " ) ; List < String > list2 = new List < String > ( ) ; list2 . Add ( " Codecademy " ) ; list2 . Add ( " Khan ▁ Academy " ) ; list2 . Add ( " GeeksforGeeks " ) ; find ( list1 , list2 ) ; } }


"Minimum Initial Points to Reach Destination | C # program Minimum Initial Points to Reach Destination ; dp [ i ] [ j ] represents the minimum initial points player should have so that when starts with cell ( i , j ) successfully reaches the destination cell ( m - 1 , n - 1 ) ; Base case ; Fill last row and last column as base to fill entire table ; fill the table in bottom - up fashion ; Driver Code"
Solution in C#:

using System ; class GFG { static int minInitialPoints ( int [ , ] points , int R , int C ) { int [ , ] dp = new int [ R , C ] ; int m = R , n = C ; dp [ m - 1 , n - 1 ] = points [ m - 1 , n - 1 ] > 0 ? 1 : Math . Abs ( points [ m - 1 , n - 1 ] ) + 1 ; for ( int i = m - 2 ; i >= 0 ; i -- ) dp [ i , n - 1 ] = Math . Max ( dp [ i + 1 , n - 1 ] - points [ i , n - 1 ] , 1 ) ; for ( int j = n - 2 ; j >= 0 ; j -- ) dp [ m - 1 , j ] = Math . Max ( dp [ m - 1 , j + 1 ] - points [ m - 1 , j ] , 1 ) ; for ( int i = m - 2 ; i >= 0 ; i -- ) { for ( int j = n - 2 ; j >= 0 ; j -- ) { int min_points_on_exit = Math . Min ( dp [ i + 1 , j ] , dp [ i , j + 1 ] ) ; dp [ i , j ] = Math . Max ( min_points_on_exit - points [ i , j ] , 1 ) ; } } return dp [ 0 , 0 ] ; } public static void Main ( ) { int [ , ] points = { { - 2 , - 3 , 3 } , { - 5 , - 10 , 1 } , { 10 , 30 , - 5 } } ; int R = 3 , C = 3 ; Console . Write ( " Minimum ▁ Initial ▁ Points ▁ Required : ▁ " + minInitialPoints ( points , R , C ) ) ; } }


"Minimum K such that every substring of length at least K contains a character c | Set | C # program for the above approach ; Function to find minimum value of K such that there exist atleast one K amazing character ; Stores the answer ; Update the string S ; Iterate over the characters in range [ a , z ] ; Stores the last index where c appears ; Stores the maximum possible length ; Update string S ; Iterate over characters of string S ; If S [ i ] is equal to c ; Stores the distance between positions of two same c ; Update max_len ; Update the value of prev ; Update the value of ans ; Return the answer ; Driver Code ; Given Input ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int MinimumLengthSubstring ( string S , int N ) { int ans = N ; S = "0" + S + "0" ; for ( char c = ' a ' ; c <= ' z ' ; ++ c ) { int prev = 0 ; int max_len = 0 ; S = S . Substring ( 0 , 0 ) + c + S . Substring ( 1 ) ; S = S . Substring ( 0 , N + 1 ) + c + S . Substring ( N + 2 ) ; for ( int i = 1 ; i <= N + 1 ; ++ i ) { if ( S [ i ] == c ) { int len = i - prev ; max_len = Math . Max ( max_len , len ) ; prev = i ; } } ans = Math . Min ( ans , max_len ) ; } return ans ; } public static void Main ( ) { string S = " abcde " ; int N = S . Length ; Console . Write ( MinimumLengthSubstring ( S , N ) ) ; } }


"Minimum K such that every substring of length atleast K contains a character c | C # Program to find minimum K such that every substring of length atleast K contains some character c ; This function checks if there exists some character which appears in all K length substrings ; Iterate over all possible characters ; stores the last occurrence ; set answer as true ; ; No occurrence found of current character in first substring of length K ; Check for every last substring of length K where last occurr - ence exists in substring ; If last occ is not present in substring ; current character is K amazing ; This function performs binary search over the answer to minimise it ; Check if answer is found try to minimise it ; Driver Code to test above functions"
Solution in C#:

using System ; class GFG { static int check ( String s , int K ) { for ( int ch = 0 ; ch < 26 ; ch ++ ) { char c = ( char ) ( ' a ' + ch ) ; int last = - 1 ; bool found = true ; for ( int i = 0 ; i < K ; i ++ ) if ( s [ i ] == c ) last = i ; if ( last == - 1 ) continue ; for ( int i = K ; i < s . Length ; i ++ ) { if ( s [ i ] == c ) last = i ; if ( last <= ( i - K ) ) { found = false ; break ; } } if ( found ) return 1 ; } return 0 ; } static int binarySearch ( String s ) { int low = 1 , high = s . Length ; int ans = 0 ; while ( low <= high ) { int mid = ( high + low ) >> 1 ; if ( check ( s , mid ) == 1 ) { ans = mid ; high = mid - 1 ; } else low = mid + 1 ; } return ans ; } public static void Main ( ) { String s = " abcde " ; Console . WriteLine ( binarySearch ( s ) ) ; s = " aaaa " ; Console . WriteLine ( binarySearch ( s ) ) ; } }


"Minimum K such that sum of array elements after division by K does not exceed S | C # implementation of the approach ; Function to return the minimum value of k that satisfies the given condition ; Find the maximum element ; Lowest answer can be 1 and the highest answer can be ( maximum + 1 ) ; Binary search ; Get the mid element ; Calculate the sum after dividing the array by new K which is mid ; Search in the second half ; First half ; Driver code"
Solution in C#:

using System ; class GFG { static int findMinimumK ( int [ ] a , int n , int s ) { int maximum = a [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { maximum = Math . Max ( maximum , a [ i ] ) ; } int low = 1 , high = maximum + 1 ; int ans = high ; while ( low <= high ) { int mid = ( low + high ) / 2 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += ( int ) ( a [ i ] / mid ) ; } if ( sum > s ) low = mid + 1 ; else { ans = Math . Min ( ans , mid ) ; high = mid - 1 ; } } return ans ; } public static void Main ( ) { int [ ] a = { 10 , 7 , 8 , 10 , 12 , 19 } ; int n = a . Length ; int s = 27 ; Console . WriteLine ( findMinimumK ( a , n , s ) ) ; } }


"Minimum LCM of all pairs in a given array | C # program to find minimum possible lcm from any pair ; Function to compute GCD of two numbers ; Function that return minimum possible lcm from any pair ; Fix the ith element and iterate over all the array to find minimum LCM ; 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 minLCM ( int [ ] arr , int n ) { int ans = Int32 . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int g = gcd ( arr [ i ] , arr [ j ] ) ; int lcm = arr [ i ] / g * arr [ j ] ; ans = Math . Min ( ans , lcm ) ; } } return ans ; } public static void Main ( ) { int [ ] arr = { 2 , 4 , 3 , 6 , 5 } ; int n = arr . Length ; Console . Write ( minLCM ( arr , n ) ) ; } }


"Minimum LCM of all pairs in a given array | C # program to find the pair having minimum LCM ; Function that return pair having minimum LCM ; Find max element in the array as the gcd of two elements from the array can 't greater than max element. ; Created a 2D array to store minimum two multiple of any particular i . ; We already found two smallest multiple ; Iterating over all gcd ; Iterating over its multiple ; If we already found the two smallest multiple of i ; Choosing smallest two multiple ; Calculating lcm ; Return readonly answer ; Driver code"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static int minLCM ( int [ ] arr , int n ) { int mx = 0 ; for ( int i = 0 ; i < n ; i ++ ) { mx = Math . Max ( mx , arr [ i ] ) ; } List < int > [ ] mul = new List < int > [ mx + 1 ] ; for ( int i = 0 ; i < mul . Length ; i ++ ) mul [ i ] = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mul [ arr [ i ] ] . Count > 1 ) { continue ; } mul [ arr [ i ] ] . Add ( arr [ i ] ) ; } for ( int i = 1 ; i <= mx ; i ++ ) { for ( int j = i + i ; j <= mx ; j += i ) { if ( mul [ i ] . Count > 1 ) { break ; } foreach ( int k in mul [ j ] ) { if ( mul [ i ] . Count > 1 ) break ; mul [ i ] . Add ( k ) ; } } } int ans = int . MaxValue ; for ( int i = 1 ; i <= mx ; i ++ ) { if ( mul [ i ] . Count <= 1 ) continue ; int a = mul [ i ] [ 0 ] , b = mul [ i ] [ 1 ] ; int lcm = ( a * b ) / i ; ans = Math . Min ( ans , lcm ) ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 3 , 6 , 5 } ; int n = arr . Length ; Console . Write ( minLCM ( arr , n ) + "
 " ) ; } }


"Minimum LCM of all subarrays of length at least 2 | C # program for the above approach ; Function to find LCM pf two numbers ; Initialise lcm value ; Check for divisibility of a and b by the lcm ; Function to find the Minimum LCM of all subarrays of length greater than 1 ; Store the minimum LCM ; Traverse the array ; Find LCM of consecutive element ; Check if the calculated LCM is less than the minLCM then update it ; Print the minimum LCM ; Driver Code ; Given array [ ] arr ; Size of the array ; Function call"
Solution in C#:

 using System ; class GFG { static int LCM ( int a , int b ) { int lcm = a > b ? a : b ; while ( true ) { if ( lcm % a == 0 && lcm % b == 0 ) break ; else lcm ++ ; } return lcm ; } static void findMinLCM ( int [ ] arr , int n ) { int minLCM = int . MaxValue ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int val = LCM ( arr [ i ] , arr [ i + 1 ] ) ; if ( val < minLCM ) { minLCM = val ; } } Console . Write ( minLCM + "
 " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 8 , 12 , 16 , 20 , 24 } ; int n = arr . Length ; findMinLCM ( arr , n ) ; } }


"Minimum MEX from all subarrays of length K | C # program for the above approach ; Function to return minimum MEX from all K - length subarrays ; Stores element from [ 1 , N + 1 ] which are not present in subarray ; Store number 1 to N + 1 in set s ; Find the MEX of K - length subarray starting from index 0 ; Find the MEX of all subarrays of length K by erasing arr [ i ] and inserting arr [ i - K ] ; Store first element of set ; Updating the mex ; Print minimum MEX of all K length subarray ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static void minimumMEX ( int [ ] arr , int N , int K ) { HashSet < int > s = new HashSet < int > ( ) ; for ( int i = 1 ; i <= N + 1 ; i ++ ) s . Add ( i ) ; for ( int i = 0 ; i < K ; i ++ ) s . Remove ( arr [ i ] ) ; int mex = s . First ( ) ; for ( int i = K ; i < N ; i ++ ) { s . Remove ( arr [ i ] ) ; s . Add ( arr [ i - K ] ) ; int firstElem = s . First ( ) ; mex = Math . Min ( mex , firstElem ) ; } Console . Write ( mex + " ▁ " ) ; } static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 } ; int K = 3 ; int N = arr . Length ; minimumMEX ( arr , N , K ) ; } }


"Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character | C # Program to find minimum number of manipulations required to make two strings identical ; Counts the no of manipulations required ; store the count of character ; iterate though the first String and update count ; iterate through the second string update char_count . if character is not found in char_count then increase count ; Driver code"
Solution in C#:

using System ; public class GFG { static int countManipulations ( string s1 , string s2 ) { int count = 0 ; int [ ] char_count = new int [ 26 ] ; for ( int i = 0 ; i < s1 . Length ; i ++ ) char_count [ s1 [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < s2 . Length ; i ++ ) char_count [ s2 [ i ] - ' a ' ] -- ; for ( int i = 0 ; i < 26 ; ++ i ) { if ( char_count [ i ] != 0 ) { count += Math . Abs ( char_count [ i ] ) ; } } return count / 2 ; } public static void Main ( ) { string s1 = " ddcf " ; string s2 = " cedk " ; Console . WriteLine ( countManipulations ( s1 , s2 ) ) ; } }


"Minimum Numbers of cells that are connected with the smallest path between 3 given cells | C # implementation of the approach ; Function to return the minimum cells that are connected via the minimum length path ; Array to store column number of the given cells ; Sort cells in ascending order of row number ; Middle row number ; Set pair to store required cells ; Range of column number ; Store all cells of middle row within column number range ; Final step to store all the column number of 1 st and 3 rd cell upto MidRow ; vector pair to store X , Y , Z"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int Minimum_Cells ( List < Tuple < int , int > > v ) { int [ ] col = new int [ 3 ] ; int i , j ; for ( i = 0 ; i < 3 ; i ++ ) { int column_number = v [ i ] . Item2 ; col [ i ] = column_number ; } Array . Sort ( col ) ; v . Sort ( ) ; int MidRow = v [ 1 ] . Item1 ; HashSet < Tuple < int , int > > s = new HashSet < Tuple < int , int > > ( ) ; int Maxcol = col [ 2 ] , MinCol = col [ 0 ] ; for ( i = MinCol ; i <= Maxcol ; i ++ ) { s . Add ( new Tuple < int , int > ( MidRow , i ) ) ; } for ( i = 0 ; i < 3 ; i ++ ) { if ( v [ i ] . Item1 == MidRow ) continue ; for ( j = Math . Min ( v [ i ] . Item1 , MidRow ) ; j <= Math . Max ( v [ i ] . Item1 , MidRow ) ; j ++ ) { s . Add ( new Tuple < int , int > ( j , v [ i ] . Item1 ) ) ; } } return s . Count ; } static void Main ( ) { List < Tuple < int , int > > v = new List < Tuple < int , int > > ( ) ; v . Add ( new Tuple < int , int > ( 0 , 0 ) ) ; v . Add ( new Tuple < int , int > ( 1 , 1 ) ) ; v . Add ( new Tuple < int , int > ( 2 , 2 ) ) ; Console . Write ( Minimum_Cells ( v ) ) ; } }


"Minimum Perimeter of n blocks | C # Code to find minimum perimeter using n blocks ; if n is a perfect square ; Number of rows ; perimeter of the rectangular grid ; if there are blocks left ; Driver code"
Solution in C#:

using System ; class GFG { public static long minPerimeter ( int n ) { int l = ( int ) Math . Sqrt ( n ) ; int sq = l * l ; if ( sq == n ) return l * 4 ; else { long row = n / l ; long perimeter = 2 * ( l + row ) ; if ( n % l != 0 ) perimeter += 2 ; return perimeter ; } } public static void Main ( ) { int n = 10 ; Console . Write ( minPerimeter ( n ) ) ; } }


"Minimum Players required to win the game | C # program to find minimum players required to win the game anyhow ; function to calculate ( a ^ b ) % ( 10 ^ 9 + 7 ) . ; function to find the minimum required player ; computing the nenomenator ; computing modulo inverse of denominator ; final result ; Driver code"
Solution in C#:

using System ; class GFG { static long mod = 1000000007 ; static long power ( long a , long b ) { long res = 1 ; while ( b != 0 ) { if ( ( b & 1 ) != 0 ) { res *= a ; res %= mod ; } b /= 2 ; a *= a ; a %= mod ; } return res ; } static long minPlayer ( long n , long k ) { long num = ( ( power ( k , n ) - 1 ) + mod ) % mod ; long den = ( power ( k - 1 , mod - 2 ) + mod ) % mod ; long ans = ( ( ( num * den ) % mod ) * k ) % mod ; return ans ; } public static void Main ( ) { long n = 3 , k = 3 ; Console . WriteLine ( minPlayer ( n , k ) ) ; } }


"Minimum Possible value of | ai + aj | C # program to find number of pairs and minimal possible value ; function for finding pairs and min value ; initialize smallest and count ; iterate over all pairs ; is abs value is smaller than smallest update smallest and reset count to 1 ; if abs value is equal to smallest increment count value ; print result ; Driver Code"
Solution in C#:

using System ; class GFG { static void pairs ( int [ ] arr , int n , int k ) { int smallest = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) { if ( Math . Abs ( arr [ i ] + arr [ j ] - k ) < smallest ) { smallest = Math . Abs ( arr [ i ] + arr [ j ] - k ) ; count = 1 ; } else if ( Math . Abs ( arr [ i ] + arr [ j ] - k ) == smallest ) count ++ ; } Console . WriteLine ( " Minimal ▁ Value ▁ = ▁ " + smallest ) ; Console . WriteLine ( " Total ▁ Pairs ▁ = ▁ " + count ) ; } public static void Main ( ) { int [ ] arr = { 3 , 5 , 7 , 5 , 1 , 9 , 9 } ; int k = 12 ; int n = arr . Length ; pairs ( arr , n , k ) ; } }


"Minimum Product Spanning Tree | C # program for getting minimum product spanning tree The program is for adjacency matrix representation of the graph ; Number of vertices in the graph ; A utility function to find the vertex with minimum key value , from the set of vertices not yet included in MST ; Initialize min value ; A utility function to print the constructed MST stored in parent [ ] and print Minimum Obtaiable product ; Function to construct and print MST for a graph represented using adjacency matrix representation inputGraph is sent for printing actual edges and logGraph is sent for actual MST operations ; Array to store constructed MST ; Key values used to pick minimum ; weight edge in cut To represent set of vertices not ; yet included in MST Initialize all keys as INFINITE ; Always include first 1 st vertex in MST . Make key 0 so that this vertex is ; picked as first vertex First node is always root of MST ; The MST will have V vertices ; Pick the minimum key vertex from the set of vertices not yet included in MST ; Add the picked vertex to the MST Set ; Update key value and parent index of the adjacent vertices of the picked vertex . Consider only those vertices which are not yet included in MST logGraph [ u , v ] is non zero only for ; adjacent vertices of m mstSet [ v ] is false for vertices not yet included in MST Update the key only if logGraph [ u , v ] is smaller than key [ v ] ; print the constructed MST ; Method to get minimum product spanning tree ; Constructing logGraph from original graph ; Applyting standard Prim 's MST algorithm on  Log graph. ; Driver code ; Let us create the following graph 2 3 ( 0 ) -- ( 1 ) -- ( 2 ) | / \ | 6 | 8 / \ 5 | 7 | / \ | ( 3 ) -- -- -- - ( 4 ) 9 ; Print the solution"
Solution in C#:

 using System ; class GFG { static int V = 5 ; static int minKey ( int [ ] key , Boolean [ ] mstSet ) { int min = int . MaxValue , min_index = 0 ; for ( int v = 0 ; v < V ; v ++ ) { if ( mstSet [ v ] == false && key [ v ] < min ) { min = key [ v ] ; min_index = v ; } } return min_index ; } static void printMST ( int [ ] parent , int n , int [ , ] graph ) { Console . Write ( " Edge ▁ Weight
 " ) ; int minProduct = 1 ; for ( int i = 1 ; i < V ; i ++ ) { Console . Write ( " { 0 } ▁ - ▁ { 1 } ▁ { 2 } ▁
 " , parent [ i ] , i , graph [ i , parent [ i ] ] ) ; minProduct *= graph [ i , parent [ i ] ] ; } Console . Write ( " Minimum ▁ Obtainable ▁ product ▁ is ▁ { 0 }
 " , minProduct ) ; } static void primMST ( int [ , ] inputGraph , double [ , ] logGraph ) { int [ ] parent = new int [ V ] ; int [ ] key = new int [ V ] ; Boolean [ ] mstSet = new Boolean [ V ] ; for ( int i = 0 ; i < V ; i ++ ) { key [ i ] = int . MaxValue ; mstSet [ i ] = false ; } key [ 0 ] = 0 ; parent [ 0 ] = - 1 ; for ( int count = 0 ; count < V - 1 ; count ++ ) { int u = minKey ( key , mstSet ) ; mstSet [ u ] = true ; for ( int v = 0 ; v < V ; v ++ ) { if ( logGraph [ u , v ] > 0 && mstSet [ v ] == false && logGraph [ u , v ] < key [ v ] ) { parent [ v ] = u ; key [ v ] = ( int ) logGraph [ u , v ] ; } } } printMST ( parent , V , inputGraph ) ; } static void minimumProductMST ( int [ , ] graph ) { double [ , ] logGraph = new double [ V , V ] ; for ( int i = 0 ; i < V ; i ++ ) { for ( int j = 0 ; j < V ; j ++ ) { if ( graph [ i , j ] > 0 ) { logGraph [ i , j ] = Math . Log ( graph [ i , j ] ) ; } else { logGraph [ i , j ] = 0 ; } } } primMST ( graph , logGraph ) ; } public static void Main ( String [ ] args ) { int [ , ] graph = { { 0 , 2 , 0 , 6 , 0 } , { 2 , 0 , 3 , 8 , 5 } , { 0 , 3 , 0 , 0 , 7 } , { 6 , 8 , 0 , 0 , 9 } , { 0 , 5 , 7 , 9 , 0 } , } ; minimumProductMST ( graph ) ; } }


"Minimum Steps to obtain N from 1 by the given operations | C # program to implement the above approach ; Base Case ; Recursive Call for n - 1 ; Check if n is divisible by 2 ; Check if n is divisible by 3 ; Returns a tuple ( a , b ) , where a : Minimum steps to obtain x from 1 b : Previous number ; Function that find the optimal solution ; Print the length ; Exit condition for loop = - 1 when n has reached 1 ; Return the sequence in reverse order ; Driver Code ; Given N ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static List < int > find_sequence ( int n ) { List < int > temp = new List < int > ( ) ; temp . Add ( 1 ) ; temp . Add ( - 1 ) ; if ( n == 1 ) return temp ; List < int > arr = find_sequence ( n - 1 ) ; List < int > ans = new List < int > ( n ) ; ans . Add ( arr [ 0 ] + 1 ) ; ans . Add ( n - 1 ) ; if ( n % 2 == 0 ) { List < int > div_by_2 = find_sequence ( n / 2 ) ; if ( div_by_2 [ 0 ] < ans [ 0 ] ) { ans . Clear ( ) ; ans . Add ( div_by_2 [ 0 ] + 1 ) ; ans . Add ( n / 2 ) ; } } if ( n % 3 == 0 ) { List < int > div_by_3 = find_sequence ( n / 3 ) ; if ( div_by_3 [ 0 ] < ans [ 0 ] ) { ans . Clear ( ) ; ans . Add ( div_by_3 [ 0 ] + 1 ) ; ans . Add ( n / 3 ) ; } } return ans ; } static List < int > find_solution ( int n ) { List < int > a = find_sequence ( n ) ; Console . WriteLine ( a [ 0 ] ) ; List < int > sequence = new List < int > ( ) ; sequence . Add ( n ) ; while ( a [ 1 ] != - 1 ) { sequence . Add ( a [ 1 ] ) ; List < int > arr = find_sequence ( a [ 1 ] ) ; a . Insert ( 1 , arr [ 1 ] ) ; } sequence . Reverse ( ) ; return sequence ; } public static void Main ( String [ ] args ) { int n = 5 ; List < int > res = find_solution ( n ) ; for ( int i = 0 ; i < res . Count ; i ++ ) { Console . Write ( res [ i ] + " ▁ " ) ; } } }


"Minimum Subarray flips required to convert all elements of a Binary Array to K | C # program to implement the above approach ; Function to count the minimum number of subarray flips required ; Iterate the array ; If arr [ i ] and flag are equal ; Return the answer ; Driver code"
Solution in C#:

using System ; class GFG { static int minSteps ( int [ ] arr , int n , int k ) { int i , cnt = 0 ; int flag ; if ( k == 1 ) flag = 0 ; else flag = 1 ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == flag ) { cnt ++ ; flag = ( flag + 1 ) % 2 ; } } return cnt ; } public static void Main ( ) { int [ ] arr = { 1 , 0 , 1 , 0 , 0 , 1 , 1 , 1 } ; int n = arr . Length ; int k = 1 ; Console . Write ( minSteps ( arr , n , k ) ) ; } }


"Minimum Sum Path In 3 | C # program for Min path sum of 3D - array ; A utility function that returns minimum of 3 integers ; function to calculate MIN path sum of 3D array ; Initialize first row of tSum array ; Initialize first column of tSum array ; Initialize first width of tSum array ; Initialize first row - First column of tSum array ; Initialize first row - First width of tSum array ; Initialize first width - First column of tSum array ; Construct rest of the tSum array ; Driver Code"
Solution in C#:


"Minimum Sum Path in a Triangle | C # program for Dynamic Programming implementation of Min Sum Path in a Triangle ; Util function to find minimum sum for a path ; For storing the result in a 1 - D array , and simultaneously updating the result . ; For the bottom row ; Calculation of the remaining rows , in bottom up manner . ; return the top element ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static int minSumPath ( ref List < List < int > > A ) { int [ ] memo = new int [ A . Count ] ; int n = A . Count - 1 ; for ( int i = 0 ; i < A [ n ] . Count ; i ++ ) memo [ i ] = A [ n ] [ i ] ; for ( int i = A . Count - 2 ; i >= 0 ; i -- ) for ( int j = 0 ; j < A [ i + 1 ] . Count - 1 ; j ++ ) memo [ j ] = A [ i ] [ j ] + ( int ) Math . Min ( memo [ j ] , memo [ j + 1 ] ) ; return memo [ 0 ] ; } public static void Main ( ) { List < List < int > > A = new List < List < int > > ( ) ; A . Add ( new List < int > { 2 } ) ; A . Add ( new List < int > { 3 , 9 } ) ; A . Add ( new List < int > { 1 , 6 , 7 } ) ; Console . WriteLine ( minSumPath ( ref A ) ) ; } }


"Minimum Sum of Euclidean Distances to all given Points | C # program to implement the above approach ; Function to calculate Euclidean distance ; Function to calculate the minimum sum of the euclidean distances to all points ; Calculate the centroid ; Calculate distance of all points ; Driver Code ; Initializing the points"
Solution in C#:

 using System ; class GFG { static double find ( double x , double y , int [ , ] p ) { double mind = 0 ; for ( int i = 0 ; i < p . GetLength ( 0 ) ; i ++ ) { double a = p [ i , 0 ] , b = p [ i , 1 ] ; mind += Math . Sqrt ( ( x - a ) * ( x - a ) + ( y - b ) * ( y - b ) ) ; } return mind ; } static double getMinDistSum ( int [ , ] p ) { double x = 0 , y = 0 ; for ( int i = 0 ; i < p . GetLength ( 0 ) ; i ++ ) { x += p [ i , 0 ] ; y += p [ i , 1 ] ; } x = x / p . Length ; y = y / p . Length ; double mind = find ( x , y , p ) ; return mind ; } public static void Main ( String [ ] args ) { int [ , ] vec = { { 0 , 1 } , { 1 , 0 } , { 1 , 2 } , { 2 , 1 } } ; int d = ( int ) getMinDistSum ( vec ) ; Console . Write ( d + "
 " ) ; } }


"Minimum Sum of a pair at least K distance apart from an Array | C # Program to implement the above approach ; Function to find the minimum sum of two elements that are atleast K distance apart ; Length of the array ; Iterate over the array ; Initialize the min value ; Iterate from i + k to N ; Find the minimum ; Update the minimum sum ; Print the answer ; Driver Code"
Solution in C#:

using System ; class GFG { public static void findMinSum ( int [ ] A , int K ) { int n = A . Length ; int minimum_sum = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { int min = int . MaxValue ; for ( int j = i + K ; j < n ; j ++ ) min = Math . Min ( min , A [ j ] ) ; if ( min == int . MaxValue ) continue ; minimum_sum = Math . Min ( minimum_sum , A [ i ] + min ) ; } Console . WriteLine ( minimum_sum ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 4 , 2 , 5 , 4 , 3 , 2 , 5 } ; int K = 3 ; findMinSum ( A , K ) ; } }


"Minimum Sum of a pair at least K distance apart from an Array | C # program to implement the above approach ; Function to find the minimum sum of two elements that are atleast K distance apart ; Length of the array ; Find the suffix array ; Iterate in the array ; Update minimum sum ; Print the answer ; Driver Code"
Solution in C#:

using System ; class GFG { public static void findMinSum ( int [ ] A , int K ) { int n = A . Length ; int [ ] suffix_min = new int [ n ] ; suffix_min [ n - 1 ] = A [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) suffix_min [ i ] = Math . Min ( suffix_min [ i + 1 ] , A [ i ] ) ; int min_sum = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { if ( i + K < n ) min_sum = Math . Min ( min_sum , A [ i ] + suffix_min [ i + K ] ) ; } Console . WriteLine ( min_sum ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 1 , 2 , 3 , 4 , 5 , 6 } ; int K = 2 ; findMinSum ( A , K ) ; } }


"Minimum Time Taken for N Points to coincide | C # program for the above approach ; Function to check for common area between three circles ; Find the distance between the centers of circle 1 & circle 2 ; Find the distance between the centers of circle 1 & circle 3 ; Find the distance between the centers of circle 2 & circle 3 ; If sum of radius is less than the distance between their centers then false ; If circle 1 lies within circle 2 or if circle 2 lies within circle 1 ; If circle 1 lies within circle 2 ; Check whether R1 ( common area between R1 and R2 ) and R3 intersect ; Check whether R2 ( common area between R1 and R2 ) and R3 intersect ; If circle 1 lies within circle 3 or if circle 3 lies within circle 1 ; If circle 1 lies within circle 3 ; Check whether R1 ( common area between R1 and R3 ) and R2 intersect ; Check whether R3 ( common area between R1 and R3 ) and R2 intersect ; If circle 2 lies within circle 3 or if circle 3 lies within circle 2 ; If circle 2 lies within circle 3 ; Check whether R2 ( common area between R2 and R3 ) and R1 intersect ; Check whether R3 ( common area between R2 and R3 ) and R1 intersect ; Find the point of intersection for circle 1 & circle 2 ; First point of intersection ( x121 , y121 ) ; Check whether the point of intersection lies within circle 3 or not ; Second point of intersection ( x122 , y122 ) ; Check whether the point of intersection lies within circle 3 or not ; Find the point of intersection for circle 1 & circle 3 ; First point of intersection ( x131 , y131 ) ; Check whether the point of intersection lies within circle 2 or not ; Second point of intersection ( x132 , y132 ) ; Check whether the point of intersection lies within circle 2 or not ; Find the point of intersection for circle 2 & circle 3 ; First point of intersection ( x231 , y231 ) ; Check whether the point of intersection lies within circle 1 or not ; Second point of intersection ( x232 , y232 ) ; Check whether the point of intersection lies within circle 1 or not ; Function to check if there..."
Solution in C#:


"Minimum XOR of OR and AND of any pair in the Array | C # program to for above approach ; Function to find the minimum value of XOR of AND and OR of any pair in the given array ; Sort the array ; Traverse the array arr [ ] ; Compare and Find the minimum XOR value of an array . ; Return the final answer ; Driver Code ; Given array arr [ ] ; Function Call"
Solution in C#:

using System ; class GFG { static int maxAndXor ( int [ ] arr , int n ) { int ans = 9999999 ; Array . Sort ( arr ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { ans = Math . Min ( ans , arr [ i ] ^ arr [ i + 1 ] ) ; } return ans ; } public static void Main ( ) { int [ ] arr = new int [ ] { 1 , 2 , 3 , 4 , 5 } ; int N = arr . Length ; Console . Write ( maxAndXor ( arr , N ) ) ; } }


"Minimum XOR of at most K elements in range [ L , R ] | C # program for the above approach ; Function for K = 2 ; Function for K = 2 ; Function for K = 2 ; Function to calculate the minimum XOR of at most K elements in [ L , R ] ; Driver code ; Input ; Function call"
Solution in C#:

using System ; class GFG { static int func2 ( int L , int R , int K ) { if ( R - L >= 2 ) return 1 ; return Math . Min ( L , L ^ R ) ; } static int func3 ( int L , int R , int K ) { if ( ( R ^ L ) > L && ( R ^ L ) < R ) return 0 ; return func2 ( L , R , K ) ; } static int func4 ( int L , int R , int K ) { if ( R - L >= 4 ) return 0 ; int minval = L ^ ( L + 1 ) ^ ( L + 2 ) ^ ( L + 3 ) ; return Math . Min ( minval , func3 ( L , R , K ) ) ; } static int minimumXor ( int L , int R , int K ) { if ( K > 4 ) return 0 ; else if ( K == 4 ) return func4 ( L , R , K ) ; else if ( K == 3 ) return func3 ( L , R , K ) ; else if ( K == 2 ) return func2 ( L , R , K ) ; else return L ; } static void Main ( ) { int L = 1 , R = 3 , K = 3 ; Console . Write ( minimumXor ( L , R , K ) ) ; } }


"Minimum absolute difference between N and a power of 2 | C # implementation of the above approach ; Function to return the minimum difference between N and a power of 2 ; Power of 2 closest to n on its left ; Power of 2 closest to n on its right ; Return the minimum abs difference ; Driver code"
Solution in C#:

using System ; public class GFG { static int minAbsDiff ( int n ) { int left = 1 << ( ( int ) Math . Floor ( Math . Log ( n ) / Math . Log ( 2 ) ) ) ; int right = left * 2 ; return Math . Min ( ( n - left ) , ( right - n ) ) ; } static public void Main ( ) { int n = 15 ; Console . WriteLine ( minAbsDiff ( n ) ) ; } }


"Minimum absolute difference between N and any power of 2 | C # implementation of the approach ; Function to return the highest power of 2 less than or equal to n ; Function to return the smallest power of 2 greater than or equal to n ; Function that returns the minimum absolute difference between n and any power of 2 ; Driver code"
Solution in C#:

using System ; class GFG { static int prevPowerof2 ( int n ) { int p = ( int ) ( Math . Log ( n ) / Math . Log ( 2 ) ) ; return ( int ) Math . Pow ( 2 , p ) ; } static int nextPowerOf2 ( int n ) { int p = 1 ; if ( ( n == 0 ) && ! ( ( n & ( n - 1 ) ) == 0 ) ) return n ; while ( p < n ) p <<= 1 ; return p ; } static int minDiff ( int n ) { int low = prevPowerof2 ( n ) ; int high = nextPowerOf2 ( n ) ; return Math . Min ( n - low , high - n ) ; } public static void Main ( String [ ] args ) { int n = 6 ; Console . WriteLine ( minDiff ( n ) ) ; } }


"Minimum absolute difference of a number and its closest prime | C # program to find the minimum absolute difference between a number and its closest prime ; Function to check if a number is prime or not ; Function to find the minimum absolute difference between a number and its closest prime ; Variables to store first prime above and below N ; Finding first prime number greater than N ; Finding first prime number less than N ; Variables to store the differences ; Driver code"
Solution in C#:

using System ; class GFG { static bool isPrime ( int N ) { for ( int i = 2 ; i <= Math . Sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) return false ; } return true ; } static int getDifference ( int N ) { if ( N == 0 ) return 2 ; else if ( N == 1 ) return 1 ; else if ( isPrime ( N ) ) return 0 ; int aboveN = - 1 , belowN = - 1 ; int n1 ; n1 = N + 1 ; while ( true ) { if ( isPrime ( n1 ) ) { aboveN = n1 ; break ; } else n1 ++ ; } n1 = N - 1 ; while ( true ) { if ( isPrime ( n1 ) ) { belowN = n1 ; break ; } else n1 -- ; } int diff1 = aboveN - N ; int diff2 = N - belowN ; return Math . Min ( diff1 , diff2 ) ; } public static void Main ( ) { int N = 25 ; Console . WriteLine ( getDifference ( N ) ) ; } }


"Minimum absolute difference of adjacent elements in a circular array | C # program to find maximum difference between adjacent elements in a circular array . ; Checking normal adjacent elements ; Checking circular link ; driver code"
Solution in C#:

using System ; class GFG { static void minAdjDifference ( int [ ] arr , int n ) { if ( n < 2 ) return ; int res = Math . Abs ( arr [ 1 ] - arr [ 0 ] ) ; for ( int i = 2 ; i < n ; i ++ ) res = Math . Min ( res , Math . Abs ( arr [ i ] - arr [ i - 1 ] ) ) ; res = Math . Min ( res , Math . Abs ( arr [ n - 1 ] - arr [ 0 ] ) ) ; Console . Write ( " Min ▁ Difference ▁ = ▁ " + res ) ; } public static void Main ( ) { int [ ] a = { 10 , 12 , 13 , 15 , 10 } ; int n = a . Length ; minAdjDifference ( a , n ) ; } }


"Minimum absolute difference of server loads | C # program to implement the above approach ; Function which returns the minimum difference of loads ; Compute the overall server load ; Stores the results of subproblems ; Fill the partition table in bottom up manner ; If i - th server is included ; If i - th server is excluded ; Server A load : total_sum - ans Server B load : ans Diff : abs ( total_sum - 2 * ans ) ; Driver Code ; Function call"
Solution in C#:

using System ; class GFG { static int minServerLoads ( int n , int [ ] servers ) { int totalLoad = 0 ; for ( int i = 0 ; i < servers . Length ; i ++ ) totalLoad += servers [ i ] ; int requiredLoad = totalLoad / 2 ; int [ , ] dp = new int [ n + 1 , requiredLoad + 1 ] ; for ( int i = 1 ; i < n + 1 ; i ++ ) { for ( int j = 1 ; j < requiredLoad + 1 ; j ++ ) { if ( servers [ i - 1 ] > j ) dp [ i , j ] = dp [ i - 1 , j ] ; else dp [ i , j ] = Math . Max ( dp [ i - 1 , j ] , servers [ i - 1 ] + dp [ i - 1 , j - servers [ i - 1 ] ] ) ; } } return totalLoad - 2 * dp [ n , requiredLoad ] ; } public static void Main ( string [ ] args ) { int N = 5 ; int [ ] servers = { 1 , 2 , 3 , 4 , 5 } ; Console . Write ( minServerLoads ( N , servers ) ) ; } }


"Minimum absolute value of ( K – arr [ i ] ) for all possible values of K over the range [ 0 , N – 1 ] | C # program for the above approach ; Functinion to find the minimum absolute value of ( K - arr [ i ] ) for each possible value of K over the range [ 0 , N - 1 ] | ; Traverse the given array arr [ ] ; Stores the mininimum distance to any array element arr [ i ] ; Check if there is no safe position smaller than i ; Check if the current position is between two safe positions ; Take the minimum of two distances ; Check if the current index is a safe position ; Check if there is no safe position greater than i ; Print the minimum distance ; driver code"
Solution in C#:

using System ; class GFG { public static void minimumDistance ( int [ ] arr , int N ) { int ind = 0 ; int prev = arr [ ind ] ; int s = arr . Length ; for ( int i = 0 ; i < N ; i ++ ) { int distance = Int32 . MaxValue ; if ( i < arr [ 0 ] ) { distance = arr [ 0 ] - i ; } else if ( i >= prev && ind + 1 < s && i <= arr [ ind + 1 ] ) { distance = Math . Min ( i - prev , arr [ ind + 1 ] - i ) ; if ( i == arr [ ind + 1 ] ) { distance = 0 ; prev = arr [ ind + 1 ] ; ind ++ ; } } else { distance = i - prev ; } Console . Write ( distance + " ▁ " ) ; } } public static void Main ( string [ ] args ) { int N = 5 ; int [ ] arr = { 0 , 4 } ; minimumDistance ( arr , N ) ; } }


"Minimum adjacent swaps required to make a binary string alternating | C # program for the above approach ; Function to find the minimum number of adjacent swaps to make the String alternating ; Count the no of zeros and ones ; Base Case ; Store no of min swaps when String starts with "1" ; Keep track of the odd positions ; Checking for when the String starts with "1" ; Adding the no of swaps to fix "1" at odd positions ; Store no of min swaps when String starts with "0" ; Keep track of the odd positions ; Checking for when the String starts with "0" ; Adding the no of swaps to fix "1" at odd positions ; Returning the answer based on the conditions when String length is even ; When String length is odd ; When no of ones is greater than no of zeros ; When no of ones is greater than no of zeros ; Driver Code"
Solution in C#:

using System ; class GFG { static int minSwaps ( String s ) { int ones = 0 , zeros = 0 ; int N = s . Length ; for ( int i = 0 ; i < N ; i ++ ) { if ( s [ i ] == '1' ) ones ++ ; else zeros ++ ; } if ( ( N % 2 == 0 && ones != zeros ) || ( N % 2 == 1 && Math . Abs ( ones - zeros ) != 1 ) ) { return - 1 ; } int ans_1 = 0 ; int j = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( s [ i ] == '1' ) { ans_1 += Math . Abs ( j - i ) ; j += 2 ; } } int ans_0 = 0 ; int k = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( s [ i ] == '0' ) { ans_0 += Math . Abs ( k - i ) ; k += 2 ; } } if ( N % 2 == 0 ) return Math . Min ( ans_1 , ans_0 ) ; else { if ( ones > zeros ) return ans_1 ; else return ans_0 ; } } public static void Main ( ) { String S = "110100" ; Console . WriteLine ( minSwaps ( S ) ) ; } }


"Minimum adjacent swaps to group similar characters together | C # program for the above approach ; Function to find minimum adjacent swaps required to make all the same character adjacent ; Initialize answer ; Create a 2D array of size 26 ; Traverse the String ; Get character ; Append the current index in the corresponding vector ; Traverse each character from a to z ; Add difference of adjacent index ; Return answer ; Driver Code ; Given String ; Size of String ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int minSwaps ( String S , int n ) { int swaps = 0 ; List < int > [ ] arr = new List < int > [ 26 ] ; for ( int i = 0 ; i < arr . Length ; i ++ ) arr [ i ] = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int pos = S [ i ] - ' a ' ; arr [ pos ] . Add ( i ) ; } for ( char ch = ' a ' ; ch <= ' z ' ; ++ ch ) { int pos = ch - ' a ' ; for ( int i = 1 ; i < arr [ pos ] . Count ; ++ i ) { swaps += Math . Abs ( arr [ pos ] [ i ] - arr [ pos ] [ i - 1 ] - 1 ) ; } } return swaps ; } public static void Main ( String [ ] args ) { String S = " abbccabbcc " ; int N = S . Length ; Console . Write ( minSwaps ( S , N ) ) ; } }


"Minimum adjacent swaps to move maximum and minimum to corners | C # program to count Minimum number of swaps so that the largest element is at beginning and the smallest element is at last ; Function performing calculations ; Index of leftmost largest element ; Index of rightmost smallest element ; Driver Code"
Solution in C#:

using System ; class GFG { public static void minimumSwaps ( int [ ] a , int n ) { int maxx = - 1 , l = 0 , minn = a [ 0 ] , r = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > maxx ) { maxx = a [ i ] ; l = i ; } if ( a [ i ] <= minn ) { minn = a [ i ] ; r = i ; } } if ( r < l ) Console . WriteLine ( l + ( n - r - 2 ) ) ; else Console . WriteLine ( l + ( n - r - 1 ) ) ; } public static void Main ( ) { int [ ] a = { 5 , 6 , 1 , 3 } ; int n = a . Length ; minimumSwaps ( a , n ) ; } }