"Minimum insertions to form a palindrome | DP | A Naive recursive C # program to find minimum number insertions needed to make a string palindrom ; Recursive function to find minimum number of insertions ; Base Cases ; Check if the first and last characters are same . On the basis of the comparison result , decide which subrpoblem ( s ) to call ; Driver Code"
Solution in C#:

using System ; class GFG { static int findMinInsertions ( char [ ] str , int l , int h ) { if ( l > h ) return int . MaxValue ; if ( l == h ) return 0 ; if ( l == h - 1 ) return ( str [ l ] == str [ h ] ) ? 0 : 1 ; return ( str [ l ] == str [ h ] ) ? findMinInsertions ( str , l + 1 , h - 1 ) : ( Math . Min ( findMinInsertions ( str , l , h - 1 ) , findMinInsertions ( str , l + 1 , h ) ) + 1 ) ; } public static void Main ( ) { string str = " geeks " ; Console . WriteLine ( findMinInsertions ( str . ToCharArray ( ) , 0 , str . Length - 1 ) ) ; } }


"Minimum insertions to form shortest palindrome | C # program to find minimum number of insertions on left side to form a palindrome . ; Returns true if a string str [ st . . end ] is palindrome ; Returns count of insertions on left side to make str [ ] a palindrome ; Find the largest prefix of given string that is palindrome . ; Characters after the palindromic prefix must be added at the beginning also to make the complete string palindrome ; Driver Code"
Solution in C#:

using System ; using System . Text ; class GFG { static bool isPalin ( char [ ] str , int st , int end ) { while ( st < end ) { if ( str [ st ] != str [ end ] ) return false ; st ++ ; end -- ; } return true ; } static int findMinInsert ( char [ ] str , int n ) { for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( isPalin ( str , 0 , i ) ) return ( n - i - 1 ) ; } return 0 ; } public static void Main ( string [ ] args ) { char [ ] Input = " JAVA " . ToCharArray ( ) ; Console . Write ( findMinInsert ( Input , Input . Length ) ) ; } }


"Minimum insertions to make XOR of an Array equal to half of its sum | C # program to make XOR of all array elements equal to half of its sum by minimum insertions ; Function to make XOR of the array equal to half of its sum ; Calculate the sum and Xor of all the elements ; If the required condition satisfies already , return the original array ; If Xor is already zero , Insert sum ; Otherwise , insert xr and insert sum + xr ; Driver code"
Solution in C#:

 using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int make_xor_half ( ArrayList arr ) { int sum = 0 , xr = 0 ; foreach ( int a in arr ) { sum += a ; xr ^= a ; } if ( 2 * xr == sum ) return - 1 ; if ( xr == 0 ) { arr . Add ( sum ) ; return 1 ; } arr . Add ( xr ) ; arr . Add ( sum + xr ) ; return 2 ; } public static void Main ( string [ ] args ) { int N = 7 ; ArrayList nums = new ArrayList ( ) { 3 , 4 , 7 , 1 , 2 , 5 , 6 } ; int count = make_xor_half ( nums ) ; if ( count == - 1 ) Console . Write ( - 1 + "
 " ) ; else if ( count == 1 ) Console . Write ( nums [ N ] + "
 " ) ; else Console . Write ( nums [ N ] + " ▁ " + nums [ N + 1 ] + "
 " ) ; } }


"Minimum insertions to sort an array | C # program to get minimum number of insertion steps to sort an array ; method returns min steps of insertion we need to perform to sort array ' arr ' ; lis [ i ] is going to store length of lis that ends with i . ; Initialize lis values for all indexes ; Compute optimized lis values in bottom up manner ; The overall LIS must end with of the array elements . Pick maximum of all lis values ; return size of array minus length of LIS as final result ; Driver code to test above methods"
Solution in C#:

using System ; class GfG { static int minInsertionStepToSortArray ( int [ ] arr , int N ) { 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 ; int max = 0 ; for ( int i = 0 ; i < N ; i ++ ) if ( max < lis [ i ] ) max = lis [ i ] ; return ( N - max ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 5 , 1 , 4 , 7 , 6 } ; int N = arr . Length ; Console . Write ( minInsertionStepToSortArray ( arr , N ) ) ; } }


"Minimum integer such that it leaves a remainder 1 on dividing with any element from the range [ 2 , N ] | C # implementation of the approach ; Function to return the smallest number which on dividing with any element from the range [ 2 , N ] leaves a remainder of 1 ; Find the LCM of the elements from the range [ 2 , N ] ; Return the required number ; Driver code"
Solution in C#:

using System ; class GFG { static long getMinNum ( int N ) { int lcm = 1 ; for ( int i = 2 ; i <= N ; i ++ ) lcm = ( ( i * lcm ) / ( __gcd ( i , lcm ) ) ) ; return ( lcm + 1 ) ; } static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } public static void Main ( ) { int N = 5 ; Console . WriteLine ( getMinNum ( N ) ) ; } }


"Minimum integer that can be obtained by swapping adjacent digits of different parity | C # implementation of the above approach . ; Function to return the minimum number ; Store the elements which are divisible by two in stack1 ; Store the elements which are not divisible by two in stack2 . ; Concatenate the answer with smaller value of the topmost elements of both the stacks and then pop that element ; Concatenate the answer with remaining values of stack1 . ; Concatenate the answer with remaining values of stack2 . ; Driver code ; Function calling"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static int minimumNo ( int n ) { int ans = 0 ; Stack < int > stack1 = new Stack < int > ( ) ; Stack < int > stack2 = new Stack < int > ( ) ; while ( n != 0 ) { int r = n % 10 ; if ( r % 2 == 0 ) { stack1 . Push ( r ) ; } else { stack2 . Push ( r ) ; } n = n / 10 ; } while ( stack1 . Count != 0 && stack2 . Count != 0 ) { if ( stack1 . Peek ( ) < stack2 . Peek ( ) ) { ans = ans * 10 + stack1 . Peek ( ) ; stack1 . Pop ( ) ; } else { ans = ans * 10 + stack2 . Peek ( ) ; stack2 . Pop ( ) ; } } while ( stack1 . Count != 0 ) { ans = ans * 10 + stack1 . Peek ( ) ; stack1 . Pop ( ) ; } while ( stack2 . Count != 0 ) { ans = ans * 10 + stack2 . Peek ( ) ; stack2 . Pop ( ) ; } return ans ; } public static void Main ( String [ ] args ) { int n1 = 64432 ; Console . Write ( minimumNo ( n1 ) + "
 " ) ; int n2 = 3137 ; Console . Write ( minimumNo ( n2 ) + "
 " ) ; } }


"Minimum integer with at most K bits set such that their bitwise AND with N is maximum | C # program for the above approach ; Function to find the integer with maximum bitwise with N ; Store answer in the bitset Initialized with 0 ; To maintain the count of set bits that should exceed k ; Start traversing from the Most significant if that bit is set in n then we will set in our answer i . e in X ; Checking if the ith bit is set in n or not ; Converting into integer ; Driver code ; Function Call"
Solution in C#:

using System ; class GFG { static int find_max ( int n , int k ) { int [ ] X = new int [ 32 ] ; int cnt = 0 ; for ( int i = 31 ; i >= 0 && cnt != k ; i -- ) { if ( ( n & ( 1 << i ) ) != 0 ) { X [ i ] = 1 ; cnt ++ ; } } String s = " " ; for ( int i = 31 ; i >= 0 ; i -- ) s += X [ i ] == 0 ? '0' : '1' ; return Convert . ToInt32 ( s , 2 ) ; } public static void Main ( String [ ] args ) { int n = 10 , k = 2 ; Console . Write ( find_max ( n , k ) ) ; } }


"Minimum inversions required so that no two adjacent elements are same | C # implementation of the approach ; Function to return the minimum inversions required so that no two adjacent elements are same ; To store the inversions required to make the array { 1 , 0 , 1 , 0 , 1 , 0 , 1 , ... } and { 0 , 1 , 0 , 1 , 0 , 1 , 0 , ... } respectively ; Find all the changes required ; Return the required answer ; Driver code"
Solution in C#:

using System ; class GFG { static int min_changes ( int [ ] a , int n ) { int ans_a = 0 , ans_b = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { if ( a [ i ] == 0 ) ans_a ++ ; else ans_b ++ ; } else { if ( a [ i ] == 0 ) ans_b ++ ; else ans_a ++ ; } } return Math . Min ( ans_a , ans_b ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 0 , 0 , 1 , 0 , 0 , 1 , 0 } ; int n = a . Length ; Console . WriteLine ( min_changes ( a , n ) ) ; } }


"Minimum jumps required to group all 1 s together in a given Binary string | C # program to find the minimum number of jumps required to group all ones together in the binary string ; Function to get the minimum jump value ; Store all indices of ones ; Populating one 's indices ; Calculate median ; Jumps required for 1 's  to the left of median ; Jumps required for 1 's  to the right of median ; Return the final answer ; Driver code"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class GFG { public static int getMinJumps ( string s ) { ArrayList ones = new ArrayList ( ) ; int jumps = 0 , median = 0 , ind = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == '1' ) ones . Add ( i ) ; } if ( ones . Count == 0 ) return jumps ; median = ( int ) ones [ ones . Count / 2 ] ; ind = median ; for ( int i = ind ; i >= 0 ; i -- ) { if ( s [ i ] == '1' ) { jumps += ind - i ; ind -- ; } } ind = median ; for ( int i = ind ; i < s . Length ; i ++ ) { if ( s [ i ] == '1' ) { jumps += i - ind ; ind ++ ; } } return jumps ; } public static void Main ( string [ ] args ) { string S = "00100000010011" ; Console . Write ( getMinJumps ( S ) ) ; } }


"Minimum jumps required to make a group of persons sit together | C # program for the above approach ; Function to find the minimum jumps required to make the whole group sit adjacently ; Store the indexes ; Stores the count of occupants ; Length of the String ; Traverse the seats ; If current place is occupied ; Push the current position in the vector ; Base Case : ; The index of the median element ; The value of the median element ; Traverse the position [ ] ; Update the ans ; Return the readonly count ; Driver Code ; Given arrange of seats ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int MOD = ( int ) 1e9 + 7 ; static int minJumps ( String seats ) { List < int > position = new List < int > ( ) ; int count = 0 ; int len = seats . Length ; for ( int i = 0 ; i < len ; i ++ ) { if ( seats [ i ] == ' x ' ) { position . Add ( i - count ) ; count ++ ; } } if ( count == len count == 0 ) return 0 ; int med_index = ( count - 1 ) / 2 ; int med_val = position [ med_index ] ; int ans = 0 ; for ( int i = 0 ; i < position . Count ; i ++ ) { ans = ( ans % MOD + Math . Abs ( position [ i ] - med_val ) % MOD ) % MOD ; } return ans % MOD ; } public static void Main ( String [ ] args ) { String S = " . . . . x . . xx . . . x . . " ; Console . Write ( minJumps ( S ) ) ; } }


"Minimum jumps to reach last building in a matrix | Recursive C # program to find minimum jumps to reach last building from first . ; Returns minimum jump path from ( 0 , 0 ) to ( m , n ) in height [ R ] [ C ] ; base case ; Find minimum jumps if we go through diagonal ; Find minimum jumps if we go through down ; Find minimum jumps if we go through right ; return minimum jumps ; Driver code"
Solution in C#:

using System ; class GFG { static bool isSafe ( int x , int y ) { return ( x < 4 && y < 3 ) ; } static int minJump ( int [ , ] height , int x , int y ) { if ( x == 4 - 1 && y == 3 - 1 ) return 0 ; int diag = int . MaxValue ; if ( isSafe ( x + 1 , y + 1 ) ) diag = minJump ( height , x + 1 , y + 1 ) + Math . Abs ( height [ x , y ] - height [ x + 1 , y + 1 ] ) ; int down = int . MaxValue ; if ( isSafe ( x + 1 , y ) ) down = minJump ( height , x + 1 , y ) + Math . Abs ( height [ x , y ] - height [ x + 1 , y ] ) ; int right = int . MaxValue ; if ( isSafe ( x , y + 1 ) ) right = minJump ( height , x , y + 1 ) + Math . Abs ( height [ x , y ] - height [ x , y + 1 ] ) ; return Math . Min ( down , Math . Min ( right , diag ) ) ; } public static void Main ( ) { int [ , ] height = { { 5 , 4 , 2 } , { 9 , 2 , 1 } , { 2 , 5 , 9 } , { 1 , 3 , 11 } } ; Console . Write ( minJump ( height , 0 , 0 ) ) ; } }


"Minimum jumps to traverse all integers in range [ 1 , N ] such that integer i can jump i steps | C # program for the above approach ; Utility function to find minimum steps ; Initialize count and result ; Traverse over the range [ 1 , N ] ; Update res ; Increment count ; Return res ; Driver Code ; Input ; Function call"
Solution in C#:

using System ; class GFG { static int minSteps ( int N ) { int count = 1 , res = 0 ; for ( int i = 1 ; i <= N ; i += count ) { res = Math . Max ( res , count ) ; count ++ ; } return res ; } public static void Main ( ) { int N = 6 ; Console . Write ( minSteps ( N ) ) ; } }


