"Modify array by replacing elements with the nearest power of its previous or next element | C # program for the above approach ; Function to calculate the power of y which is nearest to x ; Base Case ; Stores the logarithmic value of x with base y ; Function to replace each array element by the nearest power of its previous or next element ; Stores the previous and next element ; Traverse the array ; Calculate nearest power for previous and next elements ; Replacing the array values ; Print the updated array ; Driver Code ; Given array"
Solution in C#:

using System ; class GFG { static int nearestPow ( int x , int y ) { if ( y == 1 ) return 1 ; int k = ( int ) ( Math . Log ( x , y ) ) ; if ( Math . Abs ( Math . Pow ( y , k ) - x ) < Math . Abs ( Math . Pow ( y , ( k + 1 ) ) - x ) ) return ( int ) ( Math . Pow ( y , k ) ) ; return ( int ) ( Math . Pow ( y , ( k + 1 ) ) ) ; } static void replacebyNearestPower ( int [ ] arr ) { int prev = arr [ arr . Length - 1 ] ; int lastNext = arr [ 0 ] ; int next = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { int temp = arr [ i ] ; if ( i == arr . Length - 1 ) next = lastNext ; else next = arr [ ( i + 1 ) % arr . Length ] ; int prevPow = nearestPow ( arr [ i ] , prev ) ; int nextPow = nearestPow ( arr [ i ] , next ) ; if ( Math . Abs ( arr [ i ] - prevPow ) < Math . Abs ( arr [ i ] - nextPow ) ) arr [ i ] = prevPow ; else arr [ i ] = nextPow ; prev = temp ; } for ( int i = 0 ; i < arr . Length ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } public static void Main ( ) { int [ ] arr = { 2 , 3 , 4 , 1 , 2 } ; replacebyNearestPower ( arr ) ; } }


"Modify array by replacing every array element with minimum possible value of arr [ j ] + | j | C # program for the above approach ; Function to find minimum value of arr [ j ] + | j - i | for every array index ; Stores minimum of a [ j ] + | i - j | upto position i ; Stores minimum of a [ j ] + | i - j | upto position i from the end ; Traversing and storing minimum of a [ j ] + | i - j | upto i ; Traversing and storing minimum of a [ j ] + | i - j | upto i from the end ; Traversing from [ 0 , N ] and storing minimum of a [ j ] + | i - j | from starting and end ; Print the required array ; Driver code ; Given array [ ] arr ; Size of the array ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void minAtEachIndex ( int n , int [ ] arr ) { int [ ] dp1 = new int [ n ] ; int [ ] dp2 = new int [ n ] ; int i ; dp1 [ 0 ] = arr [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) dp1 [ i ] = Math . Min ( arr [ i ] , dp1 [ i - 1 ] + 1 ) ; dp2 [ n - 1 ] = arr [ n - 1 ] ; for ( i = n - 2 ; i >= 0 ; i -- ) dp2 [ i ] = Math . Min ( arr [ i ] , dp2 [ i + 1 ] + 1 ) ; List < int > v = new List < int > ( ) ; for ( i = 0 ; i < n ; i ++ ) v . Add ( Math . Min ( dp1 [ i ] , dp2 [ i ] ) ) ; foreach ( int x in v ) Console . Write ( x + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 4 , 2 , 5 , 3 } ; int N = arr . Length ; minAtEachIndex ( N , arr ) ; } }


"Modify array by sorting nearest perfect squares of array elements having their digits sorted in decreasing order | C # program of the above approach ; Function to sort array in ascending order after replacing array elements by nearest perfect square of decreasing order of digits ; Traverse the array of strings ; Convert the current array element to a string ; Sort each string in descending order ; Convert the string to equivalent integer ; Calculate square root of current array element ; Calculate perfect square ; Find the nearest perfect square ; Sort the array in ascending order ; Print the array ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void sortArr ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int num = arr [ i ] ; string s = num . ToString ( ) ; char [ ] temp = s . ToCharArray ( ) ; Array . Sort ( temp ) ; int st = 0 ; int ed = temp . Length - 1 ; while ( st < ed ) { char ch = temp [ st ] ; temp [ st ] = temp [ ed ] ; temp [ ed ] = ch ; st ++ ; ed -- ; } string charsStr = new string ( temp ) ; sort ( s . begin ( ) , s . end ( ) , greater < char > ( ) ) ; arr [ i ] = Int32 . Parse ( charsStr ) ; int sr = ( int ) Math . Sqrt ( arr [ i ] ) ; int a = sr * sr ; int b = ( sr + 1 ) * ( sr + 1 ) ; if ( ( arr [ i ] - a ) < ( b - arr [ i ] ) ) arr [ i ] = a ; else arr [ i ] = b ; } Array . Sort ( arr ) ; for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } public static void Main ( ) { int [ ] arr = { 29 , 43 , 28 , 12 } ; int N = arr . Length ; sortArr ( arr , N ) ; } }


"Modify array of strings by replacing characters repeating in the same or remaining strings | C # program for the above approach ; Function to remove duplicate characters across the strings ; Stores distinct characters ; Size of the array ; Stores the list of modified strings ; Traverse the array ; Stores the modified string ; Iterate over the characters of the modified string ; If character is already present ; Insert character into the Set ; Print the list of modified strings ; Print each string ; Driver Code ; Given array of strings ; Function Call to modify the given array of strings"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void removeDuplicateCharacters ( string [ ] arr ) { HashSet < int > cset = new HashSet < int > ( ) ; int n = arr . Length ; List < string > Out = new List < string > ( ) ; foreach ( string str in arr ) { string out_curr = " " ; foreach ( char ch in str . ToCharArray ( ) ) { if ( cset . Contains ( ch ) ) continue ; out_curr += ch ; cset . Add ( ch ) ; } if ( out_curr . Length != 0 ) Out . Add ( out_curr ) ; } for ( int i = 0 ; i < Out . Count ; i ++ ) { Console . Write ( Out [ i ] + " ▁ " ) ; } } static public void Main ( ) { string [ ] arr = { " Geeks " , " For " , " Geeks " , " Post " } ; removeDuplicateCharacters ( arr ) ; } }


"Modify array to maximize sum of adjacent differences | C # program to get maximum consecutive element difference sum ; Returns maximum - difference - sum with array modifications allowed . ; Initialize dp [ ] [ ] with 0 values . ; for [ i + 1 ] [ 0 ] ( i . e . current modified value is 1 ) , choose maximum from dp [ i ] [ 0 ] + abs ( 1 - 1 ) = dp [ i ] [ 0 ] and dp [ i ] [ 1 ] + abs ( 1 - arr [ i ] ) ; for [ i + 1 ] [ 1 ] ( i . e . current modified value is arr [ i + 1 ] ) , choose maximum from dp [ i ] [ 0 ] + abs ( arr [ i + 1 ] - 1 ) and dp [ i ] [ 1 ] + abs ( arr [ i + 1 ] - arr [ i ] ) ; Driver code"
Solution in C#:

using System ; class GFG { static int maximumDifferenceSum ( int [ ] arr , int N ) { int [ , ] dp = new int [ N , 2 ] ; for ( int i = 0 ; i < N ; i ++ ) dp [ i , 0 ] = dp [ i , 1 ] = 0 ; for ( int i = 0 ; i < ( N - 1 ) ; i ++ ) { dp [ i + 1 , 0 ] = Math . Max ( dp [ i , 0 ] , dp [ i , 1 ] + Math . Abs ( 1 - arr [ i ] ) ) ; dp [ i + 1 , 1 ] = Math . Max ( dp [ i , 0 ] + Math . Abs ( arr [ i + 1 ] - 1 ) , dp [ i , 1 ] + Math . Abs ( arr [ i + 1 ] - arr [ i ] ) ) ; } return Math . Max ( dp [ N - 1 , 0 ] , dp [ N - 1 , 1 ] ) ; } public static void Main ( ) { int [ ] arr = { 3 , 2 , 1 , 4 , 5 } ; int N = arr . Length ; Console . Write ( maximumDifferenceSum ( arr , N ) ) ; } }


"Modify characters of a string by adding integer values of same | C # program for the above approach ; Function to modify a given String by adding ASCII value of characters from a String S to integer values of same indexed characters in String N ; Traverse the String ; Stores integer value of character in String N ; Stores ASCII value of character in String S ; If b exceeds 122 ; Replace the character ; Print resultant String ; Driver Code ; Given Strings ; Function call to modify String S by given operations"
Solution in C#:

using System ; public class GFG { static void addASCII ( char [ ] S , char [ ] N ) { for ( int i = 0 ; i < S . Length ; i ++ ) { int a = ( int ) ( N [ i ] ) - '0' ; int b = ( int ) ( S [ i ] ) + a ; if ( b > 122 ) b -= 26 ; S [ i ] = ( char ) ( b ) ; } Console . Write ( S ) ; } public static void Main ( String [ ] args ) { String S = " sun " , N = "966" ; addASCII ( S . ToCharArray ( ) , N . ToCharArray ( ) ) ; } }


"Modify contents of Linked List | C # implementation to modify the contents of the linked list ; Linked list node ; Function to insert a node at the beginning of the linked list ; allocate node ; put in the data ; link the old list at the end of the new node ; move the head to point to the new node ; Split the nodes of the given list into front and back halves , and return the two lists using the reference parameters . Uses the fast / slow pointer strategy . ; Advance ' fast ' two nodes , and advance ' slow ' one node ; ' slow ' is before the midpoint in the list , so split it in two at that point . ; Function to reverse the linked list ; perfrom the required subtraction operation on the 1 st half of the linked list ; traversing both the lists simultaneously ; subtraction operation and node data modification ; function to concatenate the 2 nd ( back ) list at the end of the 1 st ( front ) list and returns the head of the new list ; function to modify the contents of the linked list ; if list is empty or contains only single node ; split the list into two halves front and back lists ; reverse the 2 nd ( back ) list ; modify the contents of 1 st half ; agains reverse the 2 nd ( back ) list ; concatenating the 2 nd list back to the end of the 1 st list ; pointer to the modified list ; function to print the linked list ; Driver Code ; creating the linked list ; modify the linked list ; print the modified linked list"
Solution in C#:

