"Minimum sum subsequence such that at least one of every four consecutive elements is picked | C # program to find minimum sum subsequence of an array such that one of every four consecutive elements is picked . ; Returns sum of minimum sum subsequence such that one of every four consecutive elements is picked from arr [ ] . ; dp [ i ] is going to store minimum sum subsequence of arr [ 0. . i ] such that arr [ i ] is part of the solution . Note that this may not be the best solution for subarray arr [ 0. . i ] ; If there is single value , we get the minimum sum equal to arr [ 0 ] ; If there are two values , we get the minimum sum equal to the minimum of two values ; If there are three values , return minimum of the three elements of array ; If there are four values , return minimum of the four elements of array ; Return the minimum of last 4 index ; Driver code"
Solution in C#:

using System ; class GFG { static int minSum ( int [ ] arr , int n ) { int [ ] dp = new int [ n ] ; if ( n == 1 ) return arr [ 0 ] ; if ( n == 2 ) return Math . Min ( arr [ 0 ] , arr [ 1 ] ) ; if ( n == 3 ) return Math . Min ( arr [ 0 ] , Math . Min ( arr [ 1 ] , arr [ 2 ] ) ) ; if ( n == 4 ) return Math . Min ( Math . Min ( arr [ 0 ] , arr [ 1 ] ) , Math . Min ( arr [ 2 ] , arr [ 3 ] ) ) ; dp [ 0 ] = arr [ 0 ] ; dp [ 1 ] = arr [ 1 ] ; dp [ 2 ] = arr [ 2 ] ; dp [ 3 ] = arr [ 3 ] ; for ( int i = 4 ; i < n ; i ++ ) dp [ i ] = arr [ i ] + Math . Min ( Math . Min ( dp [ i - 1 ] , dp [ i - 2 ] ) , Math . Min ( dp [ i - 3 ] , dp [ i - 4 ] ) ) ; return Math . Min ( Math . Min ( dp [ n - 1 ] , dp [ n - 2 ] ) , Math . Min ( dp [ n - 4 ] , dp [ n - 3 ] ) ) ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 3 , 4 , 5 , 6 , 1 } ; int n = arr . Length ; Console . WriteLine ( minSum ( arr , n ) ) ; } }


"Minimum swaps needed to convert given Binary Matrix A to Binary Matrix B | C # program for the above approach ; Function to count the minimum number of swaps required to convert matrix A to matrix B ; Stores number of cells such that matrix A contains 0 and matrix B contains 1 ; Stores number of cells such that matrix A contains 1 and matrix B contains 0 ; Iterate over the range [ 0 , N - 1 ] ; Iterate over the range [ 0 , M - 1 ] ; If A [ i ] [ j ] = 1 and B [ i ] [ j ] = 0 ; If A [ i ] [ j ] = 0 and B [ i ] [ j ] = 1 ; If count01 is equal to count10 ; Otherwise , ; Driver code"
Solution in C#:

using System ; class GFG { public static int minSwaps ( int N , int M , int [ , ] A , int [ , ] B ) { int count01 = 0 ; int count10 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( A [ i , j ] != B [ i , j ] ) { if ( A [ i , j ] == 1 ) count10 ++ ; else count01 ++ ; } } } if ( count01 == count10 ) return count01 ; else return - 1 ; } public static void Main ( String [ ] args ) { int [ , ] A = { { 1 , 1 , 0 } , { 0 , 0 , 1 } , { 0 , 1 , 0 } } ; int [ , ] B = { { 0 , 0 , 1 } , { 0 , 1 , 0 } , { 1 , 1 , 0 } } ; int N = A . GetLength ( 0 ) ; int M = 3 ; Console . Write ( minSwaps ( N , M , A , B ) ) ; } }


"Minimum swaps of same | C # program to implement the above approach ; Function to count the minimum swaps of same - indexed elements from arrays arr1 [ ] and arr2 [ ] required to make the sum of both the arrays even ; Store the sum of elements of the array arr1 and arr2 respectively ; Store the array sum of both the arrays ; If both sumArr1 and sumArr2 are even , print 0 and return ; If both sumArr1 and sumArr2 are odd and check for a pair with sum odd sum ; Stores if a pair with odd sum exists or not ; Traverse the array ; If a pair exists with odd sum , set flag = 1 ; Print the answer and return ; For all other cases , print - 1 ; Driver code ; Function Call"
Solution in C#:

using System ; class GFG { static void minimumSwaps ( int [ ] arr1 , int [ ] arr2 , int n ) { int sumArr1 = 0 , sumArr2 = 0 ; for ( int i = 0 ; i < n ; ++ i ) { sumArr1 += arr1 [ i ] ; sumArr2 += arr2 [ i ] ; } if ( sumArr1 % 2 == 0 && sumArr2 % 2 == 0 ) { Console . Write ( 0 ) ; return ; } if ( sumArr1 % 2 != 0 && sumArr2 % 2 != 0 ) { int flag = - 1 ; for ( int i = 0 ; i < n ; ++ i ) { if ( ( arr1 [ i ] + arr2 [ i ] ) % 2 == 1 ) { flag = 1 ; break ; } } Console . Write ( flag ) ; return ; } Console . Write ( - 1 ) ; } public static void Main ( ) { int [ ] arr1 = { 11 , 14 , 20 , 2 } ; int [ ] arr2 = { 5 , 9 , 6 , 3 } ; int N = arr1 . Length ; minimumSwaps ( arr1 , arr2 , N ) ; } }


"Minimum swaps required to bring all elements less than or equal to k together | C # program to find minimum swaps required to club all elements less than or equals to k together ; Utility function to find minimum swaps required to club all elements less than or equals to k together ; Find count of elements which are less than equals to k ; Find unwanted elements in current window of size ' count ' ; Initialize answer with ' bad ' value of current window ; Decrement count of previous window ; Increment count of current window ; Update ans if count of ' bad ' is less in current window ; Driver code"
Solution in C#:

using System ; class GFG { static int minSwap ( int [ ] arr , int n , int k ) { int count = 0 ; for ( int i = 0 ; i < n ; ++ i ) if ( arr [ i ] <= k ) ++ count ; int bad = 0 ; for ( int i = 0 ; i < count ; ++ i ) if ( arr [ i ] > k ) ++ bad ; int ans = bad ; for ( int i = 0 , j = count ; j < n ; ++ i , ++ j ) { if ( arr [ i ] > k ) -- bad ; if ( arr [ j ] > k ) ++ bad ; ans = Math . Min ( ans , bad ) ; } return ans ; } public static void Main ( ) { int [ ] arr = { 2 , 1 , 5 , 6 , 3 } ; int n = arr . Length ; int k = 3 ; Console . WriteLine ( minSwap ( arr , n , k ) ) ; int [ ] arr1 = { 2 , 7 , 9 , 5 , 8 , 7 , 4 } ; n = arr1 . Length ; k = 5 ; Console . WriteLine ( minSwap ( arr1 , n , k ) ) ; } }


"Minimum swaps required to make a binary string alternating | C # implementation of the above approach ; function to count minimum swaps required to make binary String alternating ; Stores total number of ones ; Stores total number of zeroes ; Checking impossible condition ; Odd length String ; Number of even positions ; Stores number of zeroes and ones at even positions ; Even length String ; Stores number of ones at odd and even position respectively ; Driver code"
Solution in C#:

using System ; class GFG { static int countMinSwaps ( string s ) { int N = s . Length ; int one = 0 ; int zero = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( s [ i ] == '1' ) one ++ ; else zero ++ ; } if ( one > zero + 1 zero > one + 1 ) return - 1 ; if ( N % 2 == 1 ) { int num = ( N + 1 ) / 2 ; int one_even = 0 , zero_even = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { if ( s [ i ] == '1' ) one_even ++ ; else zero_even ++ ; } } if ( one > zero ) return num - one_even ; else return num - zero_even ; } else { int one_odd = 0 , one_even = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( s [ i ] == '1' ) { if ( i % 2 == 1 ) one_odd ++ ; else one_even ++ ; } } return Math . Min ( N / 2 - one_odd , N / 2 - one_even ) ; } } public static void Main ( String [ ] args ) { string s = "111000" ; Console . Write ( countMinSwaps ( s ) ) ; } }


"Minimum swaps required to make a binary string alternating | C # implementation of the approach ; returns the minimum number of swaps of a binary string passed as the argument to make it alternating ; counts number of zeroes at odd and even positions ; counts number of ones at odd and even positions ; alternating string starts with 0 ; alternating string starts with 1 ; calculates the minimum number of swaps ; Driver code"
Solution in C#:

using System ; public class GFG { public static int countMinSwaps ( string st ) { int min_swaps = 0 ; int odd_0 = 0 , even_0 = 0 ; int odd_1 = 0 , even_1 = 0 ; int n = st . Length ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { if ( st [ i ] == '1' ) { even_1 ++ ; } else { even_0 ++ ; } } else { if ( st [ i ] == '1' ) { odd_1 ++ ; } else { odd_0 ++ ; } } } int cnt_swaps_1 = Math . Min ( even_0 , odd_1 ) ; int cnt_swaps_2 = Math . Min ( even_1 , odd_0 ) ; return Math . Min ( cnt_swaps_1 , cnt_swaps_2 ) ; } public static void Main ( string [ ] args ) { string st = "000111" ; Console . WriteLine ( countMinSwaps ( st ) ) ; } }


"Minimum swaps required to make a binary string divisible by 2 ^ k | C # implementation of the approach ; Function to return the minimum swaps required ; To store the final answer ; To store the count of one and zero ; Loop from end of the string ; If s [ i ] = 1 ; If s [ i ] = 0 ; If c_zero = k ; If the result can 't  be achieved ; Return the final answer ; Driver code"
Solution in C#:

using System ; class GFG { static int findMinSwaps ( string s , int k ) { int ans = 0 ; int c_one = 0 , c_zero = 0 ; for ( int i = s . Length - 1 ; i >= 0 ; i -- ) { if ( s [ i ] == '1' ) c_one ++ ; if ( s [ i ] == '0' ) { c_zero ++ ; ans += c_one ; } if ( c_zero == k ) break ; } if ( c_zero < k ) return - 1 ; return ans ; } public static void Main ( ) { string s = "100111" ; int k = 2 ; Console . WriteLine ( findMinSwaps ( s , k ) ) ; } }


"Minimum swaps so that binary search can be applied | C # program to find Minimum number of swaps to get the position of the element if we provide an unsorted array to binary search . ; Function to find minimum swaps . ; Here we are getting number of smaller and greater elements of k . ; Here we are calculating the actual position of k in the array . ; Implementing binary search as per the above - discussed cases . ; If we find the k . ; If we need minimum element swap . ; Else the element is at the right position . ; If we need maximum element swap . ; Else element is at the right position ; Calculating the required swaps . ; If it is impossible . ; Driver function"
Solution in C#:

using System ; public class GFG { static int findMinimumSwaps ( int [ ] arr , int n , int k ) { int pos = 0 , num_min , num_max , need_minimum , need_maximum , swaps ; num_min = num_max = need_minimum = 0 ; need_maximum = swaps = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < k ) { num_min ++ ; } else if ( arr [ i ] > k ) { num_max ++ ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == k ) { pos = i ; break ; } } int left , right , mid ; left = 0 ; right = n - 1 ; while ( left <= right ) { mid = ( left + right ) / 2 ; if ( arr [ mid ] == k ) { break ; } else if ( arr [ mid ] > k ) { if ( pos > mid ) { need_minimum ++ ; } else { num_min -- ; } left = mid + 1 ; } else { if ( pos < mid ) { need_maximum ++ ; } else { num_max -- ; } right = mid - 1 ; } } if ( need_minimum > need_maximum ) { swaps = swaps + need_maximum ; num_min = num_min - need_maximum ; need_minimum = need_minimum - need_maximum ; need_maximum = 0 ; } else { swaps = swaps + need_minimum ; num_max = num_max - need_minimum ; need_maximum = need_maximum - need_minimum ; need_minimum = 0 ; } if ( need_maximum > num_max need_minimum > num_min ) { return - 1 ; } else { return ( swaps + need_maximum + need_minimum ) ; } } public static void Main ( ) { int [ ] arr = { 3 , 10 , 6 , 7 , 2 , 5 , 4 } ; int k = 4 ; int n = arr . Length ; Console . WriteLine ( findMinimumSwaps ( arr , n , k ) ) ; } }


"Minimum swaps to balance the given brackets at any index | C # implementation for the above approach ; Function to balance the given bracket by swap ; To count the number of uunbalanced pairs ; if there is an opening bracket and we encounter closing bracket then it will decrement the count of unbalanced bracket . ; else it will increment unbalanced pair count ; Driver code"
Solution in C#:

using System ; class GFG { static int BalancedStringBySwapping ( String s ) { int unbalancedPair = 0 ; for ( int i = 0 ; i < s . Length ; ++ i ) { if ( unbalancedPair > 0 && s [ i ] == ' ] ' ) { -- unbalancedPair ; } else if ( s [ i ] == ' [ ' ) { ++ unbalancedPair ; } } return ( unbalancedPair + 1 ) / 2 ; } public static void Main ( String [ ] args ) { String s = " ] ] ] [ [ [ " ; Console . Write ( BalancedStringBySwapping ( s ) ) ; } }