"Minimum jumps to traverse all integers in range [ 1 , N ] such that integer i can jump i steps | Java implementation of the above approach ; Utility function to find minimum steps ; Driver code ; Input integer ; Function call"
Solution in C#:

 using System ; class GFG { static int minSteps ( int N ) { int res = ( ( int ) Math . Sqrt ( 1 + 8 * N ) - 1 ) / 2 ; return res ; } public static void Main ( String [ ] args ) { int N = 6 ; Console . Write ( minSteps ( N ) + "
 " ) ; } }


"Minimum labelled node to be removed from undirected Graph such that there is no cycle | C # implementation to find the minimum labelled node to be removed such that there is no cycle in the undirected graph ; Variables to store if a node V has at - most one back edge and store the depth of the node for the edge ; Function to swap the pairs of the graph ; If the second value is greater than x ; Put the pair in the ascending order internally ; Function to perform the DFS ; Initialise with the large value ; Storing the depth of this vertex ; Mark the vertex as visited ; Iterating through the graph ; If the node is a child node ; If the child node is unvisited ; Move to the child and increase the depth ; increase according to algorithm ; If the node is not having exactly K backedges ; If the child is already visited and in current dfs ( because colour is 1 ) then this is a back edge ; Increase the countAdj values ; Colour this vertex 2 as we are exiting out of dfs for this node ; Function to find the minimum labelled node to be removed such that there is no cycle in the undirected graph ; Construct the graph ; Mark visited as false for each node ; Apply dfs on all unmarked nodes ; If no backedges in the initial graph this means that there is no cycle So , return - 1 ; Iterate through the vertices and return the first node that satisfies the condition ; Check whether the count sum of small [ v ] and count is the same as the total back edges and if the vertex v can be removed ; Driver code"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int MAX = 100005 ; static int totBackEdges ; static int [ ] countAdj = new int [ MAX ] ; static int [ ] small = new int [ MAX ] ; static int [ ] isPossible = new int [ MAX ] ; static int [ ] depth = new int [ MAX ] ; static ArrayList adj = new ArrayList ( ) ; static int [ ] vis = new int [ MAX ] ; class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void change ( ref pair p , int x ) { if ( p . second > x ) p . second = x ; if ( p . first > p . second ) { int tmp = p . first ; p . first = p . second ; p . second = tmp ; } } static pair dfs ( int v , int p = - 1 , int de = 0 ) { pair answer = new pair ( 100000000 , 100000000 ) ; depth [ v ] = de ; vis [ v ] = 1 ; isPossible [ v ] = 1 ; foreach ( int u in ( ArrayList ) adj [ v ] ) { if ( ( u ^ p ) != 0 ) { if ( vis [ u ] == 0 ) { pair x = dfs ( u , v , de + 1 ) ; small [ v ] += small [ u ] ; change ( ref answer , x . second ) ; change ( ref answer , x . first ) ; if ( x . second < de ) isPossible [ v ] = 0 ; } else if ( vis [ u ] == 1 ) { totBackEdges ++ ; countAdj [ v ] ++ ; countAdj [ u ] ++ ; small [ p ] ++ ; small [ u ] -- ; change ( ref answer , depth [ u ] ) ; } } } vis [ v ] = 2 ; return answer ; } static int minNodetoRemove ( int n , ArrayList edges ) { for ( int i = 0 ; i < edges . Count ; i ++ ) { ( ( ArrayList ) adj [ ( ( pair ) edges [ i ] ) . first ] ) . Add ( ( ( pair ) edges [ i ] ) . second ) ; ( ( ArrayList ) adj [ ( ( pair ) edges [ i ] ) . second ] ) . Add ( ( ( pair ) edges [ i ] ) . first ) ; } Array . Fill ( vis , 0 ) ; totBackEdges = 0 ; for ( int v = 1 ; v <= n ; v ++ ) { if ( vis [ v ] == 0 ) dfs ( v ) ; } if ( totBackEdges == 0 ) return - 1 ; int node = - 1 ; for ( int v = 1 ; v <= n ; v ++ ) { if ( ( countAdj [ v ] + small [ v ] == totBackEdges ) && isPossible [ v ] != 0 ) { node = v ; } if ( node != - 1 ) break ; } return node ; } static void Main...


"Minimum length String with Sum of the alphabetical values of the characters equal to N | C # iprogram to find the Minimum length String with Sum of the alphabetical values of the characters equal to N ; Function to find the minimum length ; Function to find the minimum length String ; Driver code"
Solution in C#:

using System ; class GFG { static int minLength ( int n ) { int ans = n / 26 ; if ( n % 26 != 0 ) ans ++ ; return ans ; } static String minString ( int n ) { int ans = n / 26 ; String res = " " ; while ( ans -- != 0 ) { res = res + " z " ; } if ( n % 26 != 0 ) { res = res + ( char ) ( ( n % 26 ) + 96 ) ; } return res ; } public static void Main ( String [ ] args ) { int n = 50 ; Console . WriteLine ( minLength ( n ) ) ; Console . WriteLine ( minString ( n ) ) ; } }


"Minimum length of Run Length Encoding possible by removing at most K characters from a given string | C # program to implement the above approach ; Function which solves the desired problem ; Base Case ; If the entire string has been traversed ; If precomputed subproblem occurred ; Minimum run length encoding by removing the current character ; Minimum run length encoding by retaining the current character ; If the current and the previous characters match ; Otherwise ; Function to return minimum run - length encoding for string s by removing atmost k characters ; Driver Code"
Solution in C#:

using System ; class GFG { static int maxN = 20 ; static int [ , , , ] dp = new int [ maxN , maxN , 27 , maxN ] ; public static int solve ( String s , int n , int idx , int k , char last , int count ) { if ( k < 0 ) return n + 1 ; if ( idx == n ) return 0 ; int ans = dp [ idx , k , last - ' a ' , count ] ; if ( ans != - 1 ) return ans ; ans = n + 1 ; ans = Math . Min ( ans , solve ( s , n , idx + 1 , k - 1 , last , count ) ) ; int inc = 0 ; if ( count == 1 count == 9 count == 99 ) inc = 1 ; if ( last == s [ idx ] ) { ans = Math . Min ( ans , inc + solve ( s , n , idx + 1 , k , s [ idx ] , count + 1 ) ) ; } else { ans = Math . Min ( ans , 1 + solve ( s , n , idx + 1 , k , s [ idx ] , 1 ) ) ; } return dp [ idx , k , last - ' a ' , count ] = ans ; } public static int MinRunLengthEncoding ( String s , int n , int k ) { for ( int i = 0 ; i < maxN ; i ++ ) for ( int j = 0 ; j < maxN ; j ++ ) for ( int p = 0 ; p < 27 ; p ++ ) for ( int l = 0 ; l < maxN ; l ++ ) dp [ i , j , p , l ] = - 1 ; return solve ( s , n , 0 , k , ( char ) 123 , 0 ) ; } public static void Main ( String [ ] args ) { String S = " abbbcdcdd " ; int N = 9 , K = 2 ; Console . WriteLine ( MinRunLengthEncoding ( S , N , K ) ) ; } }


"Minimum length of a rod that can be split into N equal parts that can further be split into given number of equal parts | C # program for the above approach ; Function to find GCD of two numbers a and b ; Base Case ; Find GCD recursively ; Function to find the LCM of the resultant array ; Initialize a variable ans as the first element ; Traverse the array ; Update LCM ; Return the minimum length of the rod ; Function to find the minimum length of the rod that can be divided into N equals parts and each part can be further divided into arr [ i ] equal parts ; Print the result ; Driver code"
Solution in C#:

using System ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int findlcm ( int [ ] arr , int n ) { int ans = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { ans = ( ( ( arr [ i ] * ans ) ) / ( gcd ( arr [ i ] , ans ) ) ) ; } return ans ; } static void minimumRod ( int [ ] A , int N ) { Console . WriteLine ( N * findlcm ( A , N ) ) ; } static void Main ( ) { int [ ] arr = { 1 , 2 } ; int N = arr . Length ; minimumRod ( arr , N ) ; } }


"Minimum length of square to contain at least half of the given Coordinates | C # program to find next identical year ; Function to Calculate Absolute Value ; Function to Calculate the Minimum value of M ; To store the minimum M for each point in array ; Sort the array ; Index at which atleast required point are inside square of length 2 * M ; Driver Code"
Solution in C#:

using System ; class GFG { static int mod ( int x ) { if ( x >= 0 ) return x ; return - x ; } static void findSquare ( int n ) { int [ , ] points = new int [ 4 , 2 ] { { 1 , 2 } , { - 3 , 4 } , { 1 , 78 } , { - 3 , - 7 } } ; int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int x , y ; x = points [ i , 0 ] ; y = points [ i , 1 ] ; a [ i ] = Math . Max ( mod ( x ) , mod ( y ) ) ; } Array . Sort ( a ) ; int index = ( int ) ( n / 2 - 1 ) ; Console . WriteLine ( " Minimum ▁ M ▁ required ▁ is : ▁ " + a [ index ] ) ; } public static void Main ( String [ ] args ) { int N ; N = 4 ; findSquare ( N ) ; } }


"Minimum length of subsequence having unit GCD | C # program for the above approach ; Function that finds the prime factors of a number ; To store the prime factor ; 2 s that divide n ; N must be odd at this point Skip one element ; Update the prime factor ; If n is a prime number greater than 2 ; Function that finds the shortest subsequence ; Check if the prime factor of first number , is also the prime factor of the rest numbers in array ; Set corresponding bit of prime factor to 1 , it means both these numbers have the same prime factor ; If no states encountered so far continue for this combination of bits ; Update this state with minimum ways to reach this state ; Function that print the minimum length of subsequence ; Find the prime factors of the first number ; Initialize the array with maximum steps , size of the array + 1 for instance ; Total number of set bits is equal to the total number of prime factors ; Indicates there is one way to reach the number under consideration ; State 0 corresponds to gcd of 1 ; If not found such subsequence then print " - 1" ; Else print the length ; Driver Code ; Given array [ ] arr ; Function Call"
Solution in C#:

using System ; class GFG { private static int [ ] findPrimeFactors ( int n ) { int [ ] primeFactors = new int [ 9 ] ; int j = 0 ; if ( n % 2 == 0 ) { primeFactors [ j ++ ] = 2 ; while ( n % 2 == 0 ) n >>= 1 ; } for ( int i = 3 ; i * i <= n ; i += 2 ) { if ( n % i == 0 ) { primeFactors [ j ++ ] = i ; while ( n % i == 0 ) n /= i ; } } if ( n > 2 ) primeFactors [ j ++ ] = n ; int [ ] temp = new int [ j ] ; Array . Copy ( primeFactors , temp , j ) ; return temp ; } private static void findShortestSubsequence ( int [ ] dp , int [ ] a , int index , int [ ] primeFactors ) { int n = a . Length ; for ( int j = index ; j < n ; j ++ ) { int bitmask = 0 ; for ( int p = 0 ; p < primeFactors . Length ; p ++ ) { if ( a [ j ] % primeFactors [ p ] == 0 ) { bitmask ^= ( 1 << p ) ; } } for ( int i = 0 ; i < dp . Length ; i ++ ) { if ( dp [ i ] == n + 1 ) continue ; dp [ bitmask & i ] = Math . Min ( dp [ bitmask & i ] , dp [ i ] + 1 ) ; } } } private static void printMinimumLength ( int [ ] a ) { int min = a . Length + 1 ; for ( int i = 0 ; i < a . Length - 1 ; i ++ ) { int [ ] primeFactors = findPrimeFactors ( a [ i ] ) ; int n = primeFactors . Length ; int [ ] dp = new int [ 1 << n ] ; for ( i = 0 ; i < dp . Length ; i ++ ) dp [ i ] = a . Length + 1 ; int setBits = ( 1 << n ) - 1 ; dp [ setBits ] = 1 ; findShortestSubsequence ( dp , a , i + 1 , primeFactors ) ; min = Math . Min ( dp [ 0 ] , min ) ; } if ( min == a . Length + 1 ) Console . WriteLine ( - 1 ) ; else Console . WriteLine ( min ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 6 , 12 , 3 } ; printMinimumLength ( arr ) ; } }


"Minimum length of substring whose rotation generates a palindromic substring | C # Program to find the minimum length of substring whose rotation generates a palindromic substring ; Function to return the minimum length of substring ; Store the index of previous occurrence of the character ; Variable to store the maximum length of substring ; If the current character hasn 't appeared yet ; If the character has occured within one or two previous index , a palindromic substring already exists ; Update the maximum ; Replace the previous index of the character by the current index ; If character appeared at least twice ; Driver code"
Solution in C#:

using System ; class GFG { static int count_min_length ( string s ) { int [ ] hash = new int [ 26 ] ; int ans = int . MaxValue ; for ( int i = 0 ; i < 26 ; i ++ ) hash [ i ] = - 1 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( hash [ s [ i ] - ' a ' ] == - 1 ) hash [ s [ i ] - ' a ' ] = i ; else { if ( hash [ s [ i ] - ' a ' ] == i - 1 hash [ s [ i ] - ' a ' ] == i - 2 ) return 0 ; ans = Math . Min ( ans , i - hash [ s [ i ] - ' a ' ] - 1 ) ; hash [ s [ i ] - ' a ' ] = i ; } } if ( ans == int . MaxValue ) return - 1 ; return ans ; } public static void Main ( string [ ] args ) { string str = " abcdeba " ; Console . WriteLine ( count_min_length ( str ) ) ; } }