using System ; class GFG { public class Node { public int data ; public Node next ; } ; static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } static Node front , back ; static void frontAndBackSplit ( Node head ) { Node slow , fast ; slow = head ; fast = head . next ; while ( fast != null ) { fast = fast . next ; if ( fast != null ) { slow = slow . next ; fast = fast . next ; } } front = head ; back = slow . next ; slow . next = null ; } static Node reverseList ( Node head_ref ) { Node current , prev , next ; current = head_ref ; prev = null ; while ( current != null ) { next = current . next ; current . next = prev ; prev = current ; current = next ; } head_ref = prev ; return head_ref ; } static void modifyTheContentsOf1stHalf ( ) { Node front1 = front , back1 = back ; while ( back1 != null ) { front1 . data = front1 . data - back1 . data ; front1 = front1 . next ; back1 = back1 . next ; } } static Node concatFrontAndBackList ( Node front , Node back ) { Node head = front ; if ( front == null ) return back ; while ( front . next != null ) front = front . next ; front . next = back ; return head ; } static Node modifyTheList ( Node head ) { if ( head == null head . next == null ) return head ; front = null ; back = null ; frontAndBackSplit ( head ) ; back = reverseList ( back ) ; modifyTheContentsOf1stHalf ( ) ; back = reverseList ( back ) ; head = concatFrontAndBackList ( front , back ) ; return head ; } static void printList ( Node head ) { if ( head == null ) return ; while ( head . next != null ) { Console . Write ( head . data + " ▁ - > ▁ " ) ; head = head . next ; } Console . WriteLine ( head . data ) ; } public static void Main ( ) { Node head = null ; head = push ( head , 10 ) ; head = push ( head , 7 ) ; head = push ( head , 12 ) ; head = push ( head , 8 ) ; head = push ( head , 9 ) ; head = push ( head , 2 ) ; head = modifyTheList ( head ) ; Console . WriteLine ( "...


"Modify contents of Linked List | C # implementation to modify the contents of the linked list ; Linked list node ; Function to insert a node at the beginning of the linked list ; allocate node ; put in the data ; link the old list at the end of the new node ; move the head to point to the new node ; function to print the linked list ; Function to middle node of list . ; Advance ' fast ' two nodes , and advance ' slow ' one node ; If number of nodes are odd then update slow by slow . next ; ; function to modify the contents of the linked list . ; Create Stack . ; Traverse the list by using temp until stack is empty . ; Driver code ; creating the linked list ; Call Function to Find the starting point of second half of list . ; Call function to modify the contents of the linked list . ; print the modified linked list"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { public class Node { public int data ; public Node next ; } ; static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } static void printList ( Node head ) { if ( head == null ) { return ; } while ( head . next != null ) { Console . Write ( head . data + " - > " ) ; head = head . next ; } Console . Write ( head . data + "
 " ) ; } static Node find_mid ( Node head ) { Node temp = head , slow = head , fast = head ; while ( fast != null && fast . next != null ) { slow = slow . next ; fast = fast . next . next ; } if ( fast != null ) { slow = slow . next ; } return slow ; } static void modifyTheList ( Node head , Node slow ) { Stack < int > s = new Stack < int > ( ) ; Node temp = head ; while ( slow != null ) { s . Push ( slow . data ) ; slow = slow . next ; } while ( s . Count != 0 ) { temp . data = temp . data - s . Peek ( ) ; temp = temp . next ; s . Pop ( ) ; } } public static void Main ( String [ ] args ) { Node head = null , mid ; head = push ( head , 10 ) ; head = push ( head , 7 ) ; head = push ( head , 12 ) ; head = push ( head , 8 ) ; head = push ( head , 9 ) ; head = push ( head , 2 ) ; mid = find_mid ( head ) ; modifyTheList ( head , mid ) ; Console . Write ( " Modified ▁ List : " + "
 " ) ; printList ( head ) ; } }


"Modify given array by reducing each element by its next smaller element | C # program for the above approach ; Function to print the readonly array after reducing each array element by its next smaller element ; Initialize stack ; Array size ; To store the corresponding element ; If stack is not empty ; If top element is smaller than the current element ; Keep popping until stack is empty or top element is greater than the current element ; If stack is not empty ; Push current element ; Print the readonly array ; Driver Code ; Given array ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void printFinalPrices ( int [ ] arr ) { Stack < int > minStk = new Stack < int > ( ) ; int n = arr . Length ; int [ ] reduce = new int [ n ] ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( minStk . Count != 0 ) { if ( minStk . Peek ( ) <= arr [ i ] ) { reduce [ i ] = minStk . Peek ( ) ; } else { while ( minStk . Count != 0 && ( minStk . Peek ( ) > arr [ i ] ) ) { minStk . Pop ( ) ; } if ( minStk . Count != 0 ) { reduce [ i ] = minStk . Peek ( ) ; } } } minStk . Push ( arr [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] - reduce [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 8 , 4 , 6 , 2 , 3 } ; printFinalPrices ( arr ) ; } }


"Modify given array to a non | C # program to implement the above approach ; Function to check if a non - decreasing array can be obtained by rotating the original array ; Stores copy of original array ; Sort the given vector ; Traverse the array ; Rotate the array by 1 ; If array is sorted ; If it is not possible to sort the array ; Driver code ; Given array ; Size of the array ; Function call to check if it is possible to make array non - decreasing by rotating"
Solution in C#:

using System ; class GFG { static void rotateArray ( int [ ] arr , int N ) { int [ ] v = arr ; Array . Sort ( v ) ; for ( int i = 1 ; i <= N ; ++ i ) { int x = arr [ N - 1 ] ; i = N - 1 ; while ( i > 0 ) { arr [ i ] = arr [ i - 1 ] ; arr [ 0 ] = x ; i -= 1 ; } if ( arr == v ) { Console . Write ( " YES " ) ; return ; } } Console . Write ( " NO " ) ; } public static void Main ( ) { int [ ] arr = { 3 , 4 , 5 , 1 , 2 } ; int N = arr . Length ; rotateArray ( arr , N ) ; } }


"Modify given array to make sum of odd and even indexed elements same | C # program for the above approach ; Function to modify array to make sum of odd and even indexed elements equal ; Stores the count of 0 s , 1 s ; Stores sum of odd and even indexed elements respectively ; Count 0 s ; Count 1 s ; Calculate odd_sum and even_sum ; If both are equal ; Print the original array ; Otherwise ; Print all the 0 s ; For checking even or odd ; Update total count of 1 s ; Print all 1 s ; Driver Code ; Given array [ ] arr ; Function Call"
Solution in C#:

using System ; class GFG { static void makeArraySumEqual ( int [ ] a , int N ) { int count_0 = 0 , count_1 = 0 ; int odd_sum = 0 , even_sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( a [ i ] == 0 ) count_0 ++ ; else count_1 ++ ; if ( ( i + 1 ) % 2 == 0 ) even_sum += a [ i ] ; else if ( ( i + 1 ) % 2 > 0 ) odd_sum += a [ i ] ; } if ( odd_sum == even_sum ) { for ( int i = 0 ; i < N ; i ++ ) Console . Write ( a [ i ] + " ▁ " ) ; } else { if ( count_0 >= N / 2 ) { else { if ( count_0 >= N / 2 ) { for ( int i = 0 ; i < count_0 ; i ++ ) Console . Write ( "0 ▁ " ) ; } else { int is_Odd = count_1 % 2 ; count_1 -= is_Odd ; for ( int i = 0 ; i < count_1 ; i ++ ) Console . Write ( "1 ▁ " ) ; } } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 1 , 0 } ; int N = arr . Length ; makeArraySumEqual ( arr , N ) ; } }


"Modify given string such that odd and even indices is lexicographically largest and smallest | C # program for the above approach ; Function to modify the given string satisfying the given criteria ; Traverse the string S ; If i is even ; If the S [ i ] is ' a ' , then change S [ i ] to ' b ' ; Otherwise , change S [ i ] to ' a ' ; If S [ i ] is ' z ' , then change S [ i ] to ' y ' ; Otherwise , change S [ i ] to ' z ' ; Return the result ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static string performOperation ( string S , int N ) { for ( int i = 0 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { if ( S [ i ] == ' a ' ) { S = S . Substring ( 0 , i ) + ' b ' + S . Substring ( i + 1 ) ; } else { S = S . Substring ( 0 , i ) + ' a ' + S . Substring ( i + 1 ) ; } } else { if ( S [ i ] == ' z ' ) { S = S . Substring ( 0 , i ) + ' y ' + S . Substring ( i + 1 ) ; } else { S = S . Substring ( 0 , i ) + ' z ' + S . Substring ( i + 1 ) ; } } } return S ; } public static void Main ( ) { string S = " giad " ; int N = S . Length ; Console . Write ( performOperation ( S , N ) ) ; } }


"Modify sequence of first N natural numbers to a given array by replacing pairs with their GCD | C # program for the above approach ; Function to check if array arr [ ] can be obtained from first N natural numbers or not ; Driver code ; Function Call"
Solution in C#:

using System ; class GFG { static void isSequenceValid ( int [ ] B , int N ) { for ( int i = 0 ; i < N ; i ++ ) { if ( ( i + 1 ) % B [ i ] != 0 ) { Console . WriteLine ( " No " ) ; return ; } } Console . WriteLine ( " Yes " ) ; } public static void Main ( ) { int N = 4 ; int [ ] arr = { 1 , 2 , 3 , 2 } ; isSequenceValid ( arr , N ) ; } }


"Modify string by increasing each character by its distance from the end of the word | C # implementation of the above approach ; Function to transform the given string ; Size of string ; Stores resultant string ; Iterate over given string ; End of word is reached ; Append the word ; For the last word ; Function to transform and return the transformed word ; Stores resulting word ; Iterate over the word ; Add the position value to the letter ; Convert it back to character ; Add it to the string ; Driver Code ; Given string ; Function Call"
Solution in C#:

using System ; class GFG { public static void manipulate ( String s ) { int n = s . Length ; int i = 0 , j = 0 ; String res = " " ; while ( i < n ) { if ( s [ i ] == ' ▁ ' ) { res += util ( s . Substring ( j , i - j ) ) ; res = res + " ▁ " ; j = i + 1 ; i = j + 1 ; } else { i ++ ; } } res = res + util ( s . Substring ( j , i - j ) ) ; Console . WriteLine ( res ) ; } public static String util ( String sub ) { int n = sub . Length ; int i = 0 ; String ret = " " ; while ( i < n ) { int t = ( sub [ i ] - ' a ' ) + n - 1 - i ; char ch = ( char ) ( t % 26 + 97 ) ; ret = ret + String . Join ( " " , ch ) ; i ++ ; } return ret ; } public static void Main ( String [ ] args ) { String s = " acm ▁ fkz " ; manipulate ( s ) ; } }


"Modify string by inserting characters such that every K | C # program for the above approach ; Function to replace all ' ? ' characters in a string such that the given conditions are satisfied ; Traverse the string to Map the characters with respective positions ; Traverse the string again and replace all unknown characters ; If i % k is not found in the Map M , then return - 1 ; Update S [ i ] ; Print the string S ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void fillString ( string str , int k ) { char [ ] s = str . ToCharArray ( ) ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] != ' ? ' ) { mp [ i % k ] = s [ i ] ; } } for ( int i = 0 ; i < s . Length ; i ++ ) { if ( ! mp . ContainsKey ( i % k ) ) { Console . WriteLine ( - 1 ) ; return ; } s [ i ] = ( char ) mp [ i % k ] ; } Console . WriteLine ( new string ( s ) ) ; } static void Main ( ) { string S = " ? ? ? ? abcd " ; int K = 4 ; fillString ( S , K ) ; } }


"Modify string by rearranging vowels in alphabetical order at their respective indices | C # program for the above approach ; Function to arrange the vowels in sorted order in the string at their respective places ; Store the size of the string ; Stores vowels of string S ; Traverse the string , S and push all the vowels to string vow ; If vow is empty , then print S and return ; Convert vow to char array ; Sort vow in alphabetical order ; Traverse the string , S ; Replace S [ i ] with vow [ j ] iif S [ i ] is a vowel , and increment j by 1 ; Print the string ; Driver Code"
Solution in C#:

using System ; public class GFG { static void sortVowels ( string S ) { int n = S . Length ; string vow = " " ; for ( int i = 0 ; i < n ; i ++ ) { if ( S [ i ] == ' a ' S [ i ] == ' e ' S [ i ] == ' i ' S [ i ] == ' o ' S [ i ] == ' u ' ) { vow = vow . Substring ( 0 , vow . Length ) + S [ i ] ; } } if ( vow . Length == 0 ) { Console . Write ( S ) ; return ; } char [ ] tempArray = vow . ToCharArray ( ) ; Array . Sort ( tempArray ) ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( S [ i ] == ' a ' S [ i ] == ' e ' S [ i ] == ' i ' S [ i ] == ' o ' S [ i ] == ' u ' ) { S = S . Substring ( 0 , i ) + tempArray [ j ++ ] + S . Substring ( i + 1 , n - i - 1 ) ; } } Console . Write ( S ) ; } public static void Main ( string [ ] args ) { string S = " geeksforgeeks " ; sortVowels ( S ) ; } }


"Modify string by replacing all occurrences of given characters by specified replacing characters | C # program for the above approach ; Function to modify given string by replacing characters ; Store the size of string and the number of pairs ; Initialize 2 character arrays ; Traverse the string s Update arrays arr [ ] and brr [ ] ; Traverse the array of pairs p ; a -> Character to be replaced b -> Replacing character ; Iterate over the range [ 0 , 25 ] ; If it is equal to current character , then replace it in the array b ; Print the array brr [ ] ; Driver code"
Solution in C#:

using System ; public class GFG { static void replaceCharacters ( string s , char [ , ] p ) { int n = s . Length , k = p . GetLength ( 0 ) ; char [ ] arr = new char [ 26 ] ; char [ ] brr = new char [ 26 ] ; for ( int i = 0 ; i < n ; i ++ ) { arr [ s [ i ] - ' a ' ] = s [ i ] ; brr [ s [ i ] - ' a ' ] = s [ i ] ; } for ( int j = 0 ; j < k ; j ++ ) { char a = p [ j , 0 ] , b = p [ j , 1 ] ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( arr [ i ] == a ) { brr [ i ] = b ; } } } for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( brr [ s [ i ] - ' a ' ] ) ; } } static public void Main ( ) { String S = " aabbgg " ; char [ , ] P = { { ' a ' , ' b ' } , { ' b ' , ' g ' } , { ' g ' , ' a ' } } ; replaceCharacters ( S , P ) ; } }


"Modify string by replacing characters by alphabets whose distance from that character is equal to its frequency | C # program for the above approach ; Function to modify string by replacing characters by the alphabet present at distance equal to frequency of the string ; Stores frequency of characters ; Stores length of the string ; Traverse the given string S ; Increment frequency of current character by 1 ; Traverse the string ; Store the value to be added to the current character ; Check if after adding the frequency , the character is less than ' z ' or not ; Otherwise , update the value of add so that s [ i ] doesn ' t ▁ exceed ▁ ' z ' ; Print the modified string ; Driver Code"
Solution in C#:

using System ; class GFG { static void addFrequencyToCharacter ( char [ ] s ) { int [ ] frequency = new int [ 26 ] ; int n = s . Length ; 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 . WriteLine ( s ) ; } public static void Main ( string [ ] args ) { string str = " geeks " ; addFrequencyToCharacter ( str . ToCharArray ( ) ) ; } }


"Modify string by sorting characters after removal of characters whose frequency is not equal to power of 2 | C # program for the above approach ; Function to remove all the characters from a string that whose frequencies are not equal to a perfect power of 2 ; Stores the frequency of each character in S ; Iterate over characters of string ; Update frequency of current character in the array freq [ ] ; Traverse the array freq [ ] ; Check if the i - th letter is absent from string S ; Calculate log of frequency of the current character in the string S ; Calculate power of 2 of lg ; Check if freq [ i ] is a power of 2 ; Print letter i + ' a ' freq [ i ] times ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void countFrequency ( string S , int N ) { int [ ] freq = new int [ 26 ] ; Array . Clear ( freq , 0 , freq . Length ) ; for ( int i = 0 ; i < N ; i ++ ) { freq [ ( int ) S [ i ] - ' a ' ] += 1 ; } for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] == 0 ) continue ; int lg = ( int ) Math . Log ( ( double ) freq [ i ] , 2.0 ) ; int a = ( int ) Math . Pow ( 2 , lg ) ; if ( a == freq [ i ] ) { while ( freq [ i ] > 0 ) { freq [ i ] -= 1 ; Console . Write ( ( char ) ( i + ' a ' ) ) ; } } } } public static void Main ( ) { string S = " aaacbb " ; int N = S . Length ; countFrequency ( S , N ) ; } }


"Modify the string by swapping continuous vowels or consonants | C # implementation of the above approach ; Function to check if a character is a vowel ; Function to swap two consecutively repeated vowels or consonants ; Traverse through the length of the string ; Check if the two consecutive characters are vowels or consonants ; swap the two characters ; Driver code"
Solution in C#:

using System ; class GFG { static bool isVowel ( char c ) { c = char . ToLower ( c ) ; if ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) { return true ; } return false ; } static String swapRepeated ( char [ ] str ) { for ( int i = 0 ; i < str . Length - 1 ; i ++ ) { char c = ( char ) 0 ; if ( ( isVowel ( str [ i ] ) && isVowel ( str [ i + 1 ] ) ) || ( ! isVowel ( str [ i ] ) && ! isVowel ( str [ i + 1 ] ) ) ) { c = str [ i ] ; str [ i ] = str [ i + 1 ] ; str [ i + 1 ] = c ; } } return String . Join ( " " , str ) ; } public static void Main ( String [ ] args ) { String str = " geeksforgeeks " ; Console . WriteLine ( swapRepeated ( str . ToCharArray ( ) ) ) ; } }