"Minimum swaps to group similar characters side by side ? | checks whether a String has similar characters side by side ; If similar chars side by side , continue ; If we have found a char equal to current char and does not exist side to it , return false ; counts min swap operations to convert a String that has similar characters side by side ; Base case ; considering swapping of i and l chars ; Backtrack ; not considering swapping of i and l chars ; taking Math . min of above two ; Utility swap ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static bool sameJavaharAdj ( char [ ] str ) { int n = str . Length , i ; HashSet < char > st = new HashSet < char > ( ) ; st . Add ( str [ 0 ] ) ; for ( i = 1 ; i < n ; i ++ ) { if ( str [ i ] == str [ i - 1 ] ) { continue ; } if ( st . Contains ( str [ i ] ) & ! st . Equals ( str [ i ] ) ) { return false ; } st . Add ( str [ i ] ) ; } return true ; } static int minSwaps ( char [ ] str , int l , int r , int cnt , int minm ) { if ( l == r ) { if ( sameJavaharAdj ( str ) ) { return cnt ; } else { return int . MaxValue ; } } for ( int i = l + 1 ; i <= r ; i ++ ) { swap ( str , i , l ) ; cnt ++ ; int x = minSwaps ( str , l + 1 , r , cnt , minm ) ; swap ( str , i , l ) ; cnt -- ; int y = minSwaps ( str , l + 1 , r , cnt , minm ) ; minm = Math . Min ( minm , Math . Min ( x , y ) ) ; } return minm ; } static void swap ( char [ ] arr , int i , int j ) { char temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } public static void Main ( ) { String str = " abbaacb " ; int n = str . Length , cnt = 0 , minm = int . MaxValue ; Console . WriteLine ( minSwaps ( str . ToCharArray ( ) , 0 , n - 1 , cnt , minm ) ) ; ; } }


"Minimum swaps to reach permuted array with at most 2 positions left swaps allowed | C # program to find minimum number of swaps to reach a permutation with at most 2 left swaps allowed for every element ; This funt merges two sorted arrays and returns inversion count in the arrays . ; i is index for left subarray ; j is index for right subarray ; k is index for resultant merged subarray ; Copy the remaining elements of left subarray ( if there are any ) to temp ; Copy the remaining elements of right subarray ( if there are any ) to temp ; Copy back the merged elements to original array ; An auxiliary recursive function that sorts the input array and returns the number of inversions in the array . ; Divide the array into two parts and call _mergeSortAndCountInv ( ) for each of the parts ; Inversion count will be sum of inversions in left - part , right - part and number of inversions in merging ; Merge the two parts ; This function sorts the input array and returns the number of inversions in the array ; method returns minimum number of swaps to reach permuted array ' arr ' ; loop over all elements to check Invalid permutation condition ; if an element is at distance more than 2 from its actual position then it is not possible to reach permuted array just by swapping with 2 positions left elements so returning - 1 ; If permuted array is not Invalid , then number of Inversion in array will be our final answer ; Driver code ; change below example"
Solution in C#:

using System ; class GFG { static int merge ( int [ ] arr , int [ ] temp , int left , int mid , int right ) { int inv_count = 0 ; int i = left ; int j = mid ; int k = left ; while ( ( i <= mid - 1 ) && ( j <= right ) ) { if ( arr [ i ] <= arr [ j ] ) { temp [ k ++ ] = arr [ i ++ ] ; } else { temp [ k ++ ] = arr [ j ++ ] ; inv_count = inv_count + ( mid - i ) ; } } while ( i <= mid - 1 ) { temp [ k ++ ] = arr [ i ++ ] ; } while ( j <= right ) { temp [ k ++ ] = arr [ j ++ ] ; } for ( i = left ; i <= right ; i ++ ) { arr [ i ] = temp [ i ] ; } return inv_count ; } static int _mergeSort ( int [ ] arr , int [ ] temp , int left , int right ) { int mid , inv_count = 0 ; if ( right > left ) { mid = ( right + left ) / 2 ; inv_count = _mergeSort ( arr , temp , left , mid ) ; inv_count += _mergeSort ( arr , temp , mid + 1 , right ) ; inv_count += merge ( arr , temp , left , mid + 1 , right ) ; } return inv_count ; } static int mergeSort ( int [ ] arr , int array_size ) { int [ ] temp = new int [ array_size ] ; return _mergeSort ( arr , temp , 0 , array_size - 1 ) ; } static int minSwapToReachArr ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] - 1 ) - i > 2 ) { return - 1 ; } } int numOfInversion = mergeSort ( arr , N ) ; return numOfInversion ; } static void Main ( ) { int [ ] arr = { 1 , 2 , 5 , 3 , 4 } ; int N = arr . Length ; int res = minSwapToReachArr ( arr , N ) ; if ( res == - 1 ) Console . WriteLine ( " Not ▁ Possible " ) ; else Console . WriteLine ( res ) ; } }


"Minimum time remaining for safety alarm to start | C # program for the above approach ; Function to check if the value of mid as the minimum number of hours satisfies the condition ; Stores the sum of speed ; Iterate over the range [ 0 , N ] ; Find the value of speed ; If the bike is considered to be fast add it in sum ; Return the resultant sum ; Function to find the minimum number of time required ; Stores the range of Binary Search ; Stores the minimum number of time required ; Find the value of mid ; If the mid is the resultant speed required ; Update the ans and high ; Otherwise ; Return the minimum number of hours ; Driver Code"
Solution in C#:

using System ; class GFG { static long check ( long [ ] H , long [ ] A , long mid , long N , long M , long L ) { long sum = 0 ; for ( long i = 0 ; i < N ; i ++ ) { long speed = mid * A [ ( int ) i ] + H [ ( int ) i ] ; if ( speed >= L ) { sum += speed ; } } return sum ; } static long buzzTime ( long N , long M , long L , long [ ] H , long [ ] A ) { long low = 0 , high = 100000000 ; long ans = 0 ; while ( high >= low ) { long mid = low + ( high - low ) / 2 ; if ( check ( H , A , mid , N , M , L ) >= M ) { ans = mid ; high = mid - 1 ; } else low = mid + 1 ; } return ans ; } public static void Main ( String [ ] args ) { long M = 400 , L = 120 ; long [ ] H = { 20 , 50 , 20 } ; long [ ] A = { 20 , 70 , 90 } ; long N = A . Length ; Console . Write ( buzzTime ( N , M , L , H , A ) ) ; } }


"Minimum time required to color all edges of a Tree | C # program for the above approach ; Stores the required answer ; Stores the graph ; Function to add edges ; Function to calculate the minimum time required to color all the edges of a tree ; Starting from time = 0 , for all the child edges ; If the edge is not visited yet . ; Time of coloring of the current edge ; If the parent edge has been colored at the same time ; Update the maximum time ; Recursively call the function to its child node ; Driver code ; Function call ; Finally , print the answer"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int ans = 0 ; static List < List < int > > edges = new List < List < int > > ( ) ; static void Add_edge ( int u , int v ) { edges [ u ] . Add ( v ) ; edges [ v ] . Add ( u ) ; } static void minTimeToColor ( int node , int parent , int arrival_time ) { int current_time = 0 ; for ( int x = 0 ; x < edges [ node ] . Count ; x ++ ) { if ( edges [ node ] [ x ] != parent ) { ++ current_time ; if ( current_time == arrival_time ) ++ current_time ; ans = Math . Max ( ans , current_time ) ; minTimeToColor ( edges [ node ] [ x ] , node , current_time ) ; } } } static void Main ( ) { for ( int i = 0 ; i < 100000 ; i ++ ) { edges . Add ( new List < int > ( ) ) ; } int [ , ] A = { { 1 , 2 } , { 2 , 3 } , { 3 , 4 } } ; for ( int i = 0 ; i < 3 ; i ++ ) { Add_edge ( A [ i , 0 ] , A [ i , 1 ] ) ; } minTimeToColor ( 1 , - 1 , 0 ) ; Console . WriteLine ( ans ) ; } }


"Minimum time required to complete a work by N persons together | C # implementation of above approach ; Function to calculate the time ; Driver Code"
Solution in C#:

using System ; class GFG { static double calTime ( double [ ] arr , int n ) { double work = 0 ; for ( int i = 0 ; i < n ; i ++ ) work += 1 / arr [ i ] ; return Math . Round ( 1 / work , 5 ) ; } public static void Main ( ) { double [ ] arr = { 6.0 , 3.0 , 4.0 } ; int n = arr . Length ; Console . Write ( calTime ( arr , n ) + " ▁ Hours " ) ; } }


"Minimum time required to complete all tasks with alteration of their order allowed | C # program for the above approach ; Function to find the minimum time required to complete the tasks if the order of tasks can be changed ; If there is no task , print 0 ; Store the maximum occurring character and its frequency ; Stores the frequency of each character ; Traverse the string S ; Increment the frequency of the current character ; Update maxfreq and maxchar ; Store the number of empty slots ; Traverse the hashmap , um ; Fill the empty slots ; Store the required result ; Print the result ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void findMinimumTime ( string S , int N , int K ) { if ( N == 0 ) { Console . Write ( 0 ) ; return ; } int maxfreq = Int32 . MinValue ; char maxchar = ' ▁ ' ; Dictionary < char , int > um = new Dictionary < char , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( um . ContainsKey ( S [ i ] ) ) um [ S [ i ] ] ++ ; else um . Add ( S [ i ] , 1 ) ; if ( um . ContainsKey ( S [ i ] ) && um [ S [ i ] ] > maxfreq ) { maxfreq = um [ S [ i ] ] ; maxchar = S [ i ] ; } } int emptySlots = ( maxfreq - 1 ) * K ; foreach ( KeyValuePair < char , int > kvp in um ) { if ( kvp . Key == maxchar ) continue ; emptySlots -= Math . Min ( kvp . Value , maxfreq - 1 ) ; } int ans = N + Math . Max ( 0 , emptySlots ) ; Console . Write ( ans ) ; } public static void Main ( ) { string S = " AAABBB " ; int K = 2 ; int N = S . Length ; findMinimumTime ( S , N , K ) ; } }


"Minimum time required to complete all tasks without altering their order | C # implementation of the above approach ; Function to find the minimum time required to complete tasks without changing their order ; Keeps track of the last time instant of each task ; Stores the required result ; Traverse the given string ; Check last time instant of task , if it exists before ; Increment the time if task is within the K units of time ; Update the time of the current task in the map ; Increment the time by 1 ; Print the result ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void findMinimumTime ( String tasks , int K ) { Dictionary < char , int > map = new Dictionary < char , int > ( ) ; int curr_time = 0 ; foreach ( char c in tasks . ToCharArray ( ) ) { if ( map . ContainsKey ( c ) ) { if ( curr_time - map <= K ) { curr_time += K - ( curr_time - map ) + 1 ; } } if ( ! map . ContainsKey ( c ) ) map . Add ( c , curr_time ) ; else map = curr_time ; curr_time ++ ; } Console . WriteLine ( curr_time ) ; } public static void Main ( String [ ] args ) { String S = " ABACCA " ; int K = 2 ; findMinimumTime ( S , K ) ; } }


"Minimum time required to cover a Binary Array | C # implementation to find the Minimum time required to cover a Binary Array ; Function to calculate the time ; Map to mark or store the binary values ; Firstly fill the boolean array with all zeroes ; Mark the 1 s ; Number of 0 s until first '1' occurs ; Maximum Number of 0 s in between 2 '1' s . ; Number of 0 s from right until first '1' occurs ; Return maximum from left and right segment ; Check if count is odd ; Check ifcount is even ; Return the time ; Driver code ; Initialise N ; Array initialisation"
Solution in C#:

using System ; class GFG { static int solve ( int [ ] arr , int n ) { int k = arr . Length ; int [ ] mp = new int [ n + 2 ] ; for ( int i = 0 ; i <= n ; i ++ ) { mp [ i ] = 0 ; } for ( int i = 0 ; i < k ; i ++ ) { mp [ arr [ i ] ] = 1 ; } int leftSegment = arr [ 0 ] - 1 ; for ( int i = 1 ; i < k ; i ++ ) { leftSegment = Math . Max ( leftSegment , arr [ i ] - arr [ i - 1 ] - 1 ) ; } int rightSegment = n - arr [ k - 1 ] ; int maxSegment = Math . Max ( leftSegment , rightSegment ) ; int tim ; if ( ( maxSegment & 1 ) == 1 ) tim = ( maxSegment / 2 ) + 1 ; else tim = maxSegment / 2 ; return tim ; } public static void Main ( ) { int N = 5 ; int [ ] arr = { 1 , 4 } ; Console . Write ( solve ( arr , N ) ) ; } }