"Minimum length of the reduced Array formed using given operations | C # implementation to find the minimum length of the array ; Function to find the length of minimized array ; Creating the required dp tables ; Initialising the dp table by - 1 ; base case ; Check if the two subarray can be combined ; Initialising dp1 table with max value ; Check if the subarray can be reduced to a single element ; Minimal partition of [ 1 : j - 1 ] + 1 ; Driver code"
Solution in C#:

using System ; class GFG { static int minimalLength ( int [ ] a , int n ) { int [ , ] dp = new int [ n + 1 , n + 1 ] ; int [ ] dp1 = new int [ n ] ; int i , j , k ; for ( i = 0 ; i < n + 1 ; i ++ ) { for ( j = 0 ; j < n + 1 ; j ++ ) { dp [ i , j ] = - 1 ; } } for ( int size = 1 ; size <= n ; size ++ ) { for ( i = 0 ; i < n - size + 1 ; i ++ ) { j = i + size - 1 ; if ( i == j ) dp [ i , j ] = a [ i ] ; else { for ( k = i ; k < j ; k ++ ) { if ( dp [ i , k ] != - 1 && dp [ i , k ] == dp [ k + 1 , j ] ) dp [ i , j ] = dp [ i , k ] + 1 ; } } } } for ( i = 0 ; i < n ; i ++ ) dp1 [ i ] = ( int ) 1e7 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j <= i ; j ++ ) { if ( dp [ j , i ] != - 1 ) { if ( j == 0 ) dp1 [ i ] = 1 ; else dp1 [ i ] = Math . Min ( dp1 [ i ] , dp1 [ j - 1 ] + 1 ) ; } } } return dp1 [ n - 1 ] ; } public static void Main ( string [ ] args ) { int n = 7 ; int [ ] a = { 3 , 3 , 4 , 4 , 4 , 3 , 3 } ; Console . Write ( minimalLength ( a , n ) ) ; } }


"Minimum length of the subarray required to be replaced to make frequency of array elements equal to N / M | C # program for the above approach ; Function to find the minimum length of the subarray to be changed . ; Stores the frequencies of array elements ; Stores the number of array elements that are present more than N / M times ; Iterate over the range ; Increment the frequency ; If the frequency of all array elements are already N / M ; Stores the resultant length of the subarray ; The left and right pointers ; Iterate over the range ; If the current element is ; If the value of c is 0 , then find the possible answer ; Iterate over the range ; If the element at left is making it extra ; Update the left pointer ; Update the right pointer ; Return the resultant length ; Driver Code"
Solution in C#:

using System ; class GFG { public static int minimumSubarray ( int [ ] arr , int n , int m ) { int [ ] mapu = new int [ m + 1 ] ; Array . Fill ( mapu , 0 ) ; int c = 0 ; for ( int i = 0 ; i < n ; i ++ ) { mapu [ arr [ i ] ] ++ ; if ( mapu [ arr [ i ] ] == ( n / m ) + 1 ) c ++ ; } if ( c == 0 ) return 0 ; int ans = n ; int l = 0 , r = 0 ; while ( r < n ) { if ( -- mapu [ arr [ r ] ] == ( n / m ) ) c -- ; if ( c == 0 ) { while ( l <= r && c == 0 ) { ans = Math . Min ( ans , r - l + 1 ) ; if ( ++ mapu [ arr [ l ] ] > ( n / m ) ) c ++ ; l ++ ; } } r ++ ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 2 , 1 , 1 , 2 } ; int M = 2 ; int N = arr . Length ; Console . Write ( minimumSubarray ( arr , N , M ) ) ; } }


"Minimum length subarray containing all unique elements after Q operations | C # implementation to find the minimum size subarray containing all unique elements after processing the array for K queries of ranges ; Function to find minimum size subarray of all array elements ; Updating the array after processing each query ; Making it to 0 - indexing ; Prefix sum array concept is used to obtain the count array ; Iterating over the array to get the readonly array ; Variable to get count of all unique elements ; Hash to maintain perviously occurred elements ; Loop to find the all unique elements ; Array to maintain counter of encountered elements ; Variable to store answer ; Using two pointers approach ; Increment counter if occurred once ; When all unique elements are found ; Update answer with minimum size ; Decrement count of elements from left ; Decrement counter ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int subarrayLength ( int [ ] A , int [ , ] R , int N , int M ) { for ( int i = 0 ; i < M ; ++ i ) { int l = R [ i , 0 ] , r = R [ i , 1 ] + 1 ; l -- ; r -- ; A [ l ] ++ ; if ( r < N ) A [ r ] -- ; } for ( int i = 1 ; i < N ; ++ i ) { A [ i ] += A [ i - 1 ] ; } int count = 0 ; HashSet < int > s = new HashSet < int > ( ) ; for ( int i = 0 ; i < N ; ++ i ) { if ( ! s . Contains ( A [ i ] ) ) count ++ ; s . Add ( A [ i ] ) ; } int [ ] repeat = new int [ count + 1 ] ; int ans = N ; int counter = 0 , left = 0 , right = 0 ; while ( right < N ) { int cur_element = A [ right ] ; repeat [ cur_element ] += 1 ; if ( repeat [ cur_element ] == 1 ) ++ counter ; while ( counter == count ) { ans = Math . Min ( ans , right - left + 1 ) ; cur_element = A [ left ] ; repeat [ cur_element ] -= 1 ; ++ left ; if ( repeat [ cur_element ] == 0 ) -- counter ; } ++ right ; } return ans ; } public static void Main ( String [ ] args ) { int N = 8 , queries = 6 ; int [ , ] Q = { { 1 , 4 } , { 3 , 4 } , { 4 , 5 } , { 5 , 5 } , { 7 , 8 } , { 8 , 8 } } ; int [ ] A = new int [ N ] ; Console . Write ( subarrayLength ( A , Q , N , queries ) ) ; } }


"Minimum length substring with exactly K distinct characters | C # program to find minimum length subString having exactly k distinct character . ; Function to find minimum length subString having exactly k distinct character . ; Starting index of sliding window . ; Ending index of sliding window . ; To store count of character . ; To store count of distinct character in current sliding window . ; To store length of current sliding window . ; To store minimum length . ; To store starting index of minimum length subString . ; Increment count of current character If this count is one then a new distinct character is found in sliding window . ; If number of distinct characters is is greater than k , then move starting point of sliding window forward , until count is k . ; Remove characters from the beginning of sliding window having count more than 1 to minimize length . ; Compare length with minimum length and update if required . ; Return minimum length subString . ; Driver code"
Solution in C#:

using System ; class GFG { static String findMinLenStr ( string str , int k ) { int n = str . Length ; int st = 0 ; int end = 0 ; int [ ] cnt = new int [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) cnt [ i ] = 0 ; int distEle = 0 ; int currlen ; int minlen = n ; int startInd = - 1 ; while ( end < n ) { cnt [ str [ end ] - ' a ' ] ++ ; if ( cnt [ str [ end ] - ' a ' ] == 1 ) distEle ++ ; if ( distEle > k ) { while ( st < end && distEle > k ) { if ( cnt [ str [ st ] - ' a ' ] == 1 ) distEle -- ; cnt [ str [ st ] - ' a ' ] -- ; st ++ ; } } if ( distEle == k ) { while ( st < end && cnt [ str [ st ] - ' a ' ] > 1 ) { cnt [ str [ st ] - ' a ' ] -- ; st ++ ; } currlen = end - st + 1 ; if ( currlen < minlen ) { minlen = currlen ; startInd = st ; } } end ++ ; } return str . Substring ( startInd , minlen ) ; } public static void Main ( ) { string str = " efecfefd " ; int k = 4 ; Console . WriteLine ( findMinLenStr ( str , k ) ) ; } }