"Modify the string such that every character gets replaced with the next character in the keyboard | C # implementation of the approach ; Function to return the modified String ; Map to store the next character on the keyboard for every possible lowercase character ; Update the String ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static String CHARS = " qwertyuiopasdfghjklzxcvbnm " ; static int MAX = 26 ; static String getString ( char [ ] str , int n ) { Dictionary < char , char > uMap = new Dictionary < char , char > ( ) ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( ! uMap . ContainsKey ( CHARS [ i ] ) ) uMap . Add ( CHARS [ i ] , CHARS [ ( i + 1 ) % MAX ] ) ; else uMap [ CHARS [ i ] ] = CHARS [ ( i + 1 ) % MAX ] ; } for ( int i = 0 ; i < n ; i ++ ) { str [ i ] = uMap [ str [ i ] ] ; } return String . Join ( " " , str ) ; } public static void Main ( String [ ] args ) { String str = " geeks " ; int n = str . Length ; Console . WriteLine ( getString ( str . ToCharArray ( ) , n ) ) ; } }


"Modify the string such that it contains all vowels at least once | C # implementation of the above approach ; All vowels ; List to store distinct vowels ; If length of String is less than 5 then always Impossible ; Storing the distinct vowels in the String by checking if it in the list of String and not in the list of distinct vowels ; Storing the vowels which are not present in the String ; No replacement needed condition ; Replacing the characters to get all Vowels ; Copy th rest of the string ; Driver Code"
Solution in C#:

using System ; using System . Collections ; class GFG { static bool find ( char [ ] x , char c ) { for ( int i = 0 ; i < x . Length ; i ++ ) if ( x [ i ] == c ) return true ; return false ; } static bool find ( ArrayList v , char c ) { for ( int i = 0 ; i < v . Count ; i ++ ) if ( ( char ) v [ i ] == c ) return true ; return false ; } static void addAllVowel ( string str ) { char [ ] x = { ' A ' , ' E ' , ' I ' , ' O ' , ' U ' } ; ArrayList y = new ArrayList ( ) ; int length = str . Length ; if ( length < 5 ) Console . Write ( " Impossible " ) ; else { for ( int i = 0 ; i < length ; i ++ ) { if ( find ( x , str [ i ] ) && ! find ( y , str [ i ] ) ) y . Add ( str [ i ] ) ; } ArrayList z = new ArrayList ( ) ; for ( int i = 0 ; i < 5 ; i ++ ) if ( ! find ( y , x [ i ] ) ) z . Add ( x [ i ] ) ; if ( z . Count == 0 ) Console . Write ( str ) ; else { int cc = 0 ; ArrayList y1 = new ArrayList ( ) ; string ans = " " ; for ( int i = 0 ; i < length ; i ++ ) { if ( find ( x , str [ i ] ) && ! find ( y1 , str [ i ] ) ) { ans += str [ i ] ; y1 . Add ( str [ i ] ) ; } else { ans += ( char ) z [ cc ] ; cc ++ ; } if ( cc == z . Count ) { for ( int j = i + 1 ; j < length ; j ++ ) ans += str [ j ] ; break ; } } Console . Write ( ans ) ; } } } public static void Main ( string [ ] args ) { string str = " ABCDEFGHI " ; addAllVowel ( str ) ; } }


"Modular Exponentiation ( Power in Modular Arithmetic ) |  ; Iterative Function to calculate ( x ^ y ) in O ( log y ) ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now ; Driver Code"
Solution in C#:

using System ; public class GFG { static int power ( int x , int y , int p ) { int res = 1 ; x = x % p ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = ( res * x ) % p ; y = y >> 1 ; y = y / 2 x = ( x * x ) % p ; } return res ; } static public void Main ( ) { int x = 2 ; int y = 5 ; int p = 13 ; Console . Write ( " Power ▁ is ▁ " + power ( x , y , p ) ) ; } }


"Modular Exponentiation ( Power in Modular Arithmetic ) | Iterative Function to calculate ( x ^ y ) in O ( log y ) ; Initialize result ; If y is odd , multiply x with result ; y must be even now y = y / 2 ; Change x to x ^ 2"
Solution in C#:

static int power ( int x , int y ) { int res = 1 ; while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = res * x ; y = y >> 1 ; x = x * x ; } return res ; }


"Modular Exponentiation of Complex Numbers | C # implementation of the approach ; Utility function ; Function to multiply two complex numbers modulo M ; Multiplication of two complex numbers is ( a + ib ) ( c + id ) = ( ac - bd ) + i ( ad + bc ) ; Return the multiplied value ; Function to calculate the complex modular exponentiation ; Here , res is initialised to ( 1 + i0 ) ; If k is odd ; Multiply ' complex ' with ' res ' ; Make complex as complex * complex ; Make k as k / 2 ; Return the required answer ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { public class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static pair Multiply ( pair p , pair q , int M ) { int x = ( ( p . first * q . first ) % M - ( p . second * q . second ) % M + M ) % M ; int y = ( ( p . first * q . second ) % M + ( p . second * q . first ) % M ) % M ; return new pair ( x , y ) ; } static pair compPow ( pair complex , int k , int M ) { pair res = new pair ( 1 , 0 ) ; while ( k > 0 ) { if ( k % 2 == 1 ) { res = Multiply ( res , complex , M ) ; } complex = Multiply ( complex , complex , M ) ; k = k >> 1 ; } return res ; } public static void Main ( String [ ] args ) { int A = 7 , B = 3 , k = 10 , M = 97 ; pair ans = compPow ( new pair ( A , B ) , k , M ) ; Console . WriteLine ( ans . first + " ▁ + ▁ i " + ans . second ) ; } }


"Modular exponentiation ( Recursive ) | Recursive C # program to compute modular power ; Base cases ; If B is even ; If B is odd ; Driver Code"
Solution in C#:

class GFG { static int exponentMod ( int A , int B , int C ) { if ( A == 0 ) return 0 ; if ( B == 0 ) return 1 ; long y ; if ( B % 2 == 0 ) { y = exponentMod ( A , B / 2 , C ) ; y = ( y * y ) % C ; } else { y = A % C ; y = ( y * exponentMod ( A , B - 1 , C ) % C ) % C ; } return ( int ) ( ( y + C ) % C ) ; } public static void Main ( ) { int A = 2 , B = 5 , C = 13 ; System . Console . WriteLine ( " Power ▁ is ▁ " + exponentMod ( A , B , C ) ) ; } }


"Modular multiplicative inverse | C # program to find modular inverse of a under modulo m ; A naive method to find modulor multiplicative inverse of ' a ' under modulo ' m ' ; Driver Code ; Function call"
Solution in C#:

using System ; class GFG { static int modInverse ( int a , int m ) { for ( int x = 1 ; x < m ; x ++ ) if ( ( ( a % m ) * ( x % m ) ) % m == 1 ) return x ; return 1 ; } public static void Main ( ) { int a = 3 , m = 11 ; Console . WriteLine ( modInverse ( a , m ) ) ; } }


"Modular multiplicative inverse | Iterative C # program to find modular inverse using extended Euclid algorithm ; Returns modulo inverse of a with respect to m using extended Euclid Algorithm Assumption : a and m are coprimes , i . e . , gcd ( a , m ) = 1 ; q is quotient ; m is remainder now , process same as Euclid 's algo ; Update x and y ; Make x positive ; Driver Code ; Function call"
Solution in C#:

using System ; class GFG { static int modInverse ( int a , int m ) { int m0 = m ; int y = 0 , x = 1 ; if ( m == 1 ) return 0 ; while ( a > 1 ) { int q = a / m ; int t = m ; m = a % m ; a = t ; t = y ; y = x - q * y ; x = t ; } if ( x < 0 ) x += m0 ; return x ; } public static void Main ( ) { int a = 3 , m = 11 ; Console . WriteLine ( " Modular ▁ multiplicative ▁ " + " inverse ▁ is ▁ " + modInverse ( a , m ) ) ; } }


"Modulo 10 ^ 9 + 7 ( 1000000007 ) |  ; f = ( f * i ) % M ; Now f never can exceed 10 ^ 9 + 7"
Solution in C#:

static long factorial ( int n ) { long M = 1000000007 ; long f = 1 ; for ( int i = 1 ; i <= n ; i ++ ) return f ; }


"Modulo 10 ^ 9 + 7 ( 1000000007 ) |  ; f = f * i ; WRONG APPROACH as f may exceed ( 2 ^ 64 - 1 )"
Solution in C#:

static long factorial ( int n ) { const long M = 1000000007 ; long f = 1 ; for ( int i = 1 ; i <= n ; i ++ ) return f % M ; }


static int mod ( int a , int m ) { return ( a % m + m ) % m ; }


"Modulo Operations in Programming With Negative Results | C # program for the above approach ; Function to calculate and return the remainder of a % n ; ( a / n ) implicitly gives the truncated result ; Driver Code ; Modulo of two positive numbers ; Modulo of a negative number by a positive number ; Modulo of a positive number by a negative number ; Modulo of two negative numbers"
Solution in C#:

using System ; public class GFG { static int truncMod ( int a , int n ) { int q = a / n ; return a - n * q ; } static public void Main ( ) { int a , b ; a = 9 ; b = 4 ; Console . WriteLine ( a + " ▁ % ▁ " + b + " ▁ = ▁ " + truncMod ( a , b ) ) ; a = - 9 ; b = 4 ; Console . WriteLine ( a + " ▁ % ▁ " + b + " ▁ = ▁ " + truncMod ( a , b ) ) ; a = 9 ; b = - 4 ; Console . WriteLine ( a + " ▁ % ▁ " + b + " ▁ = ▁ " + truncMod ( a , b ) ) ; a = - 9 ; b = - 4 ; Console . WriteLine ( a + " ▁ % ▁ " + b + " ▁ = ▁ " + truncMod ( a , b ) ) ; } }


"Modulo Operations in Programming With Negative Results | C # program to illustrate modulo operations using the above equation ; Function to calculate and return the remainder of a % n ; Type casting is necessary as ( int ) / ( int ) will give int result , i . e . - 3 / 2 will give - 1 and not - 1.5 ; Return the resultant remainder ; Driver code ; Modulo of two positive numbers ; Modulo of a negative number by a positive number ; Modulo of a positive number by a negative number ; Modulo of two negative numbers"
Solution in C#:

using System ; class GFG { static int floorMod ( int a , int n ) { int q = ( int ) Math . Floor ( ( double ) a / n ) ; return a - n * q ; } static public void Main ( ) { int a , b ; a = 9 ; b = 4 ; Console . WriteLine ( a + " ▁ % ▁ " + b + " ▁ = ▁ " + floorMod ( a , b ) ) ; a = - 9 ; b = 4 ; Console . WriteLine ( a + " ▁ % ▁ " + b + " ▁ = ▁ " + floorMod ( a , b ) ) ; a = 9 ; b = - 4 ; Console . WriteLine ( a + " ▁ % ▁ " + b + " ▁ = ▁ " + floorMod ( a , b ) ) ; a = - 9 ; b = - 4 ; Console . WriteLine ( a + " ▁ % ▁ " + b + " ▁ = ▁ " + floorMod ( a , b ) ) ; } }