"Minimum time required to fill given N slots | C # program for the above approach ; Function to return the minimum time to fill all the slots ; Stores visited slots ; Checks if a slot is visited or not ; Insert all filled slots ; Iterate until queue is not empty ; Iterate through all slots in the queue ; Front index ; If previous slot is present and not visited ; If next slot is present and not visited ; Increment the time at each level ; Print the answer ; Driver Code ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void minTime ( List < int > arr , int N , int K ) { Queue < int > q = new Queue < int > ( ) ; int [ ] vis = new int [ N + 1 ] ; Array . Clear ( vis , 0 , vis . Length ) ; int time = 0 ; for ( int i = 0 ; i < K ; i ++ ) { q . Enqueue ( arr [ i ] ) ; vis [ arr [ i ] ] = 1 ; } while ( q . Count > 0 ) { for ( int i = 0 ; i < q . Count ; i ++ ) { int curr = q . Peek ( ) ; q . Dequeue ( ) ; if ( curr - 1 >= 1 && vis [ curr - 1 ] == 0 ) { vis [ curr - 1 ] = 1 ; q . Enqueue ( curr - 1 ) ; } if ( curr + 1 <= N && vis [ curr + 1 ] == 0 ) { vis [ curr + 1 ] = 1 ; q . Enqueue ( curr + 1 ) ; } } time ++ ; } Console . WriteLine ( time - 1 ) ; } public static void Main ( ) { int N = 6 ; List < int > arr = new List < int > ( ) { 2 , 6 } ; int K = arr . Count ; minTime ( arr , N , K ) ; } }


"Minimum time required to reach a given score | C # program for the above approach ; Function to calculate minimum time required to achieve given score target ; Store the frequency of elements ; Traverse the array p [ ] ; Update the frequency ; Stores the minimim time required ; Store the current score at any time instant t ; Iterate until sum is at least equal to target ; Increment time with every iteration ; Traverse the map ; Increment the points ; Print the time required ; Driver Code ; Function Call"
Solution in C#:

using System . Collections . Generic ; using System ; using System . Linq ; class GFG { static void findMinimumTime ( int [ ] p , int n , int target ) { Dictionary < int , int > um = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( um . ContainsKey ( p [ i ] ) == true ) um [ p [ i ] ] += 1 ; else um [ p [ i ] ] = 1 ; } int time = 0 ; int sum = 0 ; var val = um . Keys . ToList ( ) ; while ( sum < target ) { sum = 0 ; time ++ ; foreach ( var key in val ) { sum += um [ key ] * ( time / key ) ; } } Console . WriteLine ( time ) ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 3 , 4 } ; int N = arr . Length ; int target = 10 ; findMinimumTime ( arr , N , target ) ; } }


"Minimum time required to rot all oranges | Dynamic Programming | C # implementation of the above approach ; DP table to memoize the values ; Visited array to keep track of visited nodes in order to avoid infinite loops ; Function to return the minimum of four numbers ; Function to return the minimum distance to any rotten orange from [ i , j ] ; If i , j lie outside the array ; If 0 then it can 't lead  to any path so return  INT_MAX ; If 2 then we have reached our rotten oranges so return from here ; If this node is already visited then return to avoid infinite loops ; Mark the current node as visited ; Check in all four possible directions ; Take the minimum of all ; If result already exists in the table check if min_value is less than existing value ; Function to return the minimum time required to rot all the oranges ; Calculate the minimum distances to any rotten orange from all the fresh oranges ; Pick the maximum distance of fresh orange to some rotten orange ; If all oranges can be rotten ; Driver Code"
Solution in C#:

using System ; class GFG { static int C = 5 ; static int R = 3 ; static int INT_MAX = 10000000 ; static int [ , ] table = new int [ R , C ] ; static int [ , ] visited = new int [ R , C ] ; static int min ( int p , int q , int r , int s ) { int temp1 = p < q ? p : q ; int temp2 = r < s ? r : s ; if ( temp1 < temp2 ) return temp1 ; return temp2 ; } static int Distance ( int [ , ] arr , int i , int j ) { if ( i >= R j >= C i < 0 j < 0 ) return INT_MAX ; else if ( arr [ i , j ] == 0 ) { table [ i , j ] = INT_MAX ; return INT_MAX ; } else if ( arr [ i , j ] == 2 ) { table [ i , j ] = 0 ; return 0 ; } else if ( visited [ i , j ] == 1 ) { return INT_MAX ; } else { visited [ i , j ] = 1 ; int temp1 = Distance ( arr , i + 1 , j ) ; int temp2 = Distance ( arr , i - 1 , j ) ; int temp3 = Distance ( arr , i , j + 1 ) ; int temp4 = Distance ( arr , i , j - 1 ) ; int min_value = 1 + min ( temp1 , temp2 , temp3 , temp4 ) ; if ( table [ i , j ] > 0 && table [ i , j ] < INT_MAX ) { if ( min_value < table [ i , j ] ) table [ i , j ] = min_value ; } else table [ i , j ] = min_value ; visited [ i , j ] = 0 ; } return table [ i , j ] ; } static int minTime ( int [ , ] arr ) { int max = 0 ; for ( int i = 0 ; i < R ; i ++ ) { for ( int j = 0 ; j < C ; j ++ ) { if ( arr [ i , j ] == 1 ) Distance ( arr , i , j ) ; } } for ( int i = 0 ; i < R ; i ++ ) { for ( int j = 0 ; j < C ; j ++ ) { if ( arr [ i , j ] == 1 && table [ i , j ] > max ) max = table [ i , j ] ; } } if ( max < INT_MAX ) return max ; return - 1 ; } public static void Main ( string [ ] args ) { int [ , ] arr = { { 2 , 1 , 0 , 2 , 1 } , { 0 , 0 , 1 , 2 , 1 } , { 1 , 0 , 0 , 2 , 1 } } ; Console . Write ( minTime ( arr ) ) ; } }


"Minimum time required to schedule K processes | C # program for the above approach ; Function to find minimum required time to schedule all process ; Stores max element from A [ ] ; Find the maximum element ; Stores frequency of each element ; Stores minimum time required to schedule all process ; Count frequencies of elements ; Find the minimum time ; Decrease the value of K ; Increment tmp [ i / 2 ] ; Increment the count ; Return count , if all process are scheduled ; Increment count ; Return the count ; If it is not possible to schedule all process ; Driver Code"
Solution in C#:

using System ; class GFG { static int minTime ( int [ ] A , int n , int K ) { int max_ability = A [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { max_ability = Math . Max ( max_ability , A [ i ] ) ; } int [ ] tmp = new int [ max_ability + 1 ] ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { tmp [ A [ i ] ] ++ ; } for ( int i = max_ability ; i >= 0 ; i -- ) { if ( tmp [ i ] != 0 ) { if ( tmp [ i ] * i < K ) { K -= ( i * tmp [ i ] ) ; tmp [ i / 2 ] += tmp [ i ] ; count += tmp [ i ] ; if ( K <= 0 ) { return count ; } } else { if ( K % i != 0 ) { count += ( K / i ) + 1 ; } else { count += ( K / i ) ; } return count ; } } } return - 1 ; } public static void Main ( string [ ] args ) { int [ ] arr = { 3 , 1 , 7 , 2 , 4 } ; int N = arr . Length ; int K = 15 ; Console . WriteLine ( minTime ( arr , N , K ) ) ; } }


"Minimum time required to transport all the boxes from source to the destination under the given constraints | C # implementation of the approach ; Function that returns true if it is possible to transport all the boxes in the given amount of time ; If all the boxes can be transported in the given time ; If all the boxes can 't be  transported in the given time ; Function to return the minimum time required ; Sort the two arrays ; Stores minimum time in which all the boxes can be transported ; Check for the minimum time in which all the boxes can be transported ; If it is possible to transport all the boxes in mid amount of time ; Driver code"
Solution in C#:

using System ; class GFG { static bool isPossible ( int [ ] box , int [ ] truck , int n , int m , int min_time ) { int temp = 0 ; int count = 0 ; while ( count < m ) { for ( int j = 0 ; j < min_time && temp < n && truck [ count ] >= box [ temp ] ; j += 2 ) temp ++ ; count ++ ; } if ( temp == n ) return true ; return false ; } static int minTime ( int [ ] box , int [ ] truck , int n , int m ) { Array . Sort ( box ) ; Array . Sort ( truck ) ; int l = 0 ; int h = 2 * n ; int min_time = 0 ; while ( l <= h ) { int mid = ( l + h ) / 2 ; if ( isPossible ( box , truck , n , m , mid ) ) { min_time = mid ; h = mid - 1 ; } else l = mid + 1 ; } return min_time ; } public static void Main ( String [ ] args ) { int [ ] box = { 10 , 2 , 16 , 19 } ; int [ ] truck = { 29 , 25 } ; int n = box . Length ; int m = truck . Length ; Console . WriteLine ( " { 0 } " , minTime ( box , truck , n , m ) ) ; } }


"Minimum time taken by each job to be completed given by a Directed Acyclic Graph | C # program for the above approach ; Adjacency List to store the graph ; Array to store the in - degree of node ; Array to store the time in which the job i can be done ; Function to add directed edge between two vertices ; Insert edge from u to v ; Increasing the indegree of vertex v ; Function to find the minimum time needed by each node to get the task ; Queue to store the nodes while processing ; Update the time of the jobs who don 't require any job to  be completed before this job ; Iterate until queue is empty ; Get front element of queue ; Pop the front element ; Decrease in - degree of the current node ; Push its adjacent elements ; Print the time to complete the job ; Driver Code ; Given Nodes N and edges M ; Given directed edges of graph ; Function call"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static readonly int maxN = 100000 ; static List < int > [ ] graph = new List < int > [ maxN ] ; static int [ ] indegree = new int [ maxN ] ; static int [ ] job = new int [ maxN ] ; static void addEdge ( int u , int v ) { graph [ u ] . Add ( v ) ; indegree [ v ] ++ ; } static void printOrder ( int n , int m ) { Queue < int > q = new Queue < int > ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { if ( indegree [ i ] == 0 ) { q . Enqueue ( i ) ; job [ i ] = 1 ; } } while ( q . Count != 0 ) { int cur = q . Peek ( ) ; q . Dequeue ( ) ; foreach ( int adj in graph [ cur ] ) { indegree [ adj ] -- ; if ( indegree [ adj ] == 0 ) { job [ adj ] = 1 + job [ cur ] ; q . Enqueue ( adj ) ; } } } for ( int i = 1 ; i <= n ; i ++ ) Console . Write ( job [ i ] + " ▁ " ) ; Console . Write ( "
 " ) ; } public static void Main ( String [ ] args ) { int n , m ; n = 10 ; m = 13 ; for ( int i = 0 ; i < graph . Length ; i ++ ) graph [ i ] = new List < int > ( ) ; addEdge ( 1 , 3 ) ; addEdge ( 1 , 4 ) ; addEdge ( 1 , 5 ) ; addEdge ( 2 , 3 ) ; addEdge ( 2 , 8 ) ; addEdge ( 2 , 9 ) ; addEdge ( 3 , 6 ) ; addEdge ( 4 , 6 ) ; addEdge ( 4 , 8 ) ; addEdge ( 5 , 8 ) ; addEdge ( 6 , 7 ) ; addEdge ( 7 , 8 ) ; addEdge ( 8 , 10 ) ; printOrder ( n , m ) ; } }


"Minimum time to finish tasks without skipping two consecutive | C # program to find minimum time to finish tasks such that no two consecutive tasks are skipped . ; arr [ ] represents time taken by n given tasks ; Corner Cases ; Initialize value for the case when there is only one task in task list . First task is included ; First task is exluded ; Process remaining n - 1 tasks ; Time taken if current task is included . There are two possibilities ( a ) Previous task is also included ( b ) Previous task is not included ; Time taken when current task is not included . There is only one possibility that previous task is also included . ; Update incl and excl for next iteration ; Return maximum of two values for last task ; Driver code"
Solution in C#:

using System ; class GFG { static int minTime ( int [ ] arr , int n ) { if ( n <= 0 ) return 0 ; int incl = arr [ 0 ] ; int excl = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int incl_new = arr [ i ] + Math . Min ( excl , incl ) ; int excl_new = incl ; incl = incl_new ; excl = excl_new ; } return Math . Min ( incl , excl ) ; } public static void Main ( ) { int [ ] arr1 = { 10 , 5 , 2 , 7 , 10 } ; int n1 = arr1 . Length ; Console . WriteLine ( minTime ( arr1 , n1 ) ) ; int [ ] arr2 = { 10 , 5 , 7 , 10 } ; int n2 = arr2 . Length ; Console . WriteLine ( minTime ( arr2 , n2 ) ) ; int [ ] arr3 = { 10 , 5 , 2 , 4 , 8 , 6 , 7 , 10 } ; int n3 = arr3 . Length ; Console . WriteLine ( minTime ( arr3 , n3 ) ) ; } }


"Minimum time to reach a point with + t and | C # implementation of the above approach ; returns the minimum time required to reach ' X ' ; Stores the minimum time ; increment ' t ' by 1 ; update the sum ; Driver code"
Solution in C#:

using System ; public class GFG { static long cal_minimum_time ( long X ) { long t = 0 ; long sum = 0 ; while ( sum < X ) { t ++ ; sum = sum + t ; } return t ; } static public void Main ( ) { long n = 6 ; long ans = cal_minimum_time ( n ) ; Console . WriteLine ( " The ▁ minimum ▁ time ▁ required ▁ is ▁ : ▁ " + ans ) ; } }


"Minimum time to return array to its original state after given modifications | C # implementation of above approach ; Function to return lcm of two numbers ; Make a graph ; Add edge ; Count reachable nodes from every node . ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int lcm ( int a , int b ) { return ( a * b ) / ( __gcd ( a , b ) ) ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } static int dfs ( int src , List < int > [ ] adj , bool [ ] visited ) { visited [ src ] = true ; int count = 1 ; for ( int i = 0 ; i < adj [ src ] . Count ; i ++ ) if ( ! visited [ adj [ src ] [ i ] ] ) count += dfs ( adj [ src ] [ i ] , adj , visited ) ; return count ; } static int findMinTime ( int [ ] arr , int [ ] P , int n ) { List < int > [ ] adj = new List < int > [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) adj [ i ] = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { adj [ arr [ i ] ] . Add ( P [ i ] ) ; } bool [ ] visited = new bool [ n + 1 ] ; int ans = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! visited [ i ] ) { ans = lcm ( ans , dfs ( i , adj , visited ) ) ; } } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 } ; int [ ] P = { 3 , 2 , 1 } ; int n = arr . Length ; Console . Write ( findMinTime ( arr , P , n ) ) ; } }


"Minimum time to write characters using insert , delete and copy operation | C # program to write characters in minimum time by inserting , removing and copying operation ; method returns minimum time to write ' N ' characters ; declare dp array and initialize with zero ; first char will always take insertion time ; loop for ' N ' number of times ; if current char count is even then choose minimum from result for ( i - 1 ) chars and time for insertion and result for half of chars and time for copy ; if current char count is odd then choose minimum from result for ( i - 1 ) chars and time for insertion and result for half of chars and time for copy and one extra character deletion ; Driver code"
Solution in C#:

using System ; class GFG { static int minTimeForWritingChars ( int N , int insert , int remove , int copy ) { if ( N == 0 ) return 0 ; if ( N == 1 ) return insert ; int [ ] dp = new int [ N + 1 ] ; for ( int i = 0 ; i < N + 1 ; i ++ ) dp [ i ] = 0 ; dp [ 1 ] = insert ; for ( int i = 2 ; i <= N ; i ++ ) { if ( i % 2 == 0 ) dp [ i ] = Math . Min ( dp [ i - 1 ] + insert , dp [ i / 2 ] + copy ) ; else dp [ i ] = Math . Min ( dp [ i - 1 ] + insert , dp [ ( i + 1 ) / 2 ] + copy + remove ) ; } return dp [ N ] ; } static void Main ( ) { int N = 9 ; int insert = 1 , remove = 2 , copy = 1 ; Console . Write ( minTimeForWritingChars ( N , insert , remove , copy ) ) ; } }


"Minimum toggles to partition a binary array so that it has first 0 s then 1 s | C # program to find minimum toggle required ; Function to calculate minimum toggling required by using Dynamic programming ; Fill entries in zero [ ] such that zero [ i ] stores count of zeroes to the left of i ( exl ; If zero found update zero [ ] array ; Finding the minimum toggle required from every index ( 0 to n - 1 ) ; Driver Program"
Solution in C#:

using System ; class GFG { static int minToggle ( int [ ] arr , int n ) { int [ ] zero = new int [ n + 1 ] ; zero [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; ++ i ) { if ( arr [ i - 1 ] == 0 ) zero [ i ] = zero [ i - 1 ] + 1 ; else zero [ i ] = zero [ i - 1 ] ; } int ans = n ; for ( int i = 1 ; i <= n ; ++ i ) ans = Math . Min ( ans , i - zero [ i ] + zero [ n ] - zero [ i ] ) ; return ans ; } public static void Main ( ) { int [ ] arr = { 1 , 0 , 1 , 1 , 0 } ; int n = arr . Length ; Console . WriteLine ( minToggle ( arr , n ) ) ; } }


"Minimum total sum from the given two arrays | C # program for the above approach ; Function that prints minimum sum after selecting N elements ; Initialise the dp array ; Base Case ; Adding the element of array a if previous element is also from array a ; Adding the element of array a if previous element is from array b ; Adding the element of array b if previous element is from array a with an extra penalty of integer C ; Adding the element of array b if previous element is also from array b ; Print the minimum sum ; Driver Code ; Given array arr1 [ ] and arr2 [ ] ; Given cost ; Function call"
Solution in C#:

 using System ; class GFG { static void minimumSum ( int [ ] a , int [ ] b , int c , int n ) { int [ , ] dp = new int [ n , 2 ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < 2 ; j ++ ) { dp [ i , j ] = ( int ) 1e6 ; } } dp [ 0 , 0 ] = a [ 0 ] ; dp [ 0 , 1 ] = b [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { dp [ i , 0 ] = Math . Min ( dp [ i , 0 ] , dp [ i - 1 , 0 ] + a [ i ] ) ; dp [ i , 0 ] = Math . Min ( dp [ i , 0 ] , dp [ i - 1 , 1 ] + a [ i ] + c ) ; dp [ i , 1 ] = Math . Min ( dp [ i , 1 ] , dp [ i - 1 , 0 ] + b [ i ] + c ) ; dp [ i , 1 ] = Math . Min ( dp [ i , 1 ] , dp [ i - 1 , 1 ] + b [ i ] ) ; } Console . Write ( Math . Min ( dp [ n - 1 , 0 ] , dp [ n - 1 , 1 ] ) + "
 " ) ; } public static void Main ( String [ ] args ) { int [ ] arr1 = { 7 , 6 , 18 , 6 , 16 , 18 , 1 , 17 , 17 } ; int [ ] arr2 = { 6 , 9 , 3 , 10 , 9 , 1 , 10 , 1 , 5 } ; int C = 2 ; int N = arr1 . Length ; minimumSum ( arr1 , arr2 , C , N ) ; } }


"Minimum value among AND of elements of every subset of an array | C # program for the above approach ; Find AND of whole array ; Print the answer ; Driver code"
Solution in C#:

class GFG { static void minAND ( int [ ] arr , int n ) { int s = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { s = s & arr [ i ] ; } System . Console . WriteLine ( s ) ; } static void Main ( ) { int [ ] arr = { 1 , 2 , 3 } ; int n = arr . Length ; minAND ( arr , n ) ; } }


"Minimum value exceeding X whose count of divisors has different parity with count of divisors of X | C # program for the above approach ; Function to count divisors of n ; Function to find the minimum value exceeding x whose count of divisors has different parity with count of divisors of X ; Divisor count of x ; Iterate from x + 1 and check for each element ; Driver Code ; Given X ; Function call"
Solution in C#:

using System ; class GFG { static int divisorCount ( int n ) { int x = 0 ; for ( int i = 1 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( i == n / i ) x ++ ; else x += 2 ; } } return x ; } static int minvalue_y ( int x ) { int a = divisorCount ( x ) ; int y = x + 1 ; while ( ( a & 1 ) == ( divisorCount ( y ) & 1 ) ) y ++ ; return y ; } public static void Main ( ) { int x = 5 ; Console . WriteLine ( minvalue_y ( x ) ) ; } }


"Minimum value exceeding X whose count of divisors has different parity with count of divisors of X | C # program for the above approach ; Function to find the minimum value exceeding x whose count of divisors has different parity with count of divisors of X ; Check if x is perfect square ; Driver Code"
Solution in C#:

using System ; class GFG { static int minvalue_y ( int x ) { int n = ( int ) Math . Sqrt ( x ) ; if ( n * n == x ) return x + 1 ; return ( int ) Math . Pow ( n + 1 , 2 ) ; } public static void Main ( ) { int x = 5 ; Console . WriteLine ( minvalue_y ( x ) ) ; } }


"Minimum value of " max ▁ + ▁ min " in a subarray | C # program to find sum of maximum and minimum in any subarray of an array of positive numbers . ; Driver code"
Solution in C#:

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


"Minimum value of K such that sum of cubes of first K natural number is greater than equal to N | C # program to determine the minimum value of K such that the sum of cubes of first K natural number is greater than or equal to N ; Function to determine the minimum value of K such that the sum of cubes of first K natural number is greater than or equal to N ; Left bound ; Right bound ; Variable to store the answer ; Applying binary search ; Calculating mid value of the range ; If the sum of cubes of first mid natural numbers is greater than equal to N iterate the left half ; Sum of cubes of first mid natural numbers is less than N , then move to the right segment ; Driver code"
Solution in C#:

using System ; class GFG { static int binary_searched_find_x ( int k ) { int l = 0 ; int r = k ; int ans = 0 ; while ( l <= r ) { int mid = l + ( r - l ) / 2 ; if ( Math . Pow ( ( ( mid * ( mid + 1 ) ) / 2 ) , 2 ) >= k ) { ans = mid ; r = mid - 1 ; } else { l = mid + 1 ; } } return ans ; } public static void Main ( ) { int N = 100 ; Console . Write ( binary_searched_find_x ( N ) ) ; } }


"Minimum value of K such that sum of cubes of first K natural number is greater than equal to N | C # program to determine the minimum value of K such that the sum of cubes of first K natural number is greater than or equal to N ; Function to determine the minimum value of K such that the sum of cubes of first K natural number is greater than or equal to N ; Variable to store the sum of cubes ; Loop to find the number K ; If C is just greater then N , then break the loop ; Driver code"
Solution in C#:

using System ; class GFG { static int naive_find_x ( int N ) { int c = 0 , i ; for ( i = 1 ; i < N ; i ++ ) { c += i * i * i ; if ( c >= N ) break ; } return i ; } public static void Main ( String [ ] args ) { int N = 100 ; Console . WriteLine ( naive_find_x ( N ) ) ; } }


"Minimum value of N such that xor from 1 to N is equal to K | C # implementation of above approach ; Function to find the value of N ; variable to store the result ; handling case for '0' ; handling case for '1' ; when number is completely divided by 4 then minimum ' x ' will be ' k ' ; when number divided by 4 gives 3 as remainder then minimum ' x ' will be ' k - 1' ; else it is not possible to get k for any value of x ; Driver code ; let the given number be 7"
Solution in C#:

using System ; class GFG { static int findN ( int k ) { int ans ; if ( k == 0 ) ans = 3 ; if ( k == 1 ) ans = 1 ; else if ( k % 4 == 0 ) ans = k ; else if ( k % 4 == 3 ) ans = k - 1 ; else ans = - 1 ; return ans ; } public static void Main ( ) { int k = 7 ; int res = findN ( k ) ; if ( res == - 1 ) Console . WriteLine ( " Not ▁ possible " ) ; else Console . WriteLine ( res ) ; } }


"Minimum value of X such that sum of arr [ i ] | C # program for the above approach ; Function to check if it is possible to get desired result ; Find the required value of the given expression ; Function to find the minimum value of X using binary search . ; Boundaries of the Binary Search ; Find the middle value ; Check for the middle value ; Update the upper ; Update the lower ; Driver code"
Solution in C#:

using System ; public class GFG { static bool check ( int [ ] a , int [ ] b , int k , int x ) { int sum = 0 ; for ( int i = 0 ; i < a . Length ; i ++ ) { sum = sum + ( int ) Math . Pow ( Math . Max ( a [ i ] - x , 0 ) , b [ i ] ) ; } if ( sum <= k ) return true ; else return false ; } for ( int i = 0 ; i < a . Length ; ++ ) { = + ( int ) Math . Pow ( Math . Max ( a [ i ] - x , 0 ) , b [ i ] ) ; } if ( sum <= k ) return true ; else return false ; } static int ( int [ ] a , int [ ] b , int n , int k ) { int l = 0 , u = ( int ) 1e9 ; while ( l < u ) { int m = ( l + u ) / 2 ; if ( check ( a , b , k , m ) ) u = m ; else l = m + 1 ; } return l ; } public static void ( String [ ] args ) { int n = 5 ; int k = 12 ; int [ ] a = { 2 , 1 , 4 , 3 , 5 } ; int [ ] b = { 4 , 3 , 2 , 3 , 1 } ; Console . WriteLine ( findMin ( a , b , n , k ) ) ; } }


"Minimum value of X that can be added to N to minimize sum of the digits to Ã ¢ â €°¤ K | C # program for the above approach ; Function to find the minimum number needed to be added so that the sum of the digits does not exceed K ; Find the number of digits ; Calculate sum of the digits ; Add the digits of num2 ; If the sum of the digits of N is less than or equal to K ; No number needs to be added ; Otherwise ; Calculate the sum of digits from least significant digit ; If sum exceeds K ; Increase previous digit by 1 ; Add zeros to the end ; Calculate difference between the result and N ; Return the result ; Driver Code"
Solution in C#:

using System ; class GFG { static int minDigits ( int N , int K ) { int digits_num = ( int ) Math . Floor ( Math . Log ( N ) + 1 ) ; int temp_sum = 0 ; int temp = digits_num ; int result = 0 ; int X , var ; int sum = 0 ; int num2 = N ; while ( num2 != 0 ) { sum += num2 % 10 ; num2 /= 10 ; } if ( sum <= K ) { X = 0 ; } else { while ( temp > 0 ) { var = ( N / ( ( int ) Math . Pow ( 10 , temp - 1 ) ) ) ; . temp_sum += var % 10 ; if ( temp_sum >= K ) { var /= 10 ; var ++ ; result = var * ( int ) Math . Pow ( 10 , temp ) ; break ; } temp -- ; } X = result - N ; return X ; } return - 1 ; } public static void Main ( String [ ] args ) { int N = 11 ; int K = 1 ; Console . WriteLine ( minDigits ( N , K ) ) ; } }


"Minimum value of X to make all array elements equal by either decreasing or increasing by X | C # implementation of the approach ; Function that returns the minimum value of X ; Declare a set ; Iterate in the array element and insert them into the set ; If unique elements is 1 ; Unique elements is 2 ; Get both el2 and el1 ; Check if they are even ; If there are 3 unique elements ; Get three unique elements ; Check if their difference is same ; More than 3 unique elements ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int findMinimumX ( int [ ] a , int n ) { List < int > st = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( ! st . Contains ( a [ i ] ) ) st . Add ( a [ i ] ) ; if ( st . Count == 1 ) return 0 ; if ( st . Count == 2 ) { int el1 = st [ 0 ] ; int el2 = st [ 1 ] ; if ( ( el2 - el1 ) % 2 == 0 ) return ( el2 - el1 ) / 2 ; else return ( el2 - el1 ) ; } if ( st . Count == 3 ) { int el1 = st [ 0 ] ; int el2 = st [ 1 ] ; int el3 = st [ 2 ] ; if ( ( el2 - el1 ) == ( el3 - el2 ) ) return el2 - el1 ; else return - 1 ; } return - 1 ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 4 , 4 , 7 , 4 , 1 } ; int n = a . Length ; Console . WriteLine ( findMinimumX ( a , n ) ) ; } }


"Minimum value possible of a given function from the given set | C # implementation of above approach ; Function to find the value of F ( n ) ; Driver code"
Solution in C#:

using System ; class GFG { static long findF_N ( long n ) { long ans = 0 ; for ( long i = 0 ; i < n ; ++ i ) ans += ( i + 1 ) * ( n - i - 1 ) ; return ans ; } public static void Main ( ) { long n = 3 ; Console . WriteLine ( findF_N ( n ) ) ; } }


"Minimum value to be added to maximize Bitwise XOR of the given array | C # program for the above approach ; Function to find complement of an integer ; Count the number of bits of maxElement ; Return 1 's complement ; Function to find the value required to be added to maximize XOR of the given array ; Stores the required value to be added to the array ; Stores the maximum array element ; Traverse the array ; Update XOR of the array ; Find maximum element in array ; Calculate 1 s ' complement ; Return the answer ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int onesComplement ( int n , int maxElement ) { int bits = ( int ) Math . Floor ( ( Math . Log ( maxElement ) / Math . Log ( 2 ) ) ) + 1 ; return ( ( 1 << bits ) - 1 ) ^ n ; } static int findNumber ( int [ ] arr , int n ) { int res = 0 ; int maxElement = 0 ; for ( int i = 0 ; i < n ; i ++ ) { res = res ^ arr [ i ] ; if ( maxElement < arr [ i ] ) maxElement = arr [ i ] ; } res = onesComplement ( res , maxElement ) ; return ( res ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int N = arr . Length ; Console . Write ( findNumber ( arr , N ) ) ; } }


"Minimum value to be added to the prefix sums at each array indices to make them positive | C # program for the above approach ; Function to find minimum startValue for positive prefix sum at each index ; Store the minimum prefix sum ; Stores prefix sum at each index ; Traverse over the array ; Update the prefix sum ; Update the minValue ; Return the positive start value ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static int minStartValue ( int [ ] nums ) { int minValue = 0 ; int sum = 0 ; foreach ( int n in nums ) { sum += n ; minValue = Math . Min ( minValue , sum ) ; } int startValue = 1 - minValue ; return startValue ; } public static void Main ( ) { int [ ] nums = { - 3 , 2 , - 3 , 4 , 2 } ; Console . WriteLine ( minStartValue ( nums ) ) ; } }


"Minimum volume of cone that can be circumscribed about a sphere of radius R | C # program to find the minimum volume of the cone that can be circumscribed about a sphere of radius R ; Function to find the volume of the cone ; r = radius of cone h = height of cone Volume of cone = ( 1 / 3 ) * ( 3.14 ) * ( r * r ) * ( h ) we get radius of cone from the derivation is root ( 2 ) times multiple of R we get height of cone from the derivation is 4 times multiple of R ; Driver code"
Solution in C#:

using System ; class GFG { static double Volume_of_cone ( double R ) { double V = ( double ) ( ( 1 / 3.0 ) * ( 3.14 ) * ( 2 * ( R * R ) ) * ( 4 * R ) ) ; return V ; } public static void Main ( ) { double R = 10.0 ; Console . Write ( Volume_of_cone ( R ) ) ; } }


"Minimum window size containing atleast P primes in every window of given range | C # implementation to find the minimum window size in the range such that each window of that size contains atleast P primes ; Function to check that a number is a prime or not in O ( Math . Sqrt ( N ) ) ; Loop to check if any number number is divisible by any other number or not ; Function to check whether window size satisfies condition or not ; Loop to check each window of size have atleast P primes ; Checking condition using prefix sum ; Function to find the minimum window size possible for the given range in X and Y ; Prefix array ; Mark those numbers which are primes as 1 ; Convert to prefix sum ; Applying binary search over window size ; Check whether mid satisfies the condition or not ; If satisfies search in first half ; Else search in second half ; Driver Code"
Solution in C#:

using System ; class GFG { static bool isPrime ( int N ) { if ( N < 2 ) return false ; if ( N < 4 ) return true ; if ( ( N & 1 ) == 0 ) return false ; if ( N % 3 == 0 ) return false ; int curr = 5 , s = ( int ) Math . Sqrt ( N ) ; while ( curr <= s ) { if ( N % curr == 0 ) return false ; curr += 2 ; if ( N % curr == 0 ) return false ; curr += 4 ; } return true ; } static bool check ( int s , int p , int [ ] prefix_sum , int n ) { bool satisfies = true ; for ( int i = 0 ; i < n ; i ++ ) { if ( i + s - 1 >= n ) break ; if ( prefix_sum [ i + s - 1 ] - ( i - 1 >= 0 ? prefix_sum [ i - 1 ] : 0 ) < p ) satisfies = false ; } return satisfies ; } static int minimumWindowSize ( int x , int y , int p ) { int [ ] prefix_sum = new int [ y - x + 1 ] ; for ( int i = x ; i <= y ; i ++ ) { if ( isPrime ( i ) ) prefix_sum [ i - x ] = 1 ; } for ( int i = 1 ; i < y - x + 1 ; i ++ ) prefix_sum [ i ] += prefix_sum [ i - 1 ] ; int low = 1 , high = y - x + 1 ; int mid ; while ( high - low > 1 ) { mid = ( low + high ) / 2 ; if ( check ( mid , p , prefix_sum , y - x + 1 ) ) { high = mid ; } else low = mid ; } if ( check ( low , p , prefix_sum , y - x + 1 ) ) return low ; return high ; } public static void Main ( String [ ] args ) { int x = 12 ; int y = 42 ; int p = 3 ; Console . Write ( minimumWindowSize ( x , y , p ) ) ; } }


"Mirror characters of a string | C # code to find the reverse alphabetical order from a given position ; Function which take the given string and the position from which the reversing shall be done and returns the modified string ; Creating a string having reversed alphabetical order ; The string up to the point specified in the question , the string remains unchanged and from the point up to the length of the string , we reverse the alphabetical order ; Driver function"
Solution in C#:

using System ; class GFG { static String compute ( string str , int n ) { string reverseAlphabet = " zyxwvutsrqponmlkjihgfedcba " ; int l = str . Length ; String answer = " " ; for ( int i = 0 ; i < n ; i ++ ) answer = answer + str [ i ] ; for ( int i = n ; i < l ; i ++ ) answer = answer + reverseAlphabet [ str [ i ] - ' a ' ] ; return answer ; } public static void Main ( ) { string str = " pneumonia " ; int n = 4 ; Console . Write ( compute ( str , n - 1 ) ) ; } }


"Mirror of a point through a 3 D plane | C # program to find Mirror of a point through a 3 D plane ; Function to mirror image ; Driver Code ; function call"
Solution in C#:

using System ; class GFG { static void mirror_point ( int a , int b , int c , int d , int x1 , int y1 , int z1 ) { float k = ( - a * x1 - b * y1 - c * z1 - d ) / ( float ) ( a * a + b * b + c * c ) ; float x2 = a * k + x1 ; float y2 = b * k + y1 ; float z2 = c * k + z1 ; float x3 = 2 * x2 - x1 ; float y3 = 2 * y2 - y1 ; float z3 = 2 * z2 - z1 ; Console . Write ( " x3 ▁ = ▁ " + x3 + " ▁ " ) ; Console . Write ( " y3 ▁ = ▁ " + y3 + " ▁ " ) ; Console . Write ( " z3 ▁ = ▁ " + z3 + " ▁ " ) ; } static public void Main ( ) { int a = 1 ; int b = - 2 ; int c = 0 ; int d = 0 ; int x1 = - 1 ; int y1 = 3 ; int z1 = 4 ; mirror_point ( a , b , c , d , x1 , y1 , z1 ) ; } }


"Mirror of matrix across diagonal | Efficient C # program to find mirror of matrix across diagonal . ; traverse a matrix and swap mat [ i ] [ j ] with mat [ j ] [ i ] ; Utility function to print a matrix ; driver program to test above function"
Solution in C#:

using System ; class GFG { static int MAX = 100 ; static void imageSwap ( int [ , ] mat , int n ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j <= i ; j ++ ) mat [ i , j ] = mat [ i , j ] + mat [ j , i ] - ( mat [ j , i ] = mat [ i , j ] ) ; } static void printMatrix ( int [ , ] mat , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) Console . Write ( mat [ i , j ] + " ▁ " ) ; Console . WriteLine ( ) ; } } public static void Main ( ) { int [ , ] mat = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; int n = 4 ; imageSwap ( mat , n ) ; printMatrix ( mat , n ) ; } }


"Mirror of matrix across diagonal | Simple C # program to find mirror of matrix across diagonal . ; for diagonal which start from at first row of matrix ; traverse all top right diagonal ; here we use stack for reversing the element of diagonal ; push all element back to matrix in reverse order ; do the same process for all the diagonal which start from last column ; here we use stack for reversing the elements of diagonal ; push all element back to matrix in reverse order ; Utility function to print a matrix ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int MAX = 100 ; static void imageSwap ( int [ , ] mat , int n ) { int row = 0 ; for ( int j = 0 ; j < n ; j ++ ) { Stack < int > s = new Stack < int > ( ) ; int i = row , k = j ; while ( i < n && k >= 0 ) { s . Push ( mat [ i ++ , k -- ] ) ; } i = row ; k = j ; while ( i < n && k >= 0 ) { mat [ i ++ , k -- ] = s . Peek ( ) ; s . Pop ( ) ; } } int column = n - 1 ; for ( int j = 1 ; j < n ; j ++ ) { Stack < int > s = new Stack < int > ( ) ; int i = j , k = column ; while ( i < n && k >= 0 ) { s . Push ( mat [ i ++ , k -- ] ) ; } i = j ; k = column ; while ( i < n && k >= 0 ) { mat [ i ++ , k -- ] = s . Peek ( ) ; s . Pop ( ) ; } } } static void printMatrix ( int [ , ] mat , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { Console . Write ( mat [ i , j ] + " ▁ " ) ; } Console . WriteLine ( " " ) ; } } public static void Main ( String [ ] args ) { int [ , ] mat = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; int n = 4 ; imageSwap ( mat , n ) ; printMatrix ( mat , n ) ; } }


"Missing even and odd elements from the given arrays | C # implementation of the approach ; Function to find the missing numbers ; To store the minimum and the maximum odd and even elements from the arrays ; To store the sum of the array elements ; Get the minimum and the maximum even elements from the array ; Get the minimum and the maximum odd elements from the array ; To store the total terms in the series and the required sum of the array ; Total terms from 2 to minEven ; Sum of all even numbers from 2 to minEven ; Total terms from 2 to maxEven ; Sum of all even numbers from 2 to maxEven ; Required sum for the even array ; Missing even number ; Total terms from 1 to minOdd ; Sum of all odd numbers from 1 to minOdd ; Total terms from 1 to maxOdd ; Sum of all odd numbers from 1 to maxOdd ; Required sum for the odd array ; Missing odd number ; Driver code"
Solution in C#:

using System ; class GFG { static void findMissingNums ( int [ ] even , int sizeEven , int [ ] odd , int sizeOdd ) { int minEven = int . MaxValue ; int maxEven = int . MinValue ; int minOdd = int . MaxValue ; int maxOdd = int . MinValue ; int sumEvenArr = 0 , sumOddArr = 0 ; for ( int i = 0 ; i < sizeEven ; i ++ ) { minEven = Math . Min ( minEven , even [ i ] ) ; maxEven = Math . Max ( maxEven , even [ i ] ) ; sumEvenArr += even [ i ] ; } for ( int i = 0 ; i < sizeOdd ; i ++ ) { minOdd = Math . Min ( minOdd , odd [ i ] ) ; maxOdd = Math . Max ( maxOdd , odd [ i ] ) ; sumOddArr += odd [ i ] ; } int totalTerms = 0 , reqSum = 0 ; totalTerms = minEven / 2 ; int evenSumMin = ( totalTerms * ( totalTerms + 1 ) ) ; totalTerms = maxEven / 2 ; int evenSumMax = ( totalTerms * ( totalTerms + 1 ) ) ; reqSum = evenSumMax - evenSumMin + minEven ; Console . WriteLine ( " Even ▁ = ▁ " + ( reqSum - sumEvenArr ) ) ; totalTerms = ( minOdd / 2 ) + 1 ; int oddSumMin = totalTerms * totalTerms ; totalTerms = ( maxOdd / 2 ) + 1 ; int oddSumMax = totalTerms * totalTerms ; reqSum = oddSumMax - oddSumMin + minOdd ; Console . WriteLine ( " Odd ▁ = ▁ " + ( reqSum - sumOddArr ) ) ; } static void Main ( ) { int [ ] even = { 6 , 4 , 8 , 14 , 10 } ; int sizeEven = even . Length ; int [ ] odd = { 7 , 5 , 3 , 11 , 13 } ; int sizeOdd = odd . Length ; findMissingNums ( even , sizeEven , odd , sizeOdd ) ; } }


"Missing vertex among N axis | C # Program to implement the above approach ; Driver Code ; Number of rectangles ; Stores the coordinates ; Insert the coordinates"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void MissingPoint ( List < pair > V , int N ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < V . Count ; i ++ ) { if ( mp . ContainsKey ( V [ i ] . first ) ) mp [ V [ i ] . first ] = mp [ V [ i ] . first ] + 1 ; else mp . Add ( V [ i ] . first , 1 ) ; } int x = 0 , y = 0 ; foreach ( KeyValuePair < int , int > it in mp ) { if ( it . Value % 2 == 1 ) { x = it . Key ; break ; } } mp . Clear ( ) ; for ( int i = 0 ; i < V . Count ; i ++ ) { if ( mp . ContainsKey ( V [ i ] . second ) ) mp [ V [ i ] . second ] = mp [ V [ i ] . second ] + 1 ; else mp . Add ( V [ i ] . second , 1 ) ; } foreach ( KeyValuePair < int , int > it in mp ) { if ( it . Value % 2 == 1 ) { y = it . Key ; break ; } } Console . Write ( x + " ▁ " + y ) ; } public static void Main ( String [ ] args ) { int N = 2 ; List < pair > V = new List < pair > ( ) ; V . Add ( new pair ( 1 , 1 ) ) ; V . Add ( new pair ( 1 , 2 ) ) ; V . Add ( new pair ( 4 , 6 ) ) ; V . Add ( new pair ( 2 , 1 ) ) ; V . Add ( new pair ( 9 , 6 ) ) ; V . Add ( new pair ( 9 , 3 ) ) ; V . Add ( new pair ( 4 , 3 ) ) ; MissingPoint ( V , N ) ; } }


"Mobile Numeric Keypad Problem | A Dynamic Programming based C # program to count number of possible numbers of given Length ; Return count of all possible numbers of Length n in a given numeric keyboard ; left , up , right , down move from current location ; taking n + 1 for simplicity - count [ i , j ] will store number count starting with digit i and . Length j ; count numbers starting with digit i and of . Lengths 0 and 1 ; Bottom up - Get number count of Length 2 , 3 , 4 , ... , n ; Loop on keypad row ; Loop on keypad column ; Process for 0 to 9 digits ; Here we are counting the numbers starting with digit keypad [ i , j ] and of . Length k keypad [ i , j ] will become 1 st digit , and we need to look for ( k - 1 ) more digits ; move left , up , right , down from current location and if new location is valid , then get number count of . Length ( k - 1 ) from that new digit and . Add in count we found so far ; Get count of all possible numbers of . Length " n " starting with digit 0 , 1 , 2 , ... , 9 ; Driver Code"
Solution in C#:

 using System ; class GFG { static int getCount ( char [ , ] keypad , int n ) { if ( keypad == null n <= 0 ) return 0 ; if ( n == 1 ) return 10 ; int [ ] row = { 0 , 0 , - 1 , 0 , 1 } ; int [ ] col = { 0 , - 1 , 0 , 1 , 0 } ; int [ , ] count = new int [ 10 , n + 1 ] ; int i = 0 , j = 0 , k = 0 , move = 0 , ro = 0 , co = 0 , num = 0 ; int nextNum = 0 , totalCount = 0 ; for ( i = 0 ; i <= 9 ; i ++ ) { count [ i , 0 ] = 0 ; count [ i , 1 ] = 1 ; } for ( k = 2 ; k <= n ; k ++ ) { for ( i = 0 ; i < 4 ; i ++ ) { for ( j = 0 ; j < 3 ; j ++ ) { if ( keypad [ i , j ] != ' * ' && keypad [ i , j ] != ' # ' ) { num = keypad [ i , j ] - '0' ; count [ num , k ] = 0 ; for ( move = 0 ; move < 5 ; move ++ ) { ro = i + row [ move ] ; co = j + col [ move ] ; if ( ro >= 0 && ro <= 3 && co >= 0 && co <= 2 && keypad [ ro , co ] != ' * ' && keypad [ ro , co ] != ' # ' ) { nextNum = keypad [ ro , co ] - '0' ; count [ num , k ] += count [ nextNum , k - 1 ] ; } } } } } } totalCount = 0 ; for ( i = 0 ; i <= 9 ; i ++ ) totalCount += count [ i , n ] ; return totalCount ; } public static void Main ( String [ ] args ) { char [ , ] keypad = { { '1' , '2' , '3' } , { '4' , '5' , '6' } , { '7' , '8' , '9' } , { ' * ' , '0' , ' # ' } } ; Console . Write ( " Count ▁ for ▁ numbers ▁ of . Length ▁ { 0 } : ▁ { 1 }
 " , 1 , getCount ( keypad , 1 ) ) ; Console . Write ( " Count ▁ for ▁ numbers ▁ of . Length ▁ { 0 } : ▁ { 1 }
 " , 2 , getCount ( keypad , 2 ) ) ; Console . Write ( " Count ▁ for ▁ numbers ▁ of . Length ▁ { 0 } : ▁ { 1 }
 " , 3 , getCount ( keypad , 3 ) ) ; Console . Write ( " Count ▁ for ▁ numbers ▁ of . Length ▁ { 0 } : ▁ { 1 }
 " , 4 , getCount ( keypad , 4 ) ) ; Console . Write ( " Count ▁ for ▁ numbers ▁ of . Length ▁ { 0 } : ▁ { 1 }
 " , 5 , getCount ( keypad , 5 ) ) ; } }


"Mobile Numeric Keypad Problem | A Space Optimized C # program to count number of possible numbers of given length ; Return count of all possible numbers of length n in a given numeric keyboard ; odd [ i ] , even [ i ] arrays represent count of numbers starting with digit i for any length j ; for j = 1 ; Bottom Up calculation from j = 2 to n ; Here we are explicitly writing lines for each number 0 to 9. But it can always be written as DFS on 4 X3 grid using row , column array valid moves ; Get count of all possible numbers of length " n " starting with digit 0 , 1 , 2 , ... , 9 ; Driver Code"
Solution in C#:

 using System ; class GFG { static int getCount ( char [ , ] keypad , int n ) { if ( keypad == null n <= 0 ) return 0 ; if ( n == 1 ) return 10 ; int [ ] odd = new int [ 10 ] ; int [ ] even = new int [ 10 ] ; int i = 0 , j = 0 , useOdd = 0 , totalCount = 0 ; for ( i = 0 ; i <= 9 ; i ++ ) odd [ i ] = 1 ; for ( j = 2 ; j <= n ; j ++ ) { useOdd = 1 - useOdd ; if ( useOdd == 1 ) { even [ 0 ] = odd [ 0 ] + odd [ 8 ] ; even [ 1 ] = odd [ 1 ] + odd [ 2 ] + odd [ 4 ] ; even [ 2 ] = odd [ 2 ] + odd [ 1 ] + odd [ 3 ] + odd [ 5 ] ; even [ 3 ] = odd [ 3 ] + odd [ 2 ] + odd [ 6 ] ; even [ 4 ] = odd [ 4 ] + odd [ 1 ] + odd [ 5 ] + odd [ 7 ] ; even [ 5 ] = odd [ 5 ] + odd [ 2 ] + odd [ 4 ] + odd [ 8 ] + odd [ 6 ] ; even [ 6 ] = odd [ 6 ] + odd [ 3 ] + odd [ 5 ] + odd [ 9 ] ; even [ 7 ] = odd [ 7 ] + odd [ 4 ] + odd [ 8 ] ; even [ 8 ] = odd [ 8 ] + odd [ 0 ] + odd [ 5 ] + odd [ 7 ] + odd [ 9 ] ; even [ 9 ] = odd [ 9 ] + odd [ 6 ] + odd [ 8 ] ; } else { odd [ 0 ] = even [ 0 ] + even [ 8 ] ; odd [ 1 ] = even [ 1 ] + even [ 2 ] + even [ 4 ] ; odd [ 2 ] = even [ 2 ] + even [ 1 ] + even [ 3 ] + even [ 5 ] ; odd [ 3 ] = even [ 3 ] + even [ 2 ] + even [ 6 ] ; odd [ 4 ] = even [ 4 ] + even [ 1 ] + even [ 5 ] + even [ 7 ] ; odd [ 5 ] = even [ 5 ] + even [ 2 ] + even [ 4 ] + even [ 8 ] + even [ 6 ] ; odd [ 6 ] = even [ 6 ] + even [ 3 ] + even [ 5 ] + even [ 9 ] ; odd [ 7 ] = even [ 7 ] + even [ 4 ] + even [ 8 ] ; odd [ 8 ] = even [ 8 ] + even [ 0 ] + even [ 5 ] + even [ 7 ] + even [ 9 ] ; odd [ 9 ] = even [ 9 ] + even [ 6 ] + even [ 8 ] ; } } totalCount = 0 ; if ( useOdd == 1 ) { for ( i = 0 ; i <= 9 ; i ++ ) totalCount += even [ i ] ; } else { for ( i = 0 ; i <= 9 ; i ++ ) totalCount += odd [ i ] ; } return totalCount ; } public static void Main ( String [ ] args ) { char [ , ] keypad = { { '1' , '2' , '3' } , { '4' , '5' , '6' } , { '7' , '8' , '9' } , { ' * ' , '0' , ' # ' } } ; Console . Write ( " Count ▁ for ▁ numbers ▁ of ▁ length ▁ { 0 } : ▁ { 1 }
 " , 1 , getCount ( keypad , 1 ) ) ; Console . Write ( " Count ▁ for ▁ numbers ▁...


"Mobile Numeric Keypad Problem | Set 2 | C # implementation of the approach ; Function to return the count of numbers possible ; Array of list storing possible direction for each number from 0 to 9 list [ i ] stores possible moves from index i ; Initializing list ; Storing values for n = 1 ; To store the values for n = i ; Loop to iterate through each index ; For each move possible from j Increment the value of possible move positions by Arr [ j ] ; Update Arr [ ] for next iteration ; Find the count of numbers possible ; Driver code"
Solution in C#:

using System ; class GFG { static int MAX = 10 ; static int getCount ( int n ) { int [ ] [ ] list = new int [ MAX ] [ ] ; list [ 0 ] = new int [ ] { 0 , 8 } ; list [ 1 ] = new int [ ] { 1 , 2 , 4 } ; list [ 2 ] = new int [ ] { 2 , 1 , 3 , 5 } ; list [ 3 ] = new int [ ] { 3 , 6 , 2 } ; list [ 4 ] = new int [ ] { 4 , 1 , 7 , 5 } ; list [ 5 ] = new int [ ] { 5 , 4 , 6 , 2 , 8 } ; list [ 6 ] = new int [ ] { 6 , 3 , 5 , 9 } ; list [ 7 ] = new int [ ] { 7 , 4 , 8 } ; list [ 8 ] = new int [ ] { 8 , 5 , 0 , 7 , 9 } ; list [ 9 ] = new int [ ] { 9 , 6 , 8 } ; int [ ] Arr = new int [ ] { 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 } ; for ( int i = 2 ; i <= n ; i ++ ) { int [ ] Arr2 = new int [ MAX ] ; for ( int j = 0 ; j < MAX ; j ++ ) { for ( int x = 0 ; x < list [ j ] . Length ; x ++ ) { Arr2 [ list [ j ] [ x ] ] += Arr [ j ] ; } } for ( int j = 0 ; j < MAX ; j ++ ) Arr [ j ] = Arr2 [ j ] ; } int sum = 0 ; for ( int i = 0 ; i < MAX ; i ++ ) sum += Arr [ i ] ; return sum ; } public static void Main ( ) { int n = 2 ; Console . WriteLine ( getCount ( n ) ) ; } }


"Mode in a stream of integers ( running integers ) | C # implementation of the above approach ; Function that prints the Mode values ; Map used to map integers to its frequency ; To store the maximum frequency ; To store the element with the maximum frequency ; Loop used to read the elements one by one ; Updates the frequency of that element ; Checks for maximum Number of occurrence ; Updates the maximum frequency ; Updates the Mode ; Driver Code ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static void findMode ( int [ ] a , int n ) { Dictionary < int , int > map = new Dictionary < int , int > ( ) ; int max = 0 ; int mode = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( map . ContainsKey ( a [ i ] ) ) { map [ a [ i ] ] = map [ a [ i ] ] + 1 ; } else { map . Add ( a [ i ] , 1 ) ; } if ( map [ a [ i ] ] >= max ) { max = map [ a [ i ] ] ; mode = a [ i ] ; } Console . Write ( mode ) ; Console . Write ( " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 7 , 3 , 2 , 5 } ; int n = arr . Length ; findMode ( arr , n ) ; } }


"Mode of frequencies of given array elements | C # program of the above approach ; Function to find the mode of the frequency of the array ; Stores the frequencies of array elements ; Traverse through array elements and count frequencies ; Stores the frequencies 's of  frequencies of array elements ; Stores the minimum value ; Find the Mode in 2 nd map ; Search for this Mode ; When mode is find then return to main function . ; If mode is not found ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int countFreq ( int [ ] arr , int n ) { Dictionary < int , int > mp1 = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( mp1 . ContainsKey ( arr [ i ] ) ) { mp1 [ arr [ i ] ] = mp1 [ arr [ i ] ] + 1 ; } else { mp1 . Add ( arr [ i ] , 1 ) ; } } Dictionary < int , int > mp2 = new Dictionary < int , int > ( ) ; foreach ( KeyValuePair < int , int > it in mp1 ) { if ( mp2 . ContainsKey ( it . Value ) ) { mp2 [ it . Value ] = mp2 [ it . Value ] + 1 ; } else { mp2 . Add ( it . Value , 1 ) ; } } int M = int . MinValue ; foreach ( KeyValuePair < int , int > it in mp2 ) { M = Math . Max ( M , it . Value ) ; } foreach ( KeyValuePair < int , int > it in mp2 ) { if ( M == it . Value ) { return it . Key ; } } return 0 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 6 , 10 , 3 , 10 , 8 , 3 , 6 , 4 } ; int n = arr . Length ; Console . Write ( countFreq ( arr , n ) ) ; } }


"Mode | C # Program for Mode using Counting Sort technique ; Function that sort input array a [ ] and calculate mode and median using counting sort . ; variable to store max of input array which will to have size of count array ; auxiliary ( count ) array to store count . Initialize count array as 0. Size of count array will be equal to ( max + 1 ) . ; Store count of each element of input array ; mode is the index with maximum count ; Driver Code"
Solution in C#:

using System ; using System . Linq ; public class GFG { static void printMode ( int [ ] a , int n ) { int max = a . Max ( ) ; int t = max + 1 ; int [ ] count = new int [ t ] ; for ( int i = 0 ; i < t ; i ++ ) count [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) count [ a [ i ] ] ++ ; int mode = 0 ; int k = count [ 0 ] ; for ( int i = 1 ; i < t ; i ++ ) { if ( count [ i ] > k ) { k = count [ i ] ; mode = i ; } } Console . WriteLine ( " mode ▁ = ▁ " + mode ) ; } static public void Main ( ) { int [ ] a = { 1 , 4 , 1 , 2 , 7 , 1 , 2 , 5 , 3 , 6 } ; int n = a . Length ; printMode ( a , n ) ; } }


"Modify N by adding its smallest positive divisor exactly K times | C # program to implement the above approach ; Function to find the smallest divisor of N greater than 1 ; If i is a divisor of N ; If N is a prime number ; Function to find the value of N by performing the operations K times ; If N is an even number ; Update N ; If N is an odd number ; Update N ; Driver code"
Solution in C#:

using System ; class GFG { static int smallestDivisorGr1 ( int N ) { for ( int i = 2 ; i <= Math . Sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { return i ; } } return N ; } static int findValOfNWithOperat ( int N , int K ) { if ( N % 2 == 0 ) { N += K * 2 ; } else { N += smallestDivisorGr1 ( N ) + ( K - 1 ) * 2 ; } return N ; } static public void Main ( ) { int N = 6 , K = 4 ; Console . Write ( findValOfNWithOperat ( N , K ) ) ; } }


"Modify N by adding its smallest positive divisor exactly K times | C # program to implement the above approach ; Function to find the smallest divisor of N greater than 1 ; If i is a divisor of N ; If N is a prime number ; Function to find the value of N by performing the operations K times ; Iterate over the range [ 1 , K ] ; Update N ; Driver Code"
Solution in C#:

using System ; public class GFG { static int smallestDivisorGr1 ( int N ) { for ( int i = 2 ; i <= Math . Sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { return i ; } } return N ; } static int findValOfNWithOperat ( int N , int K ) { for ( int i = 1 ; i <= K ; i ++ ) { N += smallestDivisorGr1 ( N ) ; } return N ; } public static void Main ( String [ ] args ) { int N = 6 , K = 4 ; Console . Write ( findValOfNWithOperat ( N , K ) ) ; } }


"Modify a Binary String by flipping characters such that any pair of indices consisting of 1 s are neither co | C # program to implement the above approach ; Function to modify a string such that there doesn 't exist any pair  of indices consisting of 1s, whose  GCD is 1 and are divisible by each other ; Flips characters at indices 4 N , 4 N - 2 , 4 N - 4 . ... upto N terms ; Print the string ; Driver Code ; Initialize the string S ; function call"
Solution in C#:

using System ; public class GFG { public static void findString ( char [ ] S , int N ) { int strLen = 4 * N ; for ( int i = 1 ; i <= N ; i ++ ) { S [ strLen - 1 ] = '1' ; strLen -= 2 ; } Console . WriteLine ( S ) ; } public static void Main ( String [ ] args ) { int N = 2 ; char [ ] S = new char [ 4 * N ] ; for ( int i = 0 ; i < 4 * N ; i ++ ) S [ i ] = '0' ; findString ( S , N ) ; } }


"Modify a binary array to Bitwise AND of all elements as 1 | C # implementation of the above approach ; Function to check if it is possible or not ; Driver code"
Solution in C#:

 using System ; class GFG { static bool check ( int [ ] a , int n ) { for ( int i = 0 ; i < n ; i ++ ) if ( a [ i ] == 1 ) return true ; return false ; } public static void Main ( ) { int [ ] a = { 0 , 1 , 0 , 1 } ; int n = a . Length ; if ( check ( a , n ) == true ) Console . Write ( " YES
 " ) ; else Console . Write ( " NO
 " ) ; } }


"Modify a binary tree to get preorder traversal using right pointers only | C # code to modify binary tree for traversal using only right pointer ; A binary tree node has data , left child and right child ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; An iterative process to set the right pointer of Binary tree ; Base Case ; Create an empty stack and Push root to it ; Pop all items one by one . Do following for every Popped item a ) print it b ) Push its right child c ) Push its left child Note that right child is Pushed first so that left is processed first ; Pop the top item from stack ; Push right and left children of the Popped node to stack ; check if some previous node exists ; set the right pointer of previous node to currrent ; set previous node as current node ; printing using right pointer only ; Driver code ; Constructed binary tree is 10 / \ 8 2 / \ 3 5"
Solution in C#:

using System ; using System . Collections ; class GfG { public class Node { public int data ; public Node left ; public Node right ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } static void modifytree ( Node root ) { if ( root == null ) return ; Stack nodeStack = new Stack ( ) ; nodeStack . Push ( root ) ; Node pre = null ; while ( nodeStack . Count != 0 ) { Node node = ( Node ) nodeStack . Peek ( ) ; nodeStack . Pop ( ) ; if ( node . right != null ) nodeStack . Push ( node . right ) ; if ( node . left != null ) nodeStack . Push ( node . left ) ; if ( pre != null ) { pre . right = node ; } pre = node ; } } static void printpre ( Node root ) { while ( root != null ) { Console . Write ( root . data + " ▁ " ) ; root = root . right ; } } public static void Main ( String [ ] args ) { Node root = newNode ( 10 ) ; root . left = newNode ( 8 ) ; root . right = newNode ( 2 ) ; root . left . left = newNode ( 3 ) ; root . left . right = newNode ( 5 ) ; modifytree ( root ) ; printpre ( root ) ; } }


"Modify a binary tree to get preorder traversal using right pointers only | C # code to modify binary tree for traversal using only right pointer ; A binary tree node has data , left child and right child ; function that allocates a new node with the given data and null left and right pointers . ; Function to modify tree ; if the left tree exists ; get the right - most of the original left subtree ; set root right to left subtree ; if the right subtree does not exists we are done ! ; set right pointer of right - most of the original left subtree ; modify the rightsubtree ; printing using right pointer only ; Driver cde ; Constructed binary tree is 10 / \ 8 2 / \ 3 5"
Solution in C#:

using System ; class GFG { public class Node { public int data ; public Node left ; public Node right ; } ; static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } static Node modifytree ( Node root ) { Node right = root . right ; Node rightMost = root ; if ( root . left != null ) { rightMost = modifytree ( root . left ) ; root . right = root . left ; root . left = null ; } if ( right == null ) return rightMost ; rightMost . right = right ; rightMost = modifytree ( right ) ; return rightMost ; } static void printpre ( Node root ) { while ( root != null ) { Console . Write ( root . data + " ▁ " ) ; root = root . right ; } } public static void Main ( String [ ] args ) { Node root = newNode ( 10 ) ; root . left = newNode ( 8 ) ; root . right = newNode ( 2 ) ; root . left . left = newNode ( 3 ) ; root . left . right = newNode ( 5 ) ; modifytree ( root ) ; printpre ( root ) ; } }


"Modify a bit at a given position | C # program to modify a bit at position p in n to b . ; Returns modified n . ; Driver Code"
Solution in C#:

using System ; class GFG { public static int modifyBit ( int n , int p , int b ) { int mask = 1 << p ; return ( n & ~ mask ) | ( ( b << p ) & mask ) ; } static public void Main ( ) { Console . WriteLine ( modifyBit ( 6 , 2 , 0 ) ) ; Console . WriteLine ( modifyBit ( 6 , 5 , 1 ) ) ; } }


"Modify a given array by replacing each element with the sum or product of their digits based on a given condition | C # program for the above approach ; Function to modify the given array as per the given conditions ; Traverse the given array arr [ ] ; Initialize the count of even and odd digits ; Initialize temp with the current array element ; For count the number of even digits ; Increment the odd count ; Otherwise ; Divide temp by 10 ; Performe addition ; Performe multiplication ; Otherwise ; Driver Code"
Solution in C#:

using System ; class GFG { static void evenOdd ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int even_digits = 0 ; int odd_digits = 0 ; int temp = arr [ i ] ; while ( temp > 0 ) { if ( ( temp % 10 ) % 2 != 0 ) odd_digits ++ ; else even_digits ++ ; temp /= 10 ; } if ( even_digits > odd_digits ) { int res = 0 ; while ( arr [ i ] > 0 ) { res += arr [ i ] % 10 ; arr [ i ] /= 10 ; } Console . Write ( res + " ▁ " ) ; } else if ( odd_digits > even_digits ) { int res = 1 ; while ( arr [ i ] > 0 ) { res *= arr [ i ] % 10 ; arr [ i ] /= 10 ; } Console . Write ( res + " ▁ " ) ; } else Console . Write ( arr [ i ] + " ▁ " ) ; } } public static void Main ( ) { int [ ] arr = { 113 , 141 , 214 , 3186 } ; int N = arr . Length ; evenOdd ( arr , N ) ; } }


"Modify a given matrix by placing sorted boundary elements in clockwise manner | C # program for the above approach ; Function to print the elements of the matrix in row - wise manner ; Function to sort boundary elements of a matrix starting from the outermost to the innermost boundary and place them in a clockwise manner ; k - starting row index m - ending row index l - starting column index n - ending column index i - iterator ; Stores the current boundary elements ; Push the first row ; Push the last column ; Push the last row ; Push the first column ; Sort the boundary elements ; Update the first row ; Update the last column ; Update the last row ; Update the first column ; Print the resultant matrix ; Driver code ; Given matrix"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void printMatrix ( List < List < int > > a ) { foreach ( List < int > x in a ) { foreach ( int y in x ) { Console . Write ( y + " ▁ " ) ; } Console . WriteLine ( ) ; } } static void sortBoundaryWise ( List < List < int > > a ) { int i , k = 0 , l = 0 ; int m = a . Count , n = a [ 0 ] . Count ; int n_k = 0 , n_l = 0 , n_m = m , n_n = n ; while ( k < m && l < n ) { List < int > boundary = new List < int > ( ) ; for ( i = l ; i < n ; ++ i ) { boundary . Add ( a [ k ] [ i ] ) ; } k ++ ; for ( i = k ; i < m ; ++ i ) { boundary . Add ( a [ i ] [ n - 1 ] ) ; } n -- ; if ( k < m ) { for ( i = n - 1 ; i >= l ; -- i ) { boundary . Add ( a [ m - 1 ] [ i ] ) ; } m -- ; } if ( l < n ) { for ( i = m - 1 ; i >= k ; -- i ) { boundary . Add ( a [ i ] [ l ] ) ; } l ++ ; } boundary . Sort ( ) ; int ind = 0 ; for ( i = n_l ; i < n_n ; ++ i ) { a [ n_k ] [ i ] = boundary [ ind ++ ] ; } n_k ++ ; for ( i = n_k ; i < n_m ; ++ i ) { a [ i ] [ n_n - 1 ] = boundary [ ind ++ ] ; } n_n -- ; if ( n_k < n_m ) { for ( i = n_n - 1 ; i >= n_l ; -- i ) { a [ n_m - 1 ] [ i ] = boundary [ ind ++ ] ; } n_m -- ; } if ( n_l < n_n ) { for ( i = n_m - 1 ; i >= n_k ; -- i ) { a [ i ] [ n_l ] = boundary [ ind ++ ] ; } n_l ++ ; } } printMatrix ( a ) ; } static void Main ( ) { List < List < int > > matrix = new List < List < int > > ( ) ; matrix . Add ( new List < int > ( new int [ ] { 9 , 7 , 4 , 5 } ) ) ; matrix . Add ( new List < int > ( new int [ ] { 1 , 6 , 2 , - 6 } ) ) ; matrix . Add ( new List < int > ( new int [ ] { 12 , 20 , 2 , 0 } ) ) ; matrix . Add ( new List < int > ( new int [ ] { - 5 , - 6 , 7 , - 2 } ) ) ; sortBoundaryWise ( matrix ) ; } }


"Modify a matrix by converting each element to XOR of its digits | C # program for the above approach ; Function to calculate Bitwise XOR of digits present in X ; Stores the Bitwise XOR ; While X is true ; Update Bitwise XOR of its digits ; Return the result ; Function to print matrix after converting each matrix element to XOR of its digits ; Traverse each row of arr [ ] [ ] ; Traverse each column of arr [ ] [ ] ; Function to convert the given matrix to required XOR matrix ; Traverse each row of arr [ ] [ ] ; Traverse each column of arr [ ] [ ] ; Store the current matrix element ; Find the xor of digits present in X ; Stores the XOR value ; Print resultant matrix ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int M = 3 ; static int N = 3 ; static int findXOR ( int X ) { int ans = 0 ; while ( X != 0 ) { ans ^= ( X % 10 ) ; X /= 10 ; } return ans ; } static void printXORmatrix ( int [ , ] arr ) { for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { Console . Write ( arr [ i , j ] + " ▁ " ) ; } Console . WriteLine ( ) ; } } static void convertXOR ( int [ , ] arr ) { for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { int X = arr [ i , j ] ; int temp = findXOR ( X ) ; arr [ i , j ] = temp ; } } printXORmatrix ( arr ) ; } static public void Main ( ) { int [ , ] arr = { { 27 , 173 , 5 } , { 21 , 6 , 624 } , { 5 , 321 , 49 } } ; convertXOR ( arr ) ; } }


"Modify a numeric string to a balanced parentheses by replacements | C # program for the above approach ; Function to check if the given string can be converted to a balanced bracket sequence or not ; Check if the first and last characters are equal ; Initialize two variables to store the count of open and closed brackets ; If the current character is same as the first character ; If the current character is same as the last character ; If count of open brackets becomes less than 0 ; Print the new string ; If the current character is same as the first character ; If bracket sequence is not balanced ; Check for unbalanced bracket sequence ; Print the sequence ; Driver Code ; Given Input ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void balBracketSequence ( string str ) { int n = str . Length ; if ( str [ 0 ] == str [ n - 1 ] ) { Console . Write ( " No " ) ; } else { int cntForOpen = 0 , cntForClose = 0 ; int check = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == str [ 0 ] ) cntForOpen ++ ; else if ( str [ i ] == str [ n - 1 ] ) cntForOpen -- ; else cntForOpen ++ ; if ( cntForOpen < 0 ) { check = 0 ; break ; } } if ( check != 0 && cntForOpen == 0 ) { Console . Write ( " Yes , ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == str [ n - 1 ] ) Console . Write ( ' ) ' ) ; else Console . Write ( ' ( ' ) ; } return ; } else { for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == str [ 0 ] ) cntForClose ++ ; else cntForClose -- ; if ( cntForClose < 0 ) { check = 0 ; break ; } } if ( check != 0 && cntForClose == 0 ) { Console . Write ( " Yes , ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == str [ 0 ] ) Console . Write ( ' ( ' ) ; else Console . Write ( ' ) ' ) ; } return ; } } Console . Write ( " No " ) ; } } public static void Main ( ) { string str = "123122" ; balBracketSequence ( str ) ; } }


"Modify a sentence by reversing order of occurrences of all Palindrome Words | C # implementation of the above approach ; Function to check if a string S is a palindrome ; Function to print the modified string after reversing teh order of occurrences of all palindromic words in the sentence ; Stores the palindromic words ; Stores the words in the list ; Traversing the list ; If current word is a palindrome ; Update newlist ; Reverse the newlist ; Traverse the list ; If current word is a palindrome ; Update lis [ i ] ; Increment j ; Print the updated sentence ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static bool palindrome ( string str ) { int st = 0 ; int ed = str . Length - 1 ; while ( st < ed ) { if ( str [ st ] == str [ ed ] ) { st ++ ; ed -- ; } else return false ; } return true ; } static void printReverse ( string sentence ) { List < string > newlist = new List < string > ( ) ; List < string > lis = new List < string > ( ) ; string temp = " " ; foreach ( char x in sentence ) { if ( x == ' ▁ ' ) { lis . Add ( temp ) ; temp = " " ; } else temp += x ; } lis . Add ( temp ) ; foreach ( string x in lis ) { if ( palindrome ( x ) ) newlist . Add ( x ) ; } newlist . Reverse ( ) ; int j = 0 ; for ( int i = 0 ; i < lis . Count ; i ++ ) { if ( palindrome ( lis [ i ] ) ) { lis [ i ] = newlist [ j ] ; j = j + 1 ; } } foreach ( string x in lis ) Console . Write ( x + " ▁ " ) ; } public static void Main ( ) { string sentence = " mom ▁ and ▁ dad ▁ went ▁ to ▁ eye ▁ hospital " ; printReverse ( sentence ) ; } }


"Modify a string by circularly shifting each character to the right by respective frequencies | C # program for the above approach ; Function to replace the characters by its frequency of character in it ; Stores frequencies of characters in the string S ; Traverse the string S ; Increment the frequency of each character by 1 ; Traverse the string S ; Find the frequency of the current character ; Update the character ; Print the resultant string ; Driver Code"
Solution in C#:

using System ; class GFG { static void addFrequencyToCharacter ( string Str ) { int [ ] frequency = new int [ 26 ] ; int N = Str . Length ; char [ ] S = Str . ToCharArray ( ) ; for ( int i = 0 ; i < N ; i ++ ) { frequency [ S [ i ] - ' a ' ] += 1 ; } for ( int i = 0 ; i < N ; i ++ ) { int add = frequency [ S [ i ] - ' a ' ] % 26 ; if ( ( int ) ( S [ i ] ) + add <= ( int ) ( ' z ' ) ) S [ i ] = ( char ) ( ( int ) ( S [ i ] ) + add ) ; else { add = ( ( int ) ( S [ i ] ) + add ) - ( ( int ) ( ' z ' ) ) ; S [ i ] = ( char ) ( ( int ) ( ' a ' ) + add - 1 ) ; } } Console . Write ( new string ( S ) ) ; } public static void Main ( string [ ] args ) { string S = " geeksforgeeks " ; addFrequencyToCharacter ( S ) ; } }


"Modify a string by performing given shift operations | C # implementataion of above approach ; Function to find the string obtained after performing given shift operations ; If shift [ i , 0 ] = 0 , then left shift Otherwise , right shift ; Stores length of the string ; Effective shift calculation ; Stores modified string ; Right rotation ; Left rotation ; Driver Code"
Solution in C#:

using System ; public class GFG { static void stringShift ( String s , int [ , ] shift ) { int val = 0 ; for ( int i = 0 ; i < shift . GetLength ( 0 ) ; ++ i ) if ( shift [ i , 0 ] == 0 ) val -= shift [ i , 1 ] ; else val += shift [ i , 1 ] ; int len = s . Length ; val = val % len ; String result = " " ; if ( val > 0 ) result = s . Substring ( len - val , val ) + s . Substring ( 0 , len - val ) ; else result = s . Substring ( - val , len ) + s . Substring ( 0 , - val ) ; Console . WriteLine ( result ) ; } public static void Main ( String [ ] args ) { String s = " abc " ; int [ , ] shift = new int [ , ] { { 0 , 1 } , { 1 , 2 } } ; stringShift ( s , shift ) ; } }


"Modify array by making all array elements equal to 0 by subtracting K ^ i from an array element in every i | C # program for the above approach ; Function to check whether all array elements can be made zero or not ; Stores if a power of K has already been subtracted or not ; Stores all the Kth power ; Iterate until X is less than INT_MAX ; Traverse the array arr [ ] ; Iterate over the range [ 0 , M ] ; If MP [ V [ j ] ] is 0 and V [ j ] is less than or equal to arr [ i ] ; If arr [ i ] is not 0 ; If i is less than N ; Otherwise , ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static string isMakeZero ( int [ ] arr , int N , int K ) { Dictionary < int , int > MP = new Dictionary < int , int > ( ) ; List < int > V = new List < int > ( ) ; int X = 1 ; int i ; while ( X > 0 && X < Int32 . MaxValue ) { V . Add ( X ) ; X *= K ; } for ( i = 0 ; i < N ; i ++ ) { for ( int j = V . Count - 1 ; j >= 0 ; j -- ) { if ( MP . ContainsKey ( V [ j ] ) == false && V [ j ] <= arr [ i ] ) { arr [ i ] -= V [ j ] ; MP [ V [ j ] ] = 1 ; } } if ( arr [ i ] != 0 ) break ; } if ( i < N ) return " No " ; else return " Yes " ; } public static void Main ( String [ ] args ) { int [ ] arr = { 8 , 0 , 3 , 4 , 80 } ; int N = arr . Length ; int K = 2 ; Console . WriteLine ( isMakeZero ( arr , N , K ) ) ; } }


"Modify array by merging elements with addition such that it consists of only Primes . | C # program to find the array of primes ; dp array to store the ans for max 20 elements ; To check whether the number is prime or not ; Function to check whether the array can be modify so that there are only primes ; If curr is prime and all elements are visited , return true ; If all elements are not visited , set curr = 0 , to search for new prime sum ; If all elements are visited ; If the current sum is not prime return false ; If this state is already calculated , return the answer directly ; Try all state of mask ; If ith index is not set ; Add the current element and set ith index and recur ; If subset can be formed then return true ; After every possibility of mask , if the subset is not formed , return false by memoizing . ; Driver code"
Solution in C#:

using System ; class GFG { static bool [ ] dp = new bool [ 1 << 20 ] ; static bool isprime ( int n ) { if ( n == 1 ) return false ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { return false ; } } return true ; } static bool solve ( int [ ] arr , int curr , int mask , int n ) { if ( isprime ( curr ) ) { if ( mask == ( 1 << n ) - 1 ) { return true ; } curr = 0 ; } if ( mask == ( 1 << n ) - 1 ) { if ( ! isprime ( curr ) ) { return false ; } } if ( dp [ mask ] ) return dp [ mask ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( mask & ( 1 << i ) ) == 0 ) { if ( solve ( arr , curr + arr [ i ] , mask 1 << i , n ) ) { return true ; } } } return dp [ mask ] = false ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 6 , 7 , 13 } ; int n = arr . Length ; if ( solve ( arr , 0 , 0 , n ) ) { Console . Write ( " YES " ) ; } else { Console . Write ( " NO " ) ; } } }


"Modify array by removing ( arr [ i ] + arr [ i + 1 ] ) th element exactly K times | C # program for the above approach ; Function to modify array by removing every K - th element from the array ; Check if current element is the k - th element ; Stores the elements after removing every kth element ; Append the current element if it is not k - th element ; Return the new array after removing every k - th element ; Function to print the array ; Traverse the array l [ ] ; Function to print the array after performing the given operations exactly k times ; Store first N natural numbers ; Iterate over the range [ 0 , k - 1 ] ; Store sums of the two consecutive terms ; Remove every p - th element from the array ; Increment x by 1 for the next iteration ; Print the resultant array ; Driver Code ; Given arrays ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static List < int > removeEveryKth ( List < int > l , int k ) { for ( int i = 0 ; i < l . Count ; i ++ ) { if ( i % k == 0 ) l [ i ] = 0 ; } List < int > arr = new List < int > ( ) ; arr . Add ( 0 ) ; for ( int i = 1 ; i < l . Count ; i ++ ) { if ( l [ i ] != 0 ) arr . Add ( l [ i ] ) ; } return arr ; } static void printArray ( List < int > l ) { for ( int i = 1 ; i < l . Count ; i ++ ) Console . Write ( l [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } static void printSequence ( int n , int k ) { List < int > l = new List < int > ( ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) l . Add ( i ) ; int x = 1 ; for ( int i = 0 ; i < k ; i ++ ) { int p = l [ x ] + l [ x + 1 ] ; l = removeEveryKth ( l , p ) ; x += 1 ; } printArray ( l ) ; } public static void Main ( ) { int N = 8 ; int K = 2 ; printSequence ( N , K ) ; } }