"Minimum letters to be removed to make all occurrences of a given letter continuous | C # implementation of the above approach ; Function to find the minimum number of deletions required to make the occurrences of the given character K continuous ; Find the first occurrence of the given letter ; Iterate from the first occurrence till the end of the sequence ; Find the index from where the occurrence of the character is not continuous ; Update the answer with the number of elements between non - consecutive occurrences of the given letter ; Update the count for all letters which are not equal to the given letter ; Return the count ; Driver code ; Calling the function ; Calling the function"
Solution in C#:

 using System ; class GFG { static int noOfDeletions ( String str , char k ) { int ans = 0 , cnt = 0 , pos = 0 ; while ( pos < str . Length && str [ pos ] != k ) { pos ++ ; } int i = pos ; while ( i < str . Length ) { while ( i < str . Length && str [ i ] == k ) { i = i + 1 ; } ans = ans + cnt ; cnt = 0 ; while ( i < str . Length && str [ i ] != k ) { i = i + 1 ; cnt = cnt + 1 ; } } return ans ; } public static void Main ( String [ ] args ) { String str1 = " ababababa " ; char k1 = ' a ' ; Console . Write ( noOfDeletions ( str1 , k1 ) + "
 " ) ; String str2 = " kprkkoinkopt " ; char k2 = ' k ' ; Console . Write ( noOfDeletions ( str2 , k2 ) + "
 " ) ; } }


"Minimum matches the team needs to win to qualify | C # implementation of the approach ; Function to return the minimum number of matches to win to qualify for next round ; Do a binary search to find ; Find mid element ; Check for condition to qualify for next round ; Driver code"
Solution in C#:

using System ; class GFG { static int findMinimum ( int x , int y ) { int low = 0 , high = y ; while ( low <= high ) { int mid = ( low + high ) >> 1 ; if ( ( mid * 2 + ( y - mid ) ) >= x ) high = mid - 1 ; else low = mid + 1 ; } return low ; } static public void Main ( ) { int x = 6 , y = 5 ; Console . WriteLine ( findMinimum ( x , y ) ) ; } }


"Minimum minutes needed to make the time palindromic | C # program for the above approach ; Function to get the required minutes ; Storing hour and minute value in integral form ; Keep iterating till first digit hour becomes equal to second digit of minute and second digit of hour becomes equal to first digit of minute ; If mins is 60 , increase hour , and reinitilialized to 0 ; If hours is 60 , reinitialized to 0 ; Return the required time ; Driver code ; Given Time as a string ; Function Call"
Solution in C#:

using System ; class GFG { public static int get_palindrome_time ( string str ) { int hh , mm ; hh = ( str [ 0 ] - 48 ) * 10 + ( str [ 1 ] - 48 ) ; mm = ( str [ 3 ] - 48 ) * 10 + ( str [ 4 ] - 48 ) ; int requiredTime = 0 ; while ( hh % 10 != mm / 10 hh / 10 != mm % 10 ) { ++ mm ; if ( mm == 60 ) { mm = 0 ; ++ hh ; } if ( hh == 24 ) hh = 0 ; ++ requiredTime ; } return requiredTime ; } public static void Main ( string [ ] args ) { string str = "05:39" ; Console . Write ( get_palindrome_time ( str ) ) ; } }


"Minimum move to end operations to make all strings equal | C # program to make all strings same using move to end operations . ; Returns minimum number of moves to end operations to make all strings same . ; Consider s [ i ] as target string and count rotations required to make all other strings same as str [ i ] . ; find function returns the index where we found arr [ i ] which is actually count of move - to - front operations . ; If any two strings are not rotations of each other , we can 't make them same. ; Driver code"
Solution in C#:

using System ; public class GFG { public static int minimunMoves ( string [ ] arr , int n ) { int ans = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { int curr_count = 0 ; string tmp = " " ; for ( int j = 0 ; j < n ; j ++ ) { tmp = arr [ j ] + arr [ j ] ; int index = tmp . IndexOf ( arr [ i ] , StringComparison . Ordinal ) ; if ( index == arr [ i ] . Length ) { return - 1 ; } curr_count += index ; } ans = Math . Min ( curr_count , ans ) ; } return ans ; } public static void Main ( string [ ] args ) { string [ ] arr = new string [ ] { " xzzwo " , " zwoxz " , " zzwox " , " xzzwo " } ; int n = arr . Length ; Console . WriteLine ( minimunMoves ( arr , n ) ) ; } }


"Minimum moves required to come out of a grid safely | C # program to implement the above approach ; Structure of cell of the grid ; Stores size of the grid ; Function to find shortest distance between two cells of the grid ; Rows of the grid ; Column of the grid ; Stores possible move of the person ; Store possible cells visited by the person ; Store possible cells which are burning ; Traverse the grid ; If current cell is burning ; If person is in the current cell ; Stores shortest distance between two cells ; Check if a cell is visited by the person or not ; While pQ is not empty ; Update depth ; Popped all the cells from pQ and mark all adjacent cells of as visited ; Front element of the queue pQ ; Remove front element of the queue pQ ; If current cell is burning ; Find all adjacent cells ; Stores row number of adjacent cell ; Stores column number of adjacent cell ; Checks if current cell is valid ; Mark the cell as visited ; Enqueue the cell ; Checks the escape condition ; Burn all the adjacent cells of burning cells ; Front element of the queue fQ ; Delete front element of the queue fQ ; Find adjacent cells of burning cell ; Stores row number of adjacent cell ; Stores column number of adjacent cell ; Checks if current cell is valid ; Burn all the adjacent cells of current cell ; Function to check valid cells of the grid ; Checks for the border sides ; Driver Code ; Given grid"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { class pair { public int x , y ; public pair ( int x , int y ) { this . x = x ; this . y = y ; } } static int m , n ; static int minStep ( int [ , ] mat ) { m = mat . GetLength ( 0 ) ; n = mat . GetLength ( 1 ) ; int [ ] dx = { 1 , - 1 , 0 , 0 } ; int [ ] dy = { 0 , 0 , 1 , - 1 } ; Queue < pair > pQ = new Queue < pair > ( ) ; Queue < pair > fQ = new Queue < pair > ( ) ; for ( int i = 0 ; i < m ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) { if ( mat [ i , j ] == 2 ) fQ . Enqueue ( new pair ( i , j ) ) ; else if ( mat [ i , j ] == 1 ) { if ( border ( i , j ) ) return 0 ; pQ . Enqueue ( new pair ( i , j ) ) ; } } int depth = 0 ; bool [ , ] visited = new bool [ n , m ] ; while ( pQ . Count != 0 ) { depth ++ ; for ( int i = pQ . Count ; i > 0 ; i -- ) { pair pos = pQ . Peek ( ) ; pQ . Dequeue ( ) ; if ( mat [ pos . x , pos . y ] == 2 ) continue ; for ( int j = 0 ; j < 4 ; j ++ ) { int x = pos . x + dx [ j ] ; int y = pos . y + dy [ j ] ; if ( valid ( x , y ) && mat [ x , y ] != 2 && ! visited [ x , y ] ) { visited [ x , y ] = true ; pQ . Enqueue ( new pair ( x , y ) ) ; if ( border ( x , y ) ) return depth ; } } } for ( int i = fQ . Count ; i > 0 ; i -- ) { pair pos = fQ . Peek ( ) ; fQ . Dequeue ( ) ; for ( int j = 0 ; j < 4 ; j ++ ) { int x = pos . x + dx [ j ] ; int y = pos . y + dy [ j ] ; if ( valid ( x , y ) && mat [ x , y ] != 2 ) { mat [ x , y ] = 2 ; fQ . Enqueue ( new pair ( x , y ) ) ; } } } } return - 1 ; } static bool valid ( int x , int y ) { return ( x >= 0 && x < m && y >= 0 && y < n ) ; } static bool border ( int x , int y ) { return ( x == 0 x == m - 1 y == 0 y == n - 1 ) ; } public static void Main ( String [ ] args ) { int [ , ] grid = { { 0 , 0 , 0 , 0 } , { 2 , 0 , 0 , 0 } , { 2 , 1 , 0 , 0 } , { 2 , 2 , 0 , 0 } } ; Console . WriteLine ( minStep ( grid ) ) ; } }


"Minimum moves required to type a word in QWERTY based keyboard | C # program for the above approach ; Function that calculates the moves required to print the current String ; row1 has qwertyuiop , row2 has asdfghjkl , and row3 has zxcvbnm Store the row number of each character ; String length ; Initialise move to 1 ; Traverse the String ; If current row number is not equal to previous row then increment the moves ; Return the moves ; Driver Code ; Given String str ; Function Call"
Solution in C#:

using System ; class GFG { static int numberMoves ( String s ) { int [ ] row = { 2 , 3 , 3 , 2 , 1 , 2 , 2 , 2 , 1 , 2 , 2 , 2 , 3 , 3 , 1 , 1 , 1 , 1 , 2 , 1 , 1 , 3 , 1 , 3 , 1 , 3 } ; int n = s . Length ; int move = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( row [ s [ i ] - ' a ' ] != row [ s [ i - 1 ] - ' a ' ] ) { move ++ ; } } return move ; } public static void Main ( String [ ] args ) { String str = " geeksforgeeks " ; Console . Write ( numberMoves ( str ) ) ; } }


"Minimum moves taken to move coin of each cell to any one cell of Matrix | C # program to find the minimum number of moves taken to move the element of each cell to any one cell of the square matrix of odd length ; Function to find the minimum number of moves taken to move the element of each cell to any one cell of the square matrix of odd length ; Initializing count to 0 ; Number of layers that are around the centre element ; Iterating over ranger of layers ; Increase the value of count by 8 * k * k ; Driver code"
Solution in C#:

using System ; class GFG { public static int calculateMoves ( int n ) { int count = 0 ; int layers = n / 2 ; for ( int k = 1 ; k < layers + 1 ; k ++ ) { count += 8 * k * k ; } return count ; } public static void Main ( ) { int N = 5 ; Console . Write ( calculateMoves ( N ) ) ; } }


"Minimum moves to make count of lowercase and uppercase letters equal | C # program for the above approach ; Function to calculate minimum number of moves required to convert the string ; Stores Count of upper and lower case characters ; Traverse the string S ; If current character is uppercase ; Increment count of Uppercase characters ; Otherwise , ; Increment count of Lowercase characters ; Stores minimum number of moves needed ; If there are more upper case characters ; Iterate until upper is greater than N / 2 ; Convert uppercase into lowercase until upper = N / 2 ; Increment the pointer ; If there are more lower case characters ; Iterate until lower is greater than N / 2 ; Convert lowercase into uppercase until lower = N / 2 ; Increment the pointer ; Print moves required ; Print resultant string ; Given string ; Function call"
Solution in C#:

using System ; class GFG { static void minimumTimeToConvertString ( char [ ] S , int N ) { int upper = 0 , lower = 0 ; for ( int i = 0 ; i < N ; i ++ ) { char c = S [ i ] ; if ( Char . IsUpper ( c ) ) { upper ++ ; } else { lower ++ ; } } int moves = 0 ; if ( upper > N / 2 ) { int i = 0 ; while ( upper > N / 2 && i < N ) { if ( Char . IsUpper ( S [ i ] ) ) { S [ i ] += ( char ) 32 ; moves ++ ; upper -- ; lower ++ ; } i ++ ; } } else if ( lower > N / 2 ) { int i = 0 ; while ( lower > N / 2 && i < N ) { if ( Char . IsLower ( S [ i ] ) ) { S [ i ] = ( char ) ( ( int ) S [ i ] - 32 ) ; moves ++ ; upper ++ ; lower -- ; } i ++ ; } } Console . WriteLine ( moves ) ; Console . WriteLine ( new string ( S ) ) ; } static void Main ( ) { string S = " AbcdEf " ; int N = S . Length ; minimumTimeToConvertString ( S . ToCharArray ( ) , N ) ; } }


"Minimum moves to reach from i to j in a cyclic string | C # implementation of the approach ; Function to return the count of steps required to move from i to j ; Starting from i + 1 ; Count of steps ; Current character ; If current character is different from previous ; Increment steps ; Update current character ; Return total steps ; Function to return the minimum number of steps required to reach j from i ; Swap the values so that i <= j ; Steps to go from i to j ( left to right ) ; While going from i to j ( right to left ) First go from i to 0 then from ( n - 1 ) to j ; If first and last character is different then it 'll add a step to stepsToLeft ; Return the minimum of two paths ; Driver code"
Solution in C#:

using System ; class GFG { static int getSteps ( string str , int i , int j , int n ) { int k = i + 1 ; int steps = 0 ; char ch = str [ i ] ; while ( k <= j ) { if ( str [ k ] != ch ) { steps ++ ; ch = str [ k ] ; } k ++ ; } return steps ; } static int getMinSteps ( string str , int i , int j , int n ) { if ( j < i ) { int temp = i ; i = j ; j = temp ; } int stepsToRight = getSteps ( str , i , j , n ) ; int stepsToLeft = getSteps ( str , 0 , i , n ) + getSteps ( str , j , n - 1 , n ) ; if ( str [ 0 ] != str [ n - 1 ] ) stepsToLeft ++ ; return Math . Min ( stepsToLeft , stepsToRight ) ; } public static void Main ( ) { string str = " SSNSS " ; int n = str . Length ; int i = 0 , j = 3 ; Console . WriteLine ( getMinSteps ( str , i , j , n ) ) ; } }


"Minimum moves to reach target on a infinite line | Set 2 | C # code to find minimum moves to reach target ; Function to find minimum steps to reach target ; Handling negatives by symmetry ; Keep moving while sum is smaller i . e calculating n ; case 1 : d is even ; d is odd ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { static int StepstoReachTarget ( int target ) { target = Math . Abs ( target ) ; int n = ( int ) Math . Ceiling ( ( - 1.0 + ( int ) Math . Sqrt ( 1 + 8.0 * target ) ) / 2 ) ; int sum = n * ( n + 1 ) / 2 ; if ( sum == target ) return n ; int d = sum - target ; if ( ( d & 1 ) == 0 ) return n ; else return n + ( ( n & 1 ) != 0 ? 2 : 1 ) ; } public static void Main ( ) { int target = 5 ; Console . Write ( StepstoReachTarget ( target ) ) ; } }


"Minimum multiplications with { 2 , 3 , 7 } to make two numbers equal | C # implementation of above approach ; Function to find powers of 2 , 3 and 7 in x ; To keep count of each divisor ; To store the result ; Count powers of 2 in x ; Count powers of 3 in x ; Count powers of 7 in x ; Remaining number which is not divisible by 2 , 3 or 7 ; Function to return the minimum number of given operations required to make a and b equal ; a = x * 2 ^ a1 * 3 ^ a2 * 7 ^ a3 va [ 0 ] = a1 va [ 1 ] = a2 va [ 2 ] = a3 va [ 3 ] = x ; Similarly for b ; If a and b cannot be made equal with the given operation . Note that va [ 3 ] and vb [ 3 ] contain remaining numbers after repeated divisions with 2 , 3 and 7. If remaining numbers are not same then we cannot make them equal . ; Minimum number of operations required ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static List < int > Divisors ( int x ) { int c = 0 ; List < int > v = new List < int > ( ) ; while ( x % 2 == 0 ) { c ++ ; x /= 2 ; } v . Add ( c ) ; c = 0 ; while ( x % 3 == 0 ) { c ++ ; x /= 3 ; } v . Add ( c ) ; c = 0 ; while ( x % 7 == 0 ) { c ++ ; x /= 7 ; } v . Add ( c ) ; v . Add ( x ) ; return v ; } static int MinOperations ( int a , int b ) { List < int > va = Divisors ( a ) ; List < int > vb = Divisors ( b ) ; if ( va [ 3 ] != vb [ 3 ] ) { return - 1 ; } int minOperations = Math . Abs ( va [ 0 ] - vb [ 0 ] ) + Math . Abs ( va [ 1 ] - vb [ 1 ] ) + Math . Abs ( va [ 2 ] - vb [ 2 ] ) ; return minOperations ; } public static void Main ( String [ ] args ) { int a = 14 , b = 28 ; Console . WriteLine ( MinOperations ( a , b ) ) ; } }


"Minimum no . of iterations to pass information to all nodes in the tree | C # program to find minimum number of iterations to pass information from root to all nodes in an n - ary tree ; No . of nodes ; Adjacency list containing list of children ; function to add a child w to v ; Main function to find the minimum iterations ; Base case : if height = 0 or 1 ; ; Start Post Order Traversal from Root ; A recursive function to used by getMinIter ( ) . This function mainly does postorder traversal and get minimum iteration of all children of parent node , sort them in decreasing order and then get minimum iteration of parent node 1. Get minItr ( B ) of all children ( B ) of a node ( A ) 2. Sort all minItr ( B ) in descending order 3. Get minItr of A based on all minItr ( B ) minItr ( A ) = child ( A ) -- >> child ( A ) is children count of node A For children B from i = 0 to child ( A ) minItr ( A ) = max ( minItr ( A ) , minItr ( B ) + i + 1 ) Base cases would be : If node is leaf , minItr = 0 If node 's height is 1, minItr = children count   ; Base case : Leaf node ; Driver Code ; TestCase1 ; TestCase2 ; TestCase3 ; TestCase4 ; TestCase 5 ; TestCase 6 ; TestCase 7 ; TestCase 8 ; TestCase 9"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public int N ; static List < List < int > > adj ; public GFG ( int N ) { this . N = N ; adj = new List < List < int > > ( N ) ; for ( int i = 0 ; i < N ; i ++ ) adj . Add ( new List < int > ( ) ) ; } void addChild ( int v , int w ) { adj [ v ] . Add ( w ) ; } private int getMinIteration ( ) { if ( N == 0 N == 1 ) return 0 ; int [ ] mintItr = new int [ N ] ; getMinIterUtil ( 0 , mintItr ) ; return mintItr [ 0 ] ; } private void getMinIterUtil ( int u , int [ ] minItr ) { if ( adj [ u ] . Count == 0 ) return ; minItr [ u ] = adj [ u ] . Count ; int [ ] minItrTemp = new int [ minItr [ u ] ] ; int k = 0 ; foreach ( int itr in adj [ u ] ) { int currentChild = ( int ) itr ; getMinIterUtil ( currentChild , minItr ) ; minItrTemp [ k ++ ] = minItr [ currentChild ] ; } Array . Sort ( minItrTemp ) ; for ( int i = 0 , j = minItrTemp . Length - 1 ; i < j ; i ++ , j -- ) { int temp = minItrTemp [ i ] ; minItrTemp [ i ] = minItrTemp [ j ] ; minItrTemp [ j ] = temp ; } for ( k = 0 ; k < adj [ u ] . Count ; k ++ ) { int temp = minItrTemp [ k ] + k + 1 ; minItr [ u ] = Math . Max ( minItr [ u ] , temp ) ; } } public static void Main ( String [ ] args ) { GFG testCase1 = new GFG ( 17 ) ; testCase1 . addChild ( 0 , 1 ) ; testCase1 . addChild ( 0 , 2 ) ; testCase1 . addChild ( 0 , 3 ) ; testCase1 . addChild ( 0 , 4 ) ; testCase1 . addChild ( 0 , 5 ) ; testCase1 . addChild ( 0 , 6 ) ; testCase1 . addChild ( 1 , 7 ) ; testCase1 . addChild ( 1 , 8 ) ; testCase1 . addChild ( 1 , 9 ) ; testCase1 . addChild ( 4 , 10 ) ; testCase1 . addChild ( 4 , 11 ) ; testCase1 . addChild ( 6 , 12 ) ; testCase1 . addChild ( 7 , 13 ) ; testCase1 . addChild ( 7 , 14 ) ; testCase1 . addChild ( 10 , 15 ) ; testCase1 . addChild ( 11 , 16 ) ; Console . WriteLine ( " TestCase ▁ 1 ▁ - ▁ Minimum ▁ Iteration : ▁ " + testCase1 . getMinIteration ( ) ) ; GFG testCase2 = new GFG ( 3 ) ; testCase2 . addChild ( 0 , 1 ) ; testCase2 . addChild ( 0 , 2 ) ; Console . WriteLine ( " TestCase ▁ 2 ▁ - ▁...


"Minimum non | C # program for the above approach ; Function to find minimum flips required to remove all 0 s from a given binary string ; Length of given string ; Stores the indices of previous 0 s ; Stores the minimum operations ; Traverse string to find minimum operations to obtain required string ; Current character ; If current character is '0' ; Update temp1 with current index , if both temp variables are empty ; Update temp2 with current index , if temp1 contains prev index but temp2 is empty ; If temp1 is not empty ; Reset temp1 to - 1 ; Increase ans ; If temp2 is not empty but temp1 is empty ; Reset temp2 to - 1 ; Increase ans ; If both temp variables are not empty ; Otherwise ; Return the answer ; Driver Code"
Solution in C#:

using System ; class GFG { static int minOperation ( String s ) { int n = s . Length ; int temp1 = - 1 , temp2 = - 1 ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int curr = s [ i ] ; if ( curr == '0' ) { if ( temp1 == - 1 && temp2 == - 1 ) { temp1 = i ; } else if ( temp1 != - 1 && temp2 == - 1 && i - temp1 == 1 ) { temp2 = i ; } else if ( temp1 != - 1 ) { temp1 = - 1 ; ans ++ ; } else if ( temp1 == - 1 && temp2 != - 1 && i - temp2 != 1 ) { temp2 = - 1 ; ans ++ ; } } } if ( temp1 != - 1 && temp2 != - 1 ) { ans += 2 ; } else if ( temp1 != - 1 temp2 != - 1 ) { ans += 1 ; } return ans ; } public static void Main ( String [ ] args ) { String s = "110010" ; Console . WriteLine ( minOperation ( s ) ) ; } }


"Minimum number N such that total set bits of all numbers from 1 to N is at | C # implementation of the above approach ; Function to count sum of set bits of all numbers till N ; Function to find the minimum number ; Binary search for the lowest number ; Find mid number ; Check if it is atleast x ; Driver Code"
Solution in C#:

using System ; class solution { static int INF = 99999 ; static int size = 10 ; static int getSetBitsFromOneToN ( int N ) { int two = 2 , ans = 0 ; int n = N ; while ( n != 0 ) { ans += ( N / two ) * ( two >> 1 ) ; if ( ( N & ( two - 1 ) ) > ( two >> 1 ) - 1 ) ans += ( N & ( two - 1 ) ) - ( two >> 1 ) + 1 ; two <<= 1 ; n >>= 1 ; } return ans ; } static int findMinimum ( int x ) { int low = 0 , high = 100000 ; int ans = high ; while ( low <= high ) { int mid = ( low + high ) >> 1 ; if ( getSetBitsFromOneToN ( mid ) >= x ) { ans = Math . Min ( ans , mid ) ; high = mid - 1 ; } else low = mid + 1 ; } return ans ; } public static void Main ( ) { int x = 20 ; Console . WriteLine ( findMinimum ( x ) ) ; } }


"Minimum number greater than the maximum of array which cannot be formed using the numbers in the array | C # implementation of the approach ; Function that returns the minimum number greater than maximum of the array that cannot be formed using the elements of the array ; Sort the given array ; Maximum number in the array ; table [ i ] will store the minimum number of elements from the array to form i ; Calculate the minimum number of elements from the array required to form the numbers from 1 to ( 2 * max ) ; If there exists a number greater than the maximum element of the array that can be formed using the numbers of array ; Driver code"
Solution in C#:

using System ; class GFG { static int findNumber ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int max = arr [ n - 1 ] ; int [ ] table = new int [ ( 2 * max ) + 1 ] ; table [ 0 ] = 0 ; for ( int i = 1 ; i < ( 2 * max ) + 1 ; i ++ ) table [ i ] = int . MaxValue ; int ans = - 1 ; for ( int i = 1 ; i < ( 2 * max ) + 1 ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ j ] <= i ) { int res = table [ i - arr [ j ] ] ; if ( res != int . MaxValue && res + 1 < table [ i ] ) table [ i ] = res + 1 ; } } if ( i > arr [ n - 1 ] && table [ i ] == int . MaxValue ) { ans = i ; break ; } } return ans ; } public static void Main ( ) { int [ ] arr = { 6 , 7 , 15 } ; int n = arr . Length ; Console . WriteLine ( findNumber ( arr , n ) ) ; } }


"Minimum number of 1 's to be replaced in a binary array | C # program to find minimum number of 1 ' s ▁ to ▁ be ▁ replaced ▁ to ▁ 0' s ; Function to find minimum number of 1 ' s ▁ to ▁ be ▁ replaced ▁ to ▁ 0' s ; return final answer ; Driver Code"
Solution in C#:

using System ; class GFG { static int minChanges ( int [ ] A , int n ) { int cnt = 0 ; for ( int i = 0 ; i < n - 2 ; ++ i ) { if ( ( i - 1 >= 0 ) && A [ i - 1 ] == 1 && A [ i + 1 ] == 1 && A [ i ] == 0 ) { A [ i + 1 ] = 0 ; cnt ++ ; } } return cnt ; } public static void Main ( ) { int [ ] A = { 1 , 1 , 0 , 1 , 1 , 0 , 1 , 0 , 1 , 0 } ; int n = A . Length ; Console . Write ( minChanges ( A , n ) ) ; } }


"Minimum number of 1 s present in a submatrix of given dimensions in a Binary Matrix | C # program for the above approach ; Function to count number of 1 s present in a sub matrix from ( start_i , start_j ) to ( end_i , end_j ) ; Stores the number of 1 s present in current submatrix ; Traverse the submatrix ; If mat [ x ] [ y ] is equal to 1 ; Increase count by 1 ; Return the total count of 1 s ; Function to find the minimum number of 1 s present in a sub - matrix of size A * B or B * A ; Stores the minimum count of 1 s ; Iterate i from 0 to N ; Iterate j from 0 to M ; If a valid sub matrix of size A * B from ( i , j ) is possible ; Count the number of 1 s present in the sub matrix of size A * B from ( i , j ) ; Update minimum if count is less than the current minimum ; If a valid sub matrix of size B * A from ( i , j ) is possible ; Count the number of 1 s in the sub matrix of size B * A from ( i , j ) ; Update minimum if count is less than the current minimum ; Return minimum as the final result ; Driver Code ; Given Input ; Function call to find the minimum number of 1 s in a submatrix of size A * B or B * A"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int count1s ( int start_i , int start_j , int end_i , int end_j , List < List < int > > mat ) { int count = 0 ; for ( int x = start_i ; x < end_i ; x ++ ) { for ( int y = start_j ; y < end_j ; y ++ ) { if ( mat [ x ] [ y ] == 1 ) count ++ ; } } return count ; } static void findMinimumCount ( int N , int M , int A , int B , List < List < int > > mat ) { int minimum = 1000000 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( ( i + A <= N ) && ( j + B <= M ) ) { int count = count1s ( i , j , i + A , j + B , mat ) ; minimum = Math . Min ( count , minimum ) ; } if ( ( i + B <= N ) && ( j + A <= M ) ) { int count = count1s ( i , j , i + B , j + A , mat ) ; minimum = Math . Min ( count , minimum ) ; } } } Console . WriteLine ( minimum ) ; } public static void Main ( ) { int A = 2 , B = 2 ; int N = 3 , M = 4 ; List < List < int > > mat = new List < List < int > > ( ) ; mat . Add ( new List < int > ( new int [ ] { 1 , 0 , 1 , 0 } ) ) ; mat . Add ( new List < int > ( new int [ ] { 0 , 1 , 0 , 1 } ) ) ; mat . Add ( new List < int > ( new int [ ] { 1 , 0 , 1 , 0 } ) ) ; findMinimumCount ( N , M , A , B , mat ) ; } }


"Minimum number of Appends needed to make a string palindrome | C # program to find minimum number of appends needed to make a string Palindrome ; Checking if the string is palindrome or not ; single character is always palindrome ; pointing to first character ; pointing to last character ; Recursive function to count number of appends ; Removing first character of string by incrementing base address pointer . ; Driver code"
Solution in C#:

 using System ; class GFG { static Boolean isPalindrome ( char [ ] str ) { int len = str . Length ; if ( len == 1 ) return true ; char ptr1 = str [ 0 ] ; char ptr2 = str [ len - 1 ] ; while ( ptr2 > ptr1 ) { if ( ptr1 != ptr2 ) return false ; ptr1 ++ ; ptr2 -- ; } return true ; } static int noOfAppends ( String s ) { if ( isPalindrome ( s . ToCharArray ( ) ) ) return 0 ; s = s . Substring ( 1 ) ; return 1 + noOfAppends ( s ) ; } public static void Main ( String [ ] arr ) { String s = " abede " ; Console . Write ( " { 0 }
 " , noOfAppends ( s ) ) ; } }


"Minimum number of Apples to be collected from trees to guarantee M red apples | C # program for the above approach ; Function to minimum no . of apples ; If we get all required apple from South ; If we required trees at East and West ; If we doesn 't have enough  red apples ; Driver code ; No . of red apple for gift ; No . of red apple in each tree ; No . of tree in North ; No . of tree in South ; No . of tree in West ; No . of tree in East ; Function Call"
Solution in C#:

using System ; class GFG { static int minApples ( int M , int K , int N , int S , int W , int E ) { if ( M <= S * K ) return M ; else if ( M <= S * K + E + W ) return S * K + ( M - S * K ) * K ; else return - 1 ; } public static void Main ( String [ ] args ) { int M = 10 ; int K = 15 ; int N = 0 ; int S = 1 ; int W = 0 ; int E = 0 ; int ans = minApples ( M , K , N , S , W , E ) ; Console . Write ( ans ) ; } }


"Minimum number of Binary strings to represent a Number | C # program to find the minimum number of binary Strings to represent a number ; Function to find the minimum number of binary Strings to represent a number ; Storing digits in correct order ; Find the maximum digit in the array ; Traverse for all the binary Strings ; If digit at jth position is greater than 0 then substitute 1 ;  ; Driver code"
Solution in C#:

 using System ; class GFG { static void minBinary ( int n ) { int [ ] digit = new int [ 10 ] ; int len = 0 ; while ( n > 0 ) { digit [ len ++ ] = n % 10 ; n /= 10 ; } digit = reverse ( digit , 0 , len - 1 ) ; int ans = 0 ; for ( int i = 0 ; i < len ; i ++ ) { ans = Math . Max ( ans , digit [ i ] ) ; } Console . Write ( " Minimum ▁ Number ▁ of ▁ binary " + " ▁ Strings ▁ needed : ▁ " + ans + "
 " ) ; for ( int i = 1 ; i <= ans ; i ++ ) { int num = 0 ; for ( int j = 0 ; j < len ; j ++ ) { if ( digit [ j ] > 0 ) { num = num * 10 + 1 ; digit [ j ] -- ; } else { num *= 10 ; } } Console . Write ( num + " ▁ " ) ; } } static int [ ] reverse ( int [ ] str , int start , int end ) { int temp ; while ( start <= end ) { temp = str [ start ] ; str [ start ] = str [ end ] ; str [ end ] = temp ; start ++ ; end -- ; } return str ; } public static void Main ( String [ ] args ) { int n = 564 ; minBinary ( n ) ; } }


"Minimum number of Bottles visible when a bottle can be enclosed inside another Bottle | C # code for the above approach ; Driver code ; Find the solution"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static void min_visible_bottles ( int [ ] arr , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1 ; } else { mp . Add ( arr [ i ] , 1 ) ; } ans = Math . Max ( ans , mp [ arr [ i ] ] ) ; } Console . Write ( " Minimum ▁ number ▁ of ▁ " + " Visible ▁ Bottles ▁ are : ▁ " + ans + "
 " ) ; } public static void Main ( String [ ] args ) { int n = 8 ; int [ ] arr = { 1 , 1 , 2 , 3 , 4 , 5 , 5 , 4 } ; min_visible_bottles ( arr , n ) ; } }


"Minimum number of Circular obstacles required to obstruct the path in a Grid | C # program to find the minimum number of obstacles required ; Function to find the minimum number of obstacles required ; Find the minimum range required to put obstacles ; Sorting the radius ; If val is less than zero then we have find the number of obstacles required ; Driver code"
Solution in C#:

using System ; class GFG { static int solve ( int n , int m , int obstacles , double [ ] range ) { double val = Math . Min ( n , m ) ; Array . Sort ( range ) ; int c = 1 ; for ( int i = obstacles - 1 ; i >= 0 ; i -- ) { range [ i ] = 2 * range [ i ] ; val -= range [ i ] ; if ( val <= 0 ) { return c ; } else { c ++ ; } } if ( val > 0 ) { return - 1 ; } return 0 ; } public static void Main ( ) { int n = 4 , m = 5 , obstacles = 3 ; double [ ] range = { 1.0 , 1.25 , 1.15 } ; Console . WriteLine ( solve ( n , m , obstacles , range ) ) ; } }


"Minimum number of Cuboids required to form a Cube | C # program for the above approach ; Function to calculate and return LCM of a , b , and c ; Find GCD of a and b ; Find LCM of a and b ; LCM ( a , b , c ) = LCM ( LCM ( a , b ) , c ) ; Finding LCM of a , b , c ; return LCM ( a , b , c ) ; Function to find the minimum number of cuboids required to make the volume of a valid cube ; Find the LCM of L , B , H ; Volume of the cube ; Volume of the cuboid ; Minimum number cuboids required to form a cube ; Driver Code ; Given dimensions of cuboid ; Function Call"
Solution in C#:

using System ; class GFG { static int find_lcm ( int a , int b , int c ) { int g = __gcd ( a , b ) ; int LCM1 = ( a * b ) / g ; g = __gcd ( LCM1 , c ) ; int LCM = ( LCM1 * c ) / g ; return LCM ; } static void minimumCuboids ( int L , int B , int H ) { int lcm = find_lcm ( L , B , H ) ; int volume_cube = lcm * lcm * lcm ; int volume_cuboid = L * B * H ; Console . Write ( ( volume_cube / volume_cuboid ) ) ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void Main ( String [ ] args ) { int L = 1 , B = 1 , H = 2 ; minimumCuboids ( L , B , H ) ; } }


"Minimum number of Factorials whose sum is equal to N | C # program to find minimum number of factorials ; Array to calculate all factorials less than or equal to N Since there can be only 14 factorials till 10 ^ 10 Hence the maximum size of fact [ ] is 14 ; Store the actual size of fact [ ] ; Function to precompute factorials till N ; Precomputing factorials ; Function to find the minimum number of factorials whose sum represents N ; Precompute factorials ; Initialize result ; Traverse through all factorials ; Find factorials ; Print min count ; Print result ; Driver program"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static int [ ] fact = new int [ 14 ] ; static int size = 1 ; static void preCompute ( int N ) { fact [ 0 ] = 1 ; for ( int i = 1 ; fact [ i - 1 ] <= N ; i ++ ) { fact [ i ] = ( fact [ i - 1 ] * i ) ; size ++ ; } } static void findMin ( int N ) { preCompute ( N ) ; int originalN = N ; List < int > ans = new List < int > ( ) ; for ( int i = size - 1 ; i >= 0 ; i -- ) { while ( N >= fact [ i ] ) { N -= fact [ i ] ; ans . Add ( fact [ i ] ) ; } } Console . Write ( ans . Count + "
 " ) ; for ( int i = 0 ; i < ans . Count ; i ++ ) Console . Write ( ans [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int n = 27 ; findMin ( n ) ; } }


"Minimum number of Fibonacci jumps to reach end | A Dynamic Programming based C # program to find minimum number of jumps to reach Destination ; Function that returns the min number of jump to reach the destination ; We consider only those Fibonacci numbers which are less than n , where we can consider fib [ 30 ] to be the upper bound as this will cross 10 ^ 5 ; DP [ i ] will be storing the minimum number of jumps required for the position i . So DP [ N + 1 ] will have the result we consider 0 as source and N + 1 as the destination ; Base case ( Steps to reach source is ) ; Initialize all table values as Infinite ; Go through each positions till destination . ; Calculate the minimum of that position if all the Fibonacci numbers are considered ; If the position is safe or the position is the destination then only we calculate the minimum otherwise the cost is MAX as default ; - 1 denotes if there is no path possible ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static int minJumps ( int [ ] arr , int N ) { int MAX = 1000000 ; int [ ] fib = new int [ 30 ] ; fib [ 0 ] = 0 ; fib [ 1 ] = 1 ; for ( int i = 2 ; i < 30 ; i ++ ) fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] ; int [ ] DP = new int [ N + 2 ] ; DP [ 0 ] = 0 ; for ( int i = 1 ; i <= N + 1 ; i ++ ) DP [ i ] = MAX ; for ( int i = 1 ; i <= N + 1 ; i ++ ) { for ( int j = 1 ; j < 30 ; j ++ ) { if ( ( i == N + 1 arr [ i - 1 ] == 1 ) && i - fib [ j ] >= 0 ) DP [ i ] = Math . Min ( DP [ i ] , 1 + DP [ i - fib [ j ] ] ) ; } } if ( DP [ N + 1 ] != MAX ) return DP [ N + 1 ] ; else return - 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = new int [ ] { 0 , 0 , 0 , 1 , 1 , 0 , 1 , 0 , 0 , 0 , 0 } ; int n = 11 ; int ans = minJumps ( arr , n ) ; Console . WriteLine ( ans ) ; } }


"Minimum number of Nodes to be removed such that no subtree has more than K nodes | C # program for the above approach ; Variables used to store data globally ; Adjacency list representation of tree ; Function to perform DFS Traversal ; Mark the node as true ; Traverse adjacency list of child node ; If already visited then omit the node ; Add number of nodes in subtree ; Increment the count ; Return the nodes ; Function to add edges in graph ; Function that finds the number of nodes to be removed such that every subtree has size at most K ; Function Call to find the number of nodes to remove ; Print Removed Nodes ; Driver Code ; Creating list for all nodes ; Insert of nodes in graph ; Required subtree size ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static readonly int N = 20 ; static bool [ ] visited = new bool [ N ] ; static int K ; static int removals = 0 ; static List < int > removed_nodes = new List < int > ( ) ; static List < List < int > > adj = new List < List < int > > ( ) ; static int dfs ( int s ) { visited [ s ] = true ; int nodes = 1 ; foreach ( int child in adj [ s ] ) { if ( visited [ child ] ) continue ; nodes += dfs ( child ) ; } if ( nodes > K ) { removals ++ ; removed_nodes . Add ( s ) ; nodes = 0 ; } return nodes ; } static void addEdge ( int a , int b ) { adj [ a ] . Add ( b ) ; adj [ b ] . Add ( a ) ; } public static void findRemovedNodes ( int K ) { dfs ( 1 ) ; Console . WriteLine ( " Number ▁ of ▁ nodes " + " ▁ removed : ▁ " + removals ) ; Console . Write ( " Removed ▁ Nodes : ▁ " ) ; foreach ( int node in removed_nodes ) Console . Write ( node + " ▁ " ) ; } public static void Main ( String [ ] args ) { for ( int i = 0 ; i < N ; i ++ ) adj . Add ( new List < int > ( ) ) ; addEdge ( 1 , 2 ) ; addEdge ( 1 , 3 ) ; addEdge ( 2 , 4 ) ; addEdge ( 2 , 5 ) ; addEdge ( 3 , 6 ) ; K = 3 ; findRemovedNodes ( K ) ; } }


"Minimum number of Parentheses to be added to make it valid | C # Program to find minimum number of ' ( ' or ' ) ' must be added to make Parentheses string valid . ; Function to return required minimum number ; maintain balance of string ; It is guaranteed bal >= - 1 ; Driver code ; Function to print required answer"
Solution in C#:

using System ; class GFG { static int minParentheses ( string p ) { int bal = 0 ; int ans = 0 ; for ( int i = 0 ; i < p . Length ; ++ i ) { bal += p [ i ] == ' ( ' ? 1 : - 1 ; if ( bal == - 1 ) { ans += 1 ; bal += 1 ; } } return bal + ans ; } public static void Main ( ) { string p = " ( ) ) " ; Console . WriteLine ( minParentheses ( p ) ) ; } }


"Minimum number of Square Free Divisors | C # Program to find the minimum number of square free divisors ; Initializing MAX with SQRT ( 10 ^ 6 ) ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Print all prime numbers ; This function returns the minimum number of Square Free divisors ; Precomputing Prime Factors ; holds max of max power of all prime factors ; holds the max power of current prime factor ; If number itself is prime , it will be included as answer and thus minimum required answer is 1 ; Driver Code to test above functions"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { static int MAX = 1005 ; static void SieveOfEratosthenes ( List < int > primes ) { bool [ ] prime = new bool [ MAX ] ; for ( int i = 0 ; i < prime . Length ; i ++ ) { prime [ i ] = true ; } for ( int p = 2 ; p * p < MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i < MAX ; i += p ) { prime [ i ] = false ; } } } for ( int p = 2 ; p < MAX ; p ++ ) { if ( prime [ p ] ) { primes . Add ( p ) ; } } } static int minimumSquareFreeDivisors ( int N ) { List < int > primes = new List < int > ( ) ; SieveOfEratosthenes ( primes ) ; int max_count = 0 ; for ( int i = 0 ; i < primes . Count && primes [ i ] * primes [ i ] <= N ; i ++ ) { if ( N % primes [ i ] == 0 ) { int tmp = 0 ; while ( N % primes [ i ] == 0 ) { tmp ++ ; N /= primes [ i ] ; } max_count = Math . Max ( max_count , tmp ) ; } } if ( max_count == 0 ) { max_count = 1 ; } return max_count ; } public static void Main ( ) { int N = 24 ; Console . WriteLine ( " Minimum ▁ Number ▁ of ▁ Square ▁ Free ▁ Divisors ▁ is ▁ " + minimumSquareFreeDivisors ( N ) ) ; N = 6 ; Console . WriteLine ( " Minimum ▁ Number ▁ of ▁ Square ▁ Free ▁ Divisors ▁ is ▁ " + minimumSquareFreeDivisors ( N ) ) ; } }


"Minimum number of alternate subsequences required to be removed to empty a Binary String | C # program for the above approach ; Function to find the minimum number of operations to empty a binary string ; Stores the resultant number of operations ; Stores the number of 0 s ; Stores the number of 1 s ; Traverse the given string ; To balance 0 with 1 if possible ; Increment the value of cn0 by 1 ; To balance 1 with 0 if possible ; Increment the value of cn1 ; Update the maximum number of unused 0 s and 1 s ; Print the resultant count ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void minOpsToEmptyString ( string s ) { int ans = 0 ; int cn0 = 0 ; int cn1 = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == '0' ) { if ( cn1 > 0 ) cn1 -- ; cn0 ++ ; } else { if ( cn0 > 0 ) cn0 -- ; cn1 ++ ; } ans = Math . Max ( ans , Math . Max ( cn0 , cn1 ) ) ; } Console . Write ( ans ) ; } public static void Main ( ) { string S = "010101" ; minOpsToEmptyString ( S ) ; } }


"Minimum number of array elements from either ends required to be subtracted from X to reduce X to 0 | C # Program to implement the above approach ; Function to count the minimum number of operations required to reduce x to 0 ; If sum of the array is less than x ; Stores the count of operations ; Two pointers to traverse the array ; Reduce x by the sum of the entire array ; Iterate until l reaches the front of the array ; If sum of elements from the front exceeds x ; Shift towards left ; If sum exceeds 0 ; Reduce x by elements from the right ; If x is reduced to 0 ; Update the minimum count of operations required ; Driver Code"
Solution in C#:

using System ; class GFG { static int minOperations ( int [ ] nums , int x ) { int sum = 0 ; for ( int i = 0 ; i < x ; i ++ ) sum += nums [ i ] ; if ( sum < x ) return - 1 ; int ans = Int32 . MaxValue ; int l = nums . Length - 1 , r = nums . Length ; x -= sum ; while ( l >= 0 ) { if ( x <= 0 ) { x += nums [ l ] ; l -= 1 ; } if ( x > 0 ) { r -= 1 ; x -= nums [ r ] ; } if ( x == 0 ) { ans = Math . Min ( ans , ( l + 1 ) + ( nums . Length - r ) ) ; } } if ( ans < Int32 . MaxValue ) return ans ; else return - 1 ; } public static void Main ( ) { int [ ] nums = { 1 , 1 , 4 , 2 , 3 } ; int x = 5 ; Console . Write ( minOperations ( nums , x ) ) ; } }


"Minimum number of basic logic gates required to realize given Boolean expression | C # implementation of the above approach ; Function to count the total number of gates required to realize the boolean expression S ; Length of the string ; Stores the count of total gates ; Traverse the string ; AND , OR and NOT Gate ; Print the count of gates required ; Driver Code ; Input ; Function call to count the total number of gates required"
Solution in C#:

using System ; public class GFG { static void numberOfGates ( string s ) { int N = s . Length ; int ans = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == ' . ' s [ i ] == ' + ' s [ i ] == '1' ) { ans ++ ; } } Console . WriteLine ( ans ) ; } public static void Main ( string [ ] args ) { string S = " ( 1 - A ) . B + C " ; numberOfGates ( S ) ; } }


"Minimum number of bits of array elements required to be flipped to make all array elements equal | C # program for the above approach ; Function to count minimum number of bits required to be flipped to make all array elements equal ; Stores the count of unset bits ; Stores the count of set bits ; Traverse the array ; Traverse the bit of arr [ i ] ; If current bit is set ; Increment fre1 [ j ] ; Otherwise ; Increment fre0 [ j ] ; Right shift x by 1 ; Stores the count of total moves ; Traverse the range [ 0 , 32 ] ; Update the value of ans ; Return the minimum number of flips required ; Driver Code"
Solution in C#:

using System ; class GFG { static int makeEqual ( int [ ] arr , int n ) { int [ ] fre0 = new int [ 33 ] ; int [ ] fre1 = new int [ 33 ] ; for ( int i = 0 ; i < n ; i ++ ) { int x = arr [ i ] ; for ( int j = 0 ; j < 33 ; j ++ ) { if ( ( x & 1 ) != 0 ) { fre1 [ j ] += 1 ; } else { fre0 [ j ] += 1 ; } x = x >> 1 ; } } int ans = 0 ; for ( int i = 0 ; i < 33 ; i ++ ) { ans += Math . Min ( fre0 [ i ] , fre1 [ i ] ) ; } return ans ; } public static void Main ( ) { int [ ] arr = { 3 , 5 } ; int N = arr . Length ; Console . WriteLine ( makeEqual ( arr , N ) ) ; } }


"Minimum number of bits required to be flipped such that Bitwise OR of A and B is equal to C | C # program for the above approach ; Function to count the number of bit flips required on A and B such that Bitwise OR of A and B is C ; Stores the count of flipped bit ; Iterate over the range [ 0 , 32 ] ; Check if i - th bit of A is set ; Check if i - th bit of B is set or not ; Check if i - th bit of C is set or not ; If i - th bit of C is unset ; Check if i - th bit of A is set or not ; Check if i - th bit of B is set or not ; Check if i - th bit of C is set or not ; Check if i - th bit of both A and B is set ; Return the count of bits flipped ; Driver Code"
Solution in C#:

using System ; class GFG { static int minimumFlips ( int A , int B , int C ) { int res = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) { int x = 0 , y = 0 , z = 0 ; if ( ( A & ( 1 << i ) ) != 0 ) { x = 1 ; } if ( ( B & ( 1 << i ) ) != 0 ) { y = 1 ; } if ( ( C & ( 1 << i ) ) != 0 ) { z = 1 ; } if ( z == 0 ) { if ( x == 1 ) { res ++ ; } if ( y == 1 ) { res ++ ; } } if ( z == 1 ) { if ( x == 0 && y == 0 ) { res ++ ; } } } return res ; } public static void Main ( string [ ] args ) { int A = 2 , B = 6 , C = 5 ; Console . WriteLine ( minimumFlips ( A , B , C ) ) ; } }


"Minimum number of blocks required to form Hollow Rectangular Prism | C # Implementation to find the minimum no of blocks required to form hollow rectangular prism ; Function to display output ; Function to return minimum no of layers required to form the hollow prism ; Function to calculate no of blocks required for each layer ; No of blocks required for each row ; Check for no of layers is minimum ; Driver function ; Length , width , height of each block ; Side of one wall ; height of each wall"
Solution in C#:

using System ; class GFG { static void disp ( int row_no , int block ) { Console . Write ( row_no * block ) ; } static int row ( int ht , int h ) { return ht / h ; } static void calculate ( int l , int w , int h , int a , int ht ) { int no_block = ( 4 * a ) / l ; int row_no ; if ( h < w ) row_no = row ( ht , w ) ; else row_no = row ( ht , h ) ; disp ( row_no , no_block ) ; } public static void Main ( String [ ] args ) { int l = 50 , w = 20 , h = 35 ; int a = 700 ; int ht = 140 ; calculate ( l , w , h , a , ht ) ; } }


"Minimum number of bombs | C # program to find number of bombings required to kill all aliens . ; function to print where to shoot ; no . of bombs required ; bomb all the even positions ; bomb all the odd positions ; bomb all the even positions again ; Driver code"
Solution in C#:

using System ; class GFG { static void print ( int n ) { Console . WriteLine ( n + n / 2 ) ; for ( int i = 2 ; i <= n ; i += 2 ) Console . Write ( i + " ▁ " ) ; for ( int i = 1 ; i <= n ; i += 2 ) Console . Write ( i + " ▁ " ) ; for ( int i = 2 ; i <= n ; i += 2 ) Console . Write ( i + " ▁ " ) ; } public static void Main ( ) { int n = 3 ; print ( n ) ; } }


"Minimum number of bricks that can be intersected | C # program for the above approach ; Function to find a line across a wall such that it intersects minimum number of bricks ; Declare a hashmap ; Store the maximum number of brick ending a point on x - axis ; Iterate over all the rows ; Initialize width as 0 ; Iterate over individual bricks ; Add the width of the current brick to the total width ; Increment number of bricks ending at this width position ; Update the variable , res ; Print the answer ; Driver Code ; Given Input ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void leastBricks ( List < List < int > > wall ) { Dictionary < int , int > map = new Dictionary < int , int > ( ) ; int res = 0 ; foreach ( List < int > subList in wall ) { int width = 0 ; for ( int i = 0 ; i < subList . Count - 1 ; i ++ ) { width += subList [ i ] ; if ( map . ContainsKey ( width ) ) map [ width ] ++ ; else map . Add ( width , 1 ) ; res = Math . Max ( res , map [ width ] ) ; } } Console . Write ( wall . Count - res ) ; } public static void Main ( ) { List < List < int > > myList = new List < List < int > > ( ) ; myList . Add ( new List < int > { 1 , 2 , 2 , 1 } ) ; myList . Add ( new List < int > { 3 , 1 , 2 } ) ; myList . Add ( new List < int > { 1 , 3 , 2 } ) ; myList . Add ( new List < int > { 2 , 4 } ) ; myList . Add ( new List < int > { 3 , 1 , 2 } ) ; myList . Add ( new List < int > { 1 , 3 , 1 , 1 } ) ; leastBricks ( myList ) ; } }


"Minimum number of candies required to distribute among children based on given conditions | Function to count the minimum number of candies that is to be distributed ; Stores total count of candies ; Stores the amount of candies allocated to a student ; If the value of N is 1 ; Initialize with 1 all array element ; Traverse the array arr [ ] ; If arr [ i + 1 ] is greater than arr [ i ] and ans [ i + 1 ] is at most ans [ i ] ; Assign ans [ i ] + 1 to ans [ i + 1 ] ; Iterate until i is atleast 0 ; If arr [ i ] is greater than arr [ i + 1 ] and ans [ i ] is at most ans [ i + 1 ] ; Assign ans [ i + 1 ] + 1 to ans [ i ] ; If arr [ i ] is equals arr [ i + 1 ] and ans [ i ] is less than ans [ i + 1 ] ; Assign ans [ i + 1 ] + 1 to ans [ i ] ; Increment the sum by ans [ i ] ; Return the resultant sum ; Driver Code"
Solution in C#:

using System ; public class GFG { static int countCandies ( int [ ] arr , int n ) { int sum = 0 ; int [ ] ans = new int [ n ] ; if ( n == 1 ) { return 1 ; } for ( int i = 0 ; i < n ; i ++ ) ans [ i ] = 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i + 1 ] > arr [ i ] && ans [ i + 1 ] <= ans [ i ] ) { ans [ i + 1 ] = ans [ i ] + 1 ; } } for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] > arr [ i + 1 ] && ans [ i ] <= ans [ i + 1 ] ) { ans [ i ] = ans [ i + 1 ] + 1 ; } else if ( arr [ i ] == arr [ i + 1 ] && ans [ i ] < ans [ i + 1 ] ) { ans [ i ] = ans [ i + 1 ] + 1 ; } sum += ans [ i ] ; } sum += ans [ n - 1 ] ; return sum ; } static public void Main ( ) { int [ ] arr = { 1 , 0 , 2 } ; int N = arr . Length ; Console . WriteLine ( countCandies ( arr , N ) ) ; } }


"Minimum number of chairs required to ensure that every worker is seated at any instant | C # program to implement the above approach ; Function to find the minimum number of chairs required to ensure that every worker is seated at any time ; Stores the number of chairs required ; Pointer to iterate ; Stores minimum number of chairs required ; Iterate over every character ; If character is ' E ' ; Increase the count ; Otherwise ; Update maximum value of count obtained at any given time ; Return mini ; Driver code ; Given String ; Function call to find the minimum number of chairs"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int findMinimumChairs ( string s ) { int count = 0 ; int i = 0 ; int mini = Int32 . MinValue ; while ( i < s . Length ) { if ( s [ i ] == ' E ' ) count ++ ; else count -- ; mini = Math . Max ( count , mini ) ; i ++ ; } return mini ; } public static void Main ( ) { string s = " EELEE " ; Console . WriteLine ( findMinimumChairs ( s ) ) ; } }


"Minimum number of changes such that elements are first Negative and then Positive | C # implementation of the approach ; Function to return the count of minimum operations required ; To store the count of negative integers on the right of the current index ( inclusive ) ; Find the count of negative integers on the right ; If current element is negative ; To store the count of positive elements ; Find the positive integers on the left ; If current element is positive ; Update the answer ; Return the required answer ; Driver code"
Solution in C#:

using System ; class GFG { static int Minimum_Operations ( int [ ] a , int n ) { int [ ] np = new int [ n + 1 ] ; np [ n ] = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { np [ i ] = np [ i + 1 ] ; if ( a [ i ] <= 0 ) np [ i ] ++ ; } int pos = 0 ; int ans = n ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( a [ i ] >= 0 ) pos ++ ; ans = Math . Min ( ans , pos + np [ i + 1 ] ) ; } return ans ; } static void Main ( ) { int [ ] a = { - 1 , 0 , 1 , 2 } ; int n = a . Length ; Console . WriteLine ( Minimum_Operations ( a , n ) ) ; } }


"Minimum number of characters required to be added to a String such that all lowercase alphabets occurs as a subsequence in increasing order | C # program for the above approach ; Function to find the LCS of strings S and string T ; Base Case ; Already Calculated State ; If the characters are the same ; Otherwise ; Function to find the minimum number of characters that needs to be appended in the string to get all lowercase alphabets as a subsequences ; String containing all the characters ; Stores the result of overlapping subproblems ; Return the minimum characters required ; Driver Code"
Solution in C#:

using System ; public class GFG { static int LCS ( String S , int N , String T , int M , int [ , ] dp ) { if ( N == 0 M == 0 ) return 0 ; if ( dp [ N , M ] != 0 ) return dp [ N , M ] ; if ( S [ N - 1 ] == T [ M - 1 ] ) { return dp [ N , M ] = 1 + LCS ( S , N - 1 , T , M - 1 , dp ) ; } return dp [ N , M ] = Math . Max ( LCS ( S , N - 1 , T , M , dp ) , LCS ( S , N , T , M - 1 , dp ) ) ; } static int minimumchar ( String S ) { String T = " abcdefghijklmnopqrstuvwxyz " ; int N = S . Length , M = T . Length ; int [ , ] dp = new int [ N + 1 , M + 1 ] ; return ( 26 - LCS ( S , N , T , M , dp ) ) ; } public static void Main ( String [ ] args ) { String S = " abcdadc " ; Console . WriteLine ( minimumchar ( S ) ) ; } }


"Minimum number of characters required to be removed such that every character occurs same number of times | C # program for the above approach ; Function to find minimum number of character removals required to make frequency of all distinct characters the same ; Stores the frequency of each character ; Traverse the string ; Stores the frequency of each charachter in sorted order ; Traverse the Map ; Insert the frequency in multiset ; Stores the count of elements required to be removed ; Stores the size of multiset ; Traverse the multiset ; Update the ans ; Increment i by 1 ; Return ; Driver Code ; Input"
Solution in C#:

using System . Collections . Generic ; using System ; class GFG { static int minimumDeletion ( string s , int n ) { Dictionary < char , int > countMap = new Dictionary < char , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( countMap . ContainsKey ( s [ i ] ) == true ) { countMap [ s [ i ] ] += 1 ; } else { countMap [ s [ i ] ] = 1 ; } } List < int > countMultiset = new List < int > ( ) ; foreach ( var values in countMap . Values ) { countMultiset . Add ( values ) ; } countMultiset . Sort ( ) ; int ans = 100000000 ; int index = 0 ; int m = countMultiset . Count ; foreach ( var j in countMultiset ) { ans = Math . Min ( ans , n - ( m - index ) * j ) ; index ++ ; } return ans ; } public static void Main ( String [ ] args ) { string S = " geeksforgeeks " ; int N = S . Length ; Console . WriteLine ( minimumDeletion ( S , N ) ) ; } }


"Minimum number of characters to be removed to make a binary string alternate | C # program to find minimum number of characters to be removed to make a string alternate . ; Returns count of minimum characters to be removed to make s alternate . ; if two alternating characters of string are same ; result ++ ; then need to delete a character ; Driver code"
Solution in C#:

using System ; public class GFG { static int countToMake0lternate ( string s ) { int result = 0 ; for ( int i = 0 ; i < ( s . Length - 1 ) ; i ++ ) if ( s [ i ] == s [ i + 1 ] ) return result ; } static public void Main ( ) { Console . WriteLine ( countToMake0lternate ( "000111" ) ) ; Console . WriteLine ( countToMake0lternate ( "11111" ) ) ; Console . WriteLine ( countToMake0lternate ( "01010101" ) ) ; } }


"Minimum number of coins having value equal to powers of 2 required to obtain N | C # program for above approach ; Function to count of setbit in N ; Stores count of setbit in N ; Iterate over the range [ 0 , 31 ] ; If current bit is set ; Update result ; Driver Code"
Solution in C#:

using System ; class GFG { static void count_setbit ( int N ) { int result = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) { if ( ( ( 1 << i ) & N ) > 0 ) { result ++ ; } } Console . WriteLine ( result ) ; } static void Main ( ) { int N = 43 ; count_setbit ( N ) ; } }


"Minimum number of coins needed to remove all the elements of the array based on given rules | C # implementation for the above approach ; Function to calculate minimum number of coins needed ; Consider the first element separately , add 1 to the total if it 's of type 1 ; Iterate from the second element ; If the current element is of type 2 then any Player can remove the element ; Second pointer to reach end of type 1 elements ; Increment j until arr [ j ] is equal to 1 and j is not out of bounds ; Number of type 1 elements in a continious chunk ; From next iteration i pointer will start from index of j ; Return the minimum count of coins ; Driver Code"
Solution in C#:

using System ; public class GFG { static int minimumcoins ( int [ ] arr , int N ) { int coins = 0 ; int j = 0 ; if ( arr [ 0 ] == 1 ) coins ++ ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] == 2 ) continue ; j = i ; while ( j < N && arr [ j ] == 1 ) { j ++ ; } int x = ( j - i ) ; coins += x / 3 ; i = j - 1 ; } return coins ; } public static void Main ( String [ ] args ) { int N = 8 ; int [ ] arr = { 1 , 2 , 1 , 1 , 2 , 1 , 1 , 1 } ; Console . WriteLine ( minimumcoins ( arr , N ) ) ; } }


"Minimum number of coins that can generate all the values in the given range | C # implementation of the above approach ; Function to return the count of minimum coins required ; To store the required sequence ; Creating list of the sum of all previous bit values including that bit value ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int findCount ( int N ) { List < int > list = new List < int > ( ) ; int sum = 0 ; int i ; for ( i = 0 ; i < 20 ; i ++ ) { sum += ( int ) Math . Pow ( 2 , i ) ; list . Add ( sum ) ; } for ( i = 0 ; i < 20 ; i ++ ) { if ( ( int ) list [ i ] >= N ) return ( i + 1 ) ; } return 0 ; } public static void Main ( String [ ] args ) { int N = 10 ; Console . WriteLine ( findCount ( N ) ) ; } }


"Minimum number of coins that can generate all the values in the given range | C # program to find minimum number of coins ; Function to find minimum number of coins ; Driver code"
Solution in C#:

using System ; class GFG { static int findCount ( int n ) { return ( int ) ( Math . Log ( n ) / Math . Log ( 2 ) ) + 1 ; } public static void Main ( ) { int N = 10 ; Console . Write ( findCount ( N ) ) ; } }


"Minimum number of colors required to color a Circular Array | C # implementation of above approach ; Function that finds minimum number of colors required ; To check that if all the elements are same or not ; To check if only one adjacent exist ; Traverse the array ; If adjacent elements found different means all are not same ; If two adjacent elements found to be same then make one_adjacent_same true ; If all elements are same then print 1 ; If total number of elements are even or there exist two adjacent elements that are same then print 2 ; Else 3 type of colors are required ; Driver Code ; Function call"
Solution in C#:

 using System ; class GFG { static void colorRequired ( int [ ] arr , int n ) { bool all_same = true ; bool one_adjacent_same = false ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] != arr [ i + 1 ] ) { all_same = false ; } if ( arr [ i ] == arr [ i + 1 ] ) { one_adjacent_same = true ; } } if ( all_same == true ) { Console . Write ( 1 + "
 " ) ; return ; } if ( n % 2 == 0 one_adjacent_same == true ) { Console . Write ( 2 + "
 " ) ; return ; } Console . Write ( 3 + "
 " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 1 , 1 , 2 } ; int n = arr . Length ; colorRequired ( arr , n ) ; } }


"Minimum number of consecutive sequences that can be formed in an array | C # program find the minimum number of consecutive sequences in an array ; Driver program ; function call to print required answer"
Solution in C#:

using System ; class GFG { static int countSequences ( int [ ] arr , int n ) { int count = 1 ; Array . Sort ( arr ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i ] + 1 != arr [ i + 1 ] ) count ++ ; return count ; } static public void Main ( String [ ] args ) { int [ ] arr = { 1 , 7 , 3 , 5 , 10 } ; int n = arr . Length ; Console . WriteLine ( countSequences ( arr , n ) ) ; } }


"Minimum number of cubes whose sum equals to given number N | C # implementation of the approach ; Function to return the minimum number of cubes whose sum is k ; If k is less than the 2 ^ 3 ; Initialize with the maximum number of cubes required ; Driver code"
Solution in C#:

using System ; class GFG { static int MinOfCubed ( int k ) { if ( k < 8 ) return k ; int res = k ; for ( int i = 1 ; i <= k ; i ++ ) { if ( ( i * i * i ) > k ) return res ; res = Math . Min ( res , MinOfCubed ( k - ( i * i * i ) ) + 1 ) ; } return res ; } static public void Main ( ) { int num = 15 ; Console . WriteLine ( MinOfCubed ( num ) ) ; } }


"Minimum number of cubes whose sum equals to given number N | C # implementation of the approach ; Function to return the minimum number of cubes whose sum is k ; While current perfect cube is less than current element ; If i is a perfect cube ; i = ( i - 1 ) + 1 ^ 3 ; Next perfect cube ; Re - initialization for next element ; Driver code"
Solution in C#:

using System ; class GFG { static int MinOfCubedDP ( int k ) { int [ ] DP = new int [ k + 1 ] ; int j = 1 , t = 1 ; DP [ 0 ] = 0 ; for ( int i = 1 ; i <= k ; i ++ ) { DP [ i ] = int . MaxValue ; while ( j <= i ) { if ( j == i ) DP [ i ] = 1 ; else if ( DP [ i ] > DP [ i - j ] ) DP [ i ] = DP [ i - j ] + 1 ; t ++ ; j = t * t * t ; } t = j = 1 ; } return DP [ k ] ; } public static void Main ( ) { int num = 15 ; Console . WriteLine ( MinOfCubedDP ( num ) ) ; } }


"Minimum number of cuts required to make circle segments equal sized | C # program to find the minimum number of additional cuts required to make circle segments equal sized ; Recursive function to return gcd of two nos ; Function to find the minimum number of additional cuts required to make circle segments are equal sized ; Sort the array ; Initial gcd value ; Including the last segment ; Driver Code"
Solution in C#:

using System ; class GFG { static int findgcd ( int a , int b ) { if ( b == 0 ) return a ; return findgcd ( b , a % b ) ; } static int minimumCuts ( int [ ] a , int n ) { Array . Sort ( a ) ; int gcd = a [ 1 ] - a [ 0 ] ; int s = gcd ; for ( int i = 2 ; i < n ; i ++ ) { gcd = findgcd ( gcd , a [ i ] - a [ i - 1 ] ) ; s += a [ i ] - a [ i - 1 ] ; } if ( 360 - s > 0 ) gcd = findgcd ( gcd , 360 - s ) ; return ( 360 / gcd ) - n ; } static void Main ( ) { int [ ] arr = new int [ ] { 30 , 60 , 180 } ; int n = arr . Length ; Console . WriteLine ( minimumCuts ( arr , n ) ) ; } }


"Minimum number of cuts required to pay salary from N length Gold Bar | C # Implementation to find the minimum number of cuts to pay the worker . ; Function to find the minimum number of cuts to pay the worker . ; Nearest int to the Log value of the number N ; Driver code ; Cuts Required in the Length of 15"
Solution in C#:

 using System ; class GFG { static int pay ( int n ) { int cuts = ( int ) ( Math . Log ( n ) / Math . Log ( 2 ) ) ; return cuts ; } public static void Main ( String [ ] args ) { int n = 5 ; int cuts = pay ( n ) ; Console . Write ( cuts + "
 " ) ; n = 15 ; cuts = pay ( n ) ; Console . Write ( cuts ) ; } }


"Minimum number of days to debug all programs | C # program for the above approach ; Function to calculate the minimum work sessions ; Break condition ; All bits are set ; Check if already calculated ; Store the answer ; Check if ith bit is set or unset ; Including in current work session ; Including in next work session ; Resultant answer will be minimum of both ; Function to initialize DP array and solve the problem ; Initialize dp table with - 1 ; Resultant mask ; no . of minimum work sessions is even ; no . of minimum work sessions is odd ; Driver code"
Solution in C#:

using System ; public class GFG { public static int minSessions ( int [ ] codeTime , int [ , ] dp , int ones , int n , int mask , int currTime , int WorkingSessionTime ) { if ( currTime > WorkingSessionTime ) return Int32 . MaxValue ; if ( mask == ones ) return 1 ; if ( dp [ mask , currTime ] != - 1 ) return dp [ mask , currTime ] ; int ans = Int32 . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( mask & ( 1 << i ) ) == 0 ) { int inc = minSessions ( codeTime , dp , ones , n , mask | ( 1 << i ) , currTime + codeTime [ i ] , WorkingSessionTime ) ; int inc_next = 1 + minSessions ( codeTime , dp , ones , n , mask | ( 1 << i ) , codeTime [ i ] , WorkingSessionTime ) ; ans = Math . Min ( ans , Math . Min ( inc , inc_next ) ) ; } } return dp [ mask , currTime ] = ans ; } public static int solve ( int [ ] codeTime , int n , int WorkingSessionTime ) { int [ , ] dp = new int [ ( 1 << 14 ) , 15 ] ; for ( int i = 0 ; i < 1 << 14 ; i ++ ) { for ( int j = 0 ; j < 15 ; j ++ ) { dp [ i , j ] = - 1 ; } } int ones = ( 1 << n ) - 1 ; int ans = minSessions ( codeTime , dp , ones , n , 0 , 0 , WorkingSessionTime ) ; if ( WorkingSessionTime < 6 ) { if ( ans % 2 == 0 ) ans = ans / 2 ; else ans = ( ans / 2 ) + 1 ; } return ans ; } static public void Main ( ) { int [ ] codeTime = { 1 , 2 , 3 , 1 , 1 , 3 } ; int n = codeTime . Length ; int WorkingSessionTime = 4 ; Console . WriteLine ( solve ( codeTime , n , WorkingSessionTime ) ) ; } }


"Minimum number of decrements by 1 required to reduce all elements of a circular array to 0 | C # program for the above approach ; Function to find minimum operation require to make all elements 0 ; Stores the maximum element and its position in the array ; Traverse the array ; Update the maximum element and its index ; Print the minimum number of operations required ; Driver Code"
Solution in C#:

using System ; class GFG { static void minimumOperations ( int [ ] arr , int N ) { int mx = 0 , pos = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] >= mx ) { mx = arr [ i ] ; pos = i ; } } Console . Write ( ( mx - 1 ) * N + pos + 1 ) ; } public static void Main ( ) { int [ ] arr = { 2 , 0 , 2 } ; int N = arr . Length ; minimumOperations ( arr , N ) ; } }