"Minimum number of deletions and insertions to transform one string into another | Dynamic Programming C # implementation to find minimum number of deletions and insertions ; Returns length of length common subsequence for str1 [ 0. . m - 1 ] , str2 [ 0. . n - 1 ] ; Following steps build L [ m + 1 ] [ n + 1 ] in bottom up fashion . Note that L [ i ] [ j ] contains length of LCS of str1 [ 0. . i - 1 ] and str2 [ 0. . j - 1 ] ; L [ m ] [ n ] contains length of LCS for X [ 0. . n - 1 ] and Y [ 0. . m - 1 ] ; function to find minimum number of deletions and insertions ; Driver code ; Function Call"
Solution in C#:

using System ; class GFG { static int lcs ( string str1 , string str2 , int m , int n ) { int [ , ] L = new int [ m + 1 , n + 1 ] ; int i , j ; for ( i = 0 ; i <= m ; i ++ ) { for ( j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) L [ i , j ] = 0 ; else if ( str1 [ i - 1 ] == str2 [ j - 1 ] ) L [ i , j ] = L [ i - 1 , j - 1 ] + 1 ; else L [ i , j ] = Math . Max ( L [ i - 1 , j ] , L [ i , j - 1 ] ) ; } } return L [ m , n ] ; } static void printMinDelAndInsert ( string str1 , string str2 ) { int m = str1 . Length ; int n = str2 . Length ; int len = lcs ( str1 , str2 , m , n ) ; Console . Write ( " Minimum ▁ number ▁ of ▁ " + " deletions ▁ = ▁ " ) ; Console . WriteLine ( m - len ) ; Console . Write ( " Minimum ▁ number ▁ of ▁ " + " insertions ▁ = ▁ " ) ; Console . Write ( n - len ) ; } public static void Main ( ) { string str1 = new string ( " heap " ) ; string str2 = new string ( " pea " ) ; printMinDelAndInsert ( str1 , str2 ) ; } }


"Minimum number of deletions to make a sorted sequence | C # implementation to find minimum number of deletions to make a sorted sequence ; lis ( ) returns the length of the longest increasing subsequence in arr [ ] of size n ; Initialize LIS values for all indexes ; Compute optimized LIS values in bottom up manner ; Pick resultimum of all LIS values ; function to calculate minimum number of deletions ; Find longest increasing subsequence ; After removing elements other than the lis , we get sorted sequence . ; Driver Code"
Solution in C#:

using System ; class GfG { static int lis ( int [ ] arr , int n ) { int result = 0 ; int [ ] lis = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) lis [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && lis [ i ] < lis [ j ] + 1 ) lis [ i ] = lis [ j ] + 1 ; for ( int i = 0 ; i < n ; i ++ ) if ( result < lis [ i ] ) result = lis [ i ] ; return result ; } static int minimumNumberOfDeletions ( int [ ] arr , int n ) { int len = lis ( arr , n ) ; return ( n - len ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 30 , 40 , 2 , 5 , 1 , 7 , 45 , 50 , 8 } ; int n = arr . Length ; Console . Write ( " Minimum ▁ number ▁ of " + " ▁ deletions ▁ = ▁ " + minimumNumberOfDeletions ( arr , n ) ) ; } }


"Minimum number of deletions to make a string palindrome | C # implementation to find minimum number of deletions to make a string palindromic ; Returns the length of the longest palindromic subsequence in ' str ' ; Create a table to store results of subproblems ; Strings of length 1 are palindrome of length 1 ; Build the table . Note that the lower diagonal values of table are useless and not filled in the process . c1 is length of substring ; length of longest palindromic subsequence ; function to calculate minimum number of deletions ; Find longest palindromic subsequence ; After removing characters other than the lps , we get palindrome . ; Driver Code"
Solution in C#:

using System ; class GFG { static int lps ( String str ) { int n = str . Length ; int [ , ] L = new int [ n , n ] ; for ( int i = 0 ; i < n ; i ++ ) L [ i , i ] = 1 ; for ( int cl = 2 ; cl <= n ; cl ++ ) { for ( int i = 0 ; i < n - cl + 1 ; i ++ ) { int j = i + cl - 1 ; if ( str [ i ] == str [ j ] && cl == 2 ) L [ i , j ] = 2 ; else if ( str [ i ] == str [ j ] ) L [ i , j ] = L [ i + 1 , j - 1 ] + 2 ; else L [ i , j ] = Math . Max ( L [ i , j - 1 ] , L [ i + 1 , j ] ) ; } } return L [ 0 , n - 1 ] ; } static int minimumNumberOfDeletions ( string str ) { int n = str . Length ; int len = lps ( str ) ; return ( n - len ) ; } public static void Main ( ) { string str = " geeksforgeeks " ; Console . Write ( " Minimum ▁ number ▁ of " + " ▁ deletions ▁ = ▁ " + minimumNumberOfDeletions ( str ) ) ; } }


"Minimum number of deletions to make a string palindrome | C # program for above approach ; Function to return minimum Element between two values ; Utility function for calculating Minimum element to delete ; Condition to compare characters ; Recursive function call ; Return value , increamenting by 1 ; Function to calculate the minimum Element required to delete for Making string pelindrom ; Utility function call ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int min ( int x , int y ) { return ( x < y ) ? x : y ; } static int utility_fun_for_del ( string str , int i , int j ) { if ( i >= j ) return 0 ; if ( str [ i ] == str [ j ] ) { return utility_fun_for_del ( str , i + 1 , j - 1 ) ; } return 1 + Math . Min ( utility_fun_for_del ( str , i + 1 , j ) , utility_fun_for_del ( str , i , j - 1 ) ) ; } static int min_ele_del ( string str ) { return utility_fun_for_del ( str , 0 , str . Length - 1 ) ; } static void Main ( ) { string str = " abefbac " ; Console . WriteLine ( " Minimum ▁ element ▁ of ▁ " + " deletions ▁ = ▁ " + min_ele_del ( str ) ) ; } }


"Minimum number of deletions to make a string palindrome | Function definition ; Base cases ; Checking the ndesired condition ; If yes increment the cunt ; If no ; Return the value form the table ; Else store the max tranforamtion from the subsequence ; Return the dp [ - 1 ] [ - 1 ] ; Driver code ; Initialize the array with - 1"
Solution in C#:

using System ; class GFG { static int [ , ] dp = new int [ 2000 , 2000 ] ; static int transformation ( string s1 , string s2 , int i , int j ) { if ( i >= ( s1 . Length ) || j >= ( s2 . Length ) ) { return 0 ; } if ( s1 [ i ] == s2 [ j ] ) { dp [ i , j ] = 1 + transformation ( s1 , s2 , i + 1 , j + 1 ) ; } if ( dp [ i , j ] != - 1 ) { return dp [ i , j ] ; } else { dp [ i , j ] = Math . Max ( transformation ( s1 , s2 , i , j + i ) , transformation ( s1 , s2 , i + 1 , j ) ) ; } return dp [ s1 . Length - 1 , s2 . Length - 1 ] ; } static public void Main ( ) { string s1 = " geeksforgeeks " ; string s2 = " geeks " ; for ( int m = 0 ; m < 2000 ; m ++ ) { for ( int n = 0 ; n < 2000 ; n ++ ) { dp [ m , n ] = - 1 ; } } Console . WriteLine ( " MINIMUM ▁ NUMBER ▁ OF ▁ DELETIONS : ▁ " + ( s1 . Length - transformation ( s1 , s2 , 0 , 0 ) ) ) ; Console . WriteLine ( " MINIMUM ▁ NUMBER ▁ OF ▁ INSERTIONS : ▁ " + ( s2 . Length - transformation ( s1 , s2 , 0 , 0 ) ) ) ; Console . WriteLine ( " LCS ▁ LENGTH : ▁ " + transformation ( s1 , s2 , 0 , 0 ) ) ; } }


"Minimum number of deletions to make a string palindrome | Set 2 | C # program to find minimum deletions to make palindrome . ; Find reverse of input string ; Create a DP table for storing edit distance of string and reverse . ; Find edit distance between input and revInput considering only delete operation . ; Go from bottom left to top right and find the minimum ; Driver Code"
Solution in C#:

using System ; class GFG { static int getLevenstein ( String input ) { String revInput = Reverse ( input ) ; int n = input . Length ; int [ , ] dp = new int [ n + 1 , n + 1 ] ; for ( int i = 0 ; i <= n ; ++ i ) { dp [ 0 , i ] = i ; dp [ i , 0 ] = i ; } for ( int i = 1 ; i <= n ; ++ i ) { for ( int j = 1 ; j <= n ; ++ j ) { if ( input [ i - 1 ] == revInput [ j - 1 ] ) dp [ i , j ] = dp [ i - 1 , j - 1 ] ; else dp [ i , j ] = 1 + Math . Min ( dp [ i - 1 , j ] , dp [ i , j - 1 ] ) ; } } int res = int . MaxValue ; for ( int i = n , j = 0 ; i >= 0 ; i -- , j ++ ) { res = Math . Min ( res , dp [ i , j ] ) ; if ( i < n ) res = Math . Min ( res , dp [ i + 1 , j ] ) ; if ( i > 0 ) res = Math . Min ( res , dp [ i - 1 , j ] ) ; } return res ; } static String Reverse ( String input ) { char [ ] a = input . ToCharArray ( ) ; int l , r = a . Length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . Join ( " " , a ) ; } public static void Main ( String [ ] args ) { String input = " myfirstgeekarticle " ; Console . WriteLine ( getLevenstein ( input ) ) ; } }


"Minimum number of digits required to be removed to make a number divisible by 4 | C # program for the above approach ; Function to count the minimum number of digits required to be removed to make a given number divisible by 4 ; Store the size of the string ; Stores the required result ; Check for every pair of digits if the number formed by them is divisible by 4 or not ; Store s [ i ] in a variable ; If it is divisible by 2 ; Store the number formed by s [ j ] and s [ i ] ; Check if it is divisible by 4 ; Store the number of digits required to be deleted ; Update ans ; If value of ans is unchanged , then check if any s [ i ] is divisible by 4 ; If true , update ans to n - 1 ; Print the result ; Driver Code"
Solution in C#:

using System ; class GFG { static void minimumDeletions ( string s ) { int n = s . Length ; int ans = n ; for ( int i = n - 1 ; i >= 0 ; i -- ) { int t = s [ i ] - '0' ; if ( t % 2 == 0 ) { for ( int j = i - 1 ; j >= 0 ; j -- ) { int num = ( s [ j ] - '0' ) * 10 + t ; if ( num % 4 == 0 ) { int k1 = i - j - 1 ; int k2 = n - i - 1 ; ans = Math . Min ( ans , k1 + k2 ) ; } } } } if ( ans == n ) { for ( int i = 0 ; i < n ; i ++ ) { int num = s [ i ] - '0' ; if ( num % 4 == 0 ) { ans = n - 1 ; } } } Console . WriteLine ( ans ) ; } static public void Main ( ) { string str = "12367" ; minimumDeletions ( str ) ; } }


"Minimum number of distinct elements after removing M items | Set 2 | C # program to implement the above approach ; Function to return minimum distinct character after M removals ; Count the occurences of number and store in count ; Count the occurences of the frequencies ; Take answer as total unique numbers and remove the frequency and subtract the answer ; Remove the minimum number of times ; Return the answer ; Driver code ; Initialize array ; Size of array ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int distinctNumbers ( int [ ] arr , int m , int n ) { Dictionary < int , int > count = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( count . ContainsKey ( arr [ i ] ) ) { count [ arr [ i ] ] = count [ arr [ i ] ] + 1 ; } else { count . Add ( arr [ i ] , 1 ) ; } int [ ] fre_arr = new int [ n + 1 ] ; foreach ( int it in count . Values ) { fre_arr [ it ] ++ ; } int ans = count . Count ; for ( int i = 1 ; i <= n ; i ++ ) { int temp = fre_arr [ i ] ; if ( temp == 0 ) continue ; int t = Math . Min ( temp , m / i ) ; ans -= t ; m -= i * t ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 1 , 5 , 3 , 5 , 1 , 3 } ; int n = arr . Length ; int m = 2 ; Console . WriteLine ( distinctNumbers ( arr , m , n ) ) ; } }


"Minimum number of distinct elements after removing m items | C # program for Minimum number of distinct elements after removing m items ; Function to find distintc id 's ; Store the occurrence of ids ; Start removing elements from the beginning ; Remove if current value is less than or equal to mi ; Return the remaining size ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int distinctIds ( int [ ] arr , int n , int mi ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; int count = 0 ; int size = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . ContainsKey ( arr [ i ] ) == false ) { m [ arr [ i ] ] = 1 ; size ++ ; } else { m [ arr [ i ] ] ++ ; } } foreach ( KeyValuePair < int , int > mp in m ) { if ( mp . Key <= mi ) { mi -= mp . Key ; count ++ ; } } return size - count ; } static void Main ( ) { int [ ] arr = { 2 , 3 , 1 , 2 , 3 , 3 } ; int m = 3 ; Console . WriteLine ( distinctIds ( arr , arr . Length , m ) ) ; } }


"Minimum number of distinct powers of 2 required to express a given binary number | C # program to implement the above approach ; Function to return the minimum distinct powers of 2 required to express s ; Reverse the string to start from lower powers ; Check if the character is 1 ; Add in range provided range ; Initialize the counter ; Check if the character is not 0 ; Increment the counter ; Print the result ; utility reverse ; Driver code"
Solution in C#:

using System ; using System . Text ; class GFG { static void findMinimum ( String s ) { int n = s . Length ; int [ ] x = new int [ n + 1 ] ; StringBuilder s2 = new StringBuilder ( s ) ; s2 = reverse ( s2 . ToString ( ) ) ; String s3 = s2 . ToString ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s3 [ i ] == '1' ) { if ( x [ i ] == 1 ) { x [ i + 1 ] = 1 ; x [ i ] = 0 ; } else if ( 1 <= i && ( i & x [ i - 1 ] ) == 1 ) { x [ i + 1 ] = 1 ; x [ i - 1 ] = - 1 ; } else x [ i ] = 1 ; } } int c = 0 ; for ( int i = 0 ; i <= n ; i ++ ) { if ( x [ i ] != 0 ) c ++ ; } Console . WriteLine ( c ) ; } static StringBuilder reverse ( String input ) { char [ ] a = input . ToCharArray ( ) ; int l , r = a . Length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return new StringBuilder ( String . Join ( " " , a ) ) ; } public static void Main ( String [ ] args ) { String str = "111" ; findMinimum ( str ) ; } }


"Minimum number of edges between two vertices of a Graph | C # program to find minimum edge between given two vertex of Graph ; Method for finding minimum no . of edge using BFS ; visited [ n ] for keeping track of visited node in BFS ; Initialize distances as 0 ; queue to do BFS . ; Update distance for i ; Method for addition of edge ; Driver code ; To store adjacency list of graph"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int minEdgeBFS ( ArrayList [ ] edges , int u , int v , int n ) { ArrayList visited = new ArrayList ( ) ; for ( int i = 0 ; i < n ; i ++ ) { visited . Add ( false ) ; } ArrayList distance = new ArrayList ( ) ; for ( int i = 0 ; i < n ; i ++ ) { distance . Add ( 0 ) ; } Queue Q = new Queue ( ) ; distance [ u ] = 0 ; Q . Enqueue ( u ) ; visited [ u ] = true ; while ( Q . Count != 0 ) { int x = ( int ) Q . Dequeue ( ) ; for ( int i = 0 ; i < edges [ x ] . Count ; i ++ ) { if ( ( bool ) visited [ ( int ) edges [ x ] [ i ] ] ) continue ; distance [ ( int ) edges [ x ] [ i ] ] = ( int ) distance [ x ] + 1 ; Q . Enqueue ( ( int ) edges [ x ] [ i ] ) ; visited [ ( int ) edges [ x ] [ i ] ] = true ; } } return ( int ) distance [ v ] ; } static void addEdge ( ArrayList [ ] edges , int u , int v ) { edges [ u ] . Add ( v ) ; edges [ v ] . Add ( u ) ; } public static void Main ( string [ ] args ) { int n = 9 ; ArrayList [ ] edges = new ArrayList [ 9 ] ; for ( int i = 0 ; i < 9 ; i ++ ) { edges [ i ] = new ArrayList ( ) ; } addEdge ( edges , 0 , 1 ) ; addEdge ( edges , 0 , 7 ) ; addEdge ( edges , 1 , 7 ) ; addEdge ( edges , 1 , 2 ) ; addEdge ( edges , 2 , 3 ) ; addEdge ( edges , 2 , 5 ) ; addEdge ( edges , 2 , 8 ) ; addEdge ( edges , 3 , 4 ) ; addEdge ( edges , 3 , 5 ) ; addEdge ( edges , 4 , 5 ) ; addEdge ( edges , 5 , 6 ) ; addEdge ( edges , 6 , 7 ) ; addEdge ( edges , 7 , 8 ) ; int u = 0 ; int v = 5 ; Console . Write ( minEdgeBFS ( edges , u , v , n ) ) ; } }


"Minimum number of edges required to be removed from an Undirected Graph to make it acyclic | C # program to implement the above approach ; Stores the adjacency list ; Stores if a vertex is visited or not ; Function to perform DFS Traversal to count the number and size of all connected components ; Mark the current node as visited ; Traverse the adjacency list of the current node ; For every unvisited node ; Recursive DFS call ; Function to add undirected edge in the graph ; Function to calculate minimum number of edges to be removed ; Create Adjacency list ; Iterate over all the nodes ; Print the readonly count ; Driver Code"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static List < int > [ ] vec = new List < int > [ 100001 ] ; static bool [ ] vis = new bool [ 100001 ] ; static int cc = 1 ; static void dfs ( int node ) { vis [ node ] = true ; foreach ( int x in vec [ node ] ) { if ( ! vis [ x ] ) { cc ++ ; dfs ( x ) ; } } } static void addEdge ( int u , int v ) { vec [ u ] . Add ( v ) ; vec [ v ] . Add ( u ) ; } static void minEdgeRemoved ( int N , int M , int [ , ] Edges ) { for ( int i = 0 ; i < M ; i ++ ) { addEdge ( Edges [ i , 0 ] , Edges [ i , 1 ] ) ; } int k = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( ! vis [ i ] ) { cc = 1 ; dfs ( i ) ; k ++ ; } } Console . Write ( M - N + k + "
 " ) ; } public static void Main ( String [ ] args ) { int N = 3 , M = 2 ; int [ , ] Edges = { { 1 , 2 } , { 2 , 3 } } ; for ( int i = 0 ; i < vec . Length ; i ++ ) vec [ i ] = new List < int > ( ) ; minEdgeRemoved ( N , M , Edges ) ; } }


"Minimum number of edges that need to be added to form a triangle | C # implementation of the approach ; Pair class ; Function to return the minimum number of edges that need to be added to the given graph such that it contains at least one triangle ; adj is the adjacency matrix such that adj [ i , j ] = 1 when there is an edge between i and j ; As the graph is undirected so there will be an edge between ( i , j ) and ( j , i ) ; To store the required count of edges ; For every possible vertex triplet ; If the vertices form a triangle ; If no edges are present ; If only 1 edge is required ; Two edges are required ; Driver Code ; Number of nodes ; Storing the edges in a vector of pairs"
Solution in C#:

using System ; using System . Collections . Generic ; class Pair { public int first ; public int second ; public Pair ( int first , int second ) { this . first = first ; this . second = second ; } } class GFG { static int minEdges ( List < Pair > v , int n ) { int [ , ] adj = new int [ n + 1 , n + 1 ] ; for ( int i = 0 ; i < v . Count ; i ++ ) { adj [ v [ i ] . first , v [ i ] . second ] = 1 ; adj [ v [ i ] . second , v [ i ] . first ] = 1 ; } int edgesNeeded = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = i + 1 ; j <= n ; j ++ ) { for ( int k = j + 1 ; k <= n ; k ++ ) { if ( adj [ i , j ] == 1 && adj [ j , k ] == 1 && adj [ k , i ] == 1 ) return 0 ; if ( ! ( adj [ i , j ] == 1 adj [ j , k ] == 1 adj [ k , i ] == 1 ) ) edgesNeeded = Math . Min ( edgesNeeded , 3 ) ; else { if ( ( adj [ i , j ] == 1 && adj [ j , k ] == 1 ) || ( adj [ j , k ] == 1 && adj [ k , i ] == 1 ) || ( adj [ k , i ] == 1 && adj [ i , j ] == 1 ) ) { edgesNeeded = 1 ; } else edgesNeeded = Math . Min ( edgesNeeded , 2 ) ; } } } } return edgesNeeded ; } public static void Main ( String [ ] args ) { int n = 3 ; List < Pair > v = new List < Pair > ( ) ; v . Add ( new Pair ( 1 , 2 ) ) ; v . Add ( new Pair ( 1 , 3 ) ) ; Console . WriteLine ( minEdges ( v , n ) ) ; } }


"Minimum number of elements that should be removed to make the array good | C # implementation of the approach ; Function to return the minimum number of elements that must be removed to make the given array good ; Count frequency of each element ; For each element check if there exists another element that makes a valid pair ; If does not exist then increment answer ; Driver code"
Solution in C#:

using System . Linq ; using System ; class GFG { static int minimumRemoval ( int n , int [ ] a ) { int [ ] c = new int [ 1000 ] ; for ( int i = 0 ; i < n ; i ++ ) c [ a [ i ] ] ++ ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { bool ok = true ; for ( int j = 0 ; j < 31 ; j ++ ) { int x = ( 1 << j ) - a [ i ] ; if ( c . Contains ( x ) && ( c [ x ] > 1 || ( c [ x ] == 1 && x != a [ i ] ) ) ) { ok = false ; break ; } } if ( ! ok ) ans ++ ; } return ans ; } static void Main ( ) { int [ ] a = { 4 , 7 , 1 , 5 , 4 , 9 } ; int n = a . Length ; Console . WriteLine ( minimumRemoval ( n , a ) ) ; } }


"Minimum number of elements to add to make median equals x | C # program to find minimum number of elements needs to add to the array so that its median equals x . ; Returns count of elements to be added to make median x . This function assumes that a [ ] has enough extra space . ; to sort the array in increasing order . ; Driver code"
Solution in C#:

using System ; class GFG { static int minNumber ( int [ ] a , int n , int x ) { Array . Sort ( a ) ; int k ; for ( k = 0 ; a [ ( n ) / 2 ] != x ; k ++ ) { a [ n ++ ] = x ; Array . Sort ( a ) ; } return k ; } public static void Main ( String [ ] args ) { int x = 10 ; int [ ] a = { 10 , 20 , 30 } ; int n = 3 ; Console . WriteLine ( minNumber ( a , n - 1 , x ) ) ; } }


"Minimum number of elements to add to make median equals x | C # program to find minimum number of elements to add so that its median equals x . ; no . of elements equals to x , that is , e . ; no . of elements greater than x , that is , h . ; no . of elements smaller than x , that is , l . ; subtract the no . of elements that are equal to x . ; Driver Code"
Solution in C#:

using System ; class GFG { public static int minNumber ( int [ ] a , int n , int x ) { int l = 0 , h = 0 , e = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == x ) e ++ ; else if ( a [ i ] > x ) h ++ ; else if ( a [ i ] < x ) l ++ ; } int ans = 0 ; if ( l > h ) ans = l - h ; else if ( l < h ) ans = h - l - 1 ; return ans + 1 - e ; } public static void Main ( ) { int x = 10 ; int [ ] a = { 10 , 20 , 30 } ; int n = a . Length ; Console . WriteLine ( minNumber ( a , n , x ) ) ; } }


"Minimum number of elements to be removed so that pairwise consecutive elements are same | C # implementation of above approach ; Function to count the minimum number of elements to remove from a number so that pairwise two consecutive digits are same . ; initialize counting variable ; check if two consecutive digits are same ; Driver code"
Solution in C#:

using System ; public class GFG { static int countConsecutive ( String s ) { int count = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == s [ i + 1 ] ) { i ++ ; } else { count ++ ; } } return count ; } public static void Main ( ) { String str = "44522255" ; Console . WriteLine ( countConsecutive ( str ) ) ; } }


"Minimum number of elements to be removed to make XOR maximum | C # implementation to find minimum number of elements to remove to get maximum XOR value ; First n in the below condition is for the case where n is 0 ; Function to find minimum number of elements to be removed . ; Driver code ; print minimum number of elements to be removed"
Solution in C#:

using System ; public class GFG { static int nextPowerOf2 ( int n ) { int count = 0 ; if ( n != 0 && ( n & ( n - 1 ) ) == 0 ) return n ; while ( n != 0 ) { n >>= 1 ; count += 1 ; } return 1 << count ; } static int removeElement ( int n ) { if ( n == 1 n == 2 ) return 0 ; int a = nextPowerOf2 ( n ) ; if ( n == a n == a - 1 ) return 1 ; else if ( n == a - 2 ) return 0 ; else if ( n % 2 == 0 ) return 1 ; else return 2 ; } public static void Main ( ) { int n = 5 ; Console . Write ( removeElement ( n ) ) ; } }


"Minimum number of elements which are not part of Increasing or decreasing subsequence in array | C # program to return minimum number of elements which are not part of increasing or decreasing subsequences . ; Return minimum number of elements which is not part of any of the sequence . ; If already calculated , return value . ; If whole array is traversed . ; calculating by considering element as part of decreasing sequence . ; calculating by considering element as part of increasing sequence . ; If cannot be calculated for decreasing sequence . ; After considering once by decreasing sequence , now try for increasing sequence . ; If element cannot be part of any of the sequence . ; After considering element as part of increasing and decreasing sequence trying as not part of any of the sequence . ; Wrapper Function ; Adding two number at the end of array , so that increasing and decreasing sequence can be made . MAX - 2 index is assigned INT_MAX for decreasing sequence because next number of sequence must be less than it . Similarly , for Increasing sequence INT_MIN is assigned to MAX - 1 index . ; Driver Code"
Solution in C#:

using System ; class GFG { static int MAX = 102 ; static int countMin ( int [ ] arr , int [ , , ] dp , int n , int dec , int inc , int i ) { if ( dp [ dec , inc , i ] != - 1 ) return dp [ dec , inc , i ] ; if ( i == n ) return 0 ; if ( arr [ i ] < arr [ dec ] ) dp [ dec , inc , i ] = countMin ( arr , dp , n , i , inc , i + 1 ) ; if ( arr [ i ] > arr [ inc ] ) { if ( dp [ dec , inc , i ] == - 1 ) dp [ dec , inc , i ] = countMin ( arr , dp , n , dec , i , i + 1 ) ; else dp [ dec , inc , i ] = Math . Min ( countMin ( arr , dp , n , dec , i , i + 1 ) , dp [ dec , inc , i ] ) ; } if ( dp [ dec , inc , i ] == - 1 ) dp [ dec , inc , i ] = 1 + countMin ( arr , dp , n , dec , inc , i + 1 ) ; else dp [ dec , inc , i ] = Math . Min ( 1 + countMin ( arr , dp , n , dec , inc , i + 1 ) , dp [ dec , inc , i ] ) ; return dp [ dec , inc , i ] ; } static int wrapper ( int [ ] arr , int n ) { arr [ MAX - 2 ] = int . MaxValue ; arr [ MAX - 1 ] = int . MinValue ; int [ , , ] dp = new int [ MAX , MAX , MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) for ( int j = 0 ; j < MAX ; j ++ ) for ( int k = 0 ; k < MAX ; k ++ ) dp [ i , j , k ] = - 1 ; return countMin ( arr , dp , n , MAX - 2 , MAX - 1 , 0 ) ; } static void Main ( ) { int n = 12 ; int [ ] arr = new int [ MAX ] ; arr [ 0 ] = 7 ; arr [ 1 ] = 8 ; arr [ 2 ] = 1 ; arr [ 3 ] = 2 ; arr [ 4 ] = 4 ; arr [ 5 ] = 6 ; arr [ 6 ] = 3 ; arr [ 7 ] = 5 ; arr [ 8 ] = 2 ; arr [ 9 ] = 1 ; arr [ 10 ] = 8 ; arr [ 11 ] = 7 ; Console . Write ( wrapper ( arr , n ) ) ; } }


"Minimum number of flips or swaps of adjacent characters required to make two strings equal | C # program for the above approach ; Function to count the minimum number of operations required to make strings A and B equal ; Stores all dp - states ; Update the value of A [ i ] ; Iterate over the range [ 1 , N ] ; If A [ i - 1 ] equals to B [ i - 1 ] ; Assign Dp [ i - 1 ] to Dp [ i ] ; Otherwise ; Update dp [ i ] ; If swapping is possible ; Update dp [ i ] ; Return the minimum number of steps required ; Driver code ; Given Input ; Function Call"
Solution in C#:

using System ; class GFG { static int countMinSteps ( string A , string B , int N ) { int [ ] dp = new int [ N + 1 ] ; for ( int i = 1 ; i <= N ; i ++ ) { dp [ i ] = 0 ; } for ( int i = 1 ; i <= N ; i ++ ) { if ( A [ i - 1 ] == B [ i - 1 ] ) { dp [ i ] = dp [ i - 1 ] ; } else { dp [ i ] = dp [ i - 1 ] + 1 ; } if ( i >= 2 && A [ i - 2 ] == B [ i - 1 ] && A [ i - 1 ] == B [ i - 2 ] ) { dp [ i ] = Math . Min ( dp [ i ] , dp [ i - 2 ] + 1 ) ; } } return dp [ N ] ; } public static void Main ( String [ ] args ) { string A = "0101" ; string B = "0011" ; int N = A . Length ; Console . Write ( countMinSteps ( A , B , N ) ) ; } }


"Minimum number of flips required such that a Binary Matrix doesn 't contain any path from the top left to the bottom right consisting only of 0s | C # program for the above approach ; The four direction coordinates changes from the current cell ; Function that returns true if there exists any path from the top - left to the bottom - right cell of 0 s ; If the bottom - right cell is reached ; Update the cell to 1 ; Traverse in all four directions ; Find the new coordinates ; If the new cell is valid ; Recursively call DFS ; If path exists , then return true ; Return false , if there doesn 't  exists any such path ; Function to flip the minimum number of cells such that there doesn 't exists any such path from (0, 0) to (N - 1, M - 1) cell consisting of 0s ; Case 1 : If no such path exists already ; Case 2 : If there exists only one path ; Case 3 : If there exists two - path ; Driver code"
Solution in C#:

using System ; class GFG { static int [ , ] direction = { { - 1 , 0 } , { 0 , 1 } , { 0 , - 1 } , { 1 , 0 } } ; static bool dfs ( int [ , ] matrix , int i , int j , int N , int M ) { if ( i == N - 1 && j == M - 1 ) { return true ; } matrix [ i , j ] = 1 ; for ( int k = 0 ; k < 4 ; k ++ ) { int newX = i + direction [ k , 0 ] ; int newY = j + direction [ k , 1 ] ; if ( newX >= 0 && newX < N && newY >= 0 && newY < M && matrix [ newX , newY ] == 0 ) { if ( dfs ( matrix , newX , newY , N , M ) ) { return true ; } } } return false ; } static int solve ( int [ , ] matrix ) { int N = matrix . GetLength ( 0 ) ; int M = matrix . GetLength ( 1 ) ; if ( ! dfs ( matrix , 0 , 0 , N , M ) ) { return 0 ; } if ( ! dfs ( matrix , 0 , 0 , N , M ) ) { return 1 ; } return 2 ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 0 , 1 , 0 , 0 } , { 0 , 1 , 0 , 0 } , { 0 , 0 , 0 , 0 } } ; Console . WriteLine ( solve ( mat ) ) ; } }


"Minimum number of flips required such that the last cell of matrix can be reached from any other cell | C # program for the above approach ; Function to calculate the minimum number of flips required ; Dimensions of [ , ] mat ; Initialize answer ; Count all ' D ' s in the last row ; Count all ' R ' s in the last column ; Print answer ; Driver Code ; Given matrix ; Function call ; Print answer"
Solution in C#:

using System ; class GFG { public static int countChanges ( char [ , ] mat ) { int n = mat . GetLength ( 0 ) ; int m = mat . GetLength ( 1 ) ; int ans = 0 ; for ( int j = 0 ; j < m - 1 ; j ++ ) { if ( mat [ n - 1 , j ] != ' R ' ) ans ++ ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( mat [ i , m - 1 ] != ' D ' ) ans ++ ; } return ans ; } public static void Main ( String [ ] args ) { char [ , ] arr = { { ' R ' , ' R ' , ' R ' , ' D ' } , { ' D ' , ' D ' , ' D ' , ' R ' } , { ' R ' , ' D ' , ' R ' , ' F ' } } ; int cnt = countChanges ( arr ) ; Console . WriteLine ( cnt ) ; } }


"Minimum number of flips to make a Binary String increasing | C # program for the above approach ; ; Function to find the minimum number of flips required to make String increasing ; Length of s ; Total number of zero in s ; Stores count of 1 s till ith index ; stors the minimum count of flip ; Traverse the given String ; Update the value of res and count of 1 s ; Return the minimum number of flips ; Utility count ; Driver code ; Given String ; function call"
Solution in C#:

using System ; public class GFG { static int minimumFlips ( String s ) { int n = s . Length ; int cnt0 = count ( s , '0' ) ; int cnt1 = 0 ; int res = n - cnt0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '0' ) { cnt0 -= 1 ; } else if ( s [ i ] == '1' ) { res = Math . Min ( res , cnt1 + cnt0 ) ; cnt1 ++ ; } } return res ; } private static int count ( String s , char c ) { int ans = 0 ; for ( int j = 0 ; j < s . Length ; j ++ ) { char i = s [ j ] ; if ( c == i ) ans ++ ; } return ans ; } static public void Main ( ) { String S = "000110" ; Console . Write ( minimumFlips ( S ) ) ; } }


"Minimum number of flips with rotation to make binary string alternating | C # program for the above approach ; Function that finds the minimum number of flips to make the binary string alternating if left circular rotation is allowed ; Initialize prefix arrays to store number of changes required to put 1 s at either even or odd position ; If i is odd ; Update the oddone and evenone count ; Else i is even ; Update the oddone and evenone count ; Initialize minimum flips ; Check if substring [ 0 , i ] is appended at end how many changes will be required ; Return minimum flips ; Driver Code ; Given String ; Length of given string ; Function call"
Solution in C#:

using System ; class GFG { static int MinimumFlips ( String s , int n ) { int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = ( s [ i ] == '1' ? 1 : 0 ) ; } int [ ] oddone = new int [ n + 1 ] ; int [ ] evenone = new int [ n + 1 ] ; oddone [ 0 ] = 0 ; evenone [ 0 ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 != 0 ) { oddone [ i + 1 ] = oddone [ i ] + ( a [ i ] == 1 ? 1 : 0 ) ; evenone [ i + 1 ] = evenone [ i ] + ( a [ i ] == 0 ? 1 : 0 ) ; } else { oddone [ i + 1 ] = oddone [ i ] + ( a [ i ] == 0 ? 1 : 0 ) ; evenone [ i + 1 ] = evenone [ i ] + ( a [ i ] == 1 ? 1 : 0 ) ; } } int minimum = Math . Min ( oddone [ n ] , evenone [ n ] ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( n % 2 != 0 ) { minimum = Math . Min ( minimum , oddone [ n ] - oddone [ i + 1 ] + evenone [ i + 1 ] ) ; minimum = Math . Min ( minimum , evenone [ n ] - evenone [ i + 1 ] + oddone [ i + 1 ] ) ; } } return minimum ; } public static void Main ( String [ ] args ) { String S = "000001100" ; int n = S . Length ; Console . Write ( MinimumFlips ( S , n ) ) ; } }


"Minimum number of given moves required to make N divisible by 25 | C # implementation of the approach ; Function to return the minimum number of moves required to make n divisible by 25 ; Convert number into string ; To store required answer ; Length of the string ; To check all possible pairs ; Make a duplicate string ; Number of swaps required to place ith digit in last position ; Number of swaps required to place jth digit in 2 nd last position ; Find first non zero digit ; Place first non zero digit in the first position ; Convert string to number ; If this number is divisible by 25 then cur is one of the possible answer ; If not possible ; Driver code"
Solution in C#:

using System ; class GFG { static int minMoves ( int n ) { string s = n . ToString ( ) ; int ans = Int32 . MaxValue ; int len = s . Length ; for ( int i = 0 ; i < len ; ++ i ) { for ( int j = 0 ; j < len ; ++ j ) { if ( i == j ) continue ; char [ ] t = s . ToCharArray ( ) ; int cur = 0 ; for ( int k = i ; k < len - 1 ; ++ k ) { swap ( t , k , k + 1 ) ; ++ cur ; } for ( int k = j - ( ( j > i ) ? 1 : 0 ) ; k < len - 2 ; ++ k ) { swap ( t , k , k + 1 ) ; ++ cur ; } int pos = - 1 ; for ( int k = 0 ; k < len ; ++ k ) { if ( t [ k ] != '0' ) { pos = k ; break ; } } for ( int k = pos ; k > 0 ; -- k ) { swap ( t , k , k - 1 ) ; ++ cur ; } int nn = Convert . ToInt32 ( new String ( t ) ) ; if ( nn % 25 == 0 ) ans = Math . Min ( ans , cur ) ; } } if ( ans == Int32 . MaxValue ) return - 1 ; return ans ; } static void swap ( char [ ] t , int i , int j ) { char temp = t [ i ] ; t [ i ] = t [ j ] ; t [ j ] = temp ; } static void Main ( ) { int n = 509201 ; Console . WriteLine ( minMoves ( n ) ) ; } }


"Minimum number of given operation required to convert n to m | C # implementation of the approach ; Function to return the minimum operations required ; Counting all 2 s ; Counting all 3 s ; If q contained only 2 and 3 as the only prime factors then it must be 1 now ; Driver code"
Solution in C#:

using System ; class GFG { static int minOperations ( int n , int m ) { if ( m % n != 0 ) return - 1 ; int minOperations = 0 ; int q = m / n ; while ( q % 2 == 0 ) { q = q / 2 ; minOperations ++ ; } while ( q % 3 == 0 ) { q = q / 3 ; minOperations ++ ; } if ( q == 1 ) return minOperations ; return - 1 ; } public static void Main ( ) { int n = 120 , m = 51840 ; Console . WriteLine ( minOperations ( n , m ) ) ; } }


"Minimum number of given operations required to reduce a number to 2 | C # program for the above approach ; Function to find the minimum number of operations required to reduce n to 2 ; Initialize a dp array ; Handle the base case ; Iterate in the range [ 2 , n ] ; Check if i * 5 <= n ; Check if i + 3 <= n ; Return the result ; Driver code ; Given Input ; Function Call ; Print the result"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int findMinOperations ( int n ) { int i ; int [ ] dp = new int [ n + 1 ] ; for ( i = 0 ; i < n + 1 ; i ++ ) { dp [ i ] = 999999 ; } dp [ 2 ] = 0 ; for ( i = 2 ; i < n + 1 ; i ++ ) { if ( i * 5 <= n ) { dp [ i * 5 ] = Math . Min ( dp [ i * 5 ] , dp [ i ] + 1 ) ; } if ( i + 3 <= n ) { dp [ i + 3 ] = Math . Min ( dp [ i + 3 ] , dp [ i ] + 1 ) ; } } return dp [ n ] ; } public static void Main ( ) { int n = 28 ; int m = findMinOperations ( n ) ; if ( m != 9999 ) Console . Write ( m ) ; else Console . Write ( - 1 ) ; } }


"Minimum number of increment / decrement operations such that array contains all elements from 1 to N | C # implementation of the above approach ; Function to find the minimum operations ; Sort the given array ; Count operations by assigning a [ i ] = i + 1 ; Driver Code"
Solution in C#:

using System ; class GFG { static long minimumMoves ( int [ ] a , int n ) { long operations = 0 ; Array . Sort ( a ) ; for ( int i = 0 ; i < n ; i ++ ) operations += ( long ) Math . Abs ( a [ i ] - ( i + 1 ) ) ; return operations ; } static public void Main ( ) { int [ ] arr = { 5 , 3 , 2 } ; int n = arr . Length ; Console . WriteLine ( minimumMoves ( arr , n ) ) ; } }


"Minimum number of jumps required to sort numbers placed on a number line | C # program for the above approach ; Function to find the minimum number of jumps required to sort the array ; Base Case ; Store the required result ; Stores the current position of elements and their respective maximum jump ; Used to check if a position is already taken by another element ; Stores the sorted array a [ ] ; Traverse the array w [ ] & update positions jumps array a [ ] ; Sort the array a [ ] ; Traverse the array a [ ] over the range [ 1 , N - 1 ] ; Store the index of current element and its just smaller element in array w [ ] ; Iterate until current element position is at most its just smaller element position ; Update the position of the current element ; Print the result ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void minJumps ( int [ ] w , int [ ] l , int n ) { if ( n == 1 ) { Console . Write ( 0 ) ; return ; } int ans = 0 ; Dictionary < int , int > pos = new Dictionary < int , int > ( ) ; Dictionary < int , int > jump = new Dictionary < int , int > ( ) ; Dictionary < int , bool > filled = new Dictionary < int , bool > ( ) ; int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( pos . ContainsKey ( w [ i ] ) ) pos [ w [ i ] ] = i ; else pos . Add ( w [ i ] , i ) ; if ( filled . ContainsKey ( w [ i ] ) ) filled [ i ] = true ; else filled . Add ( i , true ) ; if ( jump . ContainsKey ( w [ i ] ) ) jump [ w [ i ] ] = l [ i ] ; else jump . Add ( w [ i ] , l [ i ] ) ; a [ i ] = w [ i ] ; } Array . Sort ( a ) ; for ( int curr = 1 ; curr < n ; curr ++ ) { int currElementPos = pos [ a [ curr ] ] ; int prevElementPos = pos [ a [ curr - 1 ] ] ; if ( currElementPos > prevElementPos ) continue ; while ( currElementPos <= prevElementPos || filled . ContainsKey ( currElementPos ) && filled [ currElementPos ] ) { currElementPos += jump [ a [ curr ] ] ; ans ++ ; } if ( pos . ContainsKey ( a [ curr ] ) ) pos [ a [ curr ] ] = currElementPos ; else pos . Add ( a [ curr ] , currElementPos ) ; if ( filled . ContainsKey ( currElementPos ) ) filled [ currElementPos ] = true ; else filled . Add ( currElementPos , true ) ; } Console . Write ( ans ) ; } public static void Main ( ) { int [ ] W = { 2 , 1 , 4 , 3 } ; int [ ] L = { 4 , 1 , 2 , 4 } ; int N = W . Length ; minJumps ( W , L , N ) ; } }


"Minimum number of jumps to reach end | C # program to find Minimum number of jumps to reach end ; Returns Minimum number of jumps to reach end ; jumps [ 0 ] will hold the result ; Minimum number of jumps needed to reach last element from last elements itself is always 0 ; Start from the second element , move from right to left and construct the jumps [ ] array where jumps [ i ] represents minimum number of jumps needed to reach arr [ m - 1 ] from arr [ i ] ; If arr [ i ] is 0 then arr [ n - 1 ] can 't be reached from here ; If we can direcly reach to the end point from here then jumps [ i ] is 1 ; Otherwise , to find out the minimum number of jumps needed to reach arr [ n - 1 ] , check all the points reachable from here and jumps [ ] value for those points ; initialize min value ; following loop checks with all reachable points and takes the minimum ; Handle overflow ; or Integer . MAX_VALUE ; Driver Code"
Solution in C#:

using System ; class GFG { public static int minJumps ( int [ ] arr , int n ) { int [ ] jumps = new int [ n ] ; int min ; jumps [ n - 1 ] = 0 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] == 0 ) { jumps [ i ] = int . MaxValue ; } else if ( arr [ i ] >= n - i - 1 ) { jumps [ i ] = 1 ; } else { min = int . MaxValue ; for ( int j = i + 1 ; j < n && j <= arr [ i ] + i ; j ++ ) { if ( min > jumps [ j ] ) { min = jumps [ j ] ; } } if ( min != int . MaxValue ) { jumps [ i ] = min + 1 ; } else { jumps [ i ] = min ; } } } return jumps [ 0 ] ; } public static void Main ( string [ ] args ) { int [ ] arr = new int [ ] { 1 , 3 , 6 , 1 , 0 , 9 } ; int size = arr . Length ; Console . WriteLine ( " Minimum ▁ number ▁ of " + " ▁ jumps ▁ to ▁ reach ▁ end ▁ is ▁ " + minJumps ( arr , size ) ) ; } }


"Minimum number of jumps to reach end | C # program to find Minimum number of jumps to reach end ; Returns minimum number of jumps to reach arr [ h ] from arr [ l ] ; Base case : when source and destination are same ; When nothing is reachable from the given source ; Traverse through all the points reachable from arr [ l ] . Recursively get the minimum number of jumps needed to reach arr [ h ] from these reachable points . ; Driver code"
Solution in C#:

using System ; class GFG { static int minJumps ( int [ ] arr , int l , int h ) { if ( h == l ) return 0 ; if ( arr [ l ] == 0 ) return int . MaxValue ; int min = int . MaxValue ; for ( int i = l + 1 ; i <= h && i <= l + arr [ l ] ; i ++ ) { int jumps = minJumps ( arr , i , h ) ; if ( jumps != int . MaxValue && jumps + 1 < min ) min = jumps + 1 ; } return min ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 6 , 3 , 2 , 3 , 6 , 8 , 9 , 5 } ; int n = arr . Length ; Console . Write ( " Minimum ▁ number ▁ of ▁ jumps ▁ to ▁ reach ▁ end ▁ is ▁ " + minJumps ( arr , 0 , n - 1 ) ) ; } }


"Minimum number of leaves required to be removed from a Tree to satisfy the given condition | C # Program to find the minimum number of leaves to be deleted ; Stores the count of safe nodes ; Function to perform DFS on the Tree to obtain the count of vertices that are not required to be deleted ; Update cost to reach the vertex ; If the vertex does not satisfy the condition ; Otherwise ; Traverse its subtree ; Driver Code ; Stores the Tree ; Perform DFS ; Print the number of nodes to be deleted"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int cnt = 0 ; static void dfs ( int [ ] val , int [ ] cost , List < int > [ ] tr , int u , int s ) { s = s + cost [ u ] ; if ( s < 0 ) s = 0 ; if ( s > val [ u ] ) return ; cnt ++ ; for ( int i = 0 ; i < tr [ u ] . Count ; i ++ ) { dfs ( val , cost , tr , tr [ u ] [ i ] , s ) ; } } public static void Main ( String [ ] args ) { int n = 9 ; int [ ] val = { 88 , 22 , 83 , 14 , 95 , 91 , 98 , 53 , 11 } ; int [ ] cost = { - 1 , 24 , - 8 , 67 , 64 , 65 , 12 , - 80 , 8 } ; List < int > [ ] tr = new List < int > [ n + 1 ] ; for ( int i = 0 ; i < tr . Length ; i ++ ) tr [ i ] = new List < int > ( ) ; tr [ 0 ] . Add ( 3 ) ; tr [ 0 ] . Add ( 4 ) ; tr [ 4 ] . Add ( 6 ) ; tr [ 6 ] . Add ( 2 ) ; tr [ 2 ] . Add ( 1 ) ; tr [ 2 ] . Add ( 8 ) ; tr [ 8 ] . Add ( 5 ) ; tr [ 5 ] . Add ( 7 ) ; dfs ( val , cost , tr , 0 , 0 ) ; Console . Write ( n - cnt ) ; } }


"Minimum number of letters needed to make a total of n | C # implementation of the approach ; Function to return the minimum letters required to make a total of n ; Driver code"
Solution in C#:

using System ; class GFG { static int minLettersNeeded ( int n ) { if ( n % 26 == 0 ) return ( n / 26 ) ; else return ( ( n / 26 ) + 1 ) ; } public static void Main ( ) { int n = 52 ; Console . Write ( minLettersNeeded ( n ) ) ; } }


"Minimum number of mails required to distribute all the questions | C # code to find the minimum number of mails ; Function returns the min no of mails required ; Using the formula derived above ; Driver Code ; no of questions ; no of students ; maximum no of questions a mail can hold ; Calling function"
Solution in C#:

 using System ; class GFG { static double MinimumMail ( int n , int k , int x ) { double m = ( n - 1 ) + Math . Ceiling ( ( n - 1 ) * 1.0 / x ) * ( n - 1 ) + Math . Ceiling ( n * 1.0 / x ) * ( k - n ) ; return m ; } public static void Main ( ) { int N = 4 ; int K = 9 ; int X = 2 ; Console . WriteLine ( ( int ) MinimumMail ( N , K , X ) + "
 " ) ; } }


"Minimum number of moves required to reach the destination by the king in a chess board | C # program to Find the minimum number of moves required to reach the destination by the king in a chess board ; function to Find the minimum number of moves required to reach the destination by the king in a chess board ; minimum number of steps ; while the king is not in the same row or column as the destination ; Go up ; Go down ; Go left ; Go right ; Driver code"
Solution in C#:

using System ; class GFG { static void MinSteps ( int SourceX , int SourceY , int DestX , int DestY ) { Console . WriteLine ( Math . Max ( Math . Abs ( SourceX - DestX ) , Math . Abs ( SourceY - DestY ) ) ) ; while ( ( SourceX != DestX ) || ( SourceY != DestY ) ) { if ( SourceX < DestX ) { Console . Write ( ' U ' ) ; SourceX ++ ; } if ( SourceX > DestX ) { Console . Write ( ' D ' ) ; SourceX -- ; } if ( SourceY > DestY ) { Console . Write ( ' L ' ) ; SourceY -- ; } if ( SourceY < DestY ) { Console . Write ( ' R ' ) ; SourceY ++ ; } Console . WriteLine ( ) ; } } public static void Main ( ) { int sourceX = 4 , sourceY = 4 ; int destinationX = 7 , destinationY = 0 ; MinSteps ( sourceX , sourceY , destinationX , destinationY ) ; } }


"Minimum number of moves to make M and N equal by repeatedly adding any divisor of number to itself except 1 and the number | C # program for the above approach . ; Function to find the minimum number of moves to make N and M equal . ; Array to maintain the numbers included . ; pair of vertex , count ; run bfs from N ; if we reached goal ; Iterate in the range ; If i is a factor of aux ; If i is less than M - aux and is not included earlier . ; If aux / i is less than M - aux and is not included earlier . ; Not possible ; Driver Code"
Solution in C#:

using System ; using System . Collections ; class GFG { static int countOperations ( int N , int M ) { bool [ ] visited = new bool [ 100001 ] ; Queue Q = new Queue ( ) ; Q . Enqueue ( new Tuple < int , int > ( N , 0 ) ) ; visited [ N ] = true ; while ( Q . Count > 0 ) { int aux = ( ( Tuple < int , int > ) ( Q . Peek ( ) ) ) . Item1 ; int cont = ( ( Tuple < int , int > ) ( Q . Peek ( ) ) ) . Item2 ; Q . Dequeue ( ) ; if ( aux == M ) return cont ; for ( int i = 2 ; i * i <= aux ; i ++ ) if ( aux % i == 0 ) { if ( aux + i <= M && ! visited [ aux + i ] ) { Q . Enqueue ( new Tuple < int , int > ( aux + i , cont + 1 ) ) ; visited [ aux + i ] = true ; } if ( aux + aux / i <= M && ! visited [ aux + aux / i ] ) { Q . Enqueue ( new Tuple < int , int > ( aux + aux / i , cont + 1 ) ) ; visited [ aux + aux / i ] = true ; } } } return - 1 ; } static void Main ( ) { int N = 4 , M = 24 ; Console . WriteLine ( countOperations ( N , M ) ) ; } }


"Minimum number of moves to reach N starting from ( 1 , 1 ) | C # implementation of the approach ; Function to return the minimum number of moves required to reach the cell containing N starting from ( 1 , 1 ) ; To store the required answer ; For all possible values of divisors ; If i is a divisor of n ; Get the moves to reach n ; Return the required answer ; Driver code"
Solution in C#:

using System ; class GFG { static int min_moves ( int n ) { int ans = int . MaxValue ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { ans = Math . Min ( ans , i + n / i - 2 ) ; } } return ans ; } public static void Main ( String [ ] args ) { int n = 10 ; Console . WriteLine ( min_moves ( n ) ) ; } }


"Minimum number of operations on a binary string such that it gives 10 ^ A as remainder when divided by 10 ^ B | C # implementation of the approach ; Function to return the minimum number of operations on a binary string such that it gives 10 ^ A as remainder when divided by 10 ^ B ; Initialize result ; Loop through last b digits ; Driver code"
Solution in C#:

using System ; class GFG { static int findCount ( string s , int n , int a , int b ) { int res = 0 ; for ( int i = 0 ; i < b ; i ++ ) { if ( i == a ) { if ( s [ n - i - 1 ] != '1' ) res += 1 ; } else { if ( s [ n - i - 1 ] != '0' ) res += 1 ; } } return res ; } static public void Main ( ) { string str = "1001011001" ; int N = str . Length ; int A = 3 , B = 6 ; Console . WriteLine ( findCount ( str , N , A , B ) ) ; } }


"Minimum number of operations required to delete all elements of the array | C # implementation of the above approach ; function to find minimum operations ; sort array ; prepare hash of array ; Driver program"
Solution in C#:

using System ; public class Solution { public const int MAX = 10000 ; public static int [ ] hashTable = new int [ MAX ] ; public static int minOperations ( int [ ] arr , int n ) { Array . Sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) { hashTable [ arr [ i ] ] ++ ; } int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( hashTable [ arr [ i ] ] != 0 ) { for ( int j = i ; j < n ; j ++ ) { if ( arr [ j ] % arr [ i ] == 0 ) { hashTable [ arr [ j ] ] = 0 ; } } res ++ ; } } return res ; } public static void Main ( string [ ] args ) { int [ ] arr = new int [ ] { 4 , 6 , 2 , 8 , 7 , 21 , 24 , 49 , 44 } ; int n = arr . Length ; Console . Write ( minOperations ( arr , n ) ) ; } }


"Minimum number of operations required to make a permutation of first N natural numbers equal | C # program for the above approach ; Function to find the minimum number of operations required to make all array elements equal ; Store the count of operations required ; Increment by K - 1 , as the last element will be used again for the next K consecutive elements ; Increment count by 1 ; Return the result ; Driver Code ; Given Input"
Solution in C#:

using System ; class GFG { static int MinimumOperations ( int [ ] A , int N , int K ) { int Count = 0 ; int i = 0 ; while ( i < N - 1 ) { i = i + K - 1 ; Count ++ ; } return Count ; } public static void Main ( ) { int [ ] A = { 5 , 4 , 3 , 1 , 2 } ; int K = 3 ; int N = A . Length ; Console . WriteLine ( MinimumOperations ( A , N , K ) ) ; } }


"Minimum number of operations required to make all elements of at least one row of given Matrix prime | C # program to implement the above approach ; Function to generate all prime numbers using Sieve of Eratosthenes ; Function to check if a number is prime or not ; prime [ i ] : Check if i is a prime number or not ; Initialize prime [ ] array to true ; Iterate over the range [ 2 , sqrt ( n ) ] ; If p is a prime number ; Mark all multiples of i to false ; Update i ; Function to find minimum operations to make all elements of at least one row of the matrix as prime numbers ; dp [ i ] : Stores minimum operations to get i prime numbers in a row ; Initialize [ ] dp array to ( n + 1 ) ; Traverse the array ; Stores count of prime numbers in a i - th row ; Iterate over the range [ ( 1 << m ) - 1 , 0 ] ; If a row exist which contains j prime numbers ; Update dp [ j bitmask ] ; Update dp [ bitmask ] ; Return minimum operations to get a row of the matrix with all prime numbers ; Function to count prime numbers in a row ; i - th bit of bitmask check if i - th column is a prime or not ; Traverse the array ; if a [ i ] is a prime number ; Update bitmask ; Driver Code ; Stores count of columns in the matrix ; Stores length ; Calculate all prime numbers in range [ 1 , max ] using sieve ; Function Call"
Solution in C#:

using System ; public class GFG { private static bool [ ] prime ; private static void sieve ( int n ) { prime = new bool [ n + 1 ] ; for ( int i = 0 ; i < prime . Length ; i ++ ) prime [ i ] = true ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) prime [ i ] = false ; } } } private static int MinWays ( int [ , ] a , int n , int m ) { int [ ] dp = new int [ 1 << m ] ; for ( int i = 0 ; i < dp . Length ; i ++ ) { dp [ i ] = n + 1 ; } for ( int i = 0 ; i < a . GetLength ( 0 ) ; i ++ ) { int bitmask = BitMask ( GetRow ( a , i ) ) ; for ( int j = ( 1 << m ) - 1 ; j >= 0 ; j -- ) { if ( dp [ j ] != n + 1 ) { dp [ j bitmask ] = Math . Min ( dp [ j bitmask ] , dp [ j ] + 1 ) ; } } dp [ bitmask ] = 1 ; } return ( dp [ ( 1 << m ) - 1 ] - 1 ) == ( n + 1 ) ? - 1 : ( dp [ ( 1 << m ) - 1 ] - 1 ) ; } private static int BitMask ( int [ ] a ) { int bitmask = 0 ; for ( int i = 0 ; i < a . Length ; i ++ ) { if ( prime [ a [ i ] ] ) { bitmask |= ( 1 << i ) ; } } return bitmask ; } public static int [ ] GetRow ( int [ , ] matrix , int row ) { var rowLength = matrix . GetLength ( 1 ) ; var rowVector = new int [ rowLength ] ; for ( var i = 0 ; i < rowLength ; i ++ ) rowVector [ i ] = matrix [ row , i ] ; return rowVector ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 4 , 6 , 5 , 8 } , { 2 , 9 , 12 , 14 } , { 32 , 7 , 18 , 16 } , { 12 , 4 , 35 , 17 } } ; int m = mat . GetLength ( 0 ) ; int n = mat . GetLength ( 1 ) ; int max = 10000 ; sieve ( max ) ; Console . WriteLine ( MinWays ( mat , n , m ) ) ; } }


"Minimum number of operations required to make an array non | C # program for the above approach ; Function to count the minimum number of steps required to make arr non - decreasing ; Stores differences ; Stores the max number ; Traverse the array arr [ ] ; Update mx ; Update val ; Stores the result ; Iterate until 2 ^ res - 1 is less than val ; Return the answer ; Driver Code ; Given input ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int countMinSteps ( int [ ] arr , int N ) { int val = 0 ; int mx = Int32 . MinValue ; for ( int i = 0 ; i < N ; i ++ ) { int curr = arr [ i ] ; mx = Math . Max ( mx , curr ) ; val = Math . Max ( val , mx - curr ) ; } int res = 0 ; while ( ( 1 << res ) - 1 < val ) { ++ res ; } return res ; } public static void Main ( ) { int [ ] arr = { 1 , 7 , 6 , 5 } ; int N = arr . Length ; Console . Write ( countMinSteps ( arr , N ) ) ; } }


"Minimum number of operations required to make two strings equal | C # implementation to find the minimum number of operations to make two Strings equal ; Function to find out parent of an alphabet ; Function to merge two different alphabets ; Merge a and b using rank compression ; Function to find the minimum number of operations required ; Initializing parent to i and rank ( size ) to 1 ; We will store our answer in this vector ; Traversing Strings ; If they have different parents ; Find their respective parents and merge them ; Store this in our Answer vector ; Number of operations ; Driver Code ; Two Strings S1 and S2 ; Function call"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static readonly int MAX = 500001 ; static int [ ] parent = new int [ MAX ] ; static int [ ] Rank = new int [ MAX ] ; class pair { public char first , second ; public pair ( char first , char second ) { this . first = first ; this . second = second ; } } static int find ( int x ) { return parent [ x ] = parent [ x ] == x ? x : find ( parent [ x ] ) ; } static void merge ( int r1 , int r2 ) { if ( r1 != r2 ) { if ( Rank [ r1 ] > Rank [ r2 ] ) { parent [ r2 ] = r1 ; Rank [ r1 ] += Rank [ r2 ] ; } else { parent [ r1 ] = r2 ; Rank [ r2 ] += Rank [ r1 ] ; } } } static void minimumOperations ( String s1 , String s2 ) { for ( int i = 1 ; i <= 26 ; i ++ ) { parent [ i ] = i ; Rank [ i ] = 1 ; } List < pair > ans = new List < pair > ( ) ; for ( int i = 0 ; i < s1 . Length ; i ++ ) { if ( s1 [ i ] != s2 [ i ] ) { if ( find ( s1 [ i ] - 96 ) != find ( s2 [ i ] - 96 ) ) { int x = find ( s1 [ i ] - 96 ) ; int y = find ( s2 [ i ] - 96 ) ; merge ( x , y ) ; ans . Add ( new pair ( s1 [ i ] , s2 [ i ] ) ) ; } } } Console . Write ( ans . Count + "
 " ) ; for ( int i = 0 ; i < ans . Count ; i ++ ) Console . Write ( ans [ i ] . first + " - > " + ans [ i ] . second + "
 " ) ; } public static void Main ( String [ ] args ) { String s1 , s2 ; s1 = " abb " ; s2 = " dad " ; minimumOperations ( s1 , s2 ) ; } }


"Minimum number of operations required to maximize the Binary String | C # program to implement the above approach ; Function to find the number of operations required ; Count of 1 's ; Count of 0 's upto (cnt1)-th index ; Return the answer ; Driver Code"
Solution in C#:

 using System ; class GFG { static int minOperation ( String s , int n ) { int cnt1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '1' ) cnt1 ++ ; } int cnt0 = 0 ; for ( int i = 0 ; i < cnt1 ; i ++ ) { if ( s [ i ] == '0' ) cnt0 ++ ; } return cnt0 ; } public static void Main ( String [ ] args ) { int n = 8 ; String s = "01001011" ; int ans = minOperation ( s , n ) ; Console . Write ( ans + "
 " ) ; } }


"Minimum number of operations required to maximize the Binary String | C # program to implement the above approach ; Function to find the number of operations required ; Swap 0 ' s ▁ and ▁ 1' s ; Return the answer ; Driver code"
Solution in C#:

using System ; class GFG { static int minOperation ( String s , int n ) { int ans = 0 ; int i = 0 , j = n - 1 ; while ( i < j ) { if ( s [ i ] == '0' && s [ j ] == '1' ) { ans ++ ; i ++ ; j -- ; continue ; } if ( s [ i ] == '1' ) { i ++ ; } if ( s [ j ] == '0' ) { j -- ; } } return ans ; } public static void Main ( String [ ] args ) { int n = 8 ; String s = "10100101" ; Console . WriteLine ( minOperation ( s , n ) ) ; } }


"Minimum number of operations required to obtain a given Binary String | C # program to implement the above approach ; Function to find the minimum number of operations required to obtain the string s ; Iterate the string s ; If first occurrence of 1 is found ; Mark the index ; Base case : If no 1 occurred ; No operations required ; Stores the character for which last operation was performed ; Stores minimum number of operations ; Iterate from pos to n ; Check if s [ i ] is 0 ; Check if last operation was performed because of 1 ; Set last to 0 ; Check if last operation was performed because of 0 ; Set last to 1 ; Return the answer ; Driver code"
Solution in C#:

using System ; class GFG { static int minOperations ( String s ) { int n = s . Length ; int pos = - 1 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == '1' ) { pos = i ; break ; } } if ( pos == - 1 ) { return 0 ; } int last = 1 ; int ans = 1 ; for ( int i = pos + 1 ; i < n ; i ++ ) { if ( s [ i ] == '0' ) { if ( last == 1 ) { ans ++ ; last = 0 ; } } else { if ( last == 0 ) { ans ++ ; last = 1 ; } } } return ans ; } public static void Main ( string [ ] args ) { String s = "10111" ; Console . Write ( minOperations ( s ) ) ; } }


"Minimum number of operations required to reduce N to 0 | C # Program to implement the above approach ; Function to find the minimum steps required to reduce n ; Base case ; Return answer based on parity of n ; Driver Code"
Solution in C#:

using System ; class GFG { static int downToZero ( int n ) { if ( n <= 3 ) return n ; return n % 2 == 0 ? 3 : 4 ; } public static void Main ( String [ ] args ) { int n = 4 ; Console . WriteLine ( downToZero ( n ) ) ; } }


"Minimum number of operations required to reduce N to 0 | C # program to implement the above approach ; Function to count the minimum steps required to reduce n ; Base case ; Allocate memory for storing intermediate results ; Store base values ; Stores square root of each number ; Compute square root ; Use rule 1 to find optimized answer ; Check if it perfectly divides n ; Use of rule 2 to find the optimized answer ; Store computed value ; Return answer ; Driver Code"
Solution in C#:

using System ; class GFG { static int downToZero ( int n ) { if ( n <= 3 ) return n ; int [ ] dp = new int [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) dp [ i ] = - 1 ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; dp [ 2 ] = 2 ; dp [ 3 ] = 3 ; int sqr ; for ( int i = 4 ; i <= n ; i ++ ) { sqr = ( int ) Math . Sqrt ( i ) ; int best = int . MaxValue ; while ( sqr > 1 ) { if ( i % sqr == 0 ) { best = Math . Min ( best , 1 + dp [ sqr ] ) ; } sqr -- ; } best = Math . Min ( best , 1 + dp [ i - 1 ] ) ; dp [ i ] = best ; } return dp [ n ] ; } public static void Main ( String [ ] args ) { int n = 4 ; Console . Write ( downToZero ( n ) ) ; } }


"Minimum number of operations required to reduce N to 1 | C # implementation of above approach ; Function that returns the minimum number of operations to be performed to reduce the number to 1 ; Base cases ; Driver code"
Solution in C#:

using System ; class GFG { static int count_minimum_operations ( int n ) { if ( n == 2 ) { return 1 ; } else if ( n == 1 ) { return 0 ; } if ( n % 3 == 0 ) { return 1 + count_minimum_operations ( n / 3 ) ; } else if ( n % 3 == 1 ) { return 1 + count_minimum_operations ( n - 1 ) ; } else { return 1 + count_minimum_operations ( n + 1 ) ; } } static void Main ( ) { int n = 4 ; int ans = count_minimum_operations ( n ) ; Console . WriteLine ( ans ) ; } }


"Minimum number of operations required to reduce N to 1 | C # implementation of above approach ; Function that returns the minimum number of operations to be performed to reduce the number to 1 ; To stores the total number of operations to be performed ; if n is divisible by 3 then reduce it to n / 3 ; if n modulo 3 is 1 decrement it by 1 ; if n modulo 3 is 2 then increment it by 1 ; update the counter ; Driver code"
Solution in C#:

using System ; public class GFG { static int count_minimum_operations ( long n ) { int count = 0 ; while ( n > 1 ) { if ( n % 3 == 0 ) n /= 3 ; else if ( n % 3 == 1 ) n -- ; else { if ( n == 2 ) n -- ; else n ++ ; } count ++ ; } return count ; } static public void Main ( ) { long n = 4 ; long ans = count_minimum_operations ( n ) ; Console . WriteLine ( ans ) ; } }


"Minimum number of operations required to sum to binary string S | C # program to find the minimum number of operations required to sum to N ; Function to return the minimum operations required to sum to a number reprented by the binary string S ; Reverse the string to consider it from LSB to MSB ; initialise the dp table ; If S [ 0 ] = '0' , there is no need to perform any operation ; If S [ 0 ] = '1' , just perform a single operation ( i . e Add 2 ^ 0 ) ; Irrespective of the LSB , dp [ 0 , 1 ] is always 1 as there is always the need of making the suffix of the binary string of the form "11 . . . . 1" as suggested by the definition of dp [ i , 1 ] ; Transition from dp [ i - 1 , 0 ] ; 1. Transition from dp [ i - 1 , 1 ] by just doing 1 extra operation of subtracting 2 ^ i 2. Transition from dp [ i - 1 , 0 ] by just doing 1 extra operation of subtracting 2 ^ ( i + 1 ) ; Transition from dp [ i - 1 , 1 ] ; 1. Transition from dp [ i - 1 , 1 ] by just doing 1 extra operation of adding 2 ^ ( i + 1 ) 2. Transition from dp [ i - 1 , 0 ] by just doing 1 extra operation of adding 2 ^ i ; Swap values of left and right ; Driver Code"
Solution in C#:

using System ; class GFG { static int findMinOperations ( String S ) { S = reverse ( S ) ; int n = S . Length ; int [ , ] dp = new int [ n + 1 , 2 ] ; if ( S [ 0 ] == '0' ) { dp [ 0 , 0 ] = 0 ; } else { dp [ 0 , 0 ] = 1 ; } dp [ 0 , 1 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( S [ i ] == '0' ) { dp [ i , 0 ] = dp [ i - 1 , 0 ] ; dp [ i , 1 ] = 1 + Math . Min ( dp [ i - 1 , 1 ] , dp [ i - 1 , 0 ] ) ; } else { dp [ i , 1 ] = dp [ i - 1 , 1 ] ; dp [ i , 0 ] = 1 + Math . Min ( dp [ i - 1 , 0 ] , dp [ i - 1 , 1 ] ) ; } } return dp [ n - 1 , 0 ] ; } static String reverse ( String input ) { char [ ] temparray = input . ToCharArray ( ) ; int left , right = 0 ; right = temparray . Length - 1 ; for ( left = 0 ; left < right ; left ++ , right -- ) { char temp = temparray [ left ] ; temparray [ left ] = temparray [ right ] ; temparray [ right ] = temp ; } return String . Join ( " " , temparray ) ; } public static void Main ( ) { String S = "100" ; Console . WriteLine ( findMinOperations ( S ) ) ; S = "111" ; Console . WriteLine ( findMinOperations ( S ) ) ; } }


"Minimum number of operations to convert a given sequence into a Geometric Progression | C # program to find minimum number of operations to convert a given sequence to an Geometric Progression ; Function to print the GP series ; Check for possibility ; Function for getting the Arithmetic Progression ; The array c describes all the given set of possible operations . ; Size of c ; candidate answer ; loop through all the permutations of the first two elements . ; a1 and a2 are the candidate first two elements of the possible GP . ; temp stores the current answer , including the modification of the first two elements . ; common ratio of the possible GP ; To check if the chosen set is valid , and id yes find the number of operations it takes . ; ai is value of a [ i ] according to the assumed first two elements a1 , a2 ith element of an GP = a1 * ( ( a2 - a1 ) ^ ( i - 1 ) ) ; Check for the " proposed " element to be only differing by one ; set the temporary ans ; to infinity and break ; update answer ; Calling function to print the sequence ; Driver Code ; array is 1 - indexed , with a [ 0 ] = 0 for the sake of simplicity ; Function to print the minimum operations and the sequence of elements"
Solution in C#:

 using System ; class GFG { class pair { public double first , second ; public pair ( double first , double second ) { this . first = first ; this . second = second ; } } static void construct ( int n , pair ans_pair ) { if ( ans_pair . first == - 1 ) { Console . Write ( " Not ▁ possible " ) ; return ; } double a1 = ans_pair . first ; double a2 = ans_pair . second ; double r = a2 / a1 ; Console . Write ( " The ▁ resultant ▁ sequence ▁ is :
 " ) ; for ( int i = 1 ; i <= n ; i ++ ) { int ai = ( int ) ( a1 * Math . Pow ( r , i - 1 ) ) ; Console . Write ( ai + " ▁ " ) ; } } static void findMinimumOperations ( double [ ] a , int n ) { int ans = int . MaxValue ; int [ ] c = { - 1 , 0 , 1 } ; int possibilities = 3 ; int pos1 = - 1 , pos2 = - 1 ; for ( int i = 0 ; i < possibilities ; i ++ ) { for ( int j = 0 ; j < possibilities ; j ++ ) { double a1 = a [ 1 ] + c [ i ] ; double a2 = a [ 2 ] + c [ j ] ; int temp = ( int ) ( Math . Abs ( a1 - a [ 1 ] ) + Math . Abs ( a2 - a [ 2 ] ) ) ; if ( a1 == 0 a2 == 0 ) continue ; double r = a2 / a1 ; for ( int pos = 3 ; pos <= n ; pos ++ ) { double ai = a1 * Math . Pow ( r , pos - 1 ) ; if ( a [ pos ] == ai ) { continue ; } else if ( a [ pos ] + 1 == ai a [ pos ] - 1 == ai ) { temp ++ ; } else { temp = int . MaxValue ; break ; } } if ( temp < ans ) { ans = temp ; pos1 = ( int ) a1 ; pos2 = ( int ) a2 ; } } } if ( ans == - 1 ) { Console . Write ( " - 1" ) ; return ; } Console . Write ( " Minimum ▁ Number ▁ of ▁ Operations ▁ are ▁ " + ans + "
 " ) ; pair ans_pair = new pair ( pos1 , pos2 ) ; construct ( n , ans_pair ) ; } public static void Main ( String [ ] args ) { double [ ] a = { 0 , 7 , 20 , 49 , 125 } ; int n = a . Length ; findMinimumOperations ( a , n - 1 ) ; } }


"Minimum number of operations to convert a given sequence into a Geometric Progression | Set 2 | C # program for the above approach ; Function to find minimum cost ; Sort the array ; Maximum possible common ratios ; Iterate over all possible common ratios ; Calculate operations required for the current common ratio ; Calculate minimum cost ; Driver Code ; Given N ; Given [ ] arr ; Function Calling"
Solution in C#:

 using System ; class GFG { static void minCost ( int [ ] arr , int n ) { if ( n == 1 ) { Console . Write ( 0 + "
 " ) ; return ; } Array . Sort ( arr ) ; float raised = 1 / ( float ) ( n - 1 ) ; float temp = ( float ) Math . Pow ( arr [ n - 1 ] , raised ) ; int r = ( int ) ( temp ) + 1 ; int i , j , min_cost = int . MaxValue ; int common_ratio = 1 ; for ( j = 1 ; j <= r ; j ++ ) { int curr_cost = 0 , prod = 1 ; for ( i = 0 ; i < n ; i ++ ) { curr_cost += Math . Abs ( arr [ i ] - prod ) ; prod *= j ; if ( curr_cost >= min_cost ) break ; } if ( i == n ) { min_cost = Math . Min ( min_cost , curr_cost ) ; common_ratio = j ; } } Console . Write ( min_cost + " ▁ " ) ; Console . Write ( common_ratio + " ▁ " ) ; } public static void Main ( String [ ] args ) { int N = 6 ; int [ ] arr = { 1 , 11 , 4 , 27 , 15 , 33 } ; minCost ( arr , N ) ; } }


"Minimum number of operations to convert array A to array B by adding an integer into a subarray | C # implementation to find the minimum number of operations in which the array A can be converted to another array B ; Function to find the minimum number of operations in which array A can be converted to array B ; Loop to iterate over the array ; if both elements are equal then move to next element ; Calculate the difference between two elements ; loop while the next pair of elements have same difference ; Increase the number of operations by 1 ; Print the number of operations required ; Driver Code"
Solution in C#:

using System ; class GFG { static void checkArray ( int [ ] a , int [ ] b , int n ) { int operations = 0 ; int i = 0 ; while ( i < n ) { if ( a [ i ] - b [ i ] == 0 ) { i ++ ; continue ; } int diff = a [ i ] - b [ i ] ; i ++ ; while ( i < n && a [ i ] - b [ i ] == diff ) { i ++ ; } operations ++ ; } Console . WriteLine ( operations ) ; } public static void Main ( string [ ] args ) { int [ ] a = { 3 , 7 , 1 , 4 , 1 , 2 } ; int [ ] b = { 3 , 7 , 3 , 6 , 3 , 2 } ; int size = a . Length ; checkArray ( a , b , size ) ; } }


"Minimum number of palindromes required to express N as a sum | Set 2 | C # program to find the minimum number of palindromes required to express N as a sum ; A utility for creating palindrome ; checks if number of digits is odd or even if odd then neglect the last digit of input in finding reverse as in case of odd number of digits middle element occur once ; Creates palindrome by just appending reverse of number to itself ; Function to generate palindromes ; Run two times for odd and even length palindromes ; Creates palindrome numbers with first half as i . Value of j decides whether we need an odd length or even length palindrome . ; Swap values of left and right ; Function to find the minimum number of palindromes required to express N as a sum ; Checking if the number is a palindrome ; Checking if the number is a sum of two palindromes Getting the list of all palindromes upto N ; Sorting the list of palindromes ; The answer is three if the control reaches till this point ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int createPalindrome ( int input , int isOdd ) { int n = input ; int palin = input ; if ( isOdd % 2 == 1 ) { n /= 10 ; } while ( n > 0 ) { palin = palin * 10 + ( n % 10 ) ; n /= 10 ; } return palin ; } static List < int > generatePalindromes ( int N ) { List < int > palindromes = new List < int > ( ) ; int number ; for ( int j = 0 ; j < 2 ; j ++ ) { int i = 1 ; while ( ( number = createPalindrome ( i ++ , j ) ) <= N ) { palindromes . Add ( number ) ; } } return palindromes ; } static String reverse ( String input ) { char [ ] temparray = input . ToCharArray ( ) ; int left , right = 0 ; right = temparray . Length - 1 ; for ( left = 0 ; left < right ; left ++ , right -- ) { char temp = temparray [ left ] ; temparray [ left ] = temparray [ right ] ; temparray [ right ] = temp ; } return String . Join ( " " , temparray ) ; } static int minimumNoOfPalindromes ( int N ) { String a = String . Join ( " " , N ) ; String b = String . Join ( " " , N ) ; b = reverse ( b ) ; if ( a . Equals ( b ) ) { return 1 ; } List < int > palindromes = generatePalindromes ( N ) ; palindromes . Sort ( ) ; int l = 0 , r = palindromes . Count - 1 ; while ( l < r ) { if ( palindromes [ l ] + palindromes [ r ] == N ) { return 2 ; } else if ( palindromes [ l ] + palindromes [ r ] < N ) { ++ l ; } else { -- r ; } } return 3 ; } public static void Main ( String [ ] args ) { int N = 65 ; Console . WriteLine ( minimumNoOfPalindromes ( N ) ) ; } }


"Minimum number of pigs required to find the poisonous bucket | C # program for the above approach ; Function to find the minimum number of pigs required to find the poisonous bucket ; Print the result ; Driver Code"
Solution in C#:

using System ; class GFG { static void poorPigs ( int buckets , int minutesToDie , int minutesToTest ) { Console . WriteLine ( ( int ) Math . Ceiling ( Math . Log ( buckets ) / Math . Log ( ( minutesToTest / minutesToDie ) + 1 ) ) ) ; } static public void Main ( ) { int N = 1000 , M = 15 , P = 60 ; poorPigs ( N , M , P ) ; } }


"Minimum number of power terms with sum equal to n | C # program to calculate minimum numberof powers of x to make sum equal to n . ; Return minimum power terms of x required ; if x is 1 , return n since any power of 1 is 1 only . ; Consider n = a * x + b where a = n / x and b = n % x . ; Update count of powers for 1 's  added ; Repeat the process for reduced n ; Driver code"
Solution in C#:

using System ; class GFG { static int minPower ( int n , int x ) { if ( x == 1 ) return n ; int ans = 0 ; while ( n > 0 ) { ans += ( n % x ) ; n /= x ; } return ans ; } public static void Main ( ) { int n = 5 , x = 3 ; Console . WriteLine ( minPower ( n , x ) ) ; } }


"Minimum number of primes required such that their sum is equal to N | C # program for the above approach ; Function to check if n is prime ; Function to count the minimum prime required for given sum N ; Case 1 : ; Case 2 : ; Case 3 : ; Case 3 a : ; Case 3 b : ; Driver Code ; Function Call"
Solution in C#:

 using System ; class GFG { static bool isPrime ( int n ) { for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { return false ; } } return true ; } static void printMinCountPrime ( int N ) { int minCount ; if ( isPrime ( N ) ) { minCount = 1 ; } else if ( N % 2 == 0 ) { minCount = 2 ; } else { if ( isPrime ( N - 2 ) ) { minCount = 2 ; } else { minCount = 3 ; } } Console . WriteLine ( minCount + "
 " ) ; } public static void Main ( string [ ] args ) { int N = 100 ; printMinCountPrime ( N ) ; } }


"Minimum number of removals required such that no subsequence of length 2 occurs more than once | C # program for the above approach ; Function to remove the minimum count of characters from the string such that no subsequence of length 2 repeats ; Initialize the final string ; Stores if any character occurs in the final string or not ; Store the index of the last character added in the string ; Traverse the string ; Add all the unique characters ; Check if S [ 0 ] appears in the range [ pos + 1 , N - 1 ] ; If the characters are the same ; Print the resultant string ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void RemoveCharacters ( string s ) { string ans = " " ; int [ ] c = new int [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) c [ i ] = 0 ; int pos = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( c [ ( int ) s [ i ] - 97 ] == 0 ) { c [ ( int ) s [ i ] - 97 ] = 1 ; pos = i ; ans += s [ i ] ; } } for ( int i = pos + 1 ; i < s . Length ; i ++ ) { if ( s [ i ] == s [ 0 ] ) { ans += s [ i ] ; break ; } } Console . Write ( ans ) ; } public static void Main ( ) { string S = " abcaadbcd " ; RemoveCharacters ( S ) ; } }


"Minimum number of replacement done of substring "01" with "110" to remove it completely | C # program for the above approach ; Function to find the minimum number of replacement of "01" with "110" s . t . S doesn 't contain substring "10" ; Stores the number of operations performed ; Stores the resultant count of substrings ; Traverse the string S from end ; If the current character is 0 ; If the current character is 1 ; Print the result ; Driver code"
Solution in C#:

using System ; class GFG { public static void minimumOperations ( string S , int N ) { int ans = 0 ; int cntOne = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( S [ i ] == '0' ) { ans += cntOne ; cntOne *= 2 ; } else cntOne ++ ; } Console . WriteLine ( ans ) ; } static void Main ( ) { string S = "001" ; int N = S . Length ; minimumOperations ( S , N ) ; } }


"Minimum number of replacements to make the binary string alternating | Set 2 | C # implementation of the approach ; Function to return the minimum number of characters of the given binary string to be replaced to make the string alternating ; If there is 1 at even index positions ; If there is 0 at odd index positions ; Driver code"
Solution in C#:

using System ; class GFG { static int minReplacement ( String s , int len ) { int ans = 0 ; for ( int i = 0 ; i < len ; i ++ ) { if ( i % 2 == 0 && s [ i ] == '1' ) ans ++ ; if ( i % 2 == 1 && s [ i ] == '0' ) ans ++ ; } return Math . Min ( ans , len - ans ) ; } public static void Main ( String [ ] args ) { String s = "1100" ; int len = s . Length ; Console . Write ( minReplacement ( s , len ) ) ; } }


"Minimum number of reversals to reach node 0 from every other node | C # program for the above approach ; Function to find minimum reversals ; Add all adjacent nodes to the node in the graph ; Insert edges in the graph ; Insert edges in the reversed graph ; Create array visited to mark all the visited nodes ; Stores the number of edges to be reversed ; BFS Traversal ; Pop the current node from the queue ; mark the current node visited ; Intitialize count of edges need to be reversed to 0 ; Enqueue adjacent nodes in the reversed graph to the queue , if not visited ; Enqueue adjacent nodes in graph to the queue , if not visited count the number of nodes added to the queue ; Update the reverse edge to the final count ; Return the result ; Driver Code ; Given edges to the graph ; Number of nodes ; Function Call"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int minRev ( ArrayList edges , int n ) { Dictionary < int , ArrayList > graph = new Dictionary < int , ArrayList > ( ) ; Dictionary < int , ArrayList > graph_rev = new Dictionary < int , ArrayList > ( ) ; for ( int i = 0 ; i < edges . Count ; i ++ ) { int x = ( int ) ( ( ArrayList ) edges [ i ] ) [ 0 ] ; int y = ( int ) ( ( ArrayList ) edges [ i ] ) [ 1 ] ; if ( ! graph . ContainsKey ( x ) ) { graph [ x ] = new ArrayList ( ) ; } graph [ x ] . Add ( y ) ; if ( ! graph_rev . ContainsKey ( y ) ) { graph_rev [ y ] = new ArrayList ( ) ; } graph_rev [ y ] . Add ( x ) ; } Queue q = new Queue ( ) ; ArrayList visited = new ArrayList ( ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) { visited . Add ( false ) ; } q . Enqueue ( 0 ) ; int ans = 0 ; while ( q . Count != 0 ) { int curr = ( int ) q . Peek ( ) ; visited [ curr ] = true ; int count = 0 ; q . Dequeue ( ) ; if ( graph_rev . ContainsKey ( curr ) ) { for ( int i = 0 ; i < graph_rev [ curr ] . Count ; i ++ ) { if ( ! ( bool ) visited [ ( int ) ( ( ArrayList ) graph_rev [ curr ] ) [ i ] ] ) { q . Enqueue ( ( int ) ( ( ArrayList ) graph_rev [ curr ] ) [ i ] ) ; } } } if ( graph . ContainsKey ( curr ) ) { for ( int i = 0 ; i < ( ( ArrayList ) graph [ curr ] ) . Count ; i ++ ) { if ( ! ( bool ) visited [ ( int ) ( ( ArrayList ) graph [ curr ] ) [ i ] ] ) { q . Enqueue ( ( int ) ( ( ArrayList ) graph [ curr ] ) [ i ] ) ; count ++ ; } } } ans += count ; } return ans ; } public static void Main ( string [ ] args ) { ArrayList edges = new ArrayList ( ) { new ArrayList ( ) { 0 , 1 } , new ArrayList ( ) { 1 , 3 } , new ArrayList ( ) { 2 , 3 } , new ArrayList ( ) { 4 , 0 } , new ArrayList ( ) { 4 , 5 } } ; int n = 6 ; Console . Write ( minRev ( edges , n ) ) ; } }


"Minimum number of sets with numbers less than Y | C # program to find the minimum number sets with consecutive numbers and less than Y ; Function to find the minimum number of shets ; Variable to count the number of sets ; Iterate in the string ; Add the number to string ; Mark that we got a number ; else Every time it exceeds ; Check if previous was anytime less than Y ; Current number ; Check for current number ; Check for last added number ; Driver Code"
Solution in C#:

using System ; class solution { static int minimumSets ( string s , int y ) { int cnt = 0 ; int num = 0 ; int l = s . Length ; bool f = false ; for ( int i = 0 ; i < l ; i ++ ) { num = num * 10 + ( s [ i ] - '0' ) ; if ( num <= y ) f = true ; { if ( f ) cnt += 1 ; num = s [ i ] - '0' ; f = false ; if ( num <= y ) f = true ; else num = 0 ; } } if ( f == true ) cnt += 1 ; return cnt ; } public static void Main ( ) { string s = "1234" ; int y = 30 ; Console . WriteLine ( minimumSets ( s , y ) ) ; } }


"Minimum number of single digit primes required whose sum is equal to N | C # program to find the minimum number of single digit prime numbers required which when summed equals to a given number N . ; function to check if i - th index is valid or not ; function to find the minimum number of single digit prime numbers required which when summed up equals to a given number N . ; Not possible ; Driver Code"
Solution in C#:

using System ; class GFG { static Boolean check ( int i , int val ) { if ( i - val < 0 ) return false ; else return true ; } static double MinimumPrimes ( int n ) { double [ ] dp ; dp = new double [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) dp [ i ] = 1e9 ; dp [ 0 ] = dp [ 2 ] = dp [ 3 ] = dp [ 5 ] = dp [ 7 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( check ( i , 2 ) ) dp [ i ] = Math . Min ( dp [ i ] , 1 + dp [ i - 2 ] ) ; if ( check ( i , 3 ) ) dp [ i ] = Math . Min ( dp [ i ] , 1 + dp [ i - 3 ] ) ; if ( check ( i , 5 ) ) dp [ i ] = Math . Min ( dp [ i ] , 1 + dp [ i - 5 ] ) ; if ( check ( i , 7 ) ) dp [ i ] = Math . Min ( dp [ i ] , 1 + dp [ i - 7 ] ) ; } if ( dp [ n ] == ( 1e9 ) ) return - 1 ; else return dp [ n ] ; } public static void Main ( String [ ] args ) { int n = 12 ; int minimal = ( int ) MinimumPrimes ( n ) ; if ( minimal != - 1 ) Console . WriteLine ( " Minimum ▁ number ▁ of ▁ single ▁ " + " digit ▁ primes ▁ required : ▁ " + minimal ) ; else Console . WriteLine ( " Not ▁ Possible " ) ; } }


"Minimum number of socks required to picked to have at least K pairs of the same color | C # program for the above approach ; Function to count the minimum number of socks to be picked ; Stores the total count of pairs of socks ; Find the total count of pairs ; If K is greater than pairs ; Otherwise ; Driver Code"
Solution in C#:

using System ; class GFG { public static int findMin ( int [ ] arr , int N , int k ) { int pairs = 0 ; for ( int i = 0 ; i < N ; i ++ ) { pairs += arr [ i ] / 2 ; } if ( k > pairs ) return - 1 ; else return 2 * k + N - 1 ; } public static void Main ( string [ ] args ) { int [ ] arr = { 4 , 5 , 6 } ; int K = 3 ; int N = arr . Length ; Console . WriteLine ( findMin ( arr , N , K ) ) ; } }


"Minimum number of square tiles required to fill the rectangular floor | C # implementation of above approach ; Function to find the number of tiles ; if breadth is divisible by side of square ; tiles required is N / s ; one more tile required ; if length is divisible by side of square ; tiles required is M / s ; one more tile required ; Driver Code ; input length and breadth of rectangle and side of square"
Solution in C#:

using System ; class GFG { static int solve ( int M , int N , int s ) { if ( N % s == 0 ) { N = N / s ; } else { N = ( N / s ) + 1 ; } if ( M % s == 0 ) { M = M / s ; } else { M = ( M / s ) + 1 ; } return M * N ; } static void Main ( ) { int N = 12 , M = 13 , s = 4 ; Console . WriteLine ( solve ( M , N , s ) ) ; } }


"Minimum number of square tiles required to fill the rectangular floor | C # implementation of above approach ; Function to find the number of tiles ; no of tiles ; Driver Code ; input length and breadth of rectangle and side of square"
Solution in C#:

using System ; class GFG { static int solve ( double M , double N , double s ) { int ans = ( ( int ) ( Math . Ceiling ( M / s ) ) * ( int ) ( Math . Ceiling ( N / s ) ) ) ; return ans ; } public static void Main ( ) { double N = 12 , M = 13 , s = 4 ; Console . WriteLine ( solve ( M , N , s ) ) ; } }


"Minimum number of squares whose sum equals to a given number N | Set | C # program for the above approach ; Function that returns true if N is a perfect square ; Function that returns true check if N is sum of three squares ; Factor out the powers of 4 ; N is NOT of the form 4 ^ a * ( 8 b + 7 ) ; Function that finds the minimum number of square whose sum is N ; If N is perfect square ; If N is sum of 2 perfect squares ; If N is sum of 3 perfect squares ; Otherwise , N is the sum of 4 perfect squares ; Driver code ; Given number ; Function call"
Solution in C#:

using System ; class GFG { static bool isPerfectSquare ( int N ) { int floorSqrt = ( int ) Math . Sqrt ( N ) ; return ( N == floorSqrt * floorSqrt ) ; } static bool legendreFunction ( int N ) { while ( N % 4 == 0 ) N /= 4 ; if ( N % 8 != 7 ) return true ; else return false ; } static int minSquares ( int N ) { if ( isPerfectSquare ( N ) ) return 1 ; for ( int i = 1 ; i * i < N ; i ++ ) { if ( isPerfectSquare ( N - i * i ) ) return 2 ; } if ( legendreFunction ( N ) ) return 3 ; return 4 ; } public static void Main ( String [ ] args ) { int N = 123 ; Console . Write ( minSquares ( N ) ) ; } }


"Minimum number of squares whose sum equals to given number N | set 2 | C # program to represent N as the sum of minimum square numbers . ; Function for finding minimum square numbers ; A [ i ] of array arr store minimum count of square number to get i ; sqrNum [ i ] store last square number to get i ; Initialize ; Find minimum count of square number for all value 1 to n ; In worst case it will be arr [ i - 1 ] + 1 we use all combination of a [ i - 1 ] and add 1 ; Check for all square number less or equal to i ; if it gives less count then update it ; List v stores optimum square number whose sum give N ; Driver code ; Calling function ; Printing vector"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static List < int > minSqrNum ( int n ) { int [ ] arr = new int [ n + 1 ] ; int k = 0 ; int [ ] sqrNum = new int [ n + 1 ] ; List < int > v = new List < int > ( ) ; arr [ 0 ] = 0 ; sqrNum [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { arr [ i ] = arr [ i - 1 ] + 1 ; sqrNum [ i ] = 1 ; k = 1 ; while ( k * k <= i ) { if ( arr [ i ] > arr [ i - k * k ] + 1 ) { arr [ i ] = arr [ i - k * k ] + 1 ; sqrNum [ i ] = k * k ; } k ++ ; } } while ( n > 0 ) { v . Add ( sqrNum [ n ] ) ; n -= sqrNum [ n ] ; } return v ; } public static void Main ( String [ ] args ) { int n = 10 ; List < int > v ; v = minSqrNum ( n ) ; for ( int i = 0 ; i < v . Count ; i ++ ) { Console . Write ( v [ i ] ) ; if ( i + 1 != v . Count ) Console . Write ( " ▁ + ▁ " ) ; } } }


"Minimum number of squares whose sum equals to given number n | A dynamic programming based C # program to find minimum number of squares whose sum is equal to a given number ; Returns count of minimum squares that sum to n ; We need to add a check here for n . If user enters 0 or 1 or 2 the below array creation will go OutOfBounds . ; Create a dynamic programming table to store sq ; getMinSquares table for base case entries ; getMinSquares for rest of the table using recursive formula ; max value is i as i can always be represented as 1 * 1 + 1 * 1 + ... ; Go through all smaller numbers to to recursively find minimum ; Store result and free dp [ ] ; Driver Code"
Solution in C#:

using System ; class squares { static int getMinSquares ( int n ) { if ( n <= 3 ) return n ; int [ ] dp = new int [ n + 1 ] ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; dp [ 2 ] = 2 ; dp [ 3 ] = 3 ; for ( int i = 4 ; i <= n ; i ++ ) { dp [ i ] = i ; for ( int x = 1 ; x <= Math . Ceiling ( Math . Sqrt ( i ) ) ; x ++ ) { int temp = x * x ; if ( temp > i ) break ; else dp [ i ] = Math . Min ( dp [ i ] , 1 + dp [ i - temp ] ) ; } } int res = dp [ n ] ; return res ; } public static void Main ( String [ ] args ) { Console . Write ( getMinSquares ( 6 ) ) ; } }


"Minimum number of squares whose sum equals to given number n | A naive recursive C # program to find minimum number of squares whose sum is equal to a given number ; Returns count of minimum squares that sum to n ; base cases ; getMinSquares rest of the table using recursive formula Maximum squares required is n ( 1 * 1 + 1 * 1 + . . ) ; Go through all smaller numbers to recursively find minimum ; Driver Code"
Solution in C#:

using System ; class GFG { static int getMinSquares ( int n ) { if ( n <= 3 ) return n ; int res = n ; for ( int x = 1 ; x <= n ; x ++ ) { int temp = x * x ; if ( temp > n ) break ; else res = Math . Min ( res , 1 + getMinSquares ( n - temp ) ) ; } return res ; } public static void Main ( ) { Console . Write ( getMinSquares ( 6 ) ) ; } }


"Minimum number of squares whose sum equals to given number n | C # program for the above approach ; Function to count minimum squares that sum to n ; Creating visited vector of size n + 1 ; Queue of pair to store node and number of steps ; Initially ans variable is initialized with inf ; Push starting node with 0 0 indicate current number of step to reach n ; Mark starting node visited ; If node reaches its destination 0 update it with answer ; Loop for all possible path from 1 to i * i <= current node ( p . first ) ; If we are standing at some node then next node it can jump to will be current node - ( some square less than or equal n ) ; Check if it is valid and not visited yet ; Mark visited ; Push it it Queue ; Return ans to calling function ; Driver code"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class GFG { public class Point { public int x , y ; public Point ( int x , int y ) { this . x = x ; this . y = y ; } } public static int numSquares ( int n ) { int [ ] visited = new int [ n + 1 ] ; Queue q = new Queue ( ) ; int ans = 1000000000 ; q . Enqueue ( new Point ( n , 0 ) ) ; visited [ n ] = 1 ; while ( q . Count != 0 ) { Point p = ( Point ) q . Dequeue ( ) ; if ( p . x == 0 ) ans = Math . Min ( ans , p . y ) ; for ( int i = 1 ; i * i <= p . x ; i ++ ) { int path = ( p . x - ( i * i ) ) ; if ( path >= 0 && ( visited [ path ] == 0 path == 0 ) ) { visited [ path ] = 1 ; q . Enqueue ( new Point ( path , p . y + 1 ) ) ; } } } return ans ; } public static void Main ( string [ ] args ) { Console . Write ( numSquares ( 12 ) ) ; } }


"Minimum number of steps required to obtain the given Array by the given operations | C # Program to implement the above approach ; Function to calculate the minimum steps to obtain the desired array ; Initialize variable ; Iterate over the array [ ] arr ; Check if i > 0 ; Update the answer ; Return the result ; Driver Code"
Solution in C#:

using System ; class GFG { static int min_operation ( int [ ] a , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i > 0 ) ans += Math . Abs ( a [ i ] - a [ i - 1 ] ) ; else ans += Math . Abs ( a [ i ] ) ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . Length ; Console . Write ( min_operation ( arr , n ) ) ; } }


"Minimum number of steps required to place all 1 s at a single index | C # Program to implement the above approach ; Function to print minimum steps required to shift all 1 s to a single index in a binary array ; Size of array ; Used to store cumulative sum ; Initialize count ; Traverse the array in forward direction ; Steps needed to store all previous ones to ith index ; Count number of 1 s present till i - th index ; Initialize count ; Traverse the array in backward direction ; Steps needed to store all 1 s to the right of i at current index ; Count number of 1 s present after i - th index ; Print the number of steps required ; Driver Code"
Solution in C#:

using System ; class GFG { static void minsteps ( int [ ] A ) { int n = A . Length ; int [ ] left = new int [ n ] ; for ( int i = 1 ; i < n ; i ++ ) { left [ i ] = 0 ; } int [ ] right = new int [ n ] ; for ( int i = 1 ; i < n ; i ++ ) { right [ i ] = 0 ; } int [ ] res = new int [ n ] ; for ( int i = 1 ; i < n ; i ++ ) { res [ i ] = 0 ; } int count = A [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { left [ i ] = left [ i - 1 ] + count ; count += A [ i ] ; } count = A [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) { right [ i ] = right [ i + 1 ] + count ; count += A [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { res [ i ] = left [ i ] + right [ i ] ; Console . Write ( res [ i ] + " ▁ " ) ; } Console . WriteLine ( ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 1 , 0 , 1 , 0 } ; minsteps ( A ) ; } }


"Minimum number of sub | C # implementation of the approach ; Function that returns true if n is a power of 5 ; Function to return the decimal value of binary equivalent ; Function to return the minimum cuts required ; Alongocating memory for dp [ ] array ; From length 1 to n ; If previous character is '0' then ignore to avoid number with leading 0 s . ; Ignore s [ j ] = '0' starting numbers ; Number formed from s [ j ... . i ] ; Check for power of 5 ; Assigning min value to get min cut possible ; ( n + 1 ) to check if all the Strings are traversed and no divisible by 5 is obtained like 000000 ; Driver code"
Solution in C#:

using System ; class GFG { static Boolean ispower ( long n ) { if ( n < 125 ) { return ( n == 1 n == 5 n == 25 ) ; } if ( n % 125 != 0 ) { return false ; } else { return ispower ( n / 125 ) ; } } static long number ( String s , int i , int j ) { long ans = 0 ; for ( int x = i ; x < j ; x ++ ) { ans = ans * 2 + ( s [ x ] - '0' ) ; } return ans ; } static int minCuts ( String s , int n ) { int [ ] dp = new int [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) dp [ i ] = n + 1 ; dp [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( s [ i - 1 ] == '0' ) { continue ; } for ( int j = 0 ; j < i ; j ++ ) { if ( s [ j ] == '0' ) { continue ; } long num = number ( s , j , i ) ; if ( ! ispower ( num ) ) { continue ; } dp [ i ] = Math . Min ( dp [ i ] , dp [ j ] + 1 ) ; } } return ( ( dp [ n ] < n + 1 ) ? dp [ n ] : - 1 ) ; } public static void Main ( String [ ] args ) { String s = "101101101" ; int n = s . Length ; Console . WriteLine ( minCuts ( s , n ) ) ; } }


"Minimum number of subsequences required to convert one string to another using Greedy Algorithm | C # implementation for minimum number of subsequences required to convert one String to another ; Function to find the minimum number of subsequences required to convert one String to another S2 == A and S1 == B ; At least 1 subsequence is required Even in best case , when A is same as B ; size of B ; size of A ; Create an 2D array next [ , ] of size 26 * sizeOfB to store the next occurrence of a character ( ' a ' to ' z ' ) as an index [ 0 , sizeOfA - 1 ] ; Array Initialization with infinite ; Loop to Store the values of index ; If the value of next [ i , j ] is infinite then update it with next [ i , j + 1 ] ; Greedy algorithm to obtain the maximum possible subsequence of B to cover the remaining String of A using next subsequence ; Loop to iterate over the String A ; Condition to check if the character is not present in the String B ; Condition to check if there is an element in B matching with character A [ i ] on or next to B [ pos ] given by next [ A [ i ] - ' a ' , pos ] ; Condition to check if reached at the end of B or no such element exists on or next to A [ pos ] , thus increment number by one and reinitialise pos to zero ; Driver Code"
Solution in C#:

using System ; class GFG { static int findMinimumSubsequences ( String A , String B ) { int numberOfSubsequences = 1 ; int sizeOfB = B . Length ; int sizeOfA = A . Length ; int inf = 1000000 ; int [ , ] next = new int [ 26 , sizeOfB ] ; for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = 0 ; j < sizeOfB ; j ++ ) { next [ i , j ] = inf ; } } for ( int i = 0 ; i < sizeOfB ; i ++ ) { next [ B [ i ] - ' a ' , i ] = i ; } for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = sizeOfB - 2 ; j >= 0 ; j -- ) { if ( next [ i , j ] == inf ) { next [ i , j ] = next [ i , j + 1 ] ; } } } int pos = 0 ; int I = 0 ; while ( I < sizeOfA ) { if ( pos == 0 && next [ A [ I ] - ' a ' , pos ] == inf ) { numberOfSubsequences = - 1 ; break ; } else if ( pos < sizeOfB && next [ A [ I ] - ' a ' , pos ] < inf ) { int nextIndex = next [ A [ I ] - ' a ' , pos ] + 1 ; pos = nextIndex ; I ++ ; } else { numberOfSubsequences ++ ; pos = 0 ; } } return numberOfSubsequences ; } public static void Main ( String [ ] args ) { String A = " aacbe " ; String B = " aceab " ; Console . Write ( findMinimumSubsequences ( A , B ) ) ; } }


"Minimum number of subtract operation to make an array decreasing | C # program to make an array decreasing ; Function to count minimum no of operation ; Count how many times we have to subtract . ; Check an additional subtraction is required or not . ; Modify the value of arr [ i ] ; Count total no of subtraction ; driver function"
Solution in C#:

using System ; public class GfG { public static int min_noOf_operation ( int [ ] arr , int n , int k ) { int noOfSubtraction ; int res = 0 ; for ( int i = 1 ; i < n ; i ++ ) { noOfSubtraction = 0 ; if ( arr [ i ] > arr [ i - 1 ] ) { noOfSubtraction = ( arr [ i ] - arr [ i - 1 ] ) / k ; if ( ( arr [ i ] - arr [ i - 1 ] ) % k != 0 ) noOfSubtraction ++ ; arr [ i ] = arr [ i ] - k * noOfSubtraction ; } res = res + noOfSubtraction ; } return res ; } public static void Main ( ) { int [ ] arr = { 1 , 1 , 2 , 3 } ; int N = 4 ; int k = 5 ; Console . WriteLine ( min_noOf_operation ( arr , N , k ) ) ; } }


"Minimum number of sum and modulo operations using given numbers to reach target | C # implementation of the above approach ; Function to find the minimum moves to reach K from N ; Initialization of dp vector ; dp [ i ] = minimum pushes required to reach i ; Traversing through the buttons ; Iterating through all the positions ; If not visited ; Next status of lock ; Advance to next state ; Return the readonly dp [ target ] ; Driver function ; Given Input ; Function Call"
Solution in C#:

using System ; public class GFG { static int minPushes ( int N , int K , int [ ] arr ) { int [ ] dp = new int [ 100000 ] ; for ( int i = 0 ; i < dp . Length ; i ++ ) dp [ i ] = - 1 ; dp [ N ] = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { for ( int xx = 0 ; xx < 100000 ; xx ++ ) { int x = xx ; if ( dp [ x ] == - 1 ) continue ; int next = ( x + arr [ i ] ) % 100000 ; while ( dp [ next ] == - 1 dp [ next ] > dp [ x ] + 1 ) { dp [ next ] = dp [ x ] + 1 ; x = next ; next = ( next + arr [ i ] ) % 100000 ; } } } return dp [ K ] ; } public static void Main ( String [ ] args ) { int N = 99880 , K = 89 ; int [ ] arr = { 100 , 3 } ; Console . Write ( minPushes ( N , K , arr ) ) ; } }