"Modulo of a large Binary String | C # implementation of the above approach ; Function to return the value of ( str % k ) ; pwrTwo [ i ] will store ( ( 2 ^ i ) % k ) ; To store the result ; If current bit is 1 ; Add the current power of 2 ; Driver code"
Solution in C#:

using System ; class GFG { static int getMod ( string str , int n , int k ) { int i ; int [ ] pwrTwo = new int [ n ] ; pwrTwo [ 0 ] = 1 % k ; for ( i = 1 ; i < n ; i ++ ) { pwrTwo [ i ] = pwrTwo [ i - 1 ] * ( 2 % k ) ; pwrTwo [ i ] %= k ; } int res = 0 ; i = 0 ; int j = n - 1 ; while ( i < n ) { if ( str [ j ] == '1' ) { res += ( pwrTwo [ i ] ) ; res %= k ; } i ++ ; j -- ; } return res ; } public static void Main ( ) { string str = "1101" ; int n = str . Length ; int k = 45 ; Console . Write ( getMod ( str , n , k ) ) ; } }


"Modulo power for large numbers represented as strings | C # program to find ( a ^ b ) % MOD where a and b may be very large and represented as strings . ; Returns modulo exponentiation for two numbers represented as long long int . It is used by powerStrings ( ) . Its complexity is log ( n ) ; Returns modulo exponentiation for two numbers represented as strings . It is used by powerStrings ( ) ; We convert strings to number ; calculating a % MOD ; calculating b % ( MOD - 1 ) ; Now a and b are long long int . We calculate a ^ b using modulo exponentiation ; Driver code ; As numbers are very large that is it may contains upto 10 ^ 6 digits . So , we use string ."
Solution in C#:

using System ; class GFG { static long MOD = ( long ) ( 1e9 + 7 ) ; static long powerLL ( long x , long n ) { long result = 1 ; while ( n > 0 ) { if ( n % 2 == 1 ) { result = result * x % MOD ; } n = n / 2 ; x = x * x % MOD ; } return result ; } static long powerStrings ( String sa , String sb ) { long a = 0 , b = 0 ; for ( int i = 0 ; i < sa . Length ; i ++ ) { a = ( a * 10 + ( sa [ i ] - '0' ) ) % MOD ; } for ( int i = 0 ; i < sb . Length ; i ++ ) { b = ( b * 10 + ( sb [ i ] - '0' ) ) % ( MOD - 1 ) ; } return powerLL ( a , b ) ; } public static void Main ( String [ ] args ) { String sa = "2" , sb = "3" ; Console . WriteLine ( powerStrings ( sa , sb ) ) ; } }


"Modulus of a Complex Number | C # program to find the Modulus of a Complex Number ; Function to find modulus of a complex number ; Storing the index of ' + ' ; Storing the index of ' - ' ; Finding the real part of the complex number ; Finding the imaginary part of the complex number ; Driver code"
Solution in C#:

 using System ; public class GFG { static void findModulo ( String s ) { int l = s . Length ; int i ; if ( s . Contains ( " + " ) ) { i = s . IndexOf ( " + " ) ; } else { i = s . IndexOf ( " - " ) ; } String real = s . Substring ( 0 , i ) ; String imaginary = s . Substring ( i + 1 , l - i - 2 ) ; int x = Int32 . Parse ( real ) ; int y = Int32 . Parse ( imaginary ) ; Console . Write ( Math . Sqrt ( x * x + y * y ) + "
 " ) ; } public static void Main ( String [ ] args ) { String s = "3 + 4i " ; findModulo ( s ) ; } }


"Modulus of two Hexadecimal Numbers | C # program to implement the above approach ; Function to calculate modulus of two Hexadecimal numbers ; Store all possible hexadecimal digits ; Iterate over the range [ '0' , '9' ] ; Convert given string to long ; Base to get 16 power ; Store N % K ; Iterate over the digits of N ; Stores i - th digit of N ; Update ans ; Update base ; Print the answer converting into hexadecimal ; Driver Code ; Given string N and K ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void hexaModK ( String N , String k ) { Dictionary < char , int > map = new Dictionary < char , int > ( ) ; for ( char i = '0' ; i <= '9' ; i ++ ) { map . Add ( i , i - '0' ) ; } map . Add ( ' A ' , 10 ) ; map . Add ( ' B ' , 11 ) ; map . Add ( ' C ' , 12 ) ; map . Add ( ' D ' , 13 ) ; map . Add ( ' E ' , 14 ) ; map . Add ( ' F ' , 15 ) ; long m = long . Parse ( k ) ; long Base = 1 ; long ans = 0 ; for ( int i = N . Length - 1 ; i >= 0 ; i -- ) { long n = map [ N [ i ] ] % m ; ans = ( ans + ( Base % m * n % m ) % m ) % m ; Base = ( Base % m * 16 % m ) % m ; } Console . WriteLine ( ans . ToString ( " X " ) ) ; } public static void Main ( String [ ] args ) { String n = "3E8" ; String k = "13" ; hexaModK ( n , k ) ; } }


"Morris traversal for Preorder | C # program to implement Morris preorder traversal ; Preorder traversal without recursion and without stack ; If left child is null , print the current node data . Move to right child . ; Find inorder predecessor ; If the right child of inorder predecessor already points to this node ; If right child doesn 't point to   this node, then print this node   and make right child point to this node  ; Function for Standard preorder traversal ; Driver Code"
Solution in C#:

using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class GFG { public Node root ; public virtual void morrisTraversalPreorder ( ) { morrisTraversalPreorder ( root ) ; } public virtual void morrisTraversalPreorder ( Node node ) { while ( node != null ) { if ( node . left == null ) { Console . Write ( node . data + " ▁ " ) ; node = node . right ; } else { Node current = node . left ; while ( current . right != null && current . right != node ) { current = current . right ; } if ( current . right == node ) { current . right = null ; node = node . right ; } else { Console . Write ( node . data + " ▁ " ) ; current . right = node ; node = node . left ; } } } } public virtual void preorder ( ) { preorder ( root ) ; } public virtual void preorder ( Node node ) { if ( node != null ) { Console . Write ( node . data + " ▁ " ) ; preorder ( node . left ) ; preorder ( node . right ) ; } } public static void Main ( string [ ] args ) { GFG tree = new GFG ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . left = new Node ( 6 ) ; tree . root . right . right = new Node ( 7 ) ; tree . root . left . left . left = new Node ( 8 ) ; tree . root . left . left . right = new Node ( 9 ) ; tree . root . left . right . left = new Node ( 10 ) ; tree . root . left . right . right = new Node ( 11 ) ; tree . morrisTraversalPreorder ( ) ; Console . WriteLine ( " " ) ; tree . preorder ( ) ; } }


"Morse Code Implementation | C # program to demonstrate Morse code ; function to encode a alphabet as Morse code ; refer to the Morse table image attached in the article ; for space ; character by character print Morse code ; Driver code"
Solution in C#:

using System ; class GFG { static string morseEncode ( char x ) { switch ( x ) { case ' a ' : return " . - " ; case ' b ' : return " - . . . " ; case ' c ' : return " - . - . " ; case ' d ' : return " - . . " ; case ' e ' : return " . " ; case ' f ' : return " . . - . " ; case ' g ' : return " - - . " ; case ' h ' : return " . . . . " ; case ' i ' : return " . . " ; case ' j ' : return " . - - - " ; case ' k ' : return " - . - " ; case ' l ' : return " . - . . " ; case ' m ' : return " - - " ; case ' n ' : return " - . " ; case ' o ' : return " - - - " ; case ' p ' : return " . - - . " ; case ' q ' : return " - - . - " ; case ' r ' : return " . - . " ; case ' s ' : return " . . . " ; case ' t ' : return " - " ; case ' u ' : return " . . - " ; case ' v ' : return " . . . - " ; case ' w ' : return " . - - " ; case ' x ' : return " - . . - " ; case ' y ' : return " - . - - " ; case ' z ' : return " - - . . " ; case '1' : return " . - - - - " ; case '2' : return " . . - - - " ; case '3' : return " . . . - - " ; case '4' : return " . . . . - " ; case '5' : return " . . . . . " ; case '6' : return " - . . . . " ; case '7' : return " - - . . . " ; case '8' : return " - - - . . " ; case '9' : return " - - - - . " ; case '0' : return " - - - - - " ; } return " " ; } static void morseCode ( string s ) { for ( int i = 0 ; i < s . Length ; i ++ ) Console . Write ( morseEncode ( s [ i ] ) ) ; Console . WriteLine ( ) ; } public static void Main ( ) { string s = " geeksforgeeks " ; morseCode ( s ) ; } }


"Moser | C # code to generate first ' n ' terms of the Moser - de Bruijn Sequence ; Function to generate nth term of Moser - de Bruijn Sequence ; S ( 0 ) = 0 ; S ( 1 ) = 1 ; S ( 2 * n ) = 4 * S ( n ) ; S ( 2 * n + 1 ) = 4 * S ( n ) + 1 ; Generating the first ' n ' terms of Moser - de Bruijn Sequence ; Driver Code"
Solution in C#:

using System ; class GFG { public static int gen ( int n ) { if ( n == 0 ) return 0 ; else if ( n == 1 ) return 1 ; else if ( n % 2 == 0 ) return 4 * gen ( n / 2 ) ; else if ( n % 2 == 1 ) return 4 * gen ( n / 2 ) + 1 ; return 0 ; } public static void moserDeBruijn ( int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( gen ( i ) + " ▁ " ) ; Console . WriteLine ( ) ; } public static void Main ( ) { int n = 15 ; Console . WriteLine ( " First ▁ " + n + " ▁ terms ▁ of ▁ " + " Moser - de ▁ Bruijn ▁ Sequence ▁ : ▁ " ) ; moserDeBruijn ( n ) ; } }


"Moser | C # code to generate first ' n ' terms of the Moser - de Bruijn Sequence ; Function to generate nth term of Moser - de Bruijn Sequence ; S ( 2 * n ) = 4 * S ( n ) ; S ( 2 * n + 1 ) = 4 * S ( n ) + 1 ; Generating the first ' n ' terms of Moser - de Bruijn Sequence ; Driver Code"
Solution in C#:

using System ; class GFG { static int gen ( int n ) { int [ ] S = new int [ n + 1 ] ; S [ 0 ] = 0 ; if ( n != 0 ) S [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { if ( i % 2 == 0 ) S [ i ] = 4 * S [ i / 2 ] ; else S [ i ] = 4 * S [ i / 2 ] + 1 ; } return S [ n ] ; } static void moserDeBruijn ( int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( gen ( i ) + " ▁ " ) ; } public static void Main ( ) { int n = 15 ; Console . WriteLine ( " First ▁ " + n + " ▁ terms ▁ of ▁ " + " Moser - de ▁ Bruijn ▁ Sequence ▁ : ▁ " ) ; moserDeBruijn ( n ) ; } }


"Most frequent element in an array | C # program to find the most frequent element in an array ; Insert all elements in hash ; find max frequency . ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int mostFrequent ( int [ ] arr , int n ) { Dictionary < int , int > hp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int key = arr [ i ] ; if ( hp . ContainsKey ( key ) ) { int freq = hp [ key ] ; freq ++ ; hp [ key ] = freq ; } else hp . Add ( key , 1 ) ; } int min_count = 0 , res = - 1 ; foreach ( KeyValuePair < int , int > pair in hp ) { if ( min_count < pair . Value ) { res = pair . Key ; min_count = pair . Value ; } } return res ; } static void Main ( ) { int [ ] arr = new int [ ] { 1 , 5 , 2 , 1 , 3 , 2 , 1 } ; int n = arr . Length ; Console . Write ( mostFrequent ( arr , n ) ) ; } }


"Most frequent element in an array | C # program to find the most frequent element in an array ; Sort the array ; find the max frequency using linear traversal ; If last element is most frequent ; Driver code"
Solution in C#:

using System ; class GFG { static int mostFrequent ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int max_count = 1 , res = arr [ 0 ] ; int curr_count = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] ) curr_count ++ ; else { if ( curr_count > max_count ) { max_count = curr_count ; res = arr [ i - 1 ] ; } curr_count = 1 ; } } if ( curr_count > max_count ) { max_count = curr_count ; res = arr [ n - 1 ] ; } return res ; } public static void Main ( ) { int [ ] arr = { 1 , 5 , 2 , 1 , 3 , 2 , 1 } ; int n = arr . Length ; Console . WriteLine ( mostFrequent ( arr , n ) ) ; } }


"Most frequent word in an array of strings | C # implementation ; Function returns word with highest frequency ; Create Dictionary to store word and it 's frequency ; Iterate through array of words ; If word already exist in Dictionary then increase it 's count by 1 ; Otherwise add word to Dictionary ; Create set to iterate over Dictionary ; Check for word having highest frequency ; Return word having highest frequency ; Driver code ; Print word having highest frequency"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static String findWord ( String [ ] arr ) { Dictionary < String , int > hs = new Dictionary < String , int > ( ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( hs . ContainsKey ( arr [ i ] ) ) { hs [ arr [ i ] ] = hs [ arr [ i ] ] + 1 ; } else { hs . Add ( arr [ i ] , 1 ) ; } } String key = " " ; int value = 0 ; foreach ( KeyValuePair < String , int > me in hs ) { if ( me . Value > value ) { value = me . Value ; key = me . Key ; } } return key ; } public static void Main ( String [ ] args ) { String [ ] arr = { " geeks " , " for " , " geeks " , " a " , " portal " , " to " , " learn " , " can " , " be " , " computer " , " science " , " zoom " , " yup " , " fire " , " in " , " be " , " data " , " geeks " } ; String sol = findWord ( arr ) ; Console . WriteLine ( sol ) ; } }


"Most frequent word in first String which is not present in second String | C # implementation of above approach ; Function to return frequent word from S1 that isn 't  present in S2 ; create map of banned words ; find smallest and most frequent word ; check if word is not banned ; return answer ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static String smallestFreq ( String S1 , String S2 ) { Dictionary < String , int > banned = new Dictionary < String , int > ( ) ; for ( int i = 0 ; i < S2 . Length ; i ++ ) { String s = " " ; while ( i < S2 . Length && S2 [ i ] != ' ▁ ' ) s += S2 [ i ++ ] ; if ( banned . ContainsKey ( s ) ) { var val = banned [ s ] ; banned . Remove ( s ) ; banned . Add ( s , val + 1 ) ; } else { banned . Add ( s , 1 ) ; } } Dictionary < String , int > result = new Dictionary < String , int > ( ) ; String ans = " " ; int freq = 0 ; for ( int i = 0 ; i < S1 . Length ; i ++ ) { String s = " " ; while ( i < S1 . Length && S1 [ i ] != ' ▁ ' ) s += S1 [ i ++ ] ; if ( ! banned . ContainsKey ( s ) ) { if ( result . ContainsKey ( s ) ) { var val = result [ s ] ; result . Remove ( s ) ; result . Add ( s , val + 1 ) ; } else { result . Add ( s , 1 ) ; } if ( result [ s ] > freq || ( result [ s ] == freq && s . CompareTo ( ans ) < 0 ) ) { ans = s ; freq = result [ s ] ; } } } return ans ; } public static void Main ( String [ ] args ) { String S1 = " geeks ▁ for ▁ geeks ▁ is ▁ best ▁ place ▁ to ▁ learn " ; String S2 = " bad ▁ place " ; Console . WriteLine ( smallestFreq ( S1 , S2 ) ) ; } }


"Motzkin number | C # Program to find Nth Motzkin Number . ; Return the nth Motzkin Number . ; Base Case ; Recursive step ; driver code"
Solution in C#:

using System ; class GFG { public static int motzkin ( int n ) { if ( n == 0 n == 1 ) return 1 ; return ( ( 2 * n + 1 ) * motzkin ( n - 1 ) + ( 3 * n - 3 ) * motzkin ( n - 2 ) ) / ( n + 2 ) ; } public static void Main ( ) { int n = 8 ; Console . WriteLine ( motzkin ( n ) ) ; } }


"Motzkin number | C # Program to find Nth Motzkin Number . ; Return the nth Motzkin Number . ; Base case ; Finding i - th Motzkin number . ; driver code"
Solution in C#:

using System ; class GFG { public static int motzkin ( int n ) { int [ ] dp = new int [ n + 1 ] ; dp [ 0 ] = dp [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) dp [ i ] = ( ( 2 * i + 1 ) * dp [ i - 1 ] + ( 3 * i - 3 ) * dp [ i - 2 ] ) / ( i + 2 ) ; return dp [ n ] ; } public static void Main ( ) { int n = 8 ; Console . WriteLine ( motzkin ( n ) ) ; } }


"Mountain Sequence Pattern | C # program for the above approach ; Function to print pattern recursively ; Base Case ; Condition to check row limit ; Condition for assigning gaps ; Conditions to print * ; Else print ' ▁ ' ; Recursive call for columns ; Recursive call for rows ; Driver code ; Given Number N ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int k1 = 2 ; static int k2 = 2 ; static int gap = 5 ; static int printPattern ( int i , int j , int n ) { if ( j >= n ) { k1 = 2 ; k2 = 2 ; k1 -- ; k2 ++ ; if ( i == 2 ) { k1 = 0 ; k2 = n - 1 ; } return 0 ; } if ( i >= 3 ) { return 1 ; } if ( j > k2 ) { k1 += gap ; k2 += gap ; } if ( j >= k1 && j <= k2 i == 2 ) { Console . Write ( " * " ) ; } else { Console . Write ( " ▁ " ) ; } if ( printPattern ( i , j + 1 , n ) == 1 ) { return 1 ; } Console . WriteLine ( ) ; return printPattern ( i + 1 , 0 , n ) ; } static void Main ( ) { int N = 3 ; printPattern ( 0 , 0 , N * 5 ) ; } }


"Move all digits to the beginning of a given string | C # program for the above approach ; Function to move all the digit at the beginning of the string ; Replace all the non - numeric characters with " ▁ " and replace all digits with " ▁ " ; Print the string ; Driver Code ; Given String str"
Solution in C#:

using System ; using System . Text . RegularExpressions ; public class GFG { static void moveAllDigitAtBeginning ( String str ) { String moveAllDigit = Regex . Replace ( str , " \\ D + " , " " ) + Regex . Replace ( str , " \\ d " , " " ) ; Console . WriteLine ( moveAllDigit ) ; } public static void Main ( String [ ] args ) { String str = " GeeksforGeeks1234" ; moveAllDigitAtBeginning ( str ) ; } }


"Move all negative elements to end in order with extra space allowed | C # program to Move All - ve Element At End Without changing order Of Array Element ; Moves all - ve element to end of array in same order . ; Create an empty array to store result ; Traversal array and store + ve element in temp array index of temp ; If array contains all positive or all negative . ; Store - ve element in temp array ; Copy contents of temp [ ] to arr [ ] ; Driver code"
Solution in C#:

using System ; class GFG { static void segregateElements ( int [ ] arr , int n ) { int [ ] temp = new int [ n ] ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] >= 0 ) temp [ j ++ ] = arr [ i ] ; if ( j == n j == 0 ) return ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] < 0 ) temp [ j ++ ] = arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = temp [ i ] ; } public static void Main ( ) { int [ ] arr = { 1 , - 1 , - 3 , - 2 , 7 , 5 , 11 , 6 } ; int n = arr . Length ; segregateElements ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }


"Move all negative numbers to beginning and positive to end with constant extra space | C # program to put all negative numbers before positive numbers ; A utility function to print an array ; Driver code"
Solution in C#:

using System ; class GFG { static void rearrange ( int [ ] arr , int n ) { int j = 0 , temp ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 0 ) { temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; j ++ ; } } } static void printArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } public static void Main ( ) { int [ ] arr = { - 1 , 2 , - 3 , 4 , 5 , 6 , - 7 , 8 , 9 } ; int n = arr . Length ; rearrange ( arr , n ) ; printArray ( arr , n ) ; } }


"Move all occurrence of letter ' x ' from the string s to the end using Recursion | C # implementation to Move all occurrence of letter x from the string s to the end using Recursion ; Function to move all ' x ' in the end ; Store current character ; Check if current character is not ' x ' ; recursive function call ; Check if current character is ' x ' ; Driver code"
Solution in C#:

using System ; class GFG { static void moveAtEnd ( string s , int i , int l ) { if ( i >= l ) return ; char curr = s [ i ] ; if ( curr != ' x ' ) Console . Write ( curr ) ; moveAtEnd ( s , i + 1 , l ) ; if ( curr == ' x ' ) Console . Write ( curr ) ; return ; } public static void Main ( ) { string s = " geekxsforgexxeksxx " ; int l = s . Length ; moveAtEnd ( s , 0 , l ) ; } }


"Move all occurrence of letter ' x ' from the string s to the end using Recursion | C # program for the above approach ; Recursive program to bring ' x ' to the end ; When the string is completed from reverse direction end of recursion ; If the character x is found ; Transverse the whole string ; Swap the x so that it moves to the last ; call to the smaller problem now ; Driver code ; Size of a ; Call to rec"
Solution in C#:

using System ; class GFG { static void rec ( char [ ] a , int i ) { if ( i == 0 ) { Console . WriteLine ( a ) ; return ; } if ( a [ i ] == ' x ' ) { int j = i ; while ( a [ j ] != ' \0' && a [ j + 1 ] != ' \0' ) { char temp = a [ j ] ; a [ j ] = a [ j + 1 ] ; a [ j + 1 ] = temp ; j ++ ; } } rec ( a , i - 1 ) ; } static void Main ( ) { char [ ] a = { ' g ' , ' e ' , ' e ' , ' k ' , ' x ' , ' s ' , ' x ' , ' x ' , ' k ' , ' s ' , ' \0' } ; int n = 10 ; rec ( a , n - 1 ) ; } }


"Move all occurrences of an element to end in a linked list | C # code to remove key element to end of linked list ; Node class ; Function to remove key to end ; Node to keep pointing to tail ; Node to point to last of linked list ; Node prev2 to point to previous when head . data != key ; loop to perform operations to remove key to end ; Function to display linked list ; Driver Code"
Solution in C#:

using System ; public class Node { public int data ; public Node next ; public Node ( int data ) { this . data = data ; this . next = null ; } } class GFG { static Node root ; public static Node keyToEnd ( Node head , int key ) { Node tail = head ; if ( head == null ) { return null ; } while ( tail . next != null ) { tail = tail . next ; } Node last = tail ; Node current = head ; Node prev = null ; Node prev2 = null ; while ( current != tail ) { if ( current . data == key && prev2 == null ) { prev = current ; current = current . next ; head = current ; last . next = prev ; last = last . next ; last . next = null ; prev = null ; } else { if ( current . data == key && prev2 != null ) { prev = current ; current = current . next ; prev2 . next = current ; last . next = prev ; last = last . next ; last . next = null ; } else if ( current != tail ) { prev2 = current ; current = current . next ; } } } return head ; } public static void display ( Node root ) { while ( root != null ) { Console . Write ( root . data + " ▁ " ) ; root = root . next ; } } public static void Main ( ) { root = new Node ( 5 ) ; root . next = new Node ( 2 ) ; root . next . next = new Node ( 2 ) ; root . next . next . next = new Node ( 7 ) ; root . next . next . next . next = new Node ( 2 ) ; root . next . next . next . next . next = new Node ( 2 ) ; root . next . next . next . next . next . next = new Node ( 2 ) ; int key = 2 ; Console . WriteLine ( " Linked ▁ List ▁ before ▁ operations ▁ : " ) ; display ( root ) ; Console . WriteLine ( " operations : " root = keyToEnd ( root , key ) ; display ( root ) ; } }


"Move all occurrences of an element to end in a linked list | C # program to move all occurrences of a given key to end . ; A Linked list Node ; A urility function to create a new node . ; Utility function to print the elements in Linked list ; Moves all occurrences of given key to end of linked list . ; Keeps track of locations where key is present . ; Traverse list ; If current pointer is not same as pointer to a key location , then we must have found a key in linked list . We swap data of pCrawl and pKey and move pKey to next position . ; Find next position where key is present ; Moving to next Node ; Driver code"
Solution in C#:

 using System ; class GFG { public class Node { public int data ; public Node next ; } static Node newNode ( int x ) { Node temp = new Node ( ) ; temp . data = x ; temp . next = null ; return temp ; } static void printList ( Node head ) { Node temp = head ; while ( temp != null ) { Console . Write ( " { 0 } ▁ " , temp . data ) ; temp = temp . next ; } Console . Write ( "
 " ) ; } static void moveToEnd ( Node head , int key ) { Node pKey = head ; Node pCrawl = head ; while ( pCrawl != null ) { if ( pCrawl != pKey && pCrawl . data != key ) { pKey . data = pCrawl . data ; pCrawl . data = key ; pKey = pKey . next ; } if ( pKey . data != key ) pKey = pKey . next ; pCrawl = pCrawl . next ; } } public static void Main ( String [ ] args ) { Node head = newNode ( 10 ) ; head . next = newNode ( 20 ) ; head . next . next = newNode ( 10 ) ; head . next . next . next = newNode ( 30 ) ; head . next . next . next . next = newNode ( 40 ) ; head . next . next . next . next . next = newNode ( 10 ) ; head . next . next . next . next . next . next = newNode ( 60 ) ; Console . Write ( " Before ▁ moveToEnd ( ) , ▁ the ▁ Linked ▁ list ▁ is
 " ) ; printList ( head ) ; int key = 10 ; moveToEnd ( head , key ) ; Console . Write ( " moveToEnd ( ) , the Linked is " printList ( head ) ; } }


"Move all zeros to start and ones to end in an Array of random integers | C # implementation of the approach ; Utility function to print the contents of an array ; Function that pushes all the zeros to the start and ones to the end of an array ; To store the count of elements which are not equal to 1 ; Traverse the array and calculate count of elements which are not 1 ; Now all non - ones elements have been shifted to front and ' count1' is set as index of first 1. Make all elements 1 from count to end . ; Initialize lastNonBinary position to zero ; Traverse the array and pull non - zero elements to the required indices ; Ignore the 1 's ; Mark the position Of last NonBinary integer ; Place non - zero element to their required indices ; Put zeros to start of array ; Driver code"
Solution in C#:

using System ; class GFG { static void printArr ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } static void pushBinaryToBorder ( int [ ] arr , int n ) { int count1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] != 1 ) arr [ count1 ++ ] = arr [ i ] ; while ( count1 < n ) arr [ count1 ++ ] = 1 ; int lastNonOne = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( arr [ i ] == 1 ) continue ; if ( lastNonOne == 0 ) { lastNonOne = i ; } if ( arr [ i ] != 0 ) arr [ lastNonOne -- ] = arr [ i ] ; } while ( lastNonOne >= 0 ) arr [ lastNonOne -- ] = 0 ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 0 , 0 , 0 , 3 , 6 } ; int n = arr . Length ; pushBinaryToBorder ( arr , n ) ; printArr ( arr , n ) ; } }


"Move spaces to front of string in single traversal | C # program to bring all spaces in front of string using swapping technique ; Function to find spaces and move to beginning ; Keep copying non - space characters ; Move spaces to be beginning ; Driver code"
Solution in C#:

using System ; class GFG { static void moveSpaceInFront ( char [ ] str ) { int i = str . Length - 1 ; for ( int j = i ; j >= 0 ; j -- ) if ( str [ j ] != ' ▁ ' ) str [ i -- ] = str [ j ] ; while ( i >= 0 ) str [ i -- ] = ' ▁ ' ; } public static void Main ( String [ ] args ) { char [ ] str = " Hey ▁ there , ▁ it ' s ▁ GeeksforGeeks " . ToCharArray ( ) ; moveSpaceInFront ( str ) ; Console . WriteLine ( String . Join ( " " , str ) ) ; } }


"Move spaces to front of string in single traversal | C # program to bring all spaces in front of string using swapping technique ; Function to find spaces and move to beginning ; Traverse from end and swap spaces ; Driver code"
Solution in C#:

using System ; class GFG { static void moveSpaceInFront ( char [ ] str ) { int i = str . Length - 1 ; for ( int j = i ; j >= 0 ; j -- ) if ( str [ j ] != ' ▁ ' ) { char c = str [ i ] ; str [ i ] = str [ j ] ; str [ j ] = c ; i -- ; } } public static void Main ( ) { char [ ] str = " Hey ▁ there , ▁ it ' s ▁ GeeksforGeeks " . ToCharArray ( ) ; moveSpaceInFront ( str ) ; Console . WriteLine ( String . Join ( " " , str ) ) ; } }


"Mth bit in Nth binary string from a sequence generated by the given operations | C # program for the above approach ; Function to calculate N Fibonacci numbers ; Function to find the mth bit in the String Sn ; Base case ; Length of left half ; Length of the right half ; Recursive check in the left half ; Recursive check in the right half ; Driver Code"
Solution in C#:

using System ; class GFG { static int maxN = 10 ; static void calculateFib ( int [ ] fib , int n ) { fib [ 0 ] = fib [ 1 ] = 1 ; for ( int x = 2 ; x < n ; x ++ ) { fib [ x ] = fib [ x - 1 ] + fib [ x - 2 ] ; } } static int find_mth_bit ( int n , int m , int [ ] fib ) { if ( n <= 1 ) { return n ; } int len_left = fib [ n - 2 ] ; int len_right = fib [ n - 1 ] ; if ( m <= len_left ) { return find_mth_bit ( n - 2 , len_left + 1 - m , fib ) ; } else { return find_mth_bit ( n - 1 , len_right + 1 - ( m - len_left ) , fib ) ; } } static void find_mth_bitUtil ( int n , int m ) { int [ ] fib = new int [ maxN ] ; calculateFib ( fib , maxN ) ; int ans = find_mth_bit ( n , m , fib ) ; Console . Write ( ans + " ▁ " ) ; } public static void Main ( ) { int n = 5 , m = 3 ; find_mth_bitUtil ( n , m ) ; } }


"Mth element after K Right Rotations of an Array | C # program to implement the above approach ; Function to return Mth element of array after k right rotations ; The array comes to original state after N rotations ; If K is greater or equal to M ; Mth element after k right rotations is ( N - K ) + ( M - 1 ) th element of the array ; Otherwise ; ( M - K - 1 ) th element of the array ; Return the result ; Driver Code"
Solution in C#:

using System ; class GFG { static int getFirstElement ( int [ ] a , int N , int K , int M ) { K %= N ; int index ; if ( K >= M ) index = ( N - K ) + ( M - 1 ) ; else index = ( M - K - 1 ) ; int result = a [ index ] ; return result ; } public static void Main ( ) { int [ ] a = { 1 , 2 , 3 , 4 , 5 } ; int N = 5 ; int K = 3 , M = 2 ; Console . Write ( getFirstElement ( a , N , K , M ) ) ; } }


"Multiple of x closest to n | C # program to calculate smallest multiple of x closest to a given number ; Function to calculate the smallest multiple ; Driver program"
Solution in C#:

using System ; class Solution { static int closestMultiple ( int n , int x ) { if ( x > n ) return x ; n = n + x / 2 ; n = n - ( n % x ) ; return n ; } public static void Main ( ) { int n = 56287 , x = 27 ; Console . WriteLine ( closestMultiple ( n , x ) ) ; } }


"Multiples of 3 or 7 | A better Java program to find count of all numbers that multiples ; Returns count of all numbers smaller than or equal to n and multiples of 3 or 7 or both ; Driver Code"
Solution in C#:

using System ; class GFG { static int countMultiples ( int n ) { return n / 3 + n / 7 - n / 21 ; } static public void Main ( ) { Console . WriteLine ( " Count ▁ = ▁ " + countMultiples ( 25 ) ) ; } }


"Multiples of 4 ( An Interesting Method ) | An interesting XOR based method to check if a number is multiple of 4. ; Returns true if n is a multiple of 4. ; Driver code to print multiples of 4 ; Printing multiples of 4 using above method"
Solution in C#:

using System ; class GFG { static bool isMultipleOf4 ( int n ) { if ( n == 0 ) return true ; return ( ( ( n >> 2 ) << 2 ) == n ) ; } static void Main ( ) { for ( int n = 0 ; n <= 42 ; n ++ ) if ( isMultipleOf4 ( n ) ) Console . Write ( n + " ▁ " ) ; } }


"Multiples of 4 ( An Interesting Method ) | An interesting XOR based method to check if a number is multiple of 4. ; Returns true if n is a multiple of 4. ; Find XOR of all numbers from 1 to n ; If XOR is equal n , then return true ; Driver method ; Printing multiples of 4 using above method"
Solution in C#:

using System ; class GFG { static bool isMultipleOf4 ( int n ) { if ( n == 1 ) return false ; int XOR = 0 ; for ( int i = 1 ; i <= n ; i ++ ) XOR = XOR ^ i ; return ( XOR == n ) ; } public static void Main ( ) { for ( int n = 0 ; n <= 42 ; n ++ ) { if ( isMultipleOf4 ( n ) ) Console . Write ( n + " ▁ " ) ; } } }


"Multiplication of two complex numbers given as strings | C # program to multiply two complex numbers given as strings . ; Spiting the real and imaginary parts of the given complex strings based on ' + ' and ' i ' symbols . ; Storing the real part of complex string a ; Storing the imaginary part of complex string a ; Storing the real part of complex string b ; Storing the imaginary part of complex string b ; Returns the product . ; Driver code"
Solution in C#:

using System ; using System . Text . RegularExpressions ; class GfG { public static String complexNumberMultiply ( String a , String b ) { String [ ] x = Regex . Split ( a , @"\+ i" ) ; String [ ] y = Regex . Split ( b , @"\+ i" ) ; int a_real = Int32 . Parse ( x [ 0 ] ) ; int a_img = Int32 . Parse ( x [ 1 ] ) ; int b_real = Int32 . Parse ( y [ 0 ] ) ; int b_img = Int32 . Parse ( y [ 1 ] ) ; return ( a_real * b_real - a_img * b_img ) + " + " + ( a_real * b_img + a_img * b_real ) + " i " ; } public static void Main ( String [ ] argc ) { String str1 = "1 + 1i " ; String str2 = "1 + 1i " ; Console . WriteLine ( complexNumberMultiply ( str1 , str2 ) ) ; } }


"Multiplication of two numbers with shift operator | C # program to find multiplication of two number without use of multiplication operator ; Function for multiplication ; check for set bit and left shift n , count times ; increment of place value ( count ) ; Driver Code"
Solution in C#:

using System ; class GFG { static int multiply ( int n , int m ) { int ans = 0 , count = 0 ; while ( m > 0 ) { if ( m % 2 == 1 ) ans += n << count ; count ++ ; m /= 2 ; } return ans ; } public static void Main ( ) { int n = 20 , m = 13 ; Console . WriteLine ( multiply ( n , m ) ) ; } }


"Multiplication on Array : Range update query in O ( 1 ) | C # implementation of the approach ; Creates a mul [ ] array for A [ ] and returns it after filling initial values . ; Does range update ; Prints updated Array ; Driver code ; ; Array to be updated ; Create and fill mul and div Array"
Solution in C#:

using System ; class GFG { static void initialize ( int [ ] mul , int [ ] div , int size ) { for ( int i = 1 ; i < size ; i ++ ) { mul [ i ] = ( mul [ i ] * mul [ i - 1 ] ) / div [ i ] ; } } static void update ( int l , int r , int x , int [ ] mul , int [ ] div ) { mul [ l ] *= x ; div [ r + 1 ] *= x ; } static void printArray ( int [ ] ar , int [ ] mul , int [ ] div , int n ) { for ( int i = 0 ; i < n ; i ++ ) { ar [ i ] = ar [ i ] * mul [ i ] ; Console . Write ( ar [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] ar = { 10 , 5 , 20 , 40 } ; int n = ar . Length ; int [ ] mul = new int [ n + 1 ] ; int [ ] div = new int [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { mul [ i ] = div [ i ] = 1 ; } update ( 0 , 1 , 10 , mul , div ) ; update ( 1 , 3 , 20 , mul , div ) ; update ( 2 , 2 , 2 , mul , div ) ; initialize ( mul , div , n + 1 ) ; printArray ( ar , mul , div , n ) ; } }


"Multiplication table till N rows where every Kth row is table of K upto Kth term | C # program to print multiplication table till N rows where every Kth row is the table of K up to Kth term ; Function to print the multiplication table upto K - th term ; For loop to iterate from 1 to N where i serves as the value of K ; Inner loop which at every iteration goes till i ; Printing the table value for i ; New line after every row ; Driver code"
Solution in C#:

using System ; class GFG { public static void printMultiples ( int N ) { for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= i ; j ++ ) { Console . Write ( ( i * j ) + " ▁ " ) ; } Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { int N = 5 ; printMultiples ( N ) ; } }


"Multiplication with a power of 2 | C # Code for Multiplication with a power of 2 ; Driver program to test above function"
Solution in C#:

using System ; class GFG { static int multiply ( int x , int n ) { return x << n ; } public static void Main ( ) { int x = 70 , n = 2 ; Console . WriteLine ( multiply ( x , n ) ) ; } }


"Multiplication with a power of 2 | Simple C # program to compute x * ( 2 ^ n ) ; Returns 2 raised to power n ; Driver program"
Solution in C#:

using System ; class GFG { static long power2 ( long n ) { if ( n == 0 ) return 1 ; if ( n == 1 ) return 2 ; return power2 ( n / 2 ) * power2 ( n / 2 ) ; } static long multiply ( long x , long n ) { return x * power2 ( n ) ; } public static void Main ( ) { long x = 70 , n = 2 ; Console . WriteLine ( multiply ( x , n ) ) ; } }


"Multiplicative Congruence method for generating Pseudo Random Numbers | C # implementation of the above approach ; Function to generate random numbers ; Initialize the seed state ; Traverse to generate required numbers of random numbers ; Follow the multiplicative congruential method ; Driver code ; Seed value ; Modulus parameter ; Multiplier term ; Number of Random numbers to be generated ; To store random numbers ; Function call ; Print the generated random numbers"
Solution in C#:

using System ; class GFG { static void multiplicativeCongruentialMethod ( int Xo , int m , int a , int [ ] randomNums , int noOfRandomNums ) { randomNums [ 0 ] = Xo ; for ( int i = 1 ; i < noOfRandomNums ; i ++ ) { randomNums [ i ] = ( randomNums [ i - 1 ] * a ) % m ; } } public static void Main ( String [ ] args ) { int Xo = 3 ; int m = 15 ; int a = 7 ; int noOfRandomNums = 10 ; int [ ] randomNums = new int [ noOfRandomNums ] ; multiplicativeCongruentialMethod ( Xo , m , a , randomNums , noOfRandomNums ) ; for ( int i = 0 ; i < noOfRandomNums ; i ++ ) { Console . Write ( randomNums [ i ] + " ▁ " ) ; } } }


"Multiplicative order | C # program to implement multiplicative order ; function for GCD ; Function return smallest + ve integer that holds condition A ^ k ( mod N ) = 1 ; result store power of A that rised to the power N - 1 ; modular arithmetic ; return smallest + ve integer ; increment power ; Driver Code"
Solution in C#:

using System ; class GFG { static int GCD ( int a , int b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } static int multiplicativeOrder ( int A , int N ) { if ( GCD ( A , N ) != 1 ) return - 1 ; int result = 1 ; int K = 1 ; while ( K < N ) { result = ( result * A ) % N ; if ( result == 1 ) return K ; K ++ ; } return - 1 ; } public static void Main ( ) { int A = 4 , N = 7 ; Console . Write ( multiplicativeOrder ( A , N ) ) ; } }


"Multiply Large Numbers represented as Strings | C # program to multiply two numbers represented as Strings . ; Multiplies str1 and str2 , and prints result . ; will keep the result number in vector in reverse order ; Below two indexes are used to find positions in result . ; Go from right to left in num1 ; To shift position to left after every multipliccharAtion of a digit in num2 ; Go from right to left in num2 ; Take current digit of second number ; Multiply with current digit of first number and add result to previously stored result charAt current position . ; Carry for next itercharAtion ; Store result ; store carry in next cell ; To shift position to left after every multipliccharAtion of a digit in num1 . ; ignore '0' s from the right ; If all were '0' s - means either both or one of num1 or num2 were '0' ; genercharAte the result String ; Driver code"
Solution in C#:

using System ; class GFG { static String multiply ( String num1 , String num2 ) { int len1 = num1 . Length ; int len2 = num2 . Length ; if ( len1 == 0 len2 == 0 ) return "0" ; int [ ] result = new int [ len1 + len2 ] ; int i_n1 = 0 ; int i_n2 = 0 ; int i ; for ( i = len1 - 1 ; i >= 0 ; i -- ) { int carry = 0 ; int n1 = num1 [ i ] - '0' ; i_n2 = 0 ; for ( int j = len2 - 1 ; j >= 0 ; j -- ) { int n2 = num2 [ j ] - '0' ; int sum = n1 * n2 + result [ i_n1 + i_n2 ] + carry ; carry = sum / 10 ; result [ i_n1 + i_n2 ] = sum % 10 ; i_n2 ++ ; } if ( carry > 0 ) result [ i_n1 + i_n2 ] += carry ; i_n1 ++ ; } i = result . Length - 1 ; while ( i >= 0 && result [ i ] == 0 ) i -- ; if ( i == - 1 ) return "0" ; String s = " " ; while ( i >= 0 ) s += ( result [ i -- ] ) ; return s ; } public static void Main ( String [ ] args ) { String str1 = "1235421415454545454545454544" ; String str2 = "1714546546546545454544548544544545" ; if ( ( str1 [ 0 ] == ' - ' str2 [ 0 ] == ' - ' ) && ( str1 [ 0 ] != ' - ' str2 [ 0 ] != ' - ' ) ) Console . Write ( " - " ) ; if ( str1 [ 0 ] == ' - ' && str2 [ 0 ] != ' - ' ) { str1 = str1 . Substring ( 1 ) ; } else if ( str1 [ 0 ] != ' - ' && str2 [ 0 ] == ' - ' ) { str2 = str2 . Substring ( 1 ) ; } else if ( str1 [ 0 ] == ' - ' && str2 [ 0 ] == ' - ' ) { str1 = str1 . Substring ( 1 ) ; str2 = str2 . Substring ( 1 ) ; } Console . WriteLine ( multiply ( str1 , str2 ) ) ; } }


"Multiply a given Integer with 3.5 | C # Program to multiply a number with 3.5 ; Driver program to test above functions"
Solution in C#:

using System ; class GFG { static int multiplyWith3Point5 ( int x ) { return ( x << 1 ) + x + ( x >> 1 ) ; } public static void Main ( ) { int x = 2 ; Console . Write ( multiplyWith3Point5 ( x ) ) ; } }


"Multiply a given Integer with 3.5 | C # program for above approach ; Function to multiple number with 3.5 ; The 3.5 is 7 / 2 , so multiply by 7 ( x * 7 ) then divide the result by 2 ( result / 2 ) x * 7 -> 7 is 0111 so by doing mutiply by 7 it means we do 2 shifting for the number but since we doing multiply we need to take care of carry one . ; Then divide by 2 r / 2 ; Driver Code"
Solution in C#:

using System ; class GFG { static int multiplyWith3Point5 ( int x ) { int r = 0 ; int x1Shift = x << 1 ; int x2Shifts = x << 2 ; r = ( x ^ x1Shift ) ^ x2Shifts ; int c = ( x & x1Shift ) | ( x & x2Shifts ) | ( x1Shift & x2Shifts ) ; while ( c > 0 ) { c <<= 1 ; int t = r ; r ^= c ; c &= t ; } r = r >> 1 ; return r ; } public static void Main ( string [ ] args ) { Console . WriteLine ( multiplyWith3Point5 ( 5 ) ) ; } }


"Multiply a number by 15 without using * and / operators | C # implementation of the approach ; Function to return ( 15 * N ) without using ' * ' or ' / ' operator ; prod = 16 * n ; ( ( 16 * n ) - n ) = 15 * n ; Driver code"
Solution in C#:

using System ; class GFG { static long multiplyByFifteen ( long n ) { long prod = ( n << 4 ) ; prod = prod - n ; return prod ; } public static void Main ( ) { long n = 7 ; Console . Write ( multiplyByFifteen ( n ) ) ; } }


"Multiply a number by 15 without using * and / operators | C # implementation of the approach ; Function to return ( 15 * N ) without using ' * ' or ' / ' operator ; prod = 8 * n ; Add ( 4 * n ) ; Add ( 2 * n ) ; Add n ; ( 8 * n ) + ( 4 * n ) + ( 2 * n ) + n = ( 15 * n ) ; Driver code"
Solution in C#:

using System ; class GFG { static long multiplyByFifteen ( long n ) { long prod = ( n << 3 ) ; prod += ( n << 2 ) ; prod += ( n << 1 ) ; prod += n ; return prod ; } public static void Main ( ) { long n = 7 ; Console . Write ( multiplyByFifteen ( n ) ) ; } }


"Multiply a number with 10 without using multiplication operator | C # Code to Multiply a number with 10 without using multiplication operator ; Function to find multiplication of n with 10 without using multiplication operator ; Driver Code"
Solution in C#:

using System ; class GFG { public static int multiplyTen ( int n ) { return ( n << 1 ) + ( n << 3 ) ; } public static void Main ( ) { int n = 50 ; Console . Write ( multiplyTen ( n ) ) ; } }


"Multiply a single digit number in place to a number represented as a Linked List | C # program for the above approach ; Node of a Linked List ; Function to create a new node with given data ; Initialize new node ; Set the data field of the node ; Return the new node ; Function to reverse the linked list ; Traverse until curr != null ; Return the head of the reversed linked list ; Utility function to multiply a single digit to a linked list ; Store the head of list ; Stores the address of previous node ; Initially set carry as 0 and product as 1 ; Multiply M with each digit ; Add carry to product if carry exist ; Update carry for next calculation ; Update the value of each nodes ; Move head and temp pointers to next nodes ; If some carry is still there , add a new node to list ; Return head of the resultant list ; Function to multiply a single digit to a linked list ; Reverse linked list ; Multiply M from left to right of reversed list ; Reverse the modified list and return its head ; Function to print the linked list ; Driver Code ; Given Input list : 1.2 . 7.3 ; Function Call ; Print resultant list"
Solution in C#:

using System ; public class GFG { class Node { public int data ; public Node next ; } ; static Node newNode ( int data ) { Node new_node = new Node ( ) ; new_node . data = data ; new_node . next = null ; return new_node ; } static Node reverse ( Node head ) { Node prev = null ; Node current = head ; Node next ; while ( current != null ) { next = current . next ; current . next = prev ; prev = current ; current = next ; } return prev ; } static Node multiplyHelp ( Node head , int M ) { Node res = head ; Node prev = null ; int carry = 0 , product = 1 ; while ( head != null ) { product = head . data * M ; product += carry ; carry = product / 10 ; head . data = product % 10 ; prev = head ; head = head . next ; } if ( carry > 0 ) prev . next = newNode ( carry ) ; return res ; } static Node multiply ( Node head , int M ) { head = reverse ( head ) ; head = multiplyHelp ( head , M ) ; return reverse ( head ) ; } static void printList ( Node node ) { while ( node != null ) { Console . Write ( node . data ) ; node = node . next ; } } public static void Main ( String [ ] args ) { Node head = newNode ( 1 ) ; head . next = newNode ( 2 ) ; head . next . next = newNode ( 7 ) ; head . next . next . next = newNode ( 3 ) ; int M = 3 ; head = multiply ( head , M ) ; printList ( head ) ; } }


"Multiply any Number with 4 using Bitwise Operator | C # program to multiply a number with 4 using Bitwise Operator ; function the return multiply a number with 4 using bitwise operator ; returning a number with multiply with 4 using 2 bit shifting right ; Driver Code"
Solution in C#:

using System ; class GFG { static int multiplyWith4 ( int n ) { return ( n << 2 ) ; } public static void Main ( String [ ] args ) { int n = 4 ; Console . Write ( multiplyWith4 ( n ) ) ; } }


"Multiply large integers under large modulo | C # program of finding modulo multiplication ; Returns ( a * b ) % mod ; Update a if it is more than or equal to mod ; If b is odd , add a with result ; Here we assume that doing 2 * a doesn 't cause overflow ; b >>= 1 ; b = b / 2 ; Driver code"
Solution in C#:

using System ; class GFG { static long moduloMultiplication ( long a , long b , long mod ) { a %= mod ; while ( b > 0 ) { if ( ( b & 1 ) > 0 ) res = ( res + a ) % mod ; a = ( 2 * a ) % mod ; } return res ; } static void Main ( ) { long a = 10123465234878998 ; long b = 65746311545646431 ; long m = 10005412336548794 ; Console . WriteLine ( moduloMultiplication ( a , b , m ) ) ; } }


"Multiply perfect number | C # implementation of the above approach ; Function to find the sum of divisors ; Note that this loop runs till square root of N ; If divisors are equal , take only one of them ; Otherwise take both ; Function to check Multiply - perfect number ; Driver code"
Solution in C#:

using System ; class GFG { static int getSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) sum = sum + i ; else { sum = sum + i ; sum = sum + ( n / i ) ; } } } return sum ; } static bool MultiplyPerfectNumber ( int n ) { if ( getSum ( n ) % n == 0 ) return true ; else return false ; } public static void Main ( ) { int n = 28 ; if ( MultiplyPerfectNumber ( n ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }


"Multiset Equivalence Problem | C # program to check if two given multisets are equivalent ; Create two unordered maps m1 and m2 and insert values of both vectors . ; Now we check if both unordered_maps are same of not . ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static bool areSame ( int [ ] a , int [ ] b ) { if ( a . Length != b . Length ) return false ; Dictionary < int , int > m1 , m2 ; m1 = new Dictionary < int , int > ( ) ; m2 = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < a . Length ; i ++ ) { if ( m1 . ContainsKey ( a [ i ] ) ) { m1 [ a [ i ] ] = m1 [ a [ i ] ] + 1 ; } else { m1 . Add ( a [ i ] , 1 ) ; } if ( m2 . ContainsKey ( b [ i ] ) ) { m2 [ b [ i ] ] = m2 [ b [ i ] ] + 1 ; } else { m2 . Add ( b [ i ] , 1 ) ; } } foreach ( KeyValuePair < int , int > x in m1 ) { if ( ! m2 . ContainsKey ( x . Key ) m2 [ x . Key ] != x . Value ) return false ; } return true ; } public static void Main ( String [ ] args ) { int [ ] a = { 7 , 7 , 5 } ; int [ ] b = { 7 , 7 , 5 } ; if ( areSame ( a , b ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Multiset Equivalence Problem | C # program to check if two given multisets are equivalent ; sort the elements of both multisets ; Return true if both multisets are same . ; Driver code"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static bool areSame ( List < int > a , List < int > b ) { a . Sort ( ) ; b . Sort ( ) ; return ( a == b ) ; } public static void Main ( ) { List < int > a = new List < int > { 7 , 7 , 5 } ; List < int > b = new List < int > { 7 , 5 , 5 } ; if ( areSame ( a , b ) ) Console . WriteLine ( " Yes
 " ) ; else Console . WriteLine ( " No
 " ) ; } }


"Multistage Graph ( Shortest Path ) | C # program to find shortest distance in a multistage graph . ; Returns shortest distance from 0 to N - 1. ; dist [ i ] is going to store shortest distance from node i to node N - 1. ; Calculating shortest path for rest of the nodes ; Initialize distance from i to destination ( N - 1 ) ; Check all nodes of next stages to find shortest distance from i to N - 1. ; Reject if no edge exists ; We apply recursive equation to distance to target through j . and compare with minimum distance so far . ; Driver code ; Graph stored in the form of an adjacency Matrix"
Solution in C#:

using System ; class GFG { static int N = 8 ; static int INF = int . MaxValue ; public static int shortestDist ( int [ , ] graph ) { int [ ] dist = new int [ N ] ; dist [ N - 1 ] = 0 ; for ( int i = N - 2 ; i >= 0 ; i -- ) { dist [ i ] = INF ; for ( int j = i ; j < N ; j ++ ) { if ( graph [ i , j ] == INF ) continue ; dist [ i ] = Math . Min ( dist [ i ] , graph [ i , j ] + dist [ j ] ) ; } } return dist [ 0 ] ; } static void Main ( ) { int [ , ] graph = new int [ , ] { { INF , 1 , 2 , 5 , INF , INF , INF , INF } , { INF , INF , INF , INF , 4 , 11 , INF , INF } , { INF , INF , INF , INF , 9 , 5 , 16 , INF } , { INF , INF , INF , INF , INF , INF , 2 , INF } , { INF , INF , INF , INF , INF , INF , INF , 18 } , { INF , INF , INF , INF , INF , INF , INF , 13 } , { INF , INF , INF , INF , INF , INF , INF , 2 } } ; Console . Write ( shortestDist ( graph ) ) ; } }


"MÃ ¼ nchhausen Number | C # code for Munchhausen Number ; pwr [ i ] is going to store i raised to power i . ; Function to check out whether the number is Munchhausen Number or not ; Precompute i raised to power i for every i ; The input here is fixed i . e . it will check up to n ; check the integer for Munchhausen Number , if yes then print out the number ; Driver Code"
Solution in C#:

using System ; class GFG { static long [ ] pwr ; static bool isMunchhausen ( int n ) { long sum = 0 ; int temp = n ; while ( temp > 0 ) { int index = temp % 10 ; sum = sum + pwr [ index ] ; temp /= 10 ; } return ( sum == n ) ; } static void printMunchhausenNumbers ( int n ) { pwr = new long [ 10 ] ; for ( int i = 0 ; i < 10 ; i ++ ) pwr [ i ] = ( long ) Math . Pow ( ( float ) i , ( float ) i ) ; for ( int i = 1 ; i <= n ; i ++ ) if ( isMunchhausen ( i ) == true ) Console . WriteLine ( i ) ; } public static void Main ( ) { int n = 10000 ; printMunchhausenNumbers ( n ) ; } }


"N 'th Smart Number | C # implementation to find n 'th smart number ; Limit on result ; Function to calculate n 'th smart number ; Initialize all numbers as not prime ; iterate to mark all primes and smart number ; Traverse all numbers till maximum limit ; ' i ' is maked as prime number because it is not multiple of any other prime ; mark all multiples of ' i ' as non prime ; If i is the third prime factor of j then add it to result as it has at least three prime factors . ; Sort all smart numbers ; return n 'th smart number ; Driver program to run the case"
Solution in C#:

using System . Collections . Generic ; class GFG { static int MAX = 3000 ; public static int smartNumber ( int n ) { int [ ] primes = new int [ MAX ] ; List < int > result = new List < int > ( ) ; for ( int i = 2 ; i < MAX ; i ++ ) { if ( primes [ i ] == 0 ) { primes [ i ] = 1 ; for ( int j = i * 2 ; j < MAX ; j = j + i ) { primes [ j ] -= 1 ; if ( ( primes [ j ] + 3 ) == 0 ) result . Add ( j ) ; } } } result . Sort ( ) ; return result [ n - 1 ] ; } public static void Main ( ) { int n = 50 ; System . Console . WriteLine ( smartNumber ( n ) ) ; } }


"N 'th palindrome of K digits | A naive approach of C # program of finding nth palindrome of k digit ; Utility function to reverse the number n ; Boolean Function to check for palindromic number ; Function for finding nth palindrome of k digits ; Get the smallest k digit number ; check the number is palindrom or not ; if n 'th palindrome found break the loop ; Increment number for checking next palindrome ; Driver code"
Solution in C#:

using System ; class GFG { static int reverseNum ( int n ) { int rem , rev = 0 ; while ( n > 0 ) { rem = n % 10 ; rev = rev * 10 + rem ; n /= 10 ; } return rev ; } static bool isPalindrom ( int num ) { return num == reverseNum ( num ) ; } static int nthPalindrome ( int n , int k ) { int num = ( int ) Math . Pow ( 10 , k - 1 ) ; while ( true ) { if ( isPalindrom ( num ) ) -- n ; if ( n == 0 ) break ; ++ num ; } return num ; } public static void Main ( ) { int n = 6 , k = 5 ; Console . WriteLine ( n + " th ▁ palindrome ▁ of ▁ " + k + " ▁ digit ▁ = ▁ " + nthPalindrome ( n , k ) ) ; n = 10 ; k = 6 ; Console . WriteLine ( n + " th ▁ palindrome ▁ of ▁ " + k + " ▁ digit ▁ = ▁ " + nthPalindrome ( n , k ) ) ; } }


"N 'th palindrome of K digits | C # program of finding nth palindrome of k digit ; Determine the first half digits ; Print the first half digits of palindrome ; If k is odd , truncate the last digit ; print the last half digits of palindrome ; Driver code"
Solution in C#:

using System ; class GFG { static void nthPalindrome ( int n , int k ) { int temp = ( k & 1 ) != 0 ? ( k / 2 ) : ( k / 2 - 1 ) ; int palindrome = ( int ) Math . Pow ( 10 , temp ) ; palindrome += n - 1 ; Console . Write ( palindrome ) ; if ( ( k & 1 ) > 0 ) palindrome /= 10 ; while ( palindrome > 0 ) { Console . Write ( palindrome % 10 ) ; palindrome /= 10 ; } Console . WriteLine ( " " ) ; } static public void Main ( ) { int n = 6 , k = 5 ; Console . Write ( n + " th ▁ palindrome ▁ of ▁ " + k + " ▁ digit ▁ = ▁ " ) ; nthPalindrome ( n , k ) ; n = 10 ; k = 6 ; Console . Write ( n + " th ▁ palindrome ▁ of ▁ " + k + " ▁ digit ▁ = ▁ " ) ; nthPalindrome ( n , k ) ; } }


"N / 3 repeated number in an array with O ( 1 ) space | C # program to find if any element appears more than n / 3. ; take the integers as the maximum value of integer hoping the integer would not be present in the array ; if this element is previously seen , increment count1 . ; if this element is previously seen , increment count2 . ; if current element is different from both the previously seen variables , decrement both the counts . ; Again traverse the array and find the actual counts . ; Driver code"
Solution in C#:

using System ; public class GFG { static int appearsNBy3 ( int [ ] arr , int n ) { int count1 = 0 , count2 = 0 ; int first = int . MaxValue ; int second = int . MaxValue ; for ( int i = 1 ; i < n ; i ++ ) { if ( first == arr [ i ] ) count1 ++ ; else if ( second == arr [ i ] ) count2 ++ ; else if ( count1 == 0 ) { count1 ++ ; first = arr [ i ] ; } else if ( count2 == 0 ) { count2 ++ ; second = arr [ i ] ; } else { count1 -- ; count2 -- ; } } count1 = 0 ; count2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == first ) count1 ++ ; else if ( arr [ i ] == second ) count2 ++ ; } if ( count1 > n / 3 ) return first ; if ( count2 > n / 3 ) return second ; return - 1 ; } static public void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 1 , 1 } ; int n = arr . Length ; Console . WriteLine ( appearsNBy3 ( arr , n ) ) ; } }


"N Queen Problem | Backtracking | C # program to solve N Queen Problem using backtracking ; ld is an array where its indices indicate row - col + N - 1 ( N - 1 ) is for shifting the difference to store negative indices ; rd is an array where its indices indicate row + col and used to check whether a queen can be placed on right diagonal or not ; column array where its indices indicates column and used to check whether a queen can be placed in that row or not ; A utility function to print solution ; A recursive utility function to solve N Queen problem ; base case : If all queens are placed then return true ; Consider this column and try placing this queen in all rows one by one ; A check if a queen can be placed on board [ row , col ] . We just need to check ld [ row - col + n - 1 ] and rd [ row + coln ] where ld and rd are for left and right diagonal respectively ; Place this queen in board [ i , col ] ; recur to place rest of the queens ; board [ i , col ] = 0 ; BACKTRACK ; If the queen cannot be placed in any row in this colum col then return false ; This function solves the N Queen problem using Backtracking . It mainly uses solveNQUtil ( ) to solve the problem . It returns false if queens cannot be placed , otherwise , return true and prints placement of queens in the form of 1 s . Please note that there may be more than one solutions , this function prints one of the feasible solutions . ; Driver Code"
Solution in C#:

 using System ; class GFG { static int N = 4 ; static int [ ] ld = new int [ 30 ] ; static int [ ] rd = new int [ 30 ] ; static int [ ] cl = new int [ 30 ] ; static void printSolution ( int [ , ] board ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) Console . Write ( " ▁ { 0 } ▁ " , board [ i , j ] ) ; Console . Write ( "
 " ) ; } } static bool solveNQUtil ( int [ , ] board , int col ) { if ( col >= N ) return true ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( ld [ i - col + N - 1 ] != 1 && rd [ i + col ] != 1 ) && cl [ i ] != 1 ) { board [ i , col ] = 1 ; ld [ i - col + N - 1 ] = rd [ i + col ] = cl [ i ] = 1 ; if ( solveNQUtil ( board , col + 1 ) ) return true ; ld [ i - col + N - 1 ] = rd [ i + col ] = cl [ i ] = 0 ; } } return false ; } static bool solveNQ ( ) { int [ , ] board = { { 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 } } ; if ( solveNQUtil ( board , 0 ) == false ) { Console . Write ( " Solution ▁ does ▁ not ▁ exist " ) ; return false ; } printSolution ( board ) ; return true ; } public static void Main ( String [ ] args ) { solveNQ ( ) ; } }


"N Queen in O ( n ) space | C # code to for n Queen placement ; Function to check queens placement ; Helper Function to check if queen can be placed ; Function to display placed queen ; Driver Code"
Solution in C#:

using System ; class GfG { static void breakLine ( ) { Console . Write ( " -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - " } static int = 10 ; static int [ ] arr = new int [ MAX ] ; static int no ; static void nQueens ( int k , int n ) { for ( int i = 1 ; i <= n ; i ++ ) { if ( canPlace ( k , i ) ) { arr [ k ] = i ; if ( k == n ) { display ( n ) ; } else { nQueens ( k + 1 , n ) ; } } } } static bool canPlace ( int k , int i ) { for ( int j = 1 ; j <= k - 1 ; j ++ ) { if ( arr [ j ] == i || ( Math . Abs ( arr [ j ] - i ) == Math . Abs ( j - k ) ) ) { return false ; } } return true ; } static void display ( int n ) { breakLine ( ) ; Console . Write ( " Arrangement ▁ No . ▁ " + ++ no ) ; breakLine ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( arr [ i ] != j ) { Console . Write ( " 	 _ " ) ; } else { Console . Write ( " 	 Q " ) ; } } Console . WriteLine ( " " ) ; } breakLine ( ) ; } public static void Main ( String [ ] args ) { int n = 4 ; nQueens ( 1 , n ) ; } }


"N digit numbers divisible by 5 formed from the M digits | C # program to find the count of all possible N digit numbers which are divisible by 5 formed from M digits ; Function to find the count of all possible N digit numbers which are divisible by 5 formed from M digits ; If it is not possible to form n digit number from the given m digits without repetition ; If both zero and five exists ; Remaining N - 1 iterations ; Remaining N - 1 iterations ; Driver code"
Solution in C#:

using System ; public class GFG { static int numbers ( int n , int [ ] arr , int m ) { int isZero = 0 , isFive = 0 ; int result = 0 ; if ( m < n ) { return - 1 ; } for ( int i = 0 ; i < m ; i ++ ) { if ( arr [ i ] == 0 ) { isZero = 1 ; } if ( arr [ i ] == 5 ) { isFive = 1 ; } } if ( isZero == 1 && isFive == 1 ) { result = 2 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { result = result * ( -- m ) ; } } else if ( isZero == 1 isFive == 1 ) { result = 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { result = result * ( -- m ) ; } } else { result = - 1 ; } return result ; } public static void Main ( ) { int n = 3 , m = 6 ; int [ ] arr = { 2 , 3 , 5 , 6 , 7 , 9 } ; Console . WriteLine ( numbers ( n , arr , m ) ) ; } }