"Minimum and Maximum element of an array which is divisible by a given number k | C # implementation of the above approach ; Function to find the minimum element ; Function to find the maximum element ; Driver code"
Solution in C#:

using System ; class GFG { static int getMin ( int [ ] arr , int n , int k ) { int res = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % k == 0 ) { res = Math . Min ( res , arr [ i ] ) ; } } return res ; } static int getMax ( int [ ] arr , int n , int k ) { int res = int . MinValue ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] % k == 0 ) { res = Math . Max ( res , arr [ i ] ) ; } } return res ; } static public void Main ( ) { int [ ] arr = { 10 , 1230 , 45 , 67 , 1 } ; int k = 10 ; int n = arr . Length ; Console . WriteLine ( " Minimum ▁ element ▁ of ▁ array ▁ " + " which ▁ is ▁ divisible ▁ by ▁ k : ▁ " + getMin ( arr , n , k ) ) ; Console . WriteLine ( " Maximum ▁ element ▁ of ▁ array ▁ " + " which ▁ is ▁ divisible ▁ by ▁ k : ▁ " + getMax ( arr , n , k ) ) ; } }


"Minimum and Maximum number of pairs in m teams of n people | C # program to find minimum and maximum no . of pairs ; Driver code"
Solution in C#:

using System ; class GFG { static void MinimumMaximumPairs ( int n , int m ) { int max_pairs = ( ( n - m + 1 ) * ( n - m ) ) / 2 ; int min_pairs = m * ( ( ( n - m ) / m + 1 ) * ( ( n - m ) / m ) ) / 2 + ( int ) Math . Ceiling ( ( double ) ( ( n - m ) / ( double ) ( m ) ) ) * ( ( n - m ) % m ) ; Console . WriteLine ( " Minimum ▁ no . ▁ of ▁ pairs ▁ = ▁ " + min_pairs ) ; Console . WriteLine ( " Maximum ▁ no . ▁ of ▁ pairs ▁ = ▁ " + max_pairs ) ; } public static void Main ( ) { int n = 5 , m = 2 ; MinimumMaximumPairs ( n , m ) ; } }


"Minimum and maximum count of elements at D distance from arr [ i ] in either direction | C # program for the above approach ; Function to find the minimum and maximum number of points included in a range of distance D ; Stores the minimum and maximum number of points that lies over the distance of D ; Iterate the array ; Count of elements included to left of point at index i ; Update the minimum number of points ; Update the maximum number of points ; Count of elements included to right of point at index i ; Update the minimum number of points ; Update the maximum number of points ; Return the array ; Function to perform the Binary Search to the left of arr [ i ] over the given range ; Base Case ; Binary Search for index to left ; update index ; Return the number of elements by subtracting indices ; Function to perform the Binary Search to the right of arr [ i ] over the given range ; Base Case ; Binary Search for index to right ; Update the index ; Return the number of elements by subtracting indices ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { public static int [ ] minMaxRange ( int [ ] arr , int D , int N ) { int max = 1 , min = N ; for ( int i = 0 ; i < N ; i ++ ) { int dist = leftSearch ( arr , arr [ i ] - D , i ) ; min = Math . Min ( min , dist ) ; max = Math . Max ( max , dist ) ; dist = rightSearch ( arr , arr [ i ] + D , i ) ; min = Math . Min ( min , dist ) ; max = Math . Max ( max , dist ) ; } return new int [ ] { min , max } ; } public static int leftSearch ( int [ ] arr , int val , int i ) { if ( i == 0 ) return 1 ; int left = 0 , right = i - 1 ; int ind = - 1 ; while ( left <= right ) { int mid = ( left + right ) / 2 ; if ( arr [ mid ] < val ) { left = mid + 1 ; } else { right = mid - 1 ; ind = mid ; } } return ind != - 1 ? i - ind + 1 : 1 ; } public static int rightSearch ( int [ ] arr , int val , int i ) { if ( i == arr . Length - 1 ) return 1 ; int left = i + 1 ; int right = arr . Length - 1 ; int ind = - 1 ; while ( left <= right ) { int mid = ( left + right ) / 2 ; if ( arr [ mid ] > val ) { right = mid - 1 ; } else { left = mid + 1 ; ind = mid ; } } return ind != - 1 ? ind - i + 1 : 1 ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 5 , 9 , 14 } ; int N = arr . Length ; int D = 4 ; int [ ] minMax = minMaxRange ( arr , D , N ) ; Console . Write ( minMax [ 0 ] + " ▁ " + minMax [ 1 ] ) ; } }


"Minimum and maximum node that lies in the path connecting two nodes in a Binary Tree | C # implementation of the approach ; Structure of binary tree ; Function to create a new node ; Function to store the path from root node to given node of the tree in path vector and then returns true if the path exists otherwise false ; Function to print the minimum and the maximum value present in the path connecting the given two nodes of the given binary tree ; To store the path from the root node to a ; To store the path from the root node to b ; To store the minimum and the maximum value in the path from LCA to a ; To store the minimum and the maximum value in the path from LCA to b ; If both a and b are present in the tree ; Compare the paths to get the first different value ; Find minimum and maximum value in the path from LCA to a ; Find minimum and maximum value in the path from LCA to b ; Minimum of min values in first path and second path ; Maximum of max values in first path and second path ; If no path exists ; Driver Code"
Solution in C#:

 using System ; using System . Collections ; class GFG { class Node { public Node left ; public Node right ; public int data ; } ; static Node newNode ( int key ) { Node node = new Node ( ) ; node . left = node . right = null ; node . data = key ; return node ; } static ArrayList path ; static bool FindPath ( Node root , int key ) { if ( root == null ) return false ; path . Add ( root . data ) ; if ( root . data == key ) return true ; if ( FindPath ( root . left , key ) || FindPath ( root . right , key ) ) return true ; path . Remove ( ( int ) path [ path . Count - 1 ] ) ; return false ; } static int minMaxNodeInPath ( Node root , int a , int b ) { path = new ArrayList ( ) ; bool flag = true ; ArrayList Path2 = new ArrayList ( ) ; ArrayList Path1 = new ArrayList ( ) ; int min1 = Int32 . MaxValue ; int max1 = Int32 . MinValue ; int min2 = Int32 . MaxValue ; int max2 = Int32 . MinValue ; int i = 0 ; int j = 0 ; flag = FindPath ( root , a ) ; Path1 = path ; path = new ArrayList ( ) ; flag &= FindPath ( root , b ) ; Path2 = path ; if ( flag ) { for ( i = 0 ; i < Path1 . Count && i < Path2 . Count ; i ++ ) if ( ( int ) Path1 [ i ] != ( int ) Path2 [ i ] ) break ; i -- ; j = i ; for ( ; i < Path1 . Count ; i ++ ) { if ( min1 > ( int ) Path1 [ i ] ) min1 = ( int ) Path1 [ i ] ; if ( max1 < ( int ) Path1 [ i ] ) max1 = ( int ) Path1 [ i ] ; } for ( ; j < Path2 . Count ; j ++ ) { if ( min2 > ( int ) Path2 [ j ] ) min2 = ( int ) Path2 [ j ] ; if ( max2 < ( int ) Path2 [ j ] ) max2 = ( int ) Path2 [ j ] ; } Console . Write ( " Min ▁ = ▁ " + Math . Min ( min1 , min2 ) + "
 " ) ; Console . Write ( " Max ▁ = ▁ " + Math . Max ( max1 , max2 ) + "
 " ) ; } else Console . Write ( " Max = - 1 " ) ; return 0 ; } public static void Main ( string [ ] arg ) { Node root = newNode ( 20 ) ; root . left = newNode ( 8 ) ; root . right = newNode ( 22 ) ; root . left . left = newNode ( 5 ) ; root . left . right = newNode ( 3 ) ; root . right . left = newNode ( 4 ) ; root . right . right = newNode ( 25 ) ; root . left . right . left =...


"Minimum and maximum number of N chocolates after distribution among K students | C # implementation of the above approach ; Driver code"
Solution in C#:

using System ; class GFG { public static void Main ( ) { int n = 7 ; int k = 3 ; if ( n % k == 0 ) Console . WriteLine ( n / k + " ▁ " + n / k ) ; else Console . WriteLine ( ( n - ( n % k ) ) / k + " ▁ " + ( ( ( n - ( n % k ) ) / k ) + 1 ) ) ; } }


"Minimum and maximum possible length of the third side of a triangle | C # implementation of the approach ; Function to find the minimum and the maximum possible length of the third side of the given triangle ; Not a valid triangle ; Not a valid triangle ; Driver code"
Solution in C#:

using System ; class GFG { static void find_length ( int s1 , int s2 ) { if ( s1 <= 0 s2 <= 0 ) { Console . Write ( - 1 ) ; return ; } int max_length = s1 + s2 - 1 ; int min_length = Math . Max ( s1 , s2 ) - Math . Min ( s1 , s2 ) + 1 ; if ( min_length > max_length ) { Console . WriteLine ( - 1 ) ; return ; } Console . WriteLine ( " Max ▁ = ▁ " + max_length ) ; Console . WriteLine ( " Min ▁ = ▁ " + min_length ) ; } public static void Main ( ) { int s1 = 8 , s2 = 5 ; find_length ( s1 , s2 ) ; } }


"Minimum area of square holding two identical rectangles | C # program for the above problem ; Function to find the area of the square ; Larger side of rectangle ; Smaller side of the rectangle ; Driver code"
Solution in C#:

using System ; class GFG { public static int areaSquare ( int L , int B ) { int large = Math . Max ( L , B ) ; int small = Math . Min ( L , B ) ; if ( large >= 2 * small ) { return large * large ; } else { return ( 2 * small ) * ( 2 * small ) ; } } public static void Main ( ) { int L = 7 ; int B = 4 ; Console . Write ( areaSquare ( L , B ) ) ; } }


"Minimum area of the triangle formed by any tangent to an ellipse with the coordinate axes | C # program for the above approach ; Function to find the minimum area of triangle formed by any tangent to ellipse with the coordinate axes ; Stores the minimum area ; Print the calculated area ; Driver Code"
Solution in C#:

using System ; class GFG { static void minimumTriangleArea ( int a , int b ) { int area = a * b ; Console . WriteLine ( area ) ; } public static void Main ( ) { int a = 1 , b = 2 ; minimumTriangleArea ( a , b ) ; } }


"Minimum array elements required to be subtracted from either end to reduce K to 0 | C # program for the above approach ; Function to find the length of longest subarray having sum K ; Stores the index of the prefix sum ; Traverse the given array ; Update sum ; If the subarray starts from index 0 ; Add the current prefix sum with index if it is not present in the map um ; Check if sum - K is present in Map um or not ; Update the maxLength ; Return the required maximum length ; Function to find the minimum removal of array elements required to reduce K to 0 ; Stores the sum of the array ; Traverse the array arr [ ] ; Update sum of the array ; Find maxLen ; If the subarray with sum doesn 't exist ; Otherwise , print the required maximum length ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int longestSubarray ( int [ ] arr , int N , int K ) { Dictionary < int , int > um = new Dictionary < int , int > ( ) ; int sum = 0 , maxLen = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; if ( sum == K ) maxLen = i + 1 ; if ( ! um . ContainsKey ( sum ) ) um [ sum ] = i ; if ( um . ContainsKey ( sum - K ) ) { if ( maxLen < ( i - um [ sum - K ] ) ) maxLen = i - um [ sum - K ] ; } } return maxLen ; } static void minRequiredOperation ( int [ ] arr , int N , int K ) { int TotalSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) TotalSum += arr [ i ] ; int maxLen = longestSubarray ( arr , N , TotalSum - K ) ; if ( maxLen == - 1 ) { Console . WriteLine ( - 1 ) ; } else Console . WriteLine ( N - maxLen ) ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 1 , 1 , 2 } ; int K = 4 ; int N = arr . Length ; minRequiredOperation ( arr , N , K ) ; } }


"Minimum array elements to be changed to make Recaman 's sequence | C # implementation of the approach ; First term of the sequence is always 0 ; Fill remaining terms using recursive formula ; If arr [ i - 1 ] - i is negative or already exists ; Function that returns minimum changes required ; Set to store first n Recaman numbers ; Generate and store first n Recaman numbers ; Insert first n Recaman numbers to set ; If current element of the array is present in the set ; Return the remaining number of elements in the set ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int recamanGenerator ( int [ ] arr , int n ) { arr [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int temp = arr [ i - 1 ] - i ; int j ; for ( j = 0 ; j < i ; j ++ ) { if ( ( arr [ j ] == temp ) temp < 0 ) { temp = arr [ i - 1 ] + i ; break ; } } arr [ i ] = temp ; } return 0 ; } static int recamanArray ( int [ ] arr , int n ) { HashSet < int > s = new HashSet < int > ( ) ; int [ ] recaman = new int [ n + 1 ] ; recamanGenerator ( recaman , n ) ; for ( int i = 0 ; i < n ; i ++ ) s . Add ( recaman [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . Contains ( arr [ i ] ) ) s . Remove ( arr [ i ] ) ; } return s . Count ; } static void Main ( ) { int [ ] arr = { 7 , 11 , 20 , 4 , 2 , 1 , 8 , 6 } ; int n = arr . Length ; Console . Write ( recamanArray ( arr , n ) ) ; } }


"Minimum array insertions required to make consecutive difference <= K | C # implementation of above approach ; Function to return minimum number of insertions required ; Initialize insertions to 0 ; return total insertions ; Driver Code"
Solution in C#:

using System ; class GFG { static int minInsertions ( int [ ] H , int n , int K ) { int inser = 0 ; for ( int i = 1 ; i < n ; ++ i ) { float diff = Math . Abs ( H [ i ] - H [ i - 1 ] ) ; if ( diff <= K ) continue ; else inser += ( int ) Math . Ceiling ( diff / K ) - 1 ; } return inser ; } static void Main ( ) { int [ ] H = new int [ ] { 2 , 4 , 8 , 16 } ; int K = 3 ; int n = H . Length ; Console . WriteLine ( minInsertions ( H , n , K ) ) ; } }


"Minimum bit changes in Binary Circular array to reach a index | C # program to find direction with minimum flips ; finding which path have minimum flip bit and the minimum flip bits ; concatenating given string to itself , to make it circular ; check x is greater than y . marking if output need to be opposite . ; iterate Clockwise ; if current bit is not equal to next index bit . ; iterate Anti - Clockwise ; if current bit is not equal to next index bit . ; Finding whether Clockwise or Anti - clockwise path take minimum flip . ; Driver code"
Solution in C#:

using System ; class GFG { static void minimumFlip ( String s , int x , int y ) { s = s + s ; bool isOpposite = false ; if ( x > y ) { swap ( x , y ) ; isOpposite = true ; } int valClockwise = 0 ; char cur = s [ x ] ; for ( int i = x ; i <= y ; i ++ ) { if ( s [ i ] != cur ) { cur = s [ i ] ; valClockwise ++ ; } } int valAnticlockwise = 0 ; cur = s [ y ] ; x += s . Length ; for ( int i = y ; i < x ; i ++ ) { if ( s [ i ] != cur ) { cur = s [ i ] ; valAnticlockwise ++ ; } } if ( valClockwise <= valAnticlockwise ) { if ( ! isOpposite ) { Console . WriteLine ( " Clockwise ▁ " + valClockwise ) ; } else { Console . WriteLine ( " Anti - clockwise ▁ " + valAnticlockwise ) ; } } else if ( ! isOpposite ) { Console . WriteLine ( " Anti - clockwise ▁ " + valAnticlockwise ) ; } else { Console . WriteLine ( " Clockwise ▁ " + valClockwise ) ; } } static void swap ( int a , int b ) { int c = a ; a = b ; b = c ; } public static void Main ( String [ ] args ) { int x = 0 , y = 8 ; String s = "000110" ; minimumFlip ( s , x , y ) ; } }


"Minimum bit swaps between given numbers to make their Bitwise OR equal to Bitwise AND | C # implementation of the above approach ; Function for counting number of set bit ; Function to return the count of minimum operations required ; cnt to count the number of bits set in A and in B ; If odd numbers of total set bits ; one_zero = 1 in A and 0 in B at ith bit similarly for zero_one ; When bitpos is set in B , unset in B ; When bitpos is set in A , unset in B ; Number of moves is half of number pairs of each group ; Odd number pairs ; Driver code ; Input ; Function call to compute the result"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { n = n & ( n - 1 ) ; count ++ ; } return count ; } static int minOperations ( int A , int B ) { int cnt1 = 0 , cnt2 = 0 ; cnt1 += countSetBits ( A ) ; cnt2 += countSetBits ( B ) ; if ( ( cnt1 + cnt2 ) % 2 != 0 ) return - 1 ; int oneZero = 0 , zeroOne = 0 ; int ans = 0 ; for ( int i = 0 ; i < Math . Max ( cnt1 , cnt2 ) ; i ++ ) { int bitpos = 1 << i ; if ( ( ( bitpos & A ) == 0 ) && ( bitpos & B ) != 0 ) zeroOne ++ ; if ( ( bitpos & A ) != 0 && ( ( bitpos & B ) == 0 ) ) oneZero ++ ; } ans = ( zeroOne / 2 ) + ( oneZero / 2 ) ; if ( zeroOne % 2 != 0 ) ans += 2 ; return ans ; } public static void Main ( ) { int A = 27 , B = 5 ; Console . Write ( minOperations ( A , B ) ) ; } }


"Minimum block jumps to reach destination | C # program to find minimum jumps to reach a given destination from a given source ; To represent point in 2D space ; To represent line of ( ax + by + c ) format ; Returns 1 if evaluation is greater > 0 , else returns - 1 ; Returns minimum jumps to reach dest point from start point ; get sign of evaluation from point co - ordinate and line equation ; if both evaluation are of opposite sign , increase jump by 1 ; Driver code"
Solution in C#:

using System ; class GFG { class point { public int x , y ; public point ( int x , int y ) { this . x = x ; this . y = y ; } } ; class line { public int a , b , c ; line ( ) { } public line ( int a , int b , int c ) { this . a = a ; this . b = b ; this . c = c ; } } ; static int evalPointOnLine ( point p , line curLine ) { int eval = curLine . a * p . x + curLine . b * p . y + curLine . c ; if ( eval > 0 ) return 1 ; return - 1 ; } static int minJumpToReachDestination ( point start , point dest , line [ ] lines , int N ) { int jumps = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int signStart = evalPointOnLine ( start , lines [ i ] ) ; int signDest = evalPointOnLine ( dest , lines [ i ] ) ; if ( signStart * signDest < 0 ) jumps ++ ; } return jumps ; } public static void Main ( String [ ] args ) { point start = new point ( 1 , 1 ) ; point dest = new point ( - 2 , - 1 ) ; line [ ] lines = new line [ 3 ] ; lines [ 0 ] = new line ( 1 , 0 , 0 ) ; lines [ 1 ] = new line ( 0 , 1 , 0 ) ; lines [ 2 ] = new line ( 1 , 1 , - 2 ) ; Console . Write ( minJumpToReachDestination ( start , dest , lines , 3 ) ) ; } }


"Minimum boxes required to carry all gifts | C # implementation of above approach ; Function to return number of boxes ; Sort the boxes in ascending order ; Try to fit smallest box with current heaviest box ( from right side ) ; Driver Code"
Solution in C#:

using System ; class GFG { static int numBoxes ( int [ ] A , int n , int K ) { Array . Sort ( A ) ; int i = 0 , j = ( n - 1 ) ; int ans = 0 ; while ( i <= j ) { ans ++ ; if ( A [ i ] + A [ j ] <= K ) i ++ ; j -- ; } return ans ; } static public void Main ( ) { int [ ] A = { 3 , 2 , 2 , 1 } ; int K = 3 ; int n = A . Length ; Console . WriteLine ( numBoxes ( A , n , K ) ) ; } }


"Minimum cells required to reach destination with jumps equal to cell values | C # implementation to count minimum cells required to be covered to reach destination ; function to count minimum cells required to be covered to reach destination ; to store min cells required to be covered to reach a particular cell ; initially no cells can be reached ; base case ; building up the dp [ ] [ ] matrix ; dp [ i ] [ j ] != INT_MAX denotes that cell ( i , j ) can be reached from cell ( 0 , 0 ) and the other half of the condition finds the cell on the right that can be reached from ( i , j ) ; the other half of the condition finds the cell right below that can be reached from ( i , j ) ; it true then cell ( m - 1 , n - 1 ) can be reached from cell ( 0 , 0 ) and returns the minimum number of cells covered ; cell ( m - 1 , n - 1 ) cannot be reached from cell ( 0 , 0 ) ; Driver code"
Solution in C#:

using System ; class GFG { static int minCells ( int [ , ] mat , int m , int n ) { int [ , ] dp = new int [ m , n ] ; for ( int i = 0 ; i < m ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) dp [ i , j ] = int . MaxValue ; dp [ 0 , 0 ] = 1 ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( dp [ i , j ] != int . MaxValue && ( j + mat [ i , j ] ) < n && ( dp [ i , j ] + 1 ) < dp [ i , j + mat [ i , j ] ] ) dp [ i , j + mat [ i , j ] ] = dp [ i , j ] + 1 ; if ( dp [ i , j ] != int . MaxValue && ( i + mat [ i , j ] ) < m && ( dp [ i , j ] + 1 ) < dp [ i + mat [ i , j ] , j ] ) dp [ i + mat [ i , j ] , j ] = dp [ i , j ] + 1 ; } } if ( dp [ m - 1 , n - 1 ] != int . MaxValue ) return dp [ m - 1 , n - 1 ] ; return - 1 ; } public static void Main ( ) { int [ , ] mat = { { 2 , 3 , 2 , 1 , 4 } , { 3 , 2 , 5 , 8 , 2 } , { 1 , 1 , 2 , 2 , 1 } } ; int m = 3 , n = 5 ; Console . WriteLine ( " Minimum ▁ number ▁ of ▁ " + " cells ▁ = ▁ " + minCells ( mat , m , n ) ) ; } }


"Minimum cells to be flipped to get a 2 * 2 submatrix with equal elements | C # implementation of the approach ; Function to return the minimum flips required such that the submatrix from mat [ i , j ] to mat [ i + 1 , j + 1 ] contains all equal elements ; Function to return the minimum number of slips required such that the matrix contains at least a single submatrix of size 2 * 2 with all equal elements ; To store the result ; For every submatrix of size 2 * 2 ; Update the count of flips required for the current submatrix ; Driver code"
Solution in C#:

using System ; class GFG { static int minFlipsSub ( String [ ] mat , int i , int j ) { int cnt0 = 0 , cnt1 = 0 ; if ( mat [ i ] [ j ] == '1' ) cnt1 ++ ; else cnt0 ++ ; if ( mat [ i ] [ j + 1 ] == '1' ) cnt1 ++ ; else cnt0 ++ ; if ( mat [ i + 1 ] [ j ] == '1' ) cnt1 ++ ; else cnt0 ++ ; if ( mat [ i + 1 ] [ j + 1 ] == '1' ) cnt1 ++ ; else cnt0 ++ ; return Math . Min ( cnt0 , cnt1 ) ; } static int minFlips ( String [ ] mat , int r , int c ) { int res = int . MaxValue ; for ( int i = 0 ; i < r - 1 ; i ++ ) { for ( int j = 0 ; j < c - 1 ; j ++ ) { res = Math . Min ( res , minFlipsSub ( mat , i , j ) ) ; } } return res ; } public static void Main ( String [ ] args ) { String [ ] mat = { "0101" , "0101" , "0101" } ; int r = mat . Length ; int c = mat . GetLength ( 0 ) ; Console . Write ( minFlips ( mat , r , c ) ) ; } }


"Minimum change in given value so that it lies in all given Ranges | C # implementation find the minimum difference in the number D such that D is inside of every range ; Function to find the minimum difference in the number D such that D is inside of every range ; Loop to find the common range out of the given array of ranges . ; Storing the start and end index ; Sorting the range ; Finding the maximum starting value of common segment ; Finding the minimum ending value of common segment ; If there is no common segment ; If the given number is between the computed common range . ; Finding the minimum distance ; Driver Code ; Given array of ranges"
Solution in C#:

using System ; class GFG { static void findMinimumOperation ( int n , int d , int [ , ] arrays ) { int cnt = 0 ; int first = int . MinValue , end = int . MaxValue ; while ( n > 0 ) { int [ ] arr = { arrays [ cnt , 0 ] , arrays [ cnt , 1 ] } ; Array . Sort ( arr ) ; first = Math . Max ( first , arr [ 0 ] ) ; end = Math . Min ( end , arr [ 1 ] ) ; cnt ++ ; n -- ; } if ( first > end ) Console . Write ( " - 1" ) ; else { if ( d >= first && d <= end ) { Console . Write ( "0" ) ; } else Console . Write ( Math . Min ( Math . Abs ( first - d ) , Math . Abs ( d - end ) ) ) ; } } public static void Main ( String [ ] args ) { int n = 3 , d = 3 ; int [ , ] arrays = { { 0 , 7 } , { 2 , 14 } , { 4 , 6 } } ; findMinimumOperation ( n , d , arrays ) ; } }


"Minimum changes required such that the string satisfies the given condition | C # implementation of the approach ; Function to return the minimum changes required ; To store the count of minimum changes , number of ones and the number of zeroes ; First character has to be '1' ; If condition fails changes need to be made ; Return the required count ; Driver code"
Solution in C#:

using System ; class GFG { static int minChanges ( char [ ] str , int n ) { int count = 0 , zeros = 0 , ones = 0 ; if ( str [ 0 ] != '1' ) { count ++ ; ones ++ ; } for ( int i = 1 ; i < n ; i ++ ) { if ( str [ i ] == '0' ) zeros ++ ; else ones ++ ; if ( zeros > ones ) { zeros -- ; ones ++ ; count ++ ; } } return count ; } public static void Main ( String [ ] args ) { char [ ] str = "0000" . ToCharArray ( ) ; int n = str . Length ; Console . Write ( minChanges ( str , n ) ) ; } }


"Minimum changes required to make all Array elements Prime | C # program to implement the above approach ; Function to generate all primes ; If p is a prime ; Mark all its multiples as non - prime ; Store all prime numbers ; Return the list of primes ; Function to calculate the minimum increments to convert every array elements to a prime ; Extract maximum element of the given array ; Extract the index which has the next greater prime ; Store the difference between the prime and the array element ; Driver code"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static List < int > SieveOfEratosthenes ( int n ) { bool [ ] prime = new bool [ 2 * n + 1 ] ; Array . Fill ( prime , true ) ; for ( int p = 2 ; p * p <= 2 * n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= 2 * n ; i += p ) prime [ i ] = false ; } } List < int > primes = new List < int > ( ) ; for ( int p = 2 ; p <= 2 * n ; p ++ ) if ( prime [ p ] ) primes . Add ( p ) ; return primes ; } static int minChanges ( int [ ] arr ) { int n = arr . Length ; int ans = 0 ; int maxi = arr [ 0 ] ; for ( int i = 1 ; i < arr . Length ; i ++ ) maxi = Math . Max ( maxi , arr [ i ] ) ; List < int > primes = SieveOfEratosthenes ( maxi ) ; for ( int i = 0 ; i < n ; i ++ ) { int x = - 1 ; for ( int j = 0 ; j < primes . Count ; j ++ ) { if ( arr [ i ] == primes [ j ] ) { x = j ; break ; } else if ( arr [ i ] < primes [ j ] ) { x = j ; break ; } } int minm = Math . Abs ( primes [ x ] - arr [ i ] ) ; if ( x > 1 ) { minm = Math . Min ( minm , Math . Abs ( primes [ x - 1 ] - arr [ i ] ) ) ; } ans += minm ; } return ans ; } public static void Main ( string [ ] args ) { int [ ] arr = { 4 , 25 , 13 , 6 , 20 } ; Console . Write ( minChanges ( arr ) ) ; } }


"Minimum changes required to make all element in an array equal | C # program to find minimum changes required to make all elements of the array equal ; Function to count of minimum changes required to make all elements equal ; Store the count of each element as key - value pair in Dictionary ; Traverse through the Dictionary and find the maximum occurring element ; Return count of all element minus count of maximum occurring element ; Driver code ; Function call"
Solution in C#:

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


"Minimum changes required to make each path in a matrix palindrome | C # program to implement the above approach ; Function for counting changes ; Maximum distance possible is ( n - 1 + m - 1 ) ; Stores the maximum element ; Update the maximum ; Stores frequencies of values for respective distances ; Initialize frequencies of cells as 0 ; Count frequencies of cell values in the matrix ; Increment frequency of value at distance i + j ; Store the most frequent value at i - th distance from ( 0 , 0 ) and ( N - 1 , M - 1 ) ; Calculate max frequency and total cells at distance i ; Count changes required to convert all cells at i - th distance to most frequent value ; Driver Code"
Solution in C#:

using System ; class GFG { static int countChanges ( int [ , ] matrix , int n , int m ) { int i , j , dist = n + m - 1 ; int Max_element = 0 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < m ; j ++ ) { Max_element = Math . Max ( Max_element , matrix [ i , j ] ) ; } } int [ , ] freq = new int [ dist , Max_element + 1 ] ; for ( i = 0 ; i < dist ; i ++ ) { for ( j = 0 ; j < Max_element + 1 ; j ++ ) freq [ i , j ] = 0 ; } for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < m ; j ++ ) { freq [ i + j , matrix [ i , j ] ] ++ ; } } int min_changes_sum = 0 ; for ( i = 0 ; i < dist / 2 ; i ++ ) { int maximum = 0 ; int total_values = 0 ; for ( j = 0 ; j < Max_element + 1 ; j ++ ) { maximum = Math . Max ( maximum , freq [ i , j ] + freq [ n + m - 2 - i , j ] ) ; total_values += freq [ i , j ] + freq [ n + m - 2 - i , j ] ; } min_changes_sum += total_values - maximum ; } return min_changes_sum ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 7 , 0 , 3 , 1 , 8 , 1 , 3 } , { 0 , 4 , 0 , 1 , 0 , 4 , 0 } , { 3 , 1 , 8 , 3 , 1 , 0 , 7 } } ; int minChanges = countChanges ( mat , 3 , 7 ) ; Console . Write ( minChanges ) ; } }


"Minimum changes required to make first string substring of second string | C # program to find the minimum number of characters to be replaced in string S2 , such that S1 is a substring of S2 ; Function to find the minimum number of characters to be replaced in string S2 , such that S1 is a substring of S2 ; Get the sizes of both strings ; Traverse the string S2 ; From every index in S2 , check the number of mis - matching characters in substring of length of S1 ; Take minimum of prev and current mis - match ; return answer ; Driver Code"
Solution in C#:

using System ; class GFG { static int minimumChar ( String S1 , String S2 ) { int n = S1 . Length ; int m = S2 . Length ; int ans = Int32 . MaxValue ; for ( int i = 0 ; i < m - n + 1 ; i ++ ) { int minRemovedChar = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( S1 [ j ] != S2 [ i + j ] ) { minRemovedChar ++ ; } } ans = Math . Min ( minRemovedChar , ans ) ; } return ans ; } public static void Main ( ) { String S1 = " abc " ; String S2 = " paxzk " ; Console . WriteLine ( minimumChar ( S1 , S2 ) ) ; } }


"Minimum changes to a string to make all substrings distinct | C # program to count number of changes to make all substrings distinct . ; Returns minimum changes to str so that no substring is repeated . ; If length is more than maximum allowed characters , we cannot get the required string . ; Variable to store count of distinct characters ; To store counts of different characters ; Answer is , n - number of distinct char ; Driver function"
Solution in C#:

using System ; class GFG { static int MAX_CHAR = 26 ; public static int minChanges ( string str ) { int n = str . Length ; if ( n > MAX_CHAR ) return - 1 ; int dist_count = 0 ; int [ ] count = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) count [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( count [ str [ i ] - ' a ' ] == 0 ) dist_count ++ ; count [ str [ i ] - ' a ' ] ++ ; } return ( n - dist_count ) ; } public static void Main ( ) { string str = " aebaecedabbee " ; Console . WriteLine ( minChanges ( str ) ) ; } }


"Minimum characters required to be removed to make frequency of each character unique | C # program to implement the above approach ; Function to find the minimum count of characters required to be deleted to make frequencies of all characters unique ; Stores frequency of each distinct character of str ; Store frequency of each distinct character such that the largest frequency is present at the top ; Stores minimum count of characters required to be deleted to make frequency of each character unique ; Traverse the String ; Update frequency of str [ i ] ; Traverse the map ; Insert current frequency into pq ; Traverse the priority_queue ; Stores topmost element of pq ; Pop the topmost element ; If pq is empty ; Return cntChar ; If frequent and topmost element of pq are equal ; If frequency of the topmost element is greater than 1 ; Insert the decremented value of frequent ; Update cntChar ; Driver Code ; Stores length of str"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int minCntCharDeletionsfrequency ( char [ ] str , int N ) { Dictionary < char , int > mp = new Dictionary < char , int > ( ) ; List < int > pq = new List < int > ( ) ; int cntChar = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . ContainsKey ( str [ i ] ) ) { mp [ str [ i ] ] ++ ; } else { mp . Add ( str [ i ] , 1 ) ; } } foreach ( KeyValuePair < char , int > it in mp ) { pq . Add ( it . Value ) ; } pq . Sort ( ) ; pq . Reverse ( ) ; while ( pq . Count != 0 ) { pq . Sort ( ) ; pq . Reverse ( ) ; int frequent = pq [ 0 ] ; pq . RemoveAt ( 0 ) ; if ( pq . Count == 0 ) { return cntChar ; } if ( frequent == pq [ 0 ] ) { if ( frequent > 1 ) { pq . Add ( frequent - 1 ) ; pq . Sort ( ) ; pq . Reverse ( ) ; } cntChar ++ ; } } return cntChar ; } public static void Main ( String [ ] args ) { String str = " abbbcccd " ; int N = str . Length ; Console . Write ( minCntCharDeletionsfrequency ( str . ToCharArray ( ) , N ) ) ; } }


"Minimum characters required to be removed to sort binary string in ascending order | C # program for the above approach ; Function to find the minimum count of characters to be removed to make the string sorted in ascending order ; Length of given string ; Stores the first occurrence of '1' ; Stores the last occurrence of '0' ; Traverse the string to find the first occurrence of '1' ; Traverse the string to find the last occurrence of '0' ; Return 0 if the str have only one type of character ; Initialize count1 and count0 to count '1' s before lastIdx0 and '0' s after firstIdx1 ; Traverse the string to count0 ; Traverse the string to count1 ; Return the minimum of count0 and count1 ; Driver Code ; Given string str ; Function call"
Solution in C#:

using System . Collections . Generic ; using System ; class GFG { static int minDeletion ( string str ) { int n = str . Length ; int firstIdx1 = - 1 ; int lastIdx0 = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == '1' ) { firstIdx1 = i ; break ; } } for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( str [ i ] == '0' ) { lastIdx0 = i ; break ; } } if ( firstIdx1 == - 1 lastIdx0 == - 1 ) return 0 ; int count1 = 0 , count0 = 0 ; for ( int i = 0 ; i < lastIdx0 ; i ++ ) { if ( str [ i ] == '1' ) { count1 ++ ; } } for ( int i = firstIdx1 + 1 ; i < n ; i ++ ) { if ( str [ i ] == '1' ) { count0 ++ ; } } return Math . Min ( count0 , count1 ) ; } public static void Main ( ) { string str = "1000101" ; Console . WriteLine ( minDeletion ( str ) ) ; } }


"Minimum characters that are to be inserted such that no three consecutive characters are same | C # implementation of the above approach ; Function to return the count of characters that are to be inserted in str such that no three consecutive characters are same ; To store the count of operations required ; A character needs to be inserted after str [ i + 1 ] ; Current three consecutive characters are not same ; Driver code"
Solution in C#:

using System ; class GFG { static int getCount ( string str , int n ) { int cnt = 0 ; int i = 0 ; while ( i < n - 2 ) { if ( str [ i ] == str [ i + 1 ] && str [ i ] == str [ i + 2 ] ) { cnt ++ ; i = i + 2 ; } else { i ++ ; } } return cnt ; } static public void Main ( ) { string str = " aabbbcc " ; int n = str . Length ; Console . WriteLine ( getCount ( str , n ) ) ; } }


"Minimum characters to be added at front to make string palindrome | C # program for getting minimum character to be added at front to make string palindrome ; function for checking string is palindrome or not ; Driver code ; if string becomes palindrome then break ; erase the last element of the string ; print the number of insertion at front"
Solution in C#:

using System ; public class GFG { static bool ispalindrome ( String s ) { int l = s . Length ; for ( int i = 0 , j = l - 1 ; i <= j ; i ++ , j -- ) { if ( s [ i ] != s [ j ] ) { return false ; } } return true ; } public static void Main ( ) { String s = " BABABAA " ; int cnt = 0 ; int flag = 0 ; while ( s . Length > 0 ) { if ( ispalindrome ( s ) ) { flag = 1 ; break ; } else { cnt ++ ; s = s . Substring ( 0 , s . Length - 1 ) ; s . erase ( s . begin ( ) + s . length ( ) - 1 ) ; } } if ( flag == 1 ) { Console . WriteLine ( cnt ) ; } } }


"Minimum characters to be deleted from the beginning of two strings to make them equal | C # Program to count minimum number of characters to be deleted from the beginning of the two strings to make the strings equal ; Function that finds minimum character required to be deleted ; Iterate in the strings ; Check if the characters are not equal ; Return the result ; Driver Code"
Solution in C#:

using System ; class GFG { static int minDel ( string s1 , string s2 ) { int i = s1 . Length ; int j = s2 . Length ; while ( i > 0 && j > 0 ) { if ( s1 [ i - 1 ] != s2 [ j - 1 ] ) { break ; } i -- ; j -- ; } return i + j ; } public static void Main ( ) { string s1 = " geeksforgeeks " , s2 = " peeks " ; Console . Write ( minDel ( s1 , s2 ) ) ; } }


"Minimum characters to be deleted from the end to make given two strings equal | C # program to count minimum number of characters to be deleted to make the strings equal ; Function that finds minimum character required to be deleted ; Iterate in the strings ; Check if the characters are not equal ; Return the result ; Driver code"
Solution in C#:

using System ; class GFG { static int minDel ( String s1 , String s2 ) { int i = 0 ; while ( i < Math . Min ( s1 . Length , s2 . Length ) ) { if ( s1 [ i ] != s2 [ i ] ) { break ; } i ++ ; } int ans = ( ( s1 . Length - i ) + ( s2 . Length - i ) ) ; return ans ; } public static void Main ( String [ ] args ) { String s1 = " geeks " ; String s2 = " geeksfor " ; Console . WriteLine ( minDel ( s1 , s2 ) ) ; } }


"Minimum characters to be replaced to make a string concatenation of a K | C # program for the above approach ; Function to add an edge to graph ; Function to perform DFS traversal on the graph recursively from a given vertex u ; Visit the current vertex ; Total number of nodes in this component ; Increment the frequency of u ; Function for finding the minimum number changes required in given String ; Form the edges according to the given conditions ; Find minimum number of operations ; Frequency array for finding the most frequent character ; Frequency array for finding the most frequent character ; Finding most frequent character ; Change rest of the characters to most frequent one ; Print total number of changes ; Driver Code ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void addEdge ( List < int > [ ] adj , int u , int v ) { adj [ u ] . Add ( v ) ; adj [ v ] . Add ( u ) ; } static int cnt = 0 ; static bool [ ] visited ; static void DFS ( int u , List < int > [ ] adj , int [ ] fre , String S ) { visited [ u ] = true ; cnt ++ ; fre [ S [ u ] - ' a ' ] ++ ; for ( int i = 0 ; i < adj [ u ] . Count ; i ++ ) { if ( ! visited [ adj [ u ] [ i ] ] ) { DFS ( adj [ u ] [ i ] , adj , fre , S ) ; } } } static void minimumOperations ( String S , int m ) { int V = 100 ; List < int > [ ] adj = new List < int > [ V ] ; int total = 0 , N = S . Length ; for ( int i = 0 ; i < adj . Length ; i ++ ) adj [ i ] = new List < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { addEdge ( adj , i , N - i - 1 ) ; addEdge ( adj , N - i - 1 , i ) ; } for ( int i = 0 ; i < N - m ; i ++ ) { addEdge ( adj , i , i + m ) ; addEdge ( adj , i + m , i ) ; } visited = new bool [ V ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! visited [ i ] ) { int [ ] fre = new int [ 26 ] ; cnt = 0 ; int maxx = - 1 ; DFS ( i , adj , fre , S ) ; for ( int j = 0 ; j < 26 ; j ++ ) maxx = Math . Max ( maxx , fre [ j ] ) ; total += cnt - maxx ; } } Console . Write ( total ) ; } public static void Main ( String [ ] args ) { String S = " abaaba " ; int K = 2 ; minimumOperations ( S , K ) ; } }


"Minimum characters to be replaced to make frequency of all characters same | C # implementation to find the minimum characters to be replaced to make frequency of all characters same ; Function to find the minimum operations to convert given string to another with equal frequencies of characters ; Frequency of characters ; Loop to find the frequency of each character ; Sort in decreasing order based on frequency ; Maximum possible answer ; Loop to find the minimum operations required such that frequency of every character is equal ; Driver Code"
Solution in C#:

using System ; class GFG { static int minOperations ( String s ) { int [ ] freq = new int [ 26 ] ; int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) { freq [ s [ i ] - ' A ' ] ++ ; } Array . Sort ( freq ) ; Array . Reverse ( freq ) ; int answer = n ; for ( int i = 1 ; i <= 26 ; i ++ ) { if ( n % i == 0 ) { int x = n / i ; int y = 0 ; for ( int j = 0 ; j < i ; j ++ ) { y += Math . Min ( freq [ j ] , x ) ; } answer = Math . Min ( answer , n - y ) ; } } return answer ; } public static void Main ( String [ ] args ) { String s = " BBC " ; Console . Write ( minOperations ( s ) ) ; } }


"Minimum characters to be replaced to remove the given substring | C # implementation of above approach ; function to calculate minimum characters to replace ; mismatch occurs ; if all characters matched , i . e , there is a substring of ' a ' which is same as string ' b ' ; increment i to index m - 1 such that minimum characters are replaced in ' a ' ; Driver Code"
Solution in C#:

using System ; class GFG { public static int replace ( string A , string B ) { int n = A . Length , m = B . Length ; int count = 0 , i , j ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < m ; j ++ ) { if ( i + j >= n ) { break ; } else if ( A [ i + j ] != B [ j ] ) { break ; } } if ( j == m ) { count ++ ; i += m - 1 ; } } return count ; } public static void Main ( string [ ] args ) { string str1 = " aaaaaaaa " ; string str2 = " aaa " ; Console . WriteLine ( replace ( str1 , str2 ) ) ; } }


"Minimum colors required such that edges forming cycle do not have same color | C # implementation to find the minimum colors required to such that edges forming cycle don 't have same color ; Variable to store the graph ; To store that the vertex is visited or not ; Boolean Value to store that graph contains cycle or not ; Variable to store the color of the edges of the graph ; Function to traverse graph using DFS Traversal ; Loop to iterate for all edges from the source vertex ; If the vertex is not visited ; Condition to check cross and forward edges of the graph ; Presence of Back Edge ; Driver Code ; Loop to run DFS Traversal on vertex which is not visited ; Loop to print the colors of the edges"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static int n = 5 , m = 7 ; class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static List < pair > [ ] g = new List < pair > [ m ] ; static int [ ] col = new int [ n ] ; static bool cyc ; static int [ ] res = new int [ m ] ; static void dfs ( int v ) { col [ v ] = 1 ; foreach ( pair p in g [ v ] ) { int to = p . first , id = p . second ; if ( col [ to ] == 0 ) { dfs ( to ) ; res [ id ] = 1 ; } else if ( col [ to ] == 2 ) { res [ id ] = 1 ; } else { res [ id ] = 2 ; cyc = true ; } } col [ v ] = 2 ; } public static void Main ( String [ ] args ) { for ( int i = 0 ; i < m ; i ++ ) g [ i ] = new List < pair > ( ) ; g [ 0 ] . Add ( new pair ( 1 , 0 ) ) ; g [ 0 ] . Add ( new pair ( 2 , 1 ) ) ; g [ 1 ] . Add ( new pair ( 2 , 2 ) ) ; g [ 1 ] . Add ( new pair ( 3 , 3 ) ) ; g [ 2 ] . Add ( new pair ( 3 , 4 ) ) ; g [ 3 ] . Add ( new pair ( 4 , 5 ) ) ; g [ 4 ] . Add ( new pair ( 2 , 6 ) ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( col [ i ] == 0 ) { dfs ( i ) ; } } Console . Write ( ( cyc ? 2 : 1 ) + "
 " ) ; for ( int i = 0 ; i < m ; ++ i ) { Console . Write ( res [ i ] + " ▁ " ) ; } } }


"Minimum common element in subarrays of all possible lengths | C # program to implement the above approach ; Function to find maximum distance between every two element ; Stores index of last occurence of each array element ; Initialize temp [ ] with - 1 ; Traverse the array ; If array element has not occurred previously ; Update index in temp ; Otherwise ; Compare temp [ a [ i ] ] with distance from its previous occurence and store the maximum ; Compare temp [ i ] with distance of its last occurence from the end of the array and store the maximum ; Function to find the minimum common element in subarrays of all possible lengths ; Function call to find a the maximum distance between every pair of repetition ; Initialize ans [ ] to - 1 ; Check if subarray of length temp [ i ] contains i as one of the common elements ; Find the minimum of all common elements ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void max_distance ( int [ ] a , int [ ] temp , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { temp [ i ] = - 1 ; } for ( int i = 0 ; i < n ; i ++ ) { if ( ! mp . ContainsKey ( a [ i ] ) ) temp [ a [ i ] ] = i + 1 ; else temp [ a [ i ] ] = Math . Max ( temp [ a [ i ] ] , i - mp [ a [ i ] ] ) ; if ( mp . ContainsKey ( a [ i ] ) ) { mp [ a [ i ] ] = i ; } else { mp . Add ( a [ i ] , i ) ; } } for ( int i = 1 ; i <= n ; i ++ ) { if ( temp [ i ] != - 1 ) { if ( mp . ContainsKey ( i ) ) { temp [ i ] = Math . Max ( temp [ i ] , n - mp [ i ] ) ; } else { temp [ i ] = Math . Max ( temp [ i ] , n ) ; } } } } static void min_comm_ele ( int [ ] a , int [ ] ans , int [ ] temp , int n ) { max_distance ( a , temp , n ) ; for ( int i = 1 ; i <= n ; i ++ ) { ans [ i ] = - 1 ; } for ( int i = 1 ; i <= n ; i ++ ) { if ( temp [ i ] >= 0 && ans [ temp [ i ] ] == - 1 ) ans [ temp [ i ] ] = i ; } for ( int i = 1 ; i <= n ; i ++ ) { if ( i > 1 && ans [ i - 1 ] != - 1 ) { if ( ans [ i ] == - 1 ) ans [ i ] = ans [ i - 1 ] ; else ans [ i ] = Math . Min ( ans [ i ] , ans [ i - 1 ] ) ; } Console . Write ( ans [ i ] + " ▁ " ) ; } } static void Main ( ) { int N = 6 ; int [ ] a = { 1 , 3 , 4 , 5 , 6 , 7 } ; int [ ] temp = new int [ 100 ] ; Array . Fill ( temp , 0 ) ; int [ ] ans = new int [ 100 ] ; Array . Fill ( ans , 0 ) ; min_comm_ele ( a , ans , temp , N ) ; } }


"Minimum cost for acquiring all coins with k extra coins allowed with every coin | C # program to acquire all n coins ; function to calculate min cost ; sort the coins value ; calculate no . of coins needed ; calculate sum of all selected coins ; Driver code"
Solution in C#:

using System ; class GFG { static int minCost ( int [ ] coin , int n , int k ) { Array . Sort ( coin ) ; int coins_needed = ( int ) Math . Ceiling ( 1.0 * n / ( k + 1 ) ) ; int ans = 0 ; for ( int i = 0 ; i <= coins_needed - 1 ; i ++ ) ans += coin [ i ] ; return ans ; } public static void Main ( ) { int [ ] coin = { 8 , 5 , 3 , 10 , 2 , 1 , 15 , 25 } ; int n = coin . Length ; int k = 3 ; Console . Write ( minCost ( coin , n , k ) ) ; } }


"Minimum cost for acquiring all coins with k extra coins allowed with every coin | C # program to acquire all n coins at minimum cost with multiple values of k . ; Converts coin [ ] to prefix sum array ; sort the coins value ; Maintain prefix sum array ; Function to calculate min cost when we can get k extra coins after paying cost of one . ; calculate no . of coins needed ; return sum of from prefix array ; Driver Code"
Solution in C#:

using System ; public class GFG { static void preprocess ( int [ ] coin , int n ) { Array . Sort ( coin ) ; for ( int i = 1 ; i <= n - 1 ; i ++ ) coin [ i ] += coin [ i - 1 ] ; } static int minCost ( int [ ] coin , int n , int k ) { int coins_needed = ( int ) Math . Ceiling ( 1.0 * n / ( k + 1 ) ) ; return coin [ coins_needed - 1 ] ; } static public void Main ( ) { int [ ] coin = { 8 , 5 , 3 , 10 , 2 , 1 , 15 , 25 } ; int n = coin . Length ; preprocess ( coin , n ) ; int k = 3 ; Console . WriteLine ( minCost ( coin , n , k ) ) ; k = 7 ; Console . WriteLine ( minCost ( coin , n , k ) ) ; } }


"Minimum cost of choosing the array element | C # program for the above approach ; Function that find the minimum cost of selecting array element ; Sorting the given array in increasing order ; To store the prefix sum of [ ] arr ; Update the pref [ ] to find the cost selecting array element by selecting at most M element ; Print the pref [ ] for the result ; Driver Code"
Solution in C#:

using System ; class GFG { static void minimumCost ( int [ ] arr , int N , int M ) { Array . Sort ( arr ) ; int [ ] pref = new int [ N ] ; pref [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { pref [ i ] = arr [ i ] + pref [ i - 1 ] ; } for ( int i = M ; i < N ; i ++ ) { pref [ i ] += pref [ i - M ] ; } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( pref [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 6 , 19 , 3 , 4 , 4 , 2 , 6 , 7 , 8 } ; int M = 2 ; int N = arr . Length ; minimumCost ( arr , N , M ) ; } }


"Minimum cost of flipping characters required to convert Binary String to 0 s only | C # program to implement the above approach ; Function to find the minimum cost to convert given string to 0 s only ; Length of string ; Stores the index of leftmost '1' in str ; Update the index of leftmost '1' in str ; Stores the index of rightmost '1' in str ; Update the index of rightmost '1' in str ; If str does not contain any '1' s ; No changes required ; Stores minimum cost ; Iterating through str form left_1 to right_1 ; Stores length of consecutive 0 s ; Calculate length of consecutive 0 s ; If a substring of 0 s exists ; Update minimum cost ; Printing the minimum cost ; Driver code"
Solution in C#:

using System ; class GFG { public static void convert_to_allzeroes ( string str , int a , int b ) { int len = str . Length ; int left_1 , i = 0 ; while ( i < len && str [ i ] == '0' ) i ++ ; left_1 = i ; int right_1 ; i = len - 1 ; while ( i >= 0 && str [ i ] == '0' ) i -- ; right_1 = i ; if ( left_1 == len && right_1 == - 1 ) { Console . Write ( 0 ) ; return ; } int cost = a , zeroes ; for ( i = left_1 ; i <= right_1 ; i ++ ) { zeroes = 0 ; while ( i < len && str [ i ] == '0' ) { zeroes ++ ; i ++ ; } if ( zeroes != 0 ) cost += Math . Min ( zeroes * b , a ) ; } Console . Write ( cost ) ; } public static void Main ( ) { string str = "01101110" ; int A = 5 , B = 1 ; convert_to_allzeroes ( str , A , B ) ; } }


"Minimum cost of reducing Array by merging any adjacent elements repetitively | C # program for the above approach ; Function to find the total minimum cost of merging two consecutive numbers ; Find the size of numbers [ ] ; If array is empty , return 0 ; To store the prefix Sum of numbers array numbers [ ] ; Traverse numbers [ ] to find the prefix sum ; dp table to memoised the value ; Iterate for length >= 1 ; Find sum in range [ i . . j ] ; Initialise dp [ i , j ] to int . MaxValue ; Iterate for all possible K to find the minimum cost ; Update the minimum sum ; Return the readonly minimum cost ; Driver Code ; Given set of numbers ; Function Call"
Solution in C#:

 using System ; class GFG { static int mergeTwoNumbers ( int [ ] numbers ) { int len , i , j , k ; int n = numbers . Length ; if ( numbers . Length == 0 ) { return 0 ; } int [ ] prefixSum = new int [ n + 1 ] ; for ( i = 1 ; i <= n ; i ++ ) { prefixSum [ i ] = prefixSum [ i - 1 ] + numbers [ i - 1 ] ; } int [ , ] dp = new int [ n + 1 , n + 1 ] ; for ( len = 2 ; len <= n ; len ++ ) { for ( i = 1 ; i <= n - len + 1 ; i ++ ) { j = i + len - 1 ; int sum = prefixSum [ j ] - prefixSum [ i - 1 ] ; dp [ i , j ] = int . MaxValue ; for ( k = i ; k < j ; k ++ ) { dp [ i , j ] = Math . Min ( dp [ i , j ] , dp [ i , k ] + dp [ k + 1 , j ] + sum ) ; } } } return dp [ 1 , n ] ; } public static void Main ( String [ ] args ) { int [ ] arr1 = { 6 , 4 , 4 , 6 } ; Console . Write ( mergeTwoNumbers ( arr1 ) + "
 " ) ; } }


"Minimum cost path in a Matrix by moving only on value difference of X | C # implementation to find the minimum number of operations required to move from ( 1 , 1 ) to ( N , M ) ; Function to find the minimum operations required to move to bottom - right cell of matrix ; Condition to check if the current cell is the bottom - right cell of the matrix ; Condition to check if the current cell is out of matrix ; Condition to check if the current indices is already computed ; Condition to check that the movement with the current value is not possible ; Recursive call to compute the number of operation required to compute the value ; Function to find the minimum number of operations required to reach the bottom - right cell ; Loop to iterate over every possible cell of the matrix ; Driver Code ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static long MAX = ( long ) 1e18 ; static long n , m ; static List < List < long > > v = new List < List < long > > ( ) ; static long [ , ] dp = new long [ 151 , 151 ] ; static long min_operation ( long i , long j , long val , long x ) { if ( i == n - 1 && j == m - 1 ) { if ( val > v [ ( int ) i ] [ ( int ) j ] ) { return dp [ ( int ) i , ( int ) j ] = MAX ; } else { return dp [ ( int ) i , ( int ) j ] = v [ ( int ) i ] [ ( int ) j ] - val ; } } if ( i == n j == m ) { return dp [ ( int ) i , ( int ) j ] = MAX ; } if ( dp [ ( int ) i , ( int ) j ] != - 1 ) { return dp [ ( int ) i , ( int ) j ] ; } if ( val > v [ ( int ) i ] [ ( int ) j ] ) { return dp [ ( int ) i , ( int ) j ] = MAX ; } long temp = v [ ( int ) i ] [ ( int ) j ] - val ; temp += Math . Min ( min_operation ( i + 1 , j , val + x , x ) , min_operation ( i , j + 1 , val + x , x ) ) ; return dp [ ( int ) i , ( int ) j ] = temp ; } static long solve ( long x ) { long ans = Int64 . MaxValue ; for ( long i = 0 ; i < n ; i ++ ) { for ( long j = 0 ; j < m ; j ++ ) { long val = v [ ( int ) i ] [ ( int ) j ] - x * ( i + j ) ; for ( long k = 0 ; k < dp . GetLength ( 0 ) ; k ++ ) { for ( long l = 0 ; l < dp . GetLength ( 1 ) ; l ++ ) { dp [ k , l ] = - 1 ; } } val = min_operation ( 0 , 0 , val , x ) ; ans = Math . Min ( ans , val ) ; } } return ans ; } static public void Main ( ) { for ( int i = 0 ; i < 151 ; i ++ ) { v . Add ( new List < long > ( ) ) ; v [ i ] . Add ( 0 ) ; v [ i ] . Add ( 0 ) ; } v [ 0 ] [ 0 ] = 15 ; v [ 0 ] [ 1 ] = 153 ; v [ 1 ] [ 0 ] = 135 ; v [ 1 ] [ 1 ] = 17 ; n = 2 ; m = 2 ; long x = 3 ; Console . WriteLine ( solve ( x ) ) ; } }


"Minimum cost required to move all elements to the same position | C # program to implement the above approach ; Function to find the minimum cost required to place all elements in the same position ; Stores the count of even and odd elements ; Traverse the array arr [ ] ; Count even elements ; Count odd elements ; Print the minimum count ; Driver Code ; Given array ; Function Call"
Solution in C#:

using System ; class GFG { public void minCost ( int [ ] arr ) { int odd = 0 , even = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( arr [ i ] % 2 == 0 ) even ++ ; else odd ++ ; } Console . Write ( Math . Min ( even , odd ) ) ; } public static void Main ( ) { GFG obj = new GFG ( ) ; int [ ] arr = { 1 , 2 , 3 } ; obj . minCost ( arr ) ; } }


"Minimum cost required to rearrange a given array to make it equal to another given array | C # program for the above approach ; Function to find lower_bound ; Function to find length of the longest common subsequence ; Find position where element is to be inserted ; Return the length of LCS ; Function to find the minimum cost required to convert the sequence A exactly same as B ; Auxiliary array ; Stores positions of elements of A [ ] ; Initialize index array with - 1 ; Update the index array with index of corresponding elements of B ; Place only A 's array values  with its mapped values  into nums array ; Find LCS ; No of elements to be added in array A [ ] ; Stores minimum cost ; Print the minimum cost ; Driver code ; Given array A [ ] ; Given C ; Size of arr A ; Size of arr B ; Function call"
Solution in C#:

using System ; class GFG { static int LowerBound ( int [ ] a , int k , int x ) { int l = - 1 ; int r = k ; while ( l + 1 < r ) { int m = ( l + r ) >> 1 ; if ( a [ m ] >= x ) { r = m ; } else { l = m ; } } return r ; } static int findLCS ( int [ ] nums , int N ) { int k = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int pos = LowerBound ( nums , k , nums [ i ] ) ; nums [ pos ] = nums [ i ] ; if ( k == pos ) { k = pos + 1 ; } } return k ; } static int minimumCost ( int [ ] A , int [ ] B , int M , int N , int C ) { int [ ] nums = new int [ 100000 ] ; int [ ] index = new int [ 100000 ] ; for ( int i = 0 ; i < 100000 ; i ++ ) index [ i ] = - 1 ; for ( int i = 0 ; i < N ; i ++ ) { index [ B [ i ] ] = i ; } int k = 0 ; for ( int i = 0 ; i < M ; i ++ ) { if ( index [ A [ i ] ] != - 1 ) { nums [ k ++ ] = index [ A [ i ] ] ; } } int lcs_length = findLCS ( nums , k ) ; int elements_to_be_added = N - lcs_length ; int min_cost = elements_to_be_added * C ; Console . WriteLine ( min_cost ) ; return 0 ; } public static void Main ( ) { int [ ] A = { 1 , 6 , 3 , 5 , 10 } ; int [ ] B = { 3 , 1 , 5 } ; int C = 2 ; int M = A . Length ; int N = B . Length ; minimumCost ( A , B , M , N , C ) ; } }


"Minimum cost to build N blocks from one block | C # program to Minimum cost to build N blocks from one block ; Function to calculate min cost to build N blocks ; Initialize base case ; Recurence when i is odd ; Recurence when i is even ; Driver code"
Solution in C#:

 using System ; class GFG { static int minCost ( int n , int x , int y , int z ) { int [ ] dp = new int [ n + 1 ] ; dp [ 0 ] = dp [ 1 ] = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { if ( i % 2 == 1 ) { dp [ i ] = Math . Min ( dp [ ( i + 1 ) / 2 ] + x + z , dp [ i - 1 ] + y ) ; } else { dp [ i ] = Math . Min ( dp [ i / 2 ] + x , dp [ i - 1 ] + y ) ; } } return dp [ n ] ; } public static void Main ( String [ ] args ) { int n = 5 , x = 2 , y = 1 , z = 3 ; Console . Write ( minCost ( n , x , y , z ) + "
 " ) ; } }


"Minimum cost to buy N kilograms of sweet for M persons | C # program to minimum cost to buy N kilograms of sweet for M persons ; Function to find the minimum cost of sweets ; Defining the sweet array ; DP array to store the values ; Since index starts from 1 we reassign the array into sweet ; Assigning base cases for dp array ; At 0 it is free ; Package not available for desirable amount of sweets ; Buying the ' k ' kg package and assigning it to dp array ; If no solution , select from previous k - 1 packages ; If solution does not exist ; Print the solution ; Driver code ; Calling the desired function"
Solution in C#:

using System ; class GFG { static int find ( int m , int n , int [ ] adj ) { int [ ] sweet = new int [ n + 1 ] ; int [ , , ] dp = new int [ n + 1 , n + 1 , n + 1 ] ; sweet [ 0 ] = 0 ; for ( int i = 1 ; i <= m ; ++ i ) sweet [ i ] = adj [ i - 1 ] ; for ( int i = 0 ; i <= m ; ++ i ) { for ( int k = 0 ; k <= n ; ++ k ) dp [ i , 0 , k ] = 0 ; for ( int k = 1 ; k <= n ; ++ k ) dp [ i , k , 0 ] = - 1 ; } for ( int i = 0 ; i <= m ; ++ i ) { for ( int j = 1 ; j <= n ; ++ j ) { for ( int k = 1 ; k <= n ; ++ k ) { dp [ i , j , k ] = - 1 ; if ( i > 0 && j >= k && sweet [ k ] > 0 && dp [ i - 1 , j - k , k ] != - 1 ) dp [ i , j , k ] = dp [ i - 1 , j - k , k ] + sweet [ k ] ; if ( dp [ i , j , k ] == - 1 || ( dp [ i , j , k - 1 ] != - 1 && dp [ i , j , k ] > dp [ i , j , k - 1 ] ) ) dp [ i , j , k ] = dp [ i , j , k - 1 ] ; } } } if ( dp [ m , n , n ] == - 1 ) return 0 ; else return dp [ m , n , n ] ; } public static void Main ( ) { int m = 3 ; int [ ] adj = { 2 , 1 , 3 , 0 , 4 , 10 } ; int n = adj . Length ; Console . Write ( find ( m , n , adj ) ) ; } }


"Minimum cost to complete given tasks if cost of 1 , 7 and 30 days are given | C # program for the above approach ; Function to find the minimum cost to hire the workers for the given days in the array days [ ] ; Initialize the array dp ; Minimum Cost for Nth day ; Pointer of the array arr [ ] ; Traverse from right to left ; If worker is hired for 1 day ; If worker is hired for 7 days ; If worker is hired for 30 days ; Update the value of dp [ i ] as minimum of 3 options ; If the day is not at the array arr [ ] ; Return the answer ; Driver Code"
Solution in C#:

using System ; class GFG { static int MinCost ( int [ ] days , int [ ] cost , int N ) { int size = days [ N - 1 ] + 1 ; int [ ] dp = new int [ size ] ; dp [ size - 1 ] = Math . Min ( cost [ 0 ] , Math . Min ( cost [ 1 ] , cost [ 2 ] ) ) ; int ptr = N - 2 ; for ( int i = size - 2 ; i > 0 ; i -- ) { if ( ptr >= 0 && days [ ptr ] == i ) { int val1 = dp [ i + 1 ] + cost [ 0 ] ; int val2 = cost [ 1 ] + ( ( i + 7 >= size ) ? 0 : dp [ i + 7 ] ) ; int val3 = cost [ 2 ] + ( ( i + 30 >= size ) ? 0 : dp [ i + 30 ] ) ; dp [ i ] = Math . Min ( val1 , Math . Min ( val2 , val3 ) ) ; ptr -- ; } else { dp [ i ] = dp [ i + 1 ] ; } } return dp [ 1 ] ; } public static void Main ( ) { int [ ] arr = { 2 , 4 , 6 , 7 , 8 , 10 , 17 } ; int [ ] cost = { 3 , 8 , 20 } ; int N = arr . Length ; Console . WriteLine ( MinCost ( arr , cost , N ) ) ; } }


"Minimum cost to construct a string | C # Program to find minimum cost to construct a string ; Initially all characters are un - seen ; Marking seen characters ; Count total seen character , and that is the cost ; Driver code ; s is the string that needs to be constructed"
Solution in C#:

using System ; class GFG { static int minCost ( char [ ] s ) { bool [ ] alphabets = new bool [ 26 ] ; for ( int i = 0 ; i < s . Length ; i ++ ) { alphabets [ ( int ) s [ i ] - 97 ] = true ; } int count = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( alphabets [ i ] ) { count ++ ; } } return count ; } public static void Main ( String [ ] args ) { String s = " geeksforgeeks " ; Console . WriteLine ( " Total ▁ cost ▁ to ▁ construct ▁ " + s + " ▁ is ▁ " + minCost ( s . ToCharArray ( ) ) ) ; } }


"Minimum cost to convert M to N by repeated addition of its even divisors | C # program for the above approach ; Function to find the value of minimum steps to convert m to n ; Base Case ; If n exceeds m ; Iterate through all possible even divisors of m ; If m is divisible by i , then find the minimum cost ; Add the cost to convert m to m + i and recursively call next state ; Return min_cost ; Driver Code ; Function Call ; If conversion is not possible ; Print the cost"
Solution in C#:

using System ; class GFG { static int inf = 1000000008 ; public static int minSteps ( int m , int n ) { if ( n == m ) return 0 ; if ( m > n ) return inf ; int min_cost = inf ; for ( int i = 2 ; i < m ; i += 2 ) { if ( m % i == 0 ) { min_cost = Math . Min ( min_cost , m / i + minSteps ( m + i , n ) ) ; } } return min_cost ; } public static void Main ( String [ ] args ) { int M = 6 ; int N = 24 ; int minimum_cost = minSteps ( M , N ) ; minimum_cost = minimum_cost == inf ? - 1 : minimum_cost ; Console . WriteLine ( minimum_cost ) ; } }


"Minimum cost to convert given string to consist of only vowels | C # program for the above approach ; Function to find the minimum cost ; Store vowels ; Loop for iteration of string ; Loop to calculate the cost ; Add minimum cost ; Driver Code ; Given string ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int min_cost ( string st ) { string vow = " aeiou " ; int cost = 0 ; for ( int i = 0 ; i < st . Length ; i ++ ) { List < int > costs = new List < int > ( ) ; for ( int j = 0 ; j < 5 ; j ++ ) costs . Add ( Math . Abs ( st [ i ] - vow [ j ] ) ) ; int minx = Int32 . MaxValue ; foreach ( int x in costs ) { if ( x < minx ) { minx = x ; } } cost += minx ; } return cost ; } public static void Main ( ) { string str = " abcde " ; Console . WriteLine ( min_cost ( str ) ) ; } }


"Minimum cost to convert one given string to another using swap , insert or delete operations | C # program for the above approach ; Function to find the minimum cost to convert string a to b ; Stores the frequency of string a and b respectively ; Store the frequencies of characters in a ; Store the frequencies of characters in b ; Minimum cost to convert A to B ; Find the minimum cost ; Print the minimum cost ; Driver code ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static void minimumCost ( string a , string b ) { int [ ] fre1 = new int [ 256 ] ; int [ ] fre2 = new int [ 256 ] ; foreach ( char c in a . ToCharArray ( ) ) fre1 [ ( int ) ( c ) ] ++ ; foreach ( char c in b . ToCharArray ( ) ) fre2 [ ( int ) ( c ) ] ++ ; int mincost = 0 ; for ( int i = 0 ; i < 256 ; i ++ ) { mincost += Math . Abs ( fre1 [ i ] - fre2 [ i ] ) ; } Console . Write ( mincost ) ; } public static void Main ( ) { string A = "1AB + - " , B = " cc " ; minimumCost ( A , B ) ; } }


"Minimum cost to convert str1 to str2 with the given operations | C # implementation of the approach ; Function to return the minimum cost to convert str1 to sr2 ; For every character of str1 ; If current character is not equal in both the strings ; If the next character is also different in both the strings then these characters can be swapped ; Change the current character ; Driver code"
Solution in C#:

using System ; class GFG { static int minCost ( string str1 , string str2 , int n ) { int cost = 0 ; char [ ] array = str1 . ToCharArray ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( str1 [ i ] != str2 [ i ] ) { if ( i < n - 1 && str1 [ i + 1 ] != str2 [ i + 1 ] ) { char temp = array [ i ] ; array [ i ] = array [ i + 1 ] ; array [ i + 1 ] = temp ; cost ++ ; } else { cost ++ ; } } } return cost ; } static public void Main ( ) { string str1 = " abb " , str2 = " bba " ; int n = str1 . Length ; Console . WriteLine ( minCost ( str1 , str2 , n ) ) ; } }


"Minimum cost to convert string into palindrome | C # program to find minimum cost to make a palindrome . ; Function to return cost ; length of string ; Iterate from both sides of string . If not equal , a cost will be there ; Driver code"
Solution in C#:

using System ; class GFG { static int cost ( String str ) { int len = str . Length ; int res = 0 ; for ( int i = 0 , j = len - 1 ; i < j ; i ++ , j -- ) if ( str [ i ] != str [ j ] ) res += Math . Min ( str [ i ] , str [ j ] ) - ' a ' + 1 ; return res ; } public static void Main ( ) { string str = " abcdef " ; Console . WriteLine ( cost ( str ) ) ; } }


"Minimum cost to empty Array where cost of removing an element is 2 ^ ( removed_count ) * arr [ i ] | C # implementation to find the minimum cost of removing all elements from the array ; Reverse array in decreasing order ; Function to find the minimum cost of removing elements from the array ; Sorting in Increasing order ; Loop to find the minimum cost of removing elements ; Driver Code ; Function call"
Solution in C#:

using System ; class GFG { static long [ ] reverse ( long [ ] a ) { int i , n = a . Length ; long t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } return a ; } static long removeElements ( long [ ] arr , int n ) { Array . Sort ( arr ) ; arr = reverse ( arr ) ; long ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans += ( long ) ( arr [ i ] * Math . Pow ( 2 , i ) ) ; } return ans ; } public static void Main ( String [ ] args ) { int n = 4 ; long [ ] arr = { 3 , 1 , 2 , 3 } ; Console . Write ( removeElements ( arr , n ) ) ; } }


"Minimum cost to fill given weight in a bag | C # Code for Minimum cost to fill given weight in a bag ; cost [ ] initial cost array including unavailable packet W capacity of bag ; val [ ] and wt [ ] arrays val [ ] array to store cost of ' i ' kg packet of orange wt [ ] array weight of packet of orange ; traverse the original cost [ ] array and skip unavailable packets and make val [ ] and wt [ ] array . size variable tells the available number of distinct weighted packets ; fill 0 th row with infinity ; fill 0 'th column with 0 ; now check for each weight one by one and fill the matrix according to the condition ; wt [ i - 1 ] > j means capacity of bag is less then weight of item ; here we check we get minimum cost either by including it or excluding it ; exactly weight W can not be made by given weights ; Driver program to test above function"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static int MinimumCost ( int [ ] cost , int n , int W ) { List < int > val = new List < int > ( ) ; List < int > wt = new List < int > ( ) ; int size = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( cost [ i ] != - 1 ) { val . Add ( cost [ i ] ) ; wt . Add ( i + 1 ) ; size ++ ; } } n = size ; int [ , ] min_cost = new int [ n + 1 , W + 1 ] ; for ( int i = 0 ; i <= W ; i ++ ) min_cost [ 0 , i ] = int . MaxValue ; for ( int i = 1 ; i <= n ; i ++ ) min_cost [ i , 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= W ; j ++ ) { if ( wt [ i - 1 ] > j ) min_cost [ i , j ] = min_cost [ i - 1 , j ] ; else min_cost [ i , j ] = Math . Min ( min_cost [ i - 1 , j ] , min_cost [ i , j - wt [ i - 1 ] ] + val [ i - 1 ] ) ; } } return ( min_cost [ n , W ] == int . MaxValue ) ? - 1 : min_cost [ n , W ] ; } public static void Main ( ) { int [ ] cost = { 1 , 2 , 3 , 4 , 5 } ; int W = 5 ; int n = cost . Length ; Console . WriteLine ( MinimumCost ( cost , n , W ) ) ; } }


"Minimum cost to fill given weight in a bag | C # program to find minimum cost to get exactly W Kg with given packets ; Returns the best obtainable price for a rod of length n and price [ ] as prices of different pieces ; Build the table val [ ] in bottom up manner and return the last entry from the table ; Driver code"
Solution in C#:

using System ; class GFG { static int minCost ( int [ ] cost , int n ) { int [ ] dp = new int [ n + 1 ] ; dp [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int min_cost = Int32 . MaxValue ; for ( int j = 0 ; j < i ; j ++ ) if ( j < n && cost [ j ] != - 1 ) min_cost = Math . Min ( min_cost , cost [ j ] + dp [ i - j - 1 ] ) ; dp [ i ] = min_cost ; } return dp [ n ] ; } static void Main ( ) { int [ ] cost = { 20 , 10 , 4 , 50 , 100 } ; int W = cost . Length ; Console . Write ( minCost ( cost , W ) ) ; } }


"Minimum cost to form a number X by adding up powers of 2 | C # implementation of the approach ; Function to return the minimum cost ; Re - compute the array ; Add answers for set bits ; If bit is set ; Increase the counter ; Right shift the number ; Driver code"
Solution in C#:

using System ; class GFG { public static int MinimumCost ( int [ ] a , int n , int x ) { for ( int i = 1 ; i < n ; i ++ ) { a [ i ] = Math . Min ( a [ i ] , 2 * a [ i - 1 ] ) ; } int ind = 0 ; int sum = 0 ; while ( x > 0 ) { if ( x != 0 ) sum += a [ ind ] ; ind ++ ; x = x >> 1 ; } return sum ; } public static void Main ( ) { int [ ] a = { 20 , 50 , 60 , 90 } ; int x = 7 ; int n = a . Length ; Console . WriteLine ( MinimumCost ( a , n , x ) ) ; } }


"Minimum cost to generate any permutation of the given string | C # program for the above approach ; Function to find the minimum cost to form any permutation of string s ; Base Case ; Return the precomputed state ; Iterate over the string and check all possible characters available for current position ; Check if character can be placed at current position ; As there is no previous character so the cost for 1 st character is 0 ; Find the cost of current character and move to next position ; Store the answer for each current state ; Function that returns true if the current bit is set ; Function that generates any permutation of the given string with minimum cost ; Initialize dp table ; Set all the bits of the current character id ; Minimum cost of generating the permutation ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { public static int solve ( int [ , ] a , String s , int n , int prev , int mask , int [ , ] dp ) { if ( mask == 0 ) return 0 ; if ( dp [ mask , prev + 1 ] != - 1 ) return dp [ mask , prev + 1 ] ; int ans = 10000 ; for ( int i = 0 ; i < s . Length ; i ++ ) { int id = s [ i ] - ' a ' ; if ( check ( mask , id ) ) { if ( prev == - 1 ) { ans = Math . Min ( ans , solve ( a , s , n , id , mask ^ ( 1 << id ) , dp ) ) ; } else { ans = Math . Min ( ans , a [ prev , id ] + solve ( a , s , n , id , mask ^ ( 1 << id ) , dp ) ) ; } } } dp [ mask , prev + 1 ] = ans ; return ans ; } public static bool check ( int mask , int i ) { int c = ( mask & ( 1 << i ) ) ; return c != 0 ; } static void generatePermutation ( int mask , int n , int [ , ] a , String s ) { int [ , ] dp = new int [ ( 1 << n ) + 5 , n + 5 ] ; for ( int i = 0 ; i < ( 1 << n ) + 5 ; i ++ ) for ( int j = 0 ; j < n + 5 ; j ++ ) dp [ i , j ] = - 1 ; for ( int i = 0 ; i < s . Length ; i ++ ) { int id = s [ i ] - ' a ' ; mask |= ( 1 << id ) ; } Console . WriteLine ( solve ( a , s , n , - 1 , mask , dp ) ) ; } public static void Main ( String [ ] args ) { int N = 5 ; String str = " abcde " ; int [ , ] mat = { { 0 , 5 , 1 , 5 , 3 } , { 4 , 0 , 9 , 4 , 2 } , { 7 , 9 , 0 , 10 , 7 } , { 1 , 2 , 8 , 0 , 2 } , { 3 , 9 , 7 , 7 , 0 } } ; generatePermutation ( 0 , N , mat , str ) ; } }


"Minimum cost to make Longest Common Subsequence of length k | C # program to find subarray with sum closest to 0 ; Return Minimum cost to make LCS of length k ; If k is 0. ; If length become less than 0 , return big number . ; If state already calculated . ; Finding the cost ; Finding minimum cost and saving the state value ; Driver code"
Solution in C#:

using System ; class GFG { static int N = 30 ; static int solve ( char [ ] X , char [ ] Y , int l , int r , int k , int [ , , ] dp ) { if ( k == 0 ) { return 0 ; } if ( l < 0 r < 0 ) { return ( int ) 1e9 ; } if ( dp [ l , r , k ] != - 1 ) { return dp [ l , r , k ] ; } int cost = ( X [ l ] - ' a ' ) ^ ( Y [ r ] - ' a ' ) ; return dp [ l , r , k ] = Math . Min ( Math . Min ( cost + solve ( X , Y , l - 1 , r - 1 , k - 1 , dp ) , solve ( X , Y , l - 1 , r , k , dp ) ) , solve ( X , Y , l , r - 1 , k , dp ) ) ; } public static void Main ( String [ ] args ) { char [ ] X = " abble " . ToCharArray ( ) ; char [ ] Y = " pie " . ToCharArray ( ) ; int n = X . Length ; int m = Y . Length ; int k = 2 ; int [ , , ] dp = new int [ N , N , N ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { for ( int l = 0 ; l < N ; l ++ ) { dp [ i , j , l ] = - 1 ; } } } int ans = solve ( X , Y , n - 1 , m - 1 , k , dp ) ; Console . WriteLine ( ans == 1e9 ? - 1 : ans ) ; } }


"Minimum cost to make a string free of a subsequence | C # implementation of the approach ; Function to return the minimum cost ; Traverse the string ; Min Cost to remove ' c ' ; Min Cost to remove subsequence " co " ; Min Cost to remove subsequence " cod " ; Min Cost to remove subsequence " code " ; Return the minimum cost ; Driver program"
Solution in C#:

using System ; class GFG { public static int findCost ( string str , int [ ] arr , int n ) { long costofC = 0 , costofO = 0 , costofD = 0 , costofE = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' c ' ) { costofC += arr [ i ] ; } else if ( str [ i ] == ' o ' ) { costofO = Math . Min ( costofC , costofO + arr [ i ] ) ; } else if ( str [ i ] == ' d ' ) { costofD = Math . Min ( costofO , costofD + arr [ i ] ) ; } else if ( str [ i ] == ' e ' ) { costofE = Math . Min ( costofD , costofE + arr [ i ] ) ; } } return ( int ) costofE ; } public static void Main ( string [ ] args ) { string str = " geekcodergeeks " ; int [ ] arr = new int [ ] { 1 , 2 , 1 , 3 , 4 , 2 , 6 , 4 , 6 , 2 , 3 , 3 , 3 , 2 } ; int n = arr . Length ; Console . Write ( findCost ( str , arr , n ) ) ; } }


"Minimum cost to make all array elements equal | C # implementation of the approach ; Function to return the minimum cost to make each array element equal ; To store the count of even numbers present in the array ; To store the count of odd numbers present in the array ; Iterate through the array and find the count of even numbers and odd numbers ; Driver code"
Solution in C#:

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


"Minimum cost to make an Array a permutation of first N natural numbers | C # program to calculate minimum cost to make an Array a permutation of first N natural numbers ; Function to calculate minimum cost for making permutation of size N ; sorting the array in ascending order ; To store the required answer ; Traverse the whole array ; Return the required answer ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { static int make_permutation ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) ans += Math . Abs ( i + 1 - arr [ i ] ) ; return ans ; } public static void Main ( string [ ] args ) { int [ ] arr = { 5 , 3 , 8 , 1 , 1 } ; int n = arr . Length ; Console . WriteLine ( make_permutation ( arr , n ) ) ; } }


"Minimum cost to make array size 1 by removing larger of pairs | C # program to find minimum cost to reduce array size to 1 , ; function to calculate the minimum cost ; Minimum cost is n - 1 multiplied with minimum element . ; Driver code ."
Solution in C#:

using System ; using System . Linq ; public class GFG { static int cost ( int [ ] a , int n ) { return ( n - 1 ) * a . Min ( ) ; } static public void Main ( ) { int [ ] a = { 4 , 3 , 2 } ; int n = a . Length ; Console . WriteLine ( cost ( a , n ) ) ; } }


"Minimum cost to make two strings identical by deleting the digits | C # code to find minimum cost to make two strings identical ; Function to returns cost of removing the identical characters in 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 cost of removing identical characters in LCS of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] ; If both characters are same , add both of them ; Otherwise find the maximum cost among them ; Returns cost of making X [ ] and Y [ ] identical ; Find LCS of X [ ] and Y [ ] ; Initialize the cost variable ; Find cost of all characters in both strings ; Driver function"
Solution in C#:

using System ; public 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 ] + 2 * ( X [ i - 1 ] - '0' ) ; else L [ i , j ] = L [ i - 1 , j ] > L [ i , j - 1 ] ? L [ i - 1 , j ] : L [ i , j - 1 ] ; } } return L [ m , n ] ; } static int findMinCost ( string X , string Y ) { int m = X . Length , n = Y . Length ; int cost = 0 ; for ( int i = 0 ; i < m ; ++ i ) cost += X [ i ] - '0' ; for ( int i = 0 ; i < n ; ++ i ) cost += Y [ i ] - '0' ; return cost - lcs ( X , Y , m , n ) ; } public static void Main ( ) { string X = "3759" ; string Y = "9350" ; Console . WriteLine ( " Minimum ▁ Cost ▁ to ▁ make ▁ two ▁ strings " + " ▁ identical ▁ is ▁ = ▁ " + findMinCost ( X , Y ) ) ; } }


"Minimum cost to make two strings same | C # implementation of the approach ; Function to return the minimum cost to make the configuration of both the strings same ; Iterate and find the cost ; Find the minimum cost ; Driver Code"
Solution in C#:

using System ; class GFG { static int findCost ( string s1 , string s2 , int a , int b , int c , int d , int n ) { int cost = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s1 [ i ] == s2 [ i ] ) continue ; else { if ( ( s1 [ i ] == '1' && s2 [ i ] == '2' ) || ( s2 [ i ] == '1' && s1 [ i ] == '2' ) ) cost += Math . Min ( d , Math . Min ( a , b + c ) ) ; else if ( ( s1 [ i ] == '2' && s2 [ i ] == '3' ) || ( s2 [ i ] == '2' && s1 [ i ] == '3' ) ) cost += Math . Min ( d , Math . Min ( b , a + c ) ) ; else if ( ( s1 [ i ] == '1' && s2 [ i ] == '3' ) || ( s2 [ i ] == '1' && s1 [ i ] == '3' ) ) cost += Math . Min ( d , Math . Min ( c , a + b ) ) ; } } return cost ; } public static void Main ( ) { string s1 = "121" ; string s2 = "223" ; int a = 2 , b = 3 , c = 4 , d = 10 ; int n = s1 . Length ; Console . WriteLine ( findCost ( s1 , s2 , a , b , c , d , n ) ) ; } }


"Minimum cost to merge all elements of List | C # implementation of the approach ; To store the states of DP ; Function to return the minimum merge cost ; Base case ; If the state has been solved before ; Marking the state as solved ; Reference to dp [ i , j ] ; To store the sum of all the elements in the subarray arr [ i ... j ] ; Loop to iterate the recurrence ; Returning the solved value ; Driver code"
Solution in C#:

using System ; class GFG { static readonly int N = 401 ; static int [ , ] dp = new int [ N , N ] ; static bool [ , ] v = new bool [ N , N ] ; static int minMergeCost ( int i , int j , int [ ] arr ) { if ( i == j ) return 0 ; if ( v [ i , j ] ) return dp [ i , j ] ; v [ i , j ] = true ; int x = dp [ i , j ] ; x = int . MaxValue ; int tot = 0 ; for ( int k = i ; k <= j ; k ++ ) tot += arr [ k ] ; for ( int k = i + 1 ; k <= j ; k ++ ) { x = Math . Min ( x , tot + minMergeCost ( i , k - 1 , arr ) + minMergeCost ( k , j , arr ) ) ; } return x ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 7 } ; int n = arr . Length ; Console . Write ( minMergeCost ( 0 , n - 1 , arr ) ) ; } }


"Minimum cost to merge numbers from 1 to N | C # program to find the Minimum cost to merge numbers from 1 to N . ; Function returns the minimum cost ; Min Heap representation ; Add all elements to heap ; First minimum ; Second minimum ; Multiply them ; Add to the cost ; Push the product into the heap again ; Return the optimal cost ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int GetMinCost ( int N ) { List < int > pq = new List < int > ( ) ; for ( int i = 1 ; i <= N ; i ++ ) { pq . Add ( i ) ; } int cost = 0 ; pq . Sort ( ) ; while ( pq . Count > 1 ) { int mini = pq [ 0 ] ; pq . RemoveAt ( 0 ) ; int secondmini = pq [ 0 ] ; pq . RemoveAt ( 0 ) ; int current = mini * secondmini ; cost += current ; pq . Add ( current ) ; pq . Sort ( ) ; } return cost ; } static void Main ( ) { int N = 5 ; Console . WriteLine ( GetMinCost ( N ) ) ; } }


"Minimum cost to modify a string | C # program to implement the above approach ; Function to return the minimum cost ; Initialize result ; To store the frequency of characters of the string ; Update the frequencies of the characters of the string ; Loop to check all windows from a - z where window size is K ; Starting index of window ; Ending index of window ; Check if the string contains character ; Check if the character is on left side of window find the cost of modification for character add value to count calculate nearest distance of modification ; Check if the character is on right side of window find the cost of modification for character add value to count calculate nearest distance of modification ; Find the minimum of all costs for modifying the string ; Loop to check all windows Here window contains characters before z and after z of window size K ; Starting index of window ; Ending index of window ; Check if the string contains character ; If characters are outside window find the cost for modifying character add value to count ; Find the minimum of all costs for modifying the string ; Driver code"
Solution in C#:

using System ; class GFG { static int minCost ( String str , int K ) { int n = str . Length ; int res = 999999999 , count = 0 , a , b ; int [ ] cnt = new int [ 27 ] ; for ( int i = 0 ; i < n ; i ++ ) cnt [ str [ i ] - ' a ' + 1 ] ++ ; for ( int i = 1 ; i < ( 26 - K + 1 ) ; i ++ ) { a = i ; b = i + K ; count = 0 ; for ( int j = 1 ; j <= 26 ; j ++ ) { if ( cnt [ j ] > 0 ) { if ( j >= a && j >= b ) count = count + ( Math . Min ( j - b , 25 - j + a + 1 ) ) * cnt [ j ] ; else if ( j <= a && j <= b ) count = count + ( Math . Min ( a - j , 25 + j - b + 1 ) ) * cnt [ j ] ; } } res = Math . Min ( res , count ) ; } for ( int i = 26 - K + 1 ; i <= 26 ; i ++ ) { a = i ; b = ( i + K ) % 26 ; count = 0 ; for ( int j = 1 ; j <= 26 ; j ++ ) { if ( cnt [ j ] > 0 ) { if ( j >= b && j <= a ) count = count + ( Math . Min ( j - b , a - j ) ) * cnt [ j ] ; } } res = Math . Min ( res , count ) ; } return res ; } public static void Main ( String [ ] args ) { String str = " abcdefghi " ; int K = 2 ; Console . WriteLine ( minCost ( str , K ) ) ; } }


"Minimum cost to reach end of array array when a maximum jump of K index is allowed | C # implementation of the above approach ; Function to return the minimum cost to reach the last index ; If we reach the last index ; Already visited state ; Initially maximum ; Visit all possible reachable index ; If inside range ; We cannot move any further ; Memoize ; Driver Code"
Solution in C#:

using System ; class GfG { static int FindMinimumCost ( int ind , int [ ] a , int n , int k , int [ ] dp ) { if ( ind == ( n - 1 ) ) return 0 ; else if ( dp [ ind ] != - 1 ) return dp [ ind ] ; else { int ans = int . MaxValue ; for ( int i = 1 ; i <= k ; i ++ ) { if ( ind + i < n ) ans = Math . Min ( ans , Math . Abs ( a [ ind + i ] - a [ ind ] ) + FindMinimumCost ( ind + i , a , n , k , dp ) ) ; else break ; } return dp [ ind ] = ans ; } } public static void Main ( ) { int [ ] a = { 10 , 30 , 40 , 50 , 20 } ; int k = 3 ; int n = a . Length ; int [ ] dp = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) dp [ i ] = - 1 ; Console . WriteLine ( FindMinimumCost ( 0 , a , n , k , dp ) ) ; } }


"Minimum cost to reach end of array array when a maximum jump of K index is allowed | C # implementation of the approach ; Function for returning the min of two elements ; for calculating the number of elements ; Allocating Memo table and initializing with INT_MAX ; Base case ; For every element relax every reachable element ie relax next k elements ; reaching next k element ; Relaxing the element ; return the last element in the array ; Driver Code"
Solution in C#:

using System ; class GFG { static int min ( int a , int b ) { return ( a > b ) ? b : a ; } static int minCostJumpsDP ( int [ ] A , int k ) { int size = A . Length ; int [ ] x = new int [ size ] ; for ( int i = 0 ; i < size ; i ++ ) x [ i ] = int . MaxValue ; x [ 0 ] = 0 ; for ( int i = 0 ; i < size ; i ++ ) { for ( int j = i + 1 ; j < i + k + 1 && j < size ; j ++ ) { x [ j ] = min ( x [ j ] , x [ i ] + Math . Abs ( A [ i ] - A [ j ] ) ) ; } } return x [ size - 1 ] ; } public static void Main ( String [ ] args ) { int [ ] input = { 83 , 26 , 37 , 35 , 33 , 35 , 56 } ; Console . WriteLine ( minCostJumpsDP ( input , 3 ) ) ; } }


"Minimum cost to reach the top of the floor by climbing stairs | C # program to find the minimum cost required to reach the n - th floor ; function to find the minimum cost to reach N - th floor ; declare an array ; base case ; initially to climb till 0 - th or 1 th stair ; iterate for finding the cost ; return the minimum ; Driver Code"
Solution in C#:

using System ; class GFG { static int minimumCost ( int [ ] cost , int n ) { int [ ] dp = new int [ n ] ; if ( n == 1 ) return cost [ 0 ] ; dp [ 0 ] = cost [ 0 ] ; dp [ 1 ] = cost [ 1 ] ; for ( int i = 2 ; i < n ; i ++ ) { dp [ i ] = Math . Min ( dp [ i - 1 ] , dp [ i - 2 ] ) + cost [ i ] ; } return Math . Min ( dp [ n - 2 ] , dp [ n - 1 ] ) ; } public static void Main ( ) { int [ ] a = { 16 , 19 , 10 , 12 , 18 } ; int n = a . Length ; Console . WriteLine ( minimumCost ( a , n ) ) ; } }


"Minimum cost to reach the top of the floor by climbing stairs | C # program to find the minimum cost required to reach the n - th floor space - optimized solution ; function to find the minimum cost to reach N - th floor ; traverse till N - th stair ; update the last two stairs value ; Driver Code"
Solution in C#:

using System ; class GFG { static int minimumCost ( int [ ] cost , int n ) { int dp1 = 0 , dp2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int dp0 = cost [ i ] + Math . Min ( dp1 , dp2 ) ; dp2 = dp1 ; dp1 = dp0 ; } return Math . Min ( dp1 , dp2 ) ; } public static void Main ( ) { int [ ] a = { 2 , 5 , 3 , 1 , 7 , 3 , 4 } ; int n = a . Length ; Console . Write ( minimumCost ( a , n ) ) ; } }


"Minimum cost to reduce the integer N to 1 as per given conditions | C # program for the above approach ; Function to find the minimum cost to reduce the integer N to 1 by the given operations ; Check if x is 1 ; Print the answer ; Prestore the answer ; Iterate till n exists ; Divide N by x ; Reduce n by x ; Add the cost ; Update the answer ; Return the answer ; Driver code ; Initialize the variables ; Function call"
Solution in C#:

using System ; class GFG { static int min_cost ( int n , int x , int p , int q ) { if ( x == 1 ) { Console . WriteLine ( ( n - 1 ) * p ) ; return 0 ; } int ans = ( n - 1 ) * p ; int pre = 0 ; while ( n > 1 ) { int tmp = n / x ; if ( tmp < 0 ) break ; pre += ( n - tmp * x ) * p ; n /= x ; pre += q ; ans = Math . Min ( ans , pre + ( n - 1 ) * p ) ; } return ans ; } public static void Main ( String [ ] args ) { int n = 5 , x = 2 , p = 2 , q = 3 ; Console . WriteLine ( min_cost ( n , x , p , q ) ) ; } }


"Minimum cost to sort a matrix of numbers from 0 to n ^ 2 | C # implementation to find the total energy required to rearrange the numbers ; function to find the total energy required to rearrange the numbers ; nested loops to access the elements of the given matrix ; store quotient ; final destination location ( i_des , j_des ) of the element mat [ i ] [ j ] is being calculated ; energy required for the movement of the element mat [ i ] [ j ] is calculated and then accumulated in the ' tot _ energy ' ; required total energy ; Driver function"
Solution in C#:

using System ; class GFG { public static int calculateEnergy ( int [ , ] mat , int n ) { int i_des , j_des , q ; int tot_energy = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { q = mat [ i , j ] / n ; i_des = q ; j_des = mat [ i , j ] - ( n * q ) ; tot_energy += Math . Abs ( i_des - i ) + Math . Abs ( j_des - j ) ; } } return tot_energy ; } public static void Main ( ) { int [ , ] mat = new int [ , ] { { 4 , 7 , 0 , 3 } , { 8 , 5 , 6 , 1 } , { 9 , 11 , 10 , 2 } , { 15 , 13 , 14 , 12 } } ; int n = 4 ; Console . Write ( " Total ▁ energy ▁ required ▁ = ▁ " + calculateEnergy ( mat , n ) + " ▁ units " ) ; } }


"Minimum cost to traverse from one index to another in the String | C # implementation of the above approach . ; function to find the minimum cost ; graph ; adjacency matrix ; initialising adjacency matrix ; creating adjacency list ; pushing left adjacent elelemt for index ' k ' ; pushing right adjacent element for index ' k ' ; queue to perform BFS ; visited array ; variable to store depth of BFS ; BFS ; number of elements in the current level ; inner loop ; current element ; popping queue ; base case ; checking if the current node is required node ; iterating through the current node ; updating depth ; Driver Code ; input variables ; function to find the minimum cost"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int findMinCost ( char [ ] s , int i , int j ) { List < int > [ ] gr = new List < int > [ 26 ] ; for ( int iN = 0 ; iN < 26 ; iN ++ ) gr [ iN ] = new List < int > ( ) ; bool [ , ] edge = new bool [ 26 , 26 ] ; for ( int k = 0 ; k < 26 ; k ++ ) for ( int l = 0 ; l < 26 ; l ++ ) edge [ k , l ] = false ; for ( int k = 0 ; k < s . Length ; k ++ ) { if ( k - 1 >= 0 && ! edge [ s [ k ] - 97 , s [ k - 1 ] - 97 ] ) { gr [ s [ k ] - 97 ] . Add ( s [ k - 1 ] - 97 ) ; edge [ s [ k ] - 97 , s [ k - 1 ] - 97 ] = true ; } if ( k + 1 <= s . Length - 1 && ! edge [ s [ k ] - 97 , s [ k + 1 ] - 97 ] ) { gr [ s [ k ] - 97 ] . Add ( s [ k + 1 ] - 97 ) ; edge [ s [ k ] - 97 , s [ k + 1 ] - 97 ] = true ; } } Queue < int > q = new Queue < int > ( ) ; q . Enqueue ( s [ i ] - 97 ) ; bool [ ] v = new bool [ 26 ] ; int d = 0 ; while ( q . Count > 0 ) { int cnt = q . Count ; while ( cnt -- > 0 ) { int curr = q . Peek ( ) ; q . Dequeue ( ) ; if ( v [ curr ] ) continue ; v [ curr ] = true ; if ( curr == s [ j ] - 97 ) return d ; foreach ( int it in gr [ curr ] ) q . Enqueue ( it ) ; } d ++ ; } return - 1 ; } public static void Main ( String [ ] args ) { String s = " abcde " ; int i = 0 ; int j = 4 ; Console . Write ( findMinCost ( s . ToCharArray ( ) , i , j ) ) ; } }


"Minimum count of 0 s to be removed from given Binary string to make all 1 s occurs consecutively | C # program for the above approach ; Function to find minimum count of 0 s removed from the string S such that all 1 s occurs consecutively ; Stores the first and the last occurrence of 1 ; Iterate over the characters the string , S ; If current character is '1' ; Update fst_occur ; Iterate over the characters the string , S ; If current character is '1' ; Update lst_occur ; Stores the count of 0 s between fst_occur and lst_occur ; Iterate over the characters of S between fst_occur and lst_occur ; If current character is '0' ; Update count ; Print the resultant minimum count ; Driver Code"
Solution in C#:

using System ; class GFG { static void makeContiguous ( string S , int N ) { int fst_occur = 0 , lst_occur = 0 ; for ( int x = 0 ; x < N ; x ++ ) { if ( S [ x ] == '1' ) { fst_occur = x ; break ; } } for ( int x = N - 1 ; x >= 0 ; x -- ) { if ( S [ x ] == '1' ) { lst_occur = x ; break ; } } int count = 0 ; for ( int x = fst_occur ; x <= lst_occur ; x ++ ) { if ( S [ x ] == '0' ) { count ++ ; } } Console . Write ( count ) ; } public static void Main ( ) { string S = "010001011" ; int N = S . Length ; makeContiguous ( S , N ) ; } }


"Minimum count of Full Binary Trees such that the count of leaves is N | C # implementation of the approach ; Function to return the minimum count of trees required ; To store the count of set bits in n ; Driver code"
Solution in C#:

using System ; class GFG { static int minTrees ( int n ) { int count = 0 ; while ( n > 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } public static void Main ( ) { int n = 7 ; Console . Write ( minTrees ( n ) ) ; } }


"Minimum count of consecutive integers till N whose bitwise AND is 0 with N | C # program for the above approach ; Function to count the minimum count of integers such that bitwise AND of that many consecutive elements is equal to 0 ; Stores the binary representation of N ; Stores the MSB bit ; Stores the count of numbers ; Return res ; Driver Code ; Given Input ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { static int count ( int N ) { String a = Convert . ToString ( N , 2 ) ; int m = a . Length - 1 ; int res = ( int ) ( N - ( Math . Pow ( 2 , m ) - 1 ) ) ; return res ; } public static void Main ( String [ ] args ) { int N = 18 ; Console . WriteLine ( count ( N ) ) ; } }


"Minimum count of digits required to obtain given Sum | C # program to implement the above approach ; Function to print the minimum count of digits ; IF N is divisible by 9 ; Count of 9 's is the answer ; If remainder is non - zero ; Driver Code"
Solution in C#:

using System ; class GFG { static void mindigits ( int n ) { if ( n % 9 == 0 ) { Console . WriteLine ( n / 9 ) ; } else { Console . WriteLine ( ( n / 9 ) + 1 ) ; } } public static void Main ( String [ ] args ) { int n1 = 24 ; int n2 = 18 ; mindigits ( n1 ) ; mindigits ( n2 ) ; } }


"Minimum count of elements required to obtain the given Array by repeated mirror operations | C # program to implement the above approach ; Function to find minimum number of elements required to form [ ] A by performing mirroring operation ; Initialize K ; Odd length array cannot be formed by mirror operation ; Check if prefix of length K is palindrome ; Check if not a palindrome ; If found to be palindrome ; Otherwise ; Return the readonly answer ; Driver Code"
Solution in C#:

using System ; class GFG { static int minimumrequired ( int [ ] A , int N ) { int K = N ; int ans = 0 ; while ( K > 0 ) { if ( K % 2 == 1 ) { ans = K ; break ; } int ispalindrome = 1 ; for ( int i = 0 ; i < K / 2 ; i ++ ) { if ( A [ i ] != A [ K - 1 - i ] ) ispalindrome = 0 ; } if ( ispalindrome == 1 ) { ans = K / 2 ; K /= 2 ; } else { ans = K ; break ; } } return ans ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 2 , 2 , 1 , 1 , 2 , 2 , 1 } ; int N = a . Length ; Console . WriteLine ( minimumrequired ( a , N ) ) ; } }


"Minimum count of elements that sums to a given number | C # implementation of the above approach ; we will only store min counts of sum upto 100 ; initialize with INT_MAX ; memo [ 0 ] = 0 as 0 is made from 0 elements ; fill memo array with min counts of elements that will constitute sum upto 100 ; min_count will store min count of elements chosen ; starting from end iterate over each 2 digits and add min count of elements to min_count ; Driver code"
Solution in C#:

using System ; class GFG { static int minCount ( int K ) { int [ ] memo = new int [ 100 ] ; for ( int i = 0 ; i < 100 ; i ++ ) { memo [ i ] = int . MaxValue ; } memo [ 0 ] = 0 ; for ( int i = 1 ; i < 100 ; i ++ ) { memo [ i ] = Math . Min ( memo [ i - 1 ] + 1 , memo [ i ] ) ; } for ( int i = 10 ; i < 100 ; i ++ ) { memo [ i ] = Math . Min ( memo [ i - 10 ] + 1 , memo [ i ] ) ; } for ( int i = 25 ; i < 100 ; i ++ ) { memo [ i ] = Math . Min ( memo [ i - 25 ] + 1 , memo [ i ] ) ; } int min_count = 0 ; while ( K > 0 ) { min_count += memo [ K % 100 ] ; K /= 100 ; } return min_count ; } static public void Main ( ) { int K = 69 ; Console . WriteLine ( minCount ( K ) ) ; } }


"Minimum count of elements to be inserted in Array to form all values in [ 1 , K ] using subset sum | C # program for the above approach ; Function to find the count of minimum elements to be inserted to form every number in a range ; Stores the count of numbers needed ; Stores the numbers upto which every numbers can be formed ; Stores the index of the array arr [ ] ; Iterate until requiredSum is less than or equal to K ; If i is less than N and requiredSum is greater than or equal to arr [ i ] ; Increment requiredSum by arr [ i ] ; Increment i by 1 ; Otherwise ; Increment count by 1 ; Increment requiredSum by requiredSum ; Return result ; Driver Code ; Input ; Function Call"
Solution in C#:

using System ; class GFG { public static int minElements ( int [ ] arr , int N , int K ) { int count = 0 ; long requiredNum = 1 ; int i = 0 ; while ( requiredNum <= K ) { if ( i < N && requiredNum >= arr [ i ] ) { requiredNum += arr [ i ] ; i ++ ; } else { count ++ ; requiredNum += requiredNum ; } } return count ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 3 } ; int K = 6 ; int N = arr . Length ; Console . Write ( minElements ( arr , N , K ) ) ; } }