"Construct an array from XOR of all elements of array except element at same index | C # program to construct array from XOR of elements of given array ; function to construct new array ; calculate xor of array ; update array ; Driver code ; print result"
Solution in C#:

using System ; class GFG { static void constructXOR ( int [ ] A , int n ) { int XOR = 0 ; for ( int i = 0 ; i < n ; i ++ ) XOR ^= A [ i ] ; for ( int i = 0 ; i < n ; i ++ ) A [ i ] = XOR ^ A [ i ] ; } public static void Main ( ) { int [ ] A = { 2 , 4 , 1 , 3 , 5 } ; int n = A . Length ; constructXOR ( A , n ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( A [ i ] + " ▁ " ) ; } }


"Construct an array from its pair | C # implementation of the approach ; Utility function to print the array ; Function to generate the original array from the pair - product array ; First element of the resulting array ; Find all the other elements ; Print the elements of the generated 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 constructArr ( int [ ] pair , int n ) { int size = ( 1 + ( int ) Math . Sqrt ( 1 + 8 * n ) ) / 2 ; int [ ] arr = new int [ size ] ; arr [ 0 ] = ( int ) Math . Sqrt ( ( pair [ 0 ] * pair [ 1 ] ) / pair [ size - 1 ] ) ; for ( int i = 1 ; i < size ; i ++ ) arr [ i ] = pair [ i - 1 ] / arr [ 0 ] ; printArr ( arr , size ) ; } public static void Main ( String [ ] args ) { int [ ] pair = { 48 , 18 , 24 , 24 , 32 , 12 } ; int n = pair . Length ; constructArr ( pair , n ) ; } }


"Construct an array from its pair | C # program to construct an array from its pair - sum array ; Fills element in arr [ ] from its pair sum array pair [ ] . n is size of arr [ ] ; Driver program to test above function"
Solution in C#:

using System ; class PairSum { static void constructArr ( int [ ] arr , int [ ] pair , int n ) { arr [ 0 ] = ( pair [ 0 ] + pair [ 1 ] - pair [ n - 1 ] ) / 2 ; for ( int i = 1 ; i < n ; i ++ ) arr [ i ] = pair [ i - 1 ] - arr [ 0 ] ; } public static void Main ( ) { int [ ] pair = { 15 , 13 , 11 , 10 , 12 , 10 , 9 , 8 , 7 , 5 } ; int n = 5 ; int [ ] arr = new int [ n ] ; constructArr ( arr , pair , n ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }


"Construct an array of first N natural numbers such that every adjacent pair is coprime | C # program to implement the above approach ; Function to construct an arrary with adjacent elements as co - prime numbers ; Iterate over the range [ 1 , N ] ; Print i ; Driver Code"
Solution in C#:

using System ; class GFG { static void ConstArrayAdjacentCoprime ( int N ) { for ( int i = 1 ; i <= N ; i ++ ) { Console . Write ( i + " ▁ " ) ; } } public static void Main ( ) { int N = 6 ; ConstArrayAdjacentCoprime ( N ) ; } }


"Construct an array whose Prefix XOR array starting from X is an N | C # program for above approach ; Function to print the required array ; Iteratie from 1 to N ; Print the i - th element ; Update prev_xor to i ; Driver Code ; Given Input ; Function Call"
Solution in C#:

using System ; class GFG { static void GenerateArray ( int N , int X ) { int prev_xor = X ; for ( int i = 1 ; i <= N ; i ++ ) { Console . Write ( i ^ prev_xor ) ; if ( i != N ) { Console . Write ( " ▁ " ) ; } prev_xor = i ; } } static void Main ( ) { int N = 4 , X = 3 ; Console . Write ( " The ▁ generated ▁ array ▁ is ▁ " ) ; GenerateArray ( N , X ) ; } }


"Construct array with sum of product of same indexed elements in the given array equal to zero | C # program to implement the above approach ; Function to generate a new array with product of same indexed elements with arr [ ] equal to 0 ; Stores sum of same indexed array elements of arr and new array ; Traverse the array ; If i is an even number ; Insert arr [ i + 1 ] into the new array newArr [ ] ; Insert - arr [ i - 1 ] into the new array newArr [ ] ; Print new array elements ; Driver Code"
Solution in C#:

using System ; class GFG { static void constructNewArraySumZero ( int [ ] arr , int N ) { int [ ] newArr = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { newArr [ i ] = arr [ i + 1 ] ; } else { newArr [ i ] = - arr [ i - 1 ] ; } } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( newArr [ i ] + " ▁ " ) ; } } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , - 5 , - 6 , 8 } ; int N = arr . Length ; constructNewArraySumZero ( arr , N ) ; } }


"Construct binary palindrome by repeated appending and trimming | C # code to form binary palindrome ; function to apply DFS ; set the parent marked ; if the node has not been visited set it and its children marked ; link which digits must be equal ; connect the two indices ; set everything connected to first character as 1 ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void dfs ( int parent , int [ ] ans , List < int > [ ] connectchars ) { ans [ parent ] = 1 ; for ( int i = 0 ; i < connectchars [ parent ] . Count ; i ++ ) { if ( ans [ connectchars [ parent ] [ i ] ] != 1 ) dfs ( connectchars [ parent ] [ i ] , ans , connectchars ) ; } } static void printBinaryPalindrome ( int n , int k ) { int [ ] arr = new int [ n ] ; int [ ] ans = new int [ n ] ; List < int > [ ] connectchars = new List < int > [ k ] ; for ( int i = 0 ; i < k ; i ++ ) connectchars [ i ] = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = i % k ; for ( int i = 0 ; i < n / 2 ; i ++ ) { connectchars [ arr [ i ] ] . Add ( arr [ n - i - 1 ] ) ; connectchars [ arr [ n - i - 1 ] ] . Add ( arr [ i ] ) ; } dfs ( 0 , ans , connectchars ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( ans [ arr [ i ] ] ) ; } public static void Main ( String [ ] args ) { int n = 10 , k = 4 ; printBinaryPalindrome ( n , k ) ; } }


"Construct lexicographically smallest palindrome | C # for constructing smallest palindrome ; function for printing palindrome ; iterate till i < j ; continue if str [ i ] == str [ j ] ; update str [ i ] = str [ j ] = ' a ' if both are ' * ' ; update str [ i ] = str [ j ] if only str [ i ] = ' * ' ; update str [ j ] = str [ i ] if only str [ j ] = ' * ' ; else print not possible and return ; Driver code"
Solution in C#:

using System ; class GFG { static String constructPalin ( char [ ] str , int len ) { int i = 0 , j = len - 1 ; for ( ; i < j ; i ++ , j -- ) { if ( str [ i ] == str [ j ] && str [ i ] != ' * ' ) continue ; else if ( str [ i ] == str [ j ] && str [ i ] == ' * ' ) { str [ i ] = ' a ' ; str [ j ] = ' a ' ; continue ; } else if ( str [ i ] == ' * ' ) { str [ i ] = str [ j ] ; continue ; } else if ( str [ j ] == ' * ' ) { str [ j ] = str [ i ] ; continue ; } Console . WriteLine ( " Not ▁ Possible " ) ; return " " ; } return String . Join ( " " , str ) ; } public static void Main ( String [ ] args ) { String str = " bca * xc * * b " ; int len = str . Length ; Console . WriteLine ( constructPalin ( str . ToCharArray ( ) , len ) ) ; } }


"Construct longest possible sequence of unique elements with given LCM | C # program to implement the above approach ; Function to construct an array of unique elements whose LCM is N ; Stores array elements whose LCM is N ; Iterate over the range [ 1 , sqrt ( N ) ] ; If N is divisible by i ; Insert i into newArr [ ] ; If N is not perfect square ; Sort the array newArr [ ] ; Print array elements ; Driver Code ; Given N ; Function Call"
Solution in C#:

using System ; class GFG { static void constructArrayWithGivenLCM ( int N ) { int [ ] newArr = new int [ N ] ; int j = 0 ; for ( int i = 1 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { newArr [ j ] = i ; j ++ ; if ( N / i != i ) { newArr [ j ] = N / i ; j ++ ; } } } Array . Sort ( newArr ) ; for ( int i = j ; i < N ; i ++ ) { Console . Write ( newArr [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int N = 12 ; constructArrayWithGivenLCM ( N ) ; } }


"Construct the Array using given bitwise AND , OR and XOR | C # program for the above approach ; Function to find the array ; Loop through all bits in number ; If bit is set in AND then set it in every element of the array ; If bit is not set in AND ; But set in b ( OR ) ; Set bit position in first element ; If bit is not set in c then set it in second element to keep xor as zero for bit position ; Calculate AND , OR and XOR of array ; Check if values are equal or not ; If not , then array is not possible ; Driver code ; Given Bitwise AND , OR , and XOR ; Function Call"
Solution in C#:

using System ; class GFG { static void findArray ( int n , int a , int b , int c ) { int [ ] arr = new int [ n + 1 ] ; for ( int bit = 30 ; bit >= 0 ; bit -- ) { int set = a & ( 1 << bit ) ; if ( set != 0 ) { for ( int i = 0 ; i < n ; i ++ ) arr [ i ] |= set ; } else { if ( ( b & ( 1 << bit ) ) != 0 ) { arr [ 0 ] |= ( 1 << bit ) ; if ( ( c & ( 1 << bit ) ) == 0 ) { arr [ 1 ] |= ( 1 << bit ) ; } } } } int aa = int . MaxValue , bb = 0 , cc = 0 ; for ( int i = 0 ; i < n ; i ++ ) { aa &= arr [ i ] ; bb |= arr [ i ] ; cc ^= arr [ i ] ; } if ( a == aa && b == bb && c == cc ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } else Console . WriteLine ( " - 1" ) ; } public static void Main ( String [ ] args ) { int n = 3 , a = 4 , b = 6 , c = 6 ; findArray ( n , a , b , c ) ; } }


"Construct the Cypher string based on the given conditions | C # program for the above approach ; Function to check whether a number is prime or not ; Function to check if a prime number can be expressed as sum of two Prime Numbers ; If the N && ( N - 2 ) is Prime ; Function to check semiPrime ; Loop from 2 to Math . Sqrt ( num ) ; Increment the count of prime numbers ; If num is greater than 1 , then add 1 to it ; Return '1' if count is 2 else return '0' ; Function to make the Cypher String ; Resultant String ; Make String for the number N ; Check for semiPrime ; Traverse to make Cypher String ; If index is odd add the current character ; Else current character is changed ; Check for sum of two primes ; Traverse to make Cypher String ; If index is odd then current character is changed ; Else add the current character ; If the resultant String is " " then print - 1 ; Else print the resultant String ; Driver Code ; Given Number ; Function Call"
Solution in C#:

using System ; class GFG { static bool isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } static bool isPossibleSum ( int N ) { if ( isPrime ( N ) && isPrime ( N - 2 ) ) { return true ; } else { return false ; } } static bool checkSemiprime ( int num ) { int cnt = 0 ; for ( int i = 2 ; cnt < 2 && i * i <= num ; ++ i ) { while ( num % i == 0 ) { num /= i ; ++ cnt ; } } if ( num > 1 ) { ++ cnt ; } return cnt == 2 ; } static void makeCypherString ( int N ) { String semiPrime = " " ; String sumOfPrime = " " ; String str = String . Join ( " " , N ) ; if ( checkSemiprime ( N ) ) { for ( int i = 0 ; i < str . Length ; i ++ ) { if ( i % 2 == 1 ) { semiPrime += str [ i ] ; } else { semiPrime += ( char ) ( str [ i ] - '0' + 65 ) ; } } } if ( isPossibleSum ( N ) ) { for ( int i = 0 ; i < str . Length ; i ++ ) { if ( i % 2 == 1 ) { sumOfPrime += ( char ) ( str [ i ] - '0' + 65 ) ; } else { sumOfPrime += str [ i ] ; } } } if ( semiPrime + sumOfPrime == " " ) { Console . Write ( " - 1" ) ; } else { Console . Write ( semiPrime + sumOfPrime ) ; } } public static void Main ( String [ ] args ) { int N = 1011243 ; makeCypherString ( N ) ; } }


"Construct the full k | C # program to build full k - ary tree from its preorder traversal and to print the postorder traversal of the tree . ; Structure of a node of an n - ary tree ; Utility function to create a new tree node with k children ; Function to build full k - ary tree ; For null tree ; For adding k children to a node ; Check if ind is in range of array Check if height of the tree is greater than 1 ; Recursively add each child ; Function to find the height of the tree ; Function to print postorder traversal of the tree ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { class Node { public int key ; public List < Node > child ; } ; static Node newNode ( int value ) { Node nNode = new Node ( ) ; nNode . key = value ; nNode . child = new List < Node > ( ) ; return nNode ; } static int ind ; static Node BuildKaryTree ( int [ ] A , int n , int k , int h ) { if ( n <= 0 ) return null ; Node nNode = newNode ( A [ ind ] ) ; if ( nNode == null ) { Console . WriteLine ( " Memory ▁ error " ) ; return null ; } for ( int i = 0 ; i < k ; i ++ ) { if ( ind < n - 1 && h > 1 ) { ind ++ ; nNode . child . Add ( BuildKaryTree ( A , n , k , h - 1 ) ) ; } else { nNode . child . Add ( null ) ; } } return nNode ; } static Node BuildKaryTree_1 ( int [ ] A , int n , int k , int iN ) { int height = ( int ) Math . Ceiling ( Math . Log ( ( double ) n * ( k - 1 ) + 1 ) / Math . Log ( ( double ) k ) ) ; ind = iN ; return BuildKaryTree ( A , n , k , height ) ; } static void postord ( Node root , int k ) { if ( root == null ) return ; for ( int i = 0 ; i < k ; i ++ ) postord ( root . child [ i ] , k ) ; Console . Write ( root . key + " ▁ " ) ; } public static void Main ( String [ ] args ) { int ind = 0 ; int k = 3 , n = 10 ; int [ ] preorder = { 1 , 2 , 5 , 6 , 7 , 3 , 8 , 9 , 10 , 4 } ; Node root = BuildKaryTree_1 ( preorder , n , k , ind ) ; Console . WriteLine ( " Postorder ▁ traversal ▁ of ▁ " + " constructed ▁ full ▁ k - ary ▁ tree ▁ is : ▁ " ) ; postord ( root , k ) ; Console . WriteLine ( ) ; } }


"Construct the largest number whose sum of cost of digits is K | C # program for the above approach ; Function to find the maximum number among the two numbers S and T ; If "0" exists in the string S ; If "0" exists in the string T ; Else return the maximum number formed ; Recursive function to find maximum number formed such that the sum of cost of digits of formed number is K ; Base Case ; Return the stored state ; Including the digit ( idx + 1 ) ; Excluding the digit ( idx + 1 ) ; Store the result and return ; Function to find the maximum number formed such that the sum of the cost digits in the formed number is K ; Stores all Dp - states ; Recursive Call ; Return the result ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static string getMaximum ( string S , string T ) { if ( S . IndexOf ( "0" ) > - 1 ) return T ; if ( T . IndexOf ( "0" ) > - 1 ) return S ; return ( S . Length > T . Length ? S : T ) ; } static string recursion ( int [ ] arr , int idx , int N , int K , List < List < string > > dp ) { if ( K == 0 ) { return " " ; } if ( K < 0 idx == N ) { return "0" ; } if ( dp [ idx ] [ K ] != " - 1" ) return dp [ idx ] [ K ] ; string include = ( idx + 1 ) . ToString ( ) + recursion ( arr , 0 , N , K - arr [ idx ] , dp ) ; string exclude = recursion ( arr , idx + 1 , N , K , dp ) ; return dp [ idx ] [ K ] = getMaximum ( include , exclude ) ; } static string largestNumber ( int [ ] arr , int N , int K ) { List < List < string > > dp = new List < List < string > > ( ) ; for ( int i = 0 ; i < N + 1 ; i ++ ) { dp . Add ( new List < string > ( ) ) ; for ( int j = 0 ; j < K + 1 ; j ++ ) { dp [ i ] . Add ( " - 1" ) ; } } string ans = recursion ( arr , 0 , N , K , dp ) ; return ( ans == " " ? "0" : ans ) ; } static void Main ( ) { int [ ] arr = { 3 , 12 , 9 , 5 , 3 , 4 , 6 , 5 , 10 } ; int K = 14 ; int N = arr . Length ; Console . Write ( largestNumber ( arr , N , K ) ) ; } }


"Construct the smallest possible Array with given Sum and XOR | C # program implementation of the approach ; Function to find array ; Array not possible ; Array possible with exactly 1 or no element ; Checking array with two elements possible or not . ; Driver code ; Given sum and value of Bitwise XOR ; Function call"
Solution in C#:

using System ; class GFG { static void findArray ( int sum , int xorr ) { if ( xorr > sum sum % 2 != xorr % 2 ) { Console . WriteLine ( " No ▁ Array ▁ Possible " ) ; return ; } if ( sum == xorr ) { if ( sum == 0 ) Console . WriteLine ( " Array ▁ is ▁ empty ▁ " + " with ▁ size ▁ 0" ) ; else Console . WriteLine ( " Array ▁ size ▁ is ▁ " + 1 ) ; Console . WriteLine ( " Array ▁ is ▁ " + sum ) ; return ; } int mid = ( sum - xorr ) / 2 ; if ( xorr == 1 && mid == 1 ) { Console . WriteLine ( " Array ▁ size ▁ is ▁ " + 3 ) ; Console . WriteLine ( " Array ▁ is ▁ " + xorr + " ▁ " + mid + " ▁ " + mid ) ; } else { Console . WriteLine ( " Array ▁ size ▁ is ▁ " + 2 ) ; Console . WriteLine ( " Array ▁ is ▁ " + ( xorr + mid ) + " ▁ " + mid ) ; } } public static void Main ( ) { int sum = 4 , xorr = 2 ; findArray ( sum , xorr ) ; } }


"Construction of Longest Increasing Subsequence ( LIS ) and printing LIS sequence |  ; int [ ] array = { 10 , 2 , 9 , 3 , 5 , 4 , 6 , 8 } ; int [ ] array = { 10 , 9 , 8 , 6 , 5 , 4 } ; ; Compare previous highest subsequence ; Print list"
Solution in C#:

using System ; using System . Collections . Generic ; class longestIncreasingSubsequence { public static void Main ( String [ ] args ) { int [ ] array = { 10 , 22 , 9 , 33 , 21 , 50 , 41 , 60 , 80 } ; List < int > list = new List < int > ( ) ; List < int > longestList = new List < int > ( ) ; int currentMax ; int highestCount = 0 ; for ( int i = 0 ; i < array . Length ; i ++ ) { currentMax = int . MinValue ; for ( int j = i ; j < array . Length ; j ++ ) { if ( array [ j ] > currentMax ) { list . Add ( array [ j ] ) ; currentMax = array [ j ] ; } } if ( highestCount < list . Count ) { highestCount = list . Count ; longestList = new List < int > ( list ) ; } list . Clear ( ) ; } Console . WriteLine ( ) ; Console . WriteLine ( " The ▁ longest ▁ subsequence " ) ; foreach ( int itr in longestList ) { Console . Write ( itr + " ▁ " ) ; } Console . WriteLine ( ) ; Console . WriteLine ( " Length ▁ of ▁ LIS : ▁ " + highestCount ) ; } }


"Construction of Longest Increasing Subsequence ( LIS ) and printing LIS sequence | Dynamic Programming solution to construct Longest Increasing Subsequence ; Utility function to print LIS ; Function to construct and print Longest Increasing Subsequence ; L [ i ] - The longest increasing sub - sequence ends with arr [ i ] ; L [ 0 ] is equal to arr [ 0 ] ; start from index 1 ; do for every j less than i ; L [ i ] = { Max ( L [ j ] ) } + arr [ i ] where j < i and arr [ j ] < arr [ i ] ; L [ i ] ends with arr [ i ] ; L [ i ] now stores increasing sub - sequence of arr [ 0. . i ] that ends with arr [ i ] ; LIS will be max of all increasing sub - sequences of arr ; max will contain LIS ; Driver code ; construct and print LIS of arr"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void printLIS ( List < int > arr ) { foreach ( int x in arr ) { Console . Write ( x + " ▁ " ) ; } Console . WriteLine ( ) ; } static void constructPrintLIS ( int [ ] arr , int n ) { List < List < int > > L = new List < List < int > > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { L . Add ( new List < int > ( ) ) ; } L [ 0 ] . Add ( arr [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( ( arr [ i ] > arr [ j ] ) && ( L [ i ] . Count < L [ j ] . Count + 1 ) ) L [ i ] = L [ j ] ; } L [ i ] . Add ( arr [ i ] ) ; } List < int > max = L [ 0 ] ; foreach ( List < int > x in L ) { if ( x . Count > max . Count ) { max = x ; } } printLIS ( max ) ; } static void Main ( ) { int [ ] arr = { 3 , 2 , 4 , 5 , 1 } ; int n = arr . Length ; constructPrintLIS ( arr , n ) ; } }


"Construction of Longest Increasing Subsequence ( N log N ) | C # implementation to find longest increasing subsequence in O ( n Log n ) time . ; Binary search ; Add boundary case , when array n is zero Depend on smart pointers ; initialized with - 1 ; it will always point to empty location ; new smallest value ; arr [ i ] wants to extend largest subsequence ; arr [ i ] wants to be a potential condidate of future subsequence It will replace ceil value in tailIndices ; Driver code"
Solution in C#:

 using System ; class GFG { static int GetCeilIndex ( int [ ] arr , int [ ] T , int l , int r , int key ) { while ( r - l > 1 ) { int m = l + ( r - l ) / 2 ; if ( arr [ T [ m ] ] >= key ) r = m ; else l = m ; } return r ; } static int LongestIncreasingSubsequence ( int [ ] arr , int n ) { int [ ] tailIndices = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) tailIndices [ i ] = 0 ; int [ ] prevIndices = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) prevIndices [ i ] = - 1 ; int len = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ tailIndices [ 0 ] ] ) tailIndices [ 0 ] = i ; else if ( arr [ i ] > arr [ tailIndices [ len - 1 ] ] ) { prevIndices [ i ] = tailIndices [ len - 1 ] ; tailIndices [ len ++ ] = i ; } else { int pos = GetCeilIndex ( arr , tailIndices , - 1 , len - 1 , arr [ i ] ) ; prevIndices [ i ] = tailIndices [ pos - 1 ] ; tailIndices [ pos ] = i ; } } Console . Write ( " LIS ▁ of ▁ given ▁ input " ) ; for ( int i = tailIndices [ len - 1 ] ; i >= 0 ; i = prevIndices [ i ] ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; return len ; } public static void Main ( ) { int [ ] arr = { 2 , 5 , 3 , 7 , 11 , 8 , 10 , 13 , 6 } ; int n = arr . Length ; Console . Write ( " LIS ▁ size
 " + LongestIncreasingSubsequence ( arr , n ) ) ; } }


"Container with Most Water | C # code for Max Water Container ; Calculating the max area ; Driver code"
Solution in C#:

using System ; class Area { public static int maxArea ( int [ ] A , int len ) { int l = 0 ; int r = len - 1 ; int area = 0 ; while ( l < r ) { area = Math . Max ( area , Math . Min ( A [ l ] , A [ r ] ) * ( r - l ) ) ; if ( A [ l ] < A [ r ] ) l += 1 ; else r -= 1 ; } return area ; } public static void Main ( ) { int [ ] a = { 1 , 5 , 4 , 3 } ; int [ ] b = { 3 , 1 , 2 , 4 , 5 } ; int len1 = 4 ; Console . WriteLine ( maxArea ( a , len1 ) ) ; int len2 = 5 ; Console . WriteLine ( maxArea ( b , len2 ) ) ; } }


using System ; class GFG { public static int maxArea ( int [ ] a ) { int Area = 0 ; for ( int i = 0 ; i < a . Length ; i ++ ) { for ( int j = i + 1 ; j < a . Length ; j ++ ) { Area = Math . Max ( Area , Math . Min ( a [ i ] , a [ j ] ) * ( j - i ) ) ; } } return Area ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 5 , 4 , 3 } ; int [ ] b = { 3 , 1 , 2 , 4 , 5 } ; Console . WriteLine ( maxArea ( a ) ) ; Console . Write ( maxArea ( b ) ) ; } }


"Content of a Polynomial | C # implementation to find the content of the polynomial ; Function to find the content of the polynomial ; Loop to iterate over the elements of the array ; __gcd ( a , b ) is a inbuilt function for Greatest Common Divisor ; Driver Code ; Function call"
Solution in C#:

using System ; class GFG { static int findContent ( int [ ] arr , int n ) { int content = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { content = __gcd ( content , arr [ i ] ) ; } return content ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void Main ( String [ ] args ) { int n = 3 ; int [ ] arr = { 9 , 6 , 12 } ; Console . Write ( findContent ( arr , n ) ) ; } }


"Contiguous unique substrings with the given length L | C # implementation for above approach ; Function to print the unique sub - String of length n ; set to store the Strings ; if the size of the String is equal to 1 then insert ; inserting unique sub - String of length L ; Printing the set of Strings ; Driver Code ; Function calling"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void result ( String s , int n ) { HashSet < String > st = new HashSet < String > ( ) ; for ( int i = 0 ; i < s . Length ; i ++ ) { String ans = " " ; for ( int j = i ; j < s . Length ; j ++ ) { ans += s [ j ] ; if ( ans . Length == n ) { st . Add ( ans ) ; break ; } } } foreach ( String it in st ) Console . Write ( it + " ▁ " ) ; } public static void Main ( String [ ] args ) { String s = " abca " ; int n = 3 ; result ( s , n ) ; } }


"Continuous Tree | C # Code to check if the tree is continuos or not ; Node structure ; Function to check if the tree is continuos or not ; If root is Null then tree isn 't Continuos ; BFS Traversal ; Move to left child ; if difference between parent and child is equal to 1 then do continue otherwise make flag = 0 and break ; Move to right child ; if difference between parent and child is equal to 1 then do continue otherwise make flag = 0 and break ; Driver Code ; Constructing the Tree ; Function Call"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { public class node { public int val ; public node left ; public node right ; public node ( ) { this . val = 0 ; this . left = null ; this . right = null ; } public node ( int x ) { this . val = x ; this . left = null ; this . right = null ; } public node ( int x , node left , node right ) { this . val = x ; this . left = left ; this . right = right ; } } ; static bool continuous ( node root ) { if ( root == null ) return false ; int flag = 1 ; Queue < node > Q = new Queue < node > ( ) ; Q . Enqueue ( root ) ; node temp ; while ( Q . Count != 0 ) { temp = Q . Peek ( ) ; Q . Dequeue ( ) ; if ( temp . left != null ) { if ( Math . Abs ( temp . left . val - temp . val ) == 1 ) Q . Enqueue ( temp . left ) ; else { flag = 0 ; break ; } } if ( temp . right != null ) { if ( Math . Abs ( temp . right . val - temp . val ) == 1 ) Q . Enqueue ( temp . right ) ; else { flag = 0 ; break ; } } } if ( flag != 0 ) return true ; else return false ; } public static void Main ( String [ ] args ) { node root = new node ( 3 ) ; root . left = new node ( 2 ) ; root . right = new node ( 4 ) ; root . left . left = new node ( 1 ) ; root . left . right = new node ( 3 ) ; root . right . right = new node ( 5 ) ; if ( continuous ( root ) ) Console . Write ( " True
 " ) ; else Console . Write ( " False
 " ) ; } }


"Continuous Tree | C # program to check if a tree is continuous or not ; A binary tree node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new node with the given data and null left and right pointers . ; Function to check tree is continuous or not ; if next node is empty then return true ; if current node is leaf node then return true because it is end of root to leaf path ; If left subtree is empty , then only check right ; If right subtree is empty , then only check left ; If both left and right subtrees are not empty , check everything ; Driver program to test mirror ( )"
Solution in C#:

using System ; class solution { class Node { public int data ; public Node left , right ; } ; static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } static Boolean treeContinuous ( Node ptr ) { if ( ptr == null ) return true ; if ( ptr . left == null && ptr . right == null ) return true ; if ( ptr . left == null ) return ( Math . Abs ( ptr . data - ptr . right . data ) == 1 ) && treeContinuous ( ptr . right ) ; if ( ptr . right == null ) return ( Math . Abs ( ptr . data - ptr . left . data ) == 1 ) && treeContinuous ( ptr . left ) ; return Math . Abs ( ptr . data - ptr . left . data ) == 1 && Math . Abs ( ptr . data - ptr . right . data ) == 1 && treeContinuous ( ptr . left ) && treeContinuous ( ptr . right ) ; } static public void Main ( String [ ] args ) { Node root = newNode ( 3 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 4 ) ; root . left . left = newNode ( 1 ) ; root . left . right = newNode ( 3 ) ; root . right . right = newNode ( 5 ) ; if ( treeContinuous ( root ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Convert 0 to N by adding 1 or multiplying by 2 in minimum steps | C # program for above approach ; Function to count number of set bits in N ; Stores the count of set bits ; If N is odd , then it a set bit ; Return the result ; Driver Code"
Solution in C#:

using System ; public class GFG { static int minimumAdditionOperation ( int N ) { int count = 0 ; while ( N != 0 ) { if ( ( N & 1 ) == 1 ) { count ++ ; } N = N >> 1 ; } return count ; } static public void Main ( ) { int N = 6 ; Console . Write ( minimumAdditionOperation ( N ) ) ; } }


"Convert 1 into X in min steps by multiplying with 2 or 3 or by adding 1 | C # program for the above approach ; Function to print the Minimum number of operations required to convert 1 into X by using three given operations ; Initialize a DP array to store min operations for sub - problems ; Base Condition : No operation required when N is 1 ; Multiply the number by 2 ; Multiply the number by 3 ; Add 1 to the number . ; Print the minimum operations ; Initialize a list to store the sequence ; Backtrack to find the sequence ; If add by 1 ; If multiply by 2 ; If multiply by 3 ; Print the sequence of operation ; Driver code ; Given number X ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void printMinOperations ( int N ) { int [ ] dp = new int [ N + 1 ] ; for ( int i = 0 ; i < N + 1 ; i ++ ) dp [ i ] = N ; dp [ 1 ] = 0 ; for ( int i = 2 ; i < N + 1 ; i ++ ) { if ( i % 2 == 0 && dp [ i ] > dp [ i / 2 ] + 1 ) dp [ i ] = dp [ i / 2 ] + 1 ; if ( i % 3 == 0 && dp [ i ] > dp [ i / 3 ] + 1 ) dp [ i ] = dp [ i / 3 ] + 1 ; if ( dp [ i ] > dp [ i - 1 ] + 1 ) dp [ i ] = dp [ i - 1 ] + 1 ; } Console . WriteLine ( dp [ N ] ) ; List < int > seq = new List < int > ( ) ; while ( N > 1 ) { seq . Add ( N ) ; if ( dp [ N - 1 ] == dp [ N ] - 1 ) N = N - 1 ; else if ( N % 2 == 0 && dp [ N / 2 ] == dp [ N ] - 1 ) N = N / 2 ; else N = N / 3 ; } seq . Add ( 1 ) ; seq . Reverse ( ) ; foreach ( var i in seq ) { Console . Write ( i + " ▁ " ) ; } } public static void Main ( ) { int X = 96234 ; printMinOperations ( X ) ; } }


"Convert A into B by incrementing or decrementing 1 , 2 , or 5 any number of times | C # program for the above approach ; Function to find minimum number of moves required to convert A into B ; Stores the minimum number of moves required ; Stores the absolute difference ; FInd the number of moves ; Return cnt ; Driver Code ; Input ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int minimumSteps ( int a , int b ) { int cnt = 0 ; a = Math . Abs ( a - b ) ; cnt = ( a / 5 ) + ( a % 5 ) / 2 + ( a % 5 ) % 2 ; return cnt ; } public static void Main ( ) { int A = 3 , B = 9 ; Console . Write ( minimumSteps ( A , B ) ) ; } }


"Convert String into Binary Sequence | C # program to convert string into binary string ; utility function ; convert each char to ASCII value ; Convert ASCII value to binary ; Utility Reverse function ; Driver code"
Solution in C#:

using System ; public class Node { static void strToBinary ( String s ) { int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) { int val = s [ i ] ; String bin = " " ; while ( val > 0 ) { if ( val % 2 == 1 ) { bin += '1' ; } else bin += '0' ; val /= 2 ; } bin = reverse ( bin ) ; Console . Write ( bin + " ▁ " ) ; } } static String reverse ( String input ) { char [ ] a = input . ToCharArray ( ) ; int l , r = 0 ; r = a . Length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . Join ( " " , a ) ; } public static void Main ( String [ ] args ) { String s = " geeks " ; strToBinary ( s ) ; } }


"Convert Ternary Expression to a Binary Tree | C # program to convert a ternary expreesion to a tree . ; Class to represent Tree node ; Class to convert a ternary expression to a Tree ; Function to convert Ternary Expression to a Binary Tree . It return the root of tree ; Base case ; store current character of expression_string [ ' a ' to ' z ' ] ; Move ahead in str ; if current character of ternary expression is ' ? ' then we add next character as a left child of current node ; else we have to add it as a right child of current node expression . at ( 0 ) == ' : ' ; function print tree ; Driver Code"
Solution in C#:

using System ; public class Node { public char data ; public Node left , right ; public Node ( char item ) { data = item ; left = null ; right = null ; } } public class BinaryTree { public virtual Node convertExpression ( char [ ] expression , int i ) { if ( i >= expression . Length ) { return null ; } Node root = new Node ( expression [ i ] ) ; ++ i ; if ( i < expression . Length && expression [ i ] == ' ? ' ) { root . left = convertExpression ( expression , i + 1 ) ; } else if ( i < expression . Length ) { root . right = convertExpression ( expression , i + 1 ) ; } return root ; } public virtual void printTree ( Node root ) { if ( root == null ) { return ; } Console . Write ( root . data + " ▁ " ) ; printTree ( root . left ) ; printTree ( root . right ) ; } public static void Main ( string [ ] args ) { string exp = " a ? b ? c : d : e " ; BinaryTree tree = new BinaryTree ( ) ; char [ ] expression = exp . ToCharArray ( ) ; Node root = tree . convertExpression ( expression , 0 ) ; tree . printTree ( root ) ; } }


"Convert Unix timestamp to DD / MM / YYYY HH : MM : SS format | Unix time is in seconds and Humar Readable Format : DATE : MONTH : YEAR : HOUR : MINUTES : SECONDS , Start of unix time : 01 Jan 1970 , 00 : 00 : 00 ; Function to convert unix time to Human readable format ; Save the time in Human readable format ; Number of days in month in normal year ; Calculate total days unix time T ; Calculating current year ; Updating extradays because it will give days till previous day and we have include current day ; Calculating MONTH and DATE ; Current Month ; Calculating HH : MM : YYYY ; Return the time ; Driver Code ; Given unix time ; Function call to convert unix time to human read able ; Print time in format DD : MM : YYYY : HH : MM : SS"
Solution in C#:

 using System ; class GFG { static String unixTimeToHumanReadable ( int seconds ) { String ans = " " ; int [ ] daysOfMonth = { 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 } ; int currYear , daysTillNow , extraTime , extraDays , index , date , month , hours , minutes , secondss , flag = 0 ; daysTillNow = seconds / ( 24 * 60 * 60 ) ; extraTime = seconds % ( 24 * 60 * 60 ) ; currYear = 1970 ; while ( daysTillNow >= 365 ) { if ( currYear % 400 == 0 || ( currYear % 4 == 0 && currYear % 100 != 0 ) ) { daysTillNow -= 366 ; } else { daysTillNow -= 365 ; } currYear += 1 ; } extraDays = daysTillNow + 1 ; if ( currYear % 400 == 0 || ( currYear % 4 == 0 && currYear % 100 != 0 ) ) flag = 1 ; month = 0 ; index = 0 ; if ( flag == 1 ) { while ( true ) { if ( index == 1 ) { if ( extraDays - 29 < 0 ) break ; month += 1 ; extraDays -= 29 ; } else { if ( extraDays - daysOfMonth [ index ] < 0 ) { break ; } month += 1 ; extraDays -= daysOfMonth [ index ] ; } index += 1 ; } } else { while ( true ) { if ( extraDays - daysOfMonth [ index ] < 0 ) { break ; } month += 1 ; extraDays -= daysOfMonth [ index ] ; index += 1 ; } } if ( extraDays > 0 ) { month += 1 ; date = extraDays ; } else { if ( month == 2 && flag == 1 ) date = 29 ; else { date = daysOfMonth [ month - 1 ] ; } } hours = extraTime / 3600 ; minutes = ( extraTime % 3600 ) / 60 ; secondss = ( extraTime % 3600 ) % 60 ; ans += String . Join ( " " , date ) ; ans += " / " ; ans += String . Join ( " " , month ) ; ans += " / " ; ans += String . Join ( " " , currYear ) ; ans += " ▁ " ; ans += String . Join ( " " , hours ) ; ans += " : " ; ans += String . Join ( " " , minutes ) ; ans += " : " ; ans += String . Join ( " " , secondss ) ; return ans ; } public static void Main ( String [ ] args ) { int T = 1595497956 ; String ans = unixTimeToHumanReadable ( T ) ; Console . Write ( ans + "
 " ) ; } }


"Convert a Binary String to another by flipping prefixes minimum number of times | C # program to implement the above approach ; Function to count minimum number of operations required to convert string a to another string b ; Store the lengths of each prefixes selected ; Traverse the string ; If first character is same as b [ i ] ; Insert 1 to ops [ ] ; And , flip the bit ; Reverse the prefix string of length i + 1 ; Flip the characters in this prefix length ; Push ( i + 1 ) to array ops [ ] ; Print the number of operations ; Print the length of each prefixes stored ; Function to reverse a [ ] from start to end ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void minOperations ( string s1 , string s2 , int n ) { char [ ] a = s1 . ToCharArray ( ) ; char [ ] b = s2 . ToCharArray ( ) ; List < int > ops = new List < int > ( ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( a [ i ] != b [ i ] ) { if ( a [ 0 ] == b [ i ] ) { ops . Add ( 1 ) ; a [ 0 ] = ( a [ 0 ] == '0' ? '1' : '0' ) ; } reverse ( a , 0 , i ) ; for ( int j = 0 ; j <= i ; j ++ ) { a [ j ] = ( a [ j ] == '0' ? '1' : '0' ) ; } ops . Add ( i + 1 ) ; } } Console . WriteLine ( ops . Count ) ; foreach ( int x in ops ) { Console . Write ( x + " ▁ " ) ; } } static void reverse ( char [ ] a , int start , int end ) { while ( start < end ) { char temp = a [ start ] ; a [ start ] = a [ end ] ; a [ end ] = temp ; start ++ ; end -- ; } } public static void Main ( ) { string a = "10" , b = "01" ; int N = a . Length ; minOperations ( a , b , N ) ; } }


"Convert a Binary Tree into Doubly Linked List in spiral fashion | C # program to convert Binary Tree into Doubly Linked List where the nodes are represented spirally ; A binary tree node ; Given a reference to a node , inserts the node on the front of the list . ; Make right of given node as head and left as NULL ; change left of head node to given node ; move the head to point to the given node ; Function to prints contents of DLL ; Function to print corner node at each level ; Base Case ; Create an empty deque for doing spiral level order traversal and enqueue root ; create a stack to store Binary Tree nodes to insert into DLL later ; nodeCount indicates number of Nodes at current level . ; Dequeue all Nodes of current level and Enqueue all Nodes of next level odd level ; dequeue node from front & push it to stack ; insert its left and right children in the back of the deque ; even level ; dequeue node from the back & push it to stack ; inserts its right and left children in the front of the deque ; pop all nodes from stack and push them in the beginning of the list ; Driver program to test above functions ; Let us create binary tree as shown in above diagram ; tree . root . right . left . left = new Node ( 12 ) ; ; tree . root . right . right . right = new Node ( 15 ) ;"
Solution in C#:

using System ; using System . Collections . Generic ; public class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } public class BinaryTree { Node root ; Node head ; void push ( Node node ) { node . right = head ; node . left = null ; if ( head != null ) head . left = node ; head = node ; } void printList ( Node node ) { while ( node != null ) { Console . Write ( node . data + " ▁ " ) ; node = node . right ; } } void spiralLevelOrder ( Node root ) { if ( root == null ) return ; LinkedList < Node > q = new LinkedList < Node > ( ) ; q . AddFirst ( root ) ; Stack < Node > stk = new Stack < Node > ( ) ; int level = 0 ; while ( q . Count != 0 ) { int nodeCount = q . Count ; if ( ( level & 1 ) % 2 != 0 ) { while ( nodeCount > 0 ) { Node node = q . First . Value ; q . RemoveFirst ( ) ; stk . Push ( node ) ; if ( node . left != null ) q . AddLast ( node . left ) ; if ( node . right != null ) q . AddLast ( node . right ) ; nodeCount -- ; } } else { while ( nodeCount > 0 ) { Node node = q . Last . Value ; q . RemoveLast ( ) ; stk . Push ( node ) ; if ( node . right != null ) q . AddFirst ( node . right ) ; if ( node . left != null ) q . AddFirst ( node . left ) ; nodeCount -- ; } } level ++ ; } while ( stk . Count != 0 ) { push ( stk . Peek ( ) ) ; stk . Pop ( ) ; } Console . WriteLine ( " Created ▁ DLL ▁ is ▁ : ▁ " ) ; printList ( head ) ; } public static void Main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; 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 . root . right ....


"Convert a Binary Tree into its Mirror Tree | C # Iterative Java program to convert a Binary Tree to its mirror ; A binary tree node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new node with the given data and null left and right pointers . ; Change a tree so that the roles of the left and right pointers are swapped at every node . So the tree ... 4 / \ 2 5 / \ 1 3 is changed to ... 4 / \ 5 2 / \ 3 1 ; Do BFS . While doing BFS , keep swapping left and right children ; pop top node from queue ; swap left child with right child ; push left and right children ; Helper function to print Inorder traversal . ; Driver code ; Print inorder traversal of the input tree ; Convert tree to its mirror ; Print inorder traversal of the mirror tree"
Solution in C#:

 using System . Collections . Generic ; using System ; class GFG { public class Node { public int data ; public Node left ; public Node right ; } ; static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } static void mirror ( Node root ) { if ( root == null ) return ; Queue < Node > q = new Queue < Node > ( ) ; q . Enqueue ( root ) ; while ( q . Count > 0 ) { Node curr = q . Peek ( ) ; q . Dequeue ( ) ; Node temp = curr . left ; curr . left = curr . right ; curr . right = temp ; ; if ( curr . left != null ) q . Enqueue ( curr . left ) ; if ( curr . right != null ) q . Enqueue ( curr . right ) ; } } static void inOrder ( Node node ) { if ( node == null ) return ; inOrder ( node . left ) ; Console . Write ( node . data + " ▁ " ) ; inOrder ( node . right ) ; } public static void Main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; Console . Write ( "
 ▁ Inorder ▁ traversal ▁ of ▁ the " + " ▁ coned ▁ tree ▁ is ▁
 " ) ; inOrder ( root ) ; mirror ( root ) ; Console . Write ( "
 ▁ Inorder ▁ traversal ▁ of ▁ the ▁ " + " mirror ▁ tree ▁ is ▁
 " ) ; inOrder ( root ) ; } }


"Convert a Binary Tree into its Mirror Tree | C # program to convert binary tree into its mirror ; Class containing left and right child of current node and key value ; Change a tree so that the roles of the left and right pointers are swapped at every node . So the tree ... 4 / \ 2 5 / \ 1 3 is changed to ... 4 / \ 5 2 / \ 3 1 ; do the subtrees ; swap the left and right pointers ; Helper function to test mirror ( ) . Given a binary search tree , print out its data elements in increasing sorted order . ; testing for example nodes ; print inorder traversal of the input tree ; convert tree to its mirror ; print inorder traversal of the minor tree"
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 mirror ( ) { root = mirror ( root ) ; } public virtual Node mirror ( Node node ) { if ( node == null ) { return node ; } Node left = mirror ( node . left ) ; Node right = mirror ( node . right ) ; node . left = right ; node . right = left ; return node ; } public virtual void inOrder ( ) { inOrder ( root ) ; } public virtual void inOrder ( Node node ) { if ( node == null ) { return ; } inOrder ( node . left ) ; Console . Write ( node . data + " ▁ " ) ; inOrder ( 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 ) ; Console . WriteLine ( " Inorder ▁ traversal ▁ " + " of ▁ input ▁ tree ▁ is ▁ : " ) ; tree . inOrder ( ) ; Console . WriteLine ( " " ) ; tree . mirror ( ) ; Console . WriteLine ( " Inorder ▁ traversal ▁ " + " of ▁ binary ▁ tree ▁ is ▁ : ▁ " ) ; tree . inOrder ( ) ; } }


"Convert a Binary Tree to Threaded binary tree | Set 2 ( Efficient ) | C # program to convert a Binary Tree to Threaded Tree ; structure of a node in threaded binary tree ; Used to indicate whether the right pointer is a normal right pointer or a pointer to inorder successor . ; Converts tree with given root to threaded binary tree . This function returns rightmost child of root . ; Base cases : Tree is empty or has single node ; Find predecessor if it exists ; Find predecessor of root ( Rightmost child in left subtree ) ; Link a thread from predecessor to root . ; If current node is rightmost child ; Recur for right subtree . ; A utility function to find leftmost node in a binary tree rooted with ' root ' . This function is used in inOrder ( ) ; Function to do inorder traversal of a threadded binary tree ; Find the leftmost node in Binary Tree ; If this Node is a thread Node , then go to inorder successor ; Else go to the leftmost child in right subtree ; A utility function to create a new node ; Driver program to test above functions ; 1 / \ 2 3 / \ / \ 4 5 6 7"
Solution in C#:

 using System ; public class solution { public class Node { public int key ; public Node left , right ; public bool isThreaded ; } public static Node createThreaded ( Node root ) { if ( root == null ) { return null ; } if ( root . left == null && root . right == null ) { return root ; } if ( root . left != null ) { Node l = createThreaded ( root . left ) ; l . right = root ; l . isThreaded = true ; } if ( root . right == null ) { return root ; } return createThreaded ( root . right ) ; } public static Node leftMost ( Node root ) { while ( root != null && root . left != null ) { root = root . left ; } return root ; } public static void inOrder ( Node root ) { if ( root == null ) { return ; } Node cur = leftMost ( root ) ; while ( cur != null ) { Console . Write ( cur . key + " ▁ " ) ; if ( cur . isThreaded ) { cur = cur . right ; } else { cur = leftMost ( cur . right ) ; } } } public static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . left = temp . right = null ; temp . key = key ; return temp ; } public static void Main ( string [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; root . right . left = newNode ( 6 ) ; root . right . right = newNode ( 7 ) ; createThreaded ( root ) ; Console . WriteLine ( " Inorder ▁ traversal ▁ of ▁ created ▁ " + " threaded ▁ tree ▁ is
 " ) ; inOrder ( root ) ; } }


"Convert a Matrix into another Matrix of given dimensions | C # program to implement the above approach ; Function to cona matrix of size A * B from the given matrix elements ; Initialize a new matrix ; Traverse the matrix , [ , ] mat ; Update idx ; Print the resultant matrix ; Driver Code"
Solution in C#:

 using System ; class GFG { static void ConstMatrix ( int [ , ] mat , int N , int M , int A , int B ) { if ( N * M != A * B ) return ; int idx = 0 ; int [ , ] res = new int [ A , B ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { res [ idx / B , idx % B ] = mat [ i , j ] ; idx ++ ; } } for ( int i = 0 ; i < A ; i ++ ) { for ( int j = 0 ; j < B ; j ++ ) Console . Write ( res [ i , j ] + " ▁ " ) ; Console . Write ( "
 " ) ; } } public static void Main ( String [ ] args ) { int [ , ] mat = { { 1 , 2 , 3 , 4 , 5 , 6 } } ; int A = 2 ; int B = 3 ; int N = mat . GetLength ( 0 ) ; int M = mat . GetLength ( 1 ) ; ConstMatrix ( mat , N , M , A , B ) ; } }


"Convert a Mobile Numeric Keypad sequence to equivalent sentence | C # program for the above approach ; Function to convert mobile numeric keypad sequence into its equivalent string ; Store the mobile keypad mappings ; Traverse the string str ; If the current character is ' . ' , then continue to the next iteration ; Stores the number of continuous clicks ; Iterate a loop to find the count of same characters ; 2 , 3 , 4 , 5 , 6 and 8 keys will have maximum of 3 letters ; 7 and 9 keys will have maximum of 4 keys ; Handle the end condition ; Check if the current pressed key is 7 or 9 ; Else , the key pressed is either 2 , 3 , 4 , 5 , 6 or 8 ; Driver Code"
Solution in C#:

using System ; public class GFG { static void printSentence ( string S ) { string [ ] nums = { " " , " " , " ABC " , " DEF " , " GHI " , " JKL " , " MNO " , " PQRS " , " TUV " , " WXYZ " } ; char [ ] str = S . ToCharArray ( ) ; int i = 0 ; while ( i < str . Length ) { if ( str [ i ] == ' . ' ) { i ++ ; continue ; } int count = 0 ; while ( i + 1 < str . Length && str [ i ] == str [ i + 1 ] ) { if ( count == 2 && ( ( str [ i ] >= '2' && str [ i ] <= '6' ) || ( str [ i ] == '8' ) ) ) break ; else if ( count == 3 && ( str [ i ] == '7' str [ i ] == '9' ) ) break ; count ++ ; i ++ ; if ( i == str . Length ) break ; } if ( str [ i ] == '7' str [ i ] == '9' ) { Console . Write ( nums [ str [ i ] - 48 ] [ count % 4 ] ) ; } else { Console . Write ( nums [ str [ i ] - 48 ] [ count % 3 ] ) ; } i ++ ; } } public static void Main ( string [ ] args ) { string str = "234" ; printSentence ( str ) ; } }


"Convert a String to a Singly Linked List | C # program to Convert a String to a Singly Linked List ; Structure for a Singly Linked List ; Function to add a new node to the Linked List ; Function to convert the string to Linked List . ; curr pointer points to the current node where the insertion should take place ; Function to print the data present in all the nodes ; Driver code"
Solution in C#:

using System ; class GFG { class node { public char data ; public node next ; } ; static node add ( char data ) { node newnode = new node ( ) ; newnode . data = data ; newnode . next = null ; return newnode ; } static node string_to_SLL ( String text , node head ) { head = add ( text [ 0 ] ) ; node curr = head ; for ( int i = 1 ; i < text . Length ; i ++ ) { curr . next = add ( text [ i ] ) ; curr = curr . next ; } return head ; } static void print ( node head ) { node curr = head ; while ( curr != null ) { Console . Write ( curr . data + " ▁ - > ▁ " ) ; curr = curr . next ; } } public static void Main ( String [ ] args ) { String text = " GEEKS " ; node head = null ; head = string_to_SLL ( text , head ) ; print ( head ) ; } }


"Convert a given Binary Tree to Circular Doubly Linked List | Set 2 | C # program for conversion of Binary Tree to CDLL ; A binary tree node has data , and left and right pointers ; Function to perform In - Order traversal of the tree and store the nodes in a vector ; first recur on left child ; append the data of node in vector ; now recur on right child ; Function to convert Binary Tree to Circular Doubly Linked list using the vector which stores In - Order traversal of the Binary Tree ; Base cases ; Vector to be used for storing the nodes of tree in In - order form ; Calling the In - Order traversal function ; Create the head of the linked list pointing to the root of the tree ; Create a current pointer to be used in traversal ; Traversing the nodes of the tree starting from the second elements ; Create a temporary pointer pointing to current ; Current 's right points to the current  node in traversal ; Current points to its right ; Current 's left points to temp ; Current 's right points to head of the list ; Head 's left points to current ; Return head of the list ; Display Circular Link List ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public class Node { public int data ; public Node left ; public Node right ; public Node ( int x ) { data = x ; left = right = null ; } } ; static void inorder ( Node root , List < int > v ) { if ( root == null ) return ; inorder ( root . left , v ) ; v . Add ( root . data ) ; inorder ( root . right , v ) ; } static Node bTreeToCList ( Node root ) { if ( root == null ) return null ; List < int > v = new List < int > ( ) ; inorder ( root , v ) ; Node head_ref = new Node ( v [ 0 ] ) ; Node curr = head_ref ; for ( int i = 1 ; i < v . Count ; i ++ ) { Node temp = curr ; curr . right = new Node ( v [ i ] ) ; curr = curr . right ; curr . left = temp ; } curr . right = head_ref ; head_ref . left = curr ; return head_ref ; } static void displayCList ( Node head ) { Console . WriteLine ( " Circular ▁ Doubly ▁ " + " Linked ▁ List ▁ is ▁ : " ) ; Node itr = head ; do { Console . Write ( itr . data + " ▁ " ) ; itr = itr . right ; } while ( head != itr ) ; Console . WriteLine ( ) ; } public static void Main ( String [ ] args ) { Node root = new Node ( 10 ) ; root . left = new Node ( 12 ) ; root . right = new Node ( 15 ) ; root . left . left = new Node ( 25 ) ; root . left . right = new Node ( 30 ) ; root . right . left = new Node ( 36 ) ; Node head = bTreeToCList ( root ) ; displayCList ( head ) ; } }


"Convert a given Binary Tree to Doubly Linked List | Set 1 | C # program to convert binary tree to double linked list ; A binary tree node has data , and left and right pointers ; This is the core function to convert Tree to list . This function follows steps 1 and 2 of the above algorithm ; Base case ; Convert the left subtree and link to root ; Convert the left subtree ; Find inorder predecessor . After this loop , left will point to the inorder predecessor ; Make root as next of the predecessor ; Make predecssor as previous of root ; Convert the right subtree and link to root ; Convert the right subtree ; Find inorder successor . After this loop , right will point to the inorder successor ; Make root as previous of successor ; Make successor as next of root ; The main function that first calls bintree2listUtil ( ) , then follows step 3 of the above algorithm ; Base case ; Convert to DLL using bintree2listUtil ( ) ; bintree2listUtil ( ) returns root node of the converted DLL . We need pointer to the leftmost node which is head of the constructed DLL , so move to the leftmost node ; Function to print nodes in a given doubly linked list ; Driver program to test above functions ; Let us create the tree shown in above diagram ; Convert to DLL ; Print the converted list"
Solution in C#:

using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class BinaryTree { public Node root ; public virtual Node bintree2listUtil ( Node node ) { if ( node == null ) { return node ; } if ( node . left != null ) { Node left = bintree2listUtil ( node . left ) ; for ( ; left . right != null ; left = left . right ) { ; } left . right = node ; node . left = left ; } if ( node . right != null ) { Node right = bintree2listUtil ( node . right ) ; for ( ; right . left != null ; right = right . left ) { ; } right . left = node ; node . right = right ; } return node ; } public virtual Node bintree2list ( Node node ) { if ( node == null ) { return node ; } node = bintree2listUtil ( node ) ; while ( node . left != null ) { node = node . left ; } return node ; } public virtual void printList ( Node node ) { while ( node != null ) { Console . Write ( node . data + " ▁ " ) ; node = node . right ; } } public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( 12 ) ; tree . root . right = new Node ( 15 ) ; tree . root . left . left = new Node ( 25 ) ; tree . root . left . right = new Node ( 30 ) ; tree . root . right . left = new Node ( 36 ) ; Node head = tree . bintree2list ( tree . root ) ; tree . printList ( head ) ; } }


"Convert a given Binary Tree to Doubly Linked List | Set 2 | C # program to convert BTT to DLL using simple inorder traversal ; A tree node ; Standard Inorder traversal of tree ; Changes left pointers to work as previous pointers in converted DLL The function simply does inorder traversal of Binary Tree and updates left pointer using previously visited node ; Changes right pointers to work as next pointers in converted DLL ; Find the right most node in BT or last node in DLL ; Start from the rightmost node , traverse back using left pointers . While traversing , change right pointer of nodes ; The leftmost node is head of linked list , return it ; The main function that converts BST to DLL and returns head of DLL ; Set the previous pointer ; Set the next pointer and return head of DLL ; Traverses the DLL from left tor right ; Driver code ; Let us create the tree shown in above diagram"
Solution in C#:

 using System ; class GFG { public class node { public int data ; public node left , right ; public node ( int data ) { this . data = data ; } } public static node prev ; public static void inorder ( node root ) { if ( root == null ) { return ; } inorder ( root . left ) ; Console . Write ( root . data + " ▁ " ) ; inorder ( root . right ) ; } public static void fixPrevptr ( node root ) { if ( root == null ) { return ; } fixPrevptr ( root . left ) ; root . left = prev ; prev = root ; fixPrevptr ( root . right ) ; } public static node fixNextptr ( node root ) { while ( root . right != null ) { root = root . right ; } while ( root != null && root . left != null ) { node left = root . left ; left . right = root ; root = root . left ; } return root ; } public static node BTTtoDLL ( node root ) { prev = null ; fixPrevptr ( root ) ; return fixNextptr ( root ) ; } public static void printlist ( node root ) { while ( root != null ) { Console . Write ( root . data + " ▁ " ) ; root = root . right ; } } public static void Main ( ) { node root = new node ( 10 ) ; root . left = new node ( 12 ) ; root . right = new node ( 15 ) ; root . left . left = new node ( 25 ) ; root . left . right = new node ( 30 ) ; root . right . left = new node ( 36 ) ; Console . WriteLine ( " Inorder ▁ Tree ▁ Traversal " ) ; inorder ( root ) ; node head = BTTtoDLL ( root ) ; Console . WriteLine ( "
 DLL ▁ Traversal " ) ; printlist ( head ) ; } }


"Convert a given Binary Tree to Doubly Linked List | Set 3 | A C # program for in - place conversion of Binary Tree to DLL ; A binary tree node has data , left pointers and right pointers ; head -- > Pointer to head node of created doubly linked list ; A simple recursive function to convert a given Binary tree to Doubly Linked List root -- > Root of Binary Tree ; Base case ; Initialize previously visited node as NULL . This is static so that the same value is accessible in all recursive calls ; Recursively convert left subtree ; Now convert this node ; Finally convert right subtree ; Function to print nodes in a given doubly linked list ; Driver Code ; Let us create the tree as shown in above diagram ; convert to DLL ; Print the converted List"
Solution in C#:

using System ; public class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } class GFG { public Node root ; public Node head ; public virtual void BinaryTree2DoubleLinkedList ( Node root ) { if ( root == null ) { return ; } public static Node prev = null ; BinaryTree2DoubleLinkedList ( root . left ) ; if ( prev == null ) { head = root ; } else { root . left = prev ; prev . right = root ; } prev = root ; BinaryTree2DoubleLinkedList ( root . right ) ; } public virtual void printList ( Node node ) { while ( node != null ) { Console . Write ( node . data + " ▁ " ) ; node = node . right ; } } public static void Main ( string [ ] args ) { GFG tree = new GFG ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( 12 ) ; tree . root . right = new Node ( 15 ) ; tree . root . left . left = new Node ( 25 ) ; tree . root . left . right = new Node ( 30 ) ; tree . root . right . left = new Node ( 36 ) ; tree . BinaryTree2DoubleLinkedList ( tree . root ) ; tree . printList ( tree . head ) ; } }


"Convert a given Binary tree to a tree that holds Logical AND property | C # code to convert a given binary tree to a tree that holds logical AND property . ; Structure of binary tree ; function to create a new node ; Convert the given tree to a tree where each node is logical AND of its children The main idea is to do Postorder traversal ; first recur on left child ; then recur on right child ; first recur on left child ; then print the data of node ; now recur on right child ; Driver code ; Create following Binary Tree 1 / \ 1 0 / \ / \ 0 1 1 1"
Solution in C#:

using System ; class GfG { class Node { public int data ; public Node left ; public Node right ; } static Node newNode ( int key ) { Node node = new Node ( ) ; node . data = key ; node . left = null ; node . right = null ; return node ; } static void convertTree ( Node root ) { if ( root == null ) return ; convertTree ( root . left ) ; convertTree ( root . right ) ; if ( root . left != null && root . right != null ) root . data = ( root . left . data ) & ( root . right . data ) ; } static void printInorder ( Node root ) { if ( root == null ) return ; printInorder ( root . left ) ; Console . Write ( root . data + " ▁ " ) ; printInorder ( root . right ) ; } public static void Main ( ) { Node root = newNode ( 0 ) ; root . left = newNode ( 1 ) ; root . right = newNode ( 0 ) ; root . left . left = newNode ( 0 ) ; root . left . right = newNode ( 1 ) ; root . right . left = newNode ( 1 ) ; root . right . right = newNode ( 1 ) ; Console . Write ( " Inorder ▁ traversal ▁ before ▁ conversion ▁ " ) ; printInorder ( root ) ; convertTree ( root ) ; Console . WriteLine ( ) ; Console . Write ( " Inorder ▁ traversal ▁ after ▁ conversion ▁ " ) ; printInorder ( root ) ; } }


"Convert a given temperature to another system based on given boiling and freezing points | C # program for above approach ; Function to return temperature in the second thermometer ; Calculate the temperature ; Driver Code"
Solution in C#:

using System ; class GFG { static double temp_convert ( int F1 , int B1 , int F2 , int B2 , int T ) { float t2 ; t2 = F2 + ( float ) ( B2 - F2 ) / ( B1 - F1 ) * ( T - F1 ) ; return t2 ; } public static void Main ( ) { int F1 = 0 , B1 = 100 ; int F2 = 32 , B2 = 212 ; int T = 37 ; Console . Write ( String . Format ( " { 0:0 . # # } " , temp_convert ( F1 , B1 , F2 , B2 , T ) ) ) ; } }


"Convert a given tree to its Sum Tree | C # program to convert a tree into its sum tree ; A binary tree node ; Convert a given tree to a tree where every node contains sum of values of nodes in left and right subtrees in the original tree ; Base case ; Store the old value ; Recursively call for left and right subtrees and store the sum as new value of this node ; Return the sum of values of nodes in left and right subtrees old_value of this node ; A utility function to print inorder traversal of a Binary Tree ; Driver Code ; Constructing tree given in the above figure ; Print inorder traversal of the converted tree to test result of toSumTree ( )"
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 int toSumTree ( Node node ) { if ( node == null ) { return 0 ; } int old_val = node . data ; node . data = toSumTree ( node . left ) + toSumTree ( node . right ) ; return node . data + old_val ; } public virtual void printInorder ( Node node ) { if ( node == null ) { return ; } printInorder ( node . left ) ; Console . Write ( node . data + " ▁ " ) ; printInorder ( node . right ) ; } public static void Main ( string [ ] args ) { GFG tree = new GFG ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( - 2 ) ; tree . root . right = new Node ( 6 ) ; tree . root . left . left = new Node ( 8 ) ; tree . root . left . right = new Node ( - 4 ) ; tree . root . right . left = new Node ( 7 ) ; tree . root . right . right = new Node ( 5 ) ; tree . toSumTree ( tree . root ) ; Console . WriteLine ( " Inorder ▁ Traversal ▁ of ▁ " + " the ▁ resultant ▁ tree ▁ is : " ) ; tree . printInorder ( tree . root ) ; } }


"Convert a normal BST to Balanced BST | C # program to convert a left unbalanced BST to a balanced BST ; A binary tree node has data , pointer to left child and a pointer to right child ; This function traverse the skewed binary tree and stores its nodes pointers in vector nodes [ ] ; Base case ; Store nodes in Inorder ( which is sorted order for BST ) ; Recursive function to construct binary tree ; base case ; Get the middle element and make it root ; Using index in Inorder traversal , construct left and right subtress ; This functions converts an unbalanced BST to a balanced BST ; Store nodes of given BST in sorted order ; Constucts BST from nodes [ ] ; Function to do preorder traversal of tree ; Driver program to test the above functions ; Constructed skewed binary tree is 10 / 8 / 7 / 6 / 5"
Solution in C#:

using System ; using System . Collections . Generic ; public class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } public class BinaryTree { public Node root ; public virtual void storeBSTNodes ( Node root , List < Node > nodes ) { if ( root == null ) { return ; } storeBSTNodes ( root . left , nodes ) ; nodes . Add ( root ) ; storeBSTNodes ( root . right , nodes ) ; } public virtual Node buildTreeUtil ( List < Node > nodes , int start , int end ) { if ( start > end ) { return null ; } int mid = ( start + end ) / 2 ; Node node = nodes [ mid ] ; node . left = buildTreeUtil ( nodes , start , mid - 1 ) ; node . right = buildTreeUtil ( nodes , mid + 1 , end ) ; return node ; } public virtual Node buildTree ( Node root ) { List < Node > nodes = new List < Node > ( ) ; storeBSTNodes ( root , nodes ) ; int n = nodes . Count ; return buildTreeUtil ( nodes , 0 , n - 1 ) ; } public virtual void preOrder ( Node node ) { if ( node == null ) { return ; } Console . Write ( node . data + " ▁ " ) ; preOrder ( node . left ) ; preOrder ( node . right ) ; } public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( 8 ) ; tree . root . left . left = new Node ( 7 ) ; tree . root . left . left . left = new Node ( 6 ) ; tree . root . left . left . left . left = new Node ( 5 ) ; tree . root = tree . buildTree ( tree . root ) ; Console . WriteLine ( " Preorder ▁ traversal ▁ of ▁ balanced ▁ BST ▁ is ▁ : " ) ; tree . preOrder ( tree . root ) ; } }


"Convert a number from base A to base B | C # program for the above approach ; Function to return ASCII value of a character ; Function to convert a number from given basse to decimal number ; Stores the length of the string ; Initialize power of basse ; Initialize result ; Decimal equivalent is str [ len - 1 ] * 1 + str [ len - 2 ] * basse + str [ len - 3 ] * ( basse ^ 2 ) + ... ; A digit in input number must be less than number 's basse ; Update num ; Update power ; Function to return equivalent character of a given value ; Function to convert a given decimal number to a given basse ; Store the result ; Repeatedly divide inputNum by basse and take remainder ; Update res ; Update inputNum ; Reverse the result ; Function to convert a given number from a basse to another basse ; Convert the number from basse A to decimal ; Convert the number from decimal to basse B ; Print the result ; Driver Code ; Given input ; Function Call"
Solution in C#:

using System ; public class GFG { static int val ( char c ) { if ( c >= '0' && c <= '9' ) return ( int ) c - '0' ; else return ( int ) c - ' A ' + 10 ; } static int toDeci ( string str , int basse ) { int len = str . Length ; int power = 1 ; int num = 0 ; for ( int i = len - 1 ; i >= 0 ; i -- ) { if ( val ( str [ i ] ) >= basse ) { Console . Write ( " Invalid ▁ Number " ) ; return - 1 ; } num += val ( str [ i ] ) * power ; power = power * basse ; } return num ; } static char reVal ( int num ) { if ( num >= 0 && num <= 9 ) return ( char ) ( num + '0' ) ; else return ( char ) ( num - 10 + ' A ' ) ; } static string fromDeci ( int basse , int inputNum ) { string res = " " ; while ( inputNum > 0 ) { res += reVal ( inputNum % basse ) ; inputNum /= basse ; } res = reverse ( res ) ; return res ; } static void convertbasse ( string s , int a , int b ) { int num = toDeci ( s , a ) ; string ans = fromDeci ( b , num ) ; Console . Write ( ans ) ; } static string reverse ( string input ) { char [ ] a = input . ToCharArray ( ) ; int l , r = a . Length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return new string ( a ) ; } static public void Main ( ) { string s = "10B " ; int a = 16 , b = 10 ; convertbasse ( s , a , b ) ; } }


"Convert a number to another by dividing by its factor or removing first occurrence of a digit from an array | C # program for the above approach ; Function to check if a digit x is present in the number N or not ; Convert N to string ; Traverse the string num ; Return first occurrence of the digit x ; Function to remove the character at a given index from the number ; Convert N to string ; Store the resultant string ; Traverse the string num ; If the number becomes empty after deletion , then return - 1 ; Return the number ; Function to check if A can be reduced to B by performing the operations any number of times ; Create a queue ; Push A into the queue ; Hashmap to check if the element is present in the Queue or not ; Set A as visited ; Iterate while the queue is not empty ; Store the front value of the queue and pop it from it ; If top is equal to B , then return true ; Traverse the array , D [ ] ; Divide top by D [ i ] if it is possible and push the result in q ; If D [ i ] is present at the top ; Remove the first occurrence of D [ i ] from the top and store the new number ; Push newElement into the queue q ; Return false if A can not be reduced to B ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int isPresent ( int n , int x ) { string num = n . ToString ( ) ; for ( int i = 0 ; i < num . Length ; i ++ ) { if ( ( ( int ) num [ i ] - 97 ) == x ) return i ; } return - 1 ; } static int removeDigit ( int n , int index ) { string num = n . ToString ( ) ; string ans = " " ; for ( int i = 0 ; i < num . Length ; i ++ ) { if ( i != index ) ans += num [ i ] ; } if ( ans == " " || ( ans . Length == 1 && ans [ 0 ] == '0' ) ) return - 1 ; int x = Int32 . Parse ( ans ) ; ; return x ; } static bool reduceNtoX ( int a , int b , int [ ] d , int n ) { Queue < int > q = new Queue < int > ( ) ; q . Enqueue ( a ) ; Dictionary < int , bool > visited = new Dictionary < int , bool > ( ) ; visited [ a ] = true ; while ( q . Count > 0 ) { int top = q . Peek ( ) ; q . Dequeue ( ) ; if ( top < 0 ) continue ; if ( top == b ) return true ; for ( int i = 0 ; i < n ; i ++ ) { if ( d [ i ] != 0 && top % d [ i ] == 0 && visited . ContainsKey ( top / d [ i ] ) && visited [ top / d [ i ] ] == false ) { q . Enqueue ( top / d [ i ] ) ; visited [ top / d [ i ] ] = true ; } int index = isPresent ( top , d [ i ] ) ; if ( index != - 1 ) { int newElement = removeDigit ( top , index ) ; if ( newElement != - 1 && ( visited . ContainsKey ( newElement ) && visited [ newElement ] == false ) ) { q . Enqueue ( newElement ) ; visited [ newElement ] = true ; } } } } return true ; } public static void Main ( ) { int A = 5643 , B = 81 ; int [ ] D = { 3 , 8 , 1 } ; int N = D . Length ; if ( reduceNtoX ( A , B , D , N ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Convert a sentence into its equivalent mobile numeric keypad sequence | C # implementation to convert a sentence into its equivalent mobile numeric keypad sequence ; Function which computes the sequence ; length of input string ; Checking for space ; Calculating index for each character ; Output sequence ; Driver Function ; storing the sequence in array"
Solution in C#:

using System ; class GFG { static String printSequence ( string [ ] arr , string input ) { string output = " " ; int n = input . Length ; for ( int i = 0 ; i < n ; i ++ ) { if ( input [ i ] == ' ▁ ' ) output = output + "0" ; else { int position = input [ i ] - ' A ' ; output = output + arr [ position ] ; } } return output ; } public static void Main ( ) { string [ ] str = { "2" , "22" , "222" , "3" , "33" , "333" , "4" , "44" , "444" , "5" , "55" , "555" , "6" , "66" , "666" , "7" , "77" , "777" , "7777" , "8" , "88" , "888" , "9" , "99" , "999" , "9999" } ; string input = " GEEKSFORGEEKS " ; Console . WriteLine ( printSequence ( str , input ) ) ; } }


"Convert a tree to forest of even nodes | C # program to find maximum number to be removed to convert a tree into forest containing trees of even number of nodes ; Return the number of nodes of subtree having node as a root . ; Mark node as visited . ; Traverse the adjacency list to find non - visited node . ; Finding number of nodes of the subtree of a subtree . ; If nodes are even , increment number of edges to removed . Else leave the node as child of subtree . ; Return the maximum number of edge to remove to make forest . ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int N = 12 , ans ; static List < List < int > > tree = new List < List < int > > ( ) ; static int dfs ( int [ ] visit , int node ) { int num = 0 , temp = 0 ; visit [ node ] = 1 ; for ( int i = 0 ; i < tree [ node ] . Count ; i ++ ) { if ( visit [ tree [ node ] [ i ] ] == 0 ) { temp = dfs ( visit , tree [ node ] [ i ] ) ; if ( temp % 2 != 0 ) num += temp ; else ans ++ ; } } return num + 1 ; } static int minEdge ( int n ) { int [ ] visit = new int [ n + 2 ] ; ans = 0 ; dfs ( visit , 1 ) ; return ans ; } public static void Main ( String [ ] args ) { int n = 10 ; for ( int i = 0 ; i < n + 2 ; i ++ ) tree . Add ( new List < int > ( ) ) ; tree [ 1 ] . Add ( 3 ) ; tree [ 3 ] . Add ( 1 ) ; tree [ 1 ] . Add ( 6 ) ; tree [ 6 ] . Add ( 1 ) ; tree [ 1 ] . Add ( 2 ) ; tree [ 2 ] . Add ( 1 ) ; tree [ 3 ] . Add ( 4 ) ; tree [ 4 ] . Add ( 3 ) ; tree [ 6 ] . Add ( 8 ) ; tree [ 8 ] . Add ( 6 ) ; tree [ 2 ] . Add ( 7 ) ; tree [ 7 ] . Add ( 2 ) ; tree [ 2 ] . Add ( 5 ) ; tree [ 5 ] . Add ( 2 ) ; tree [ 4 ] . Add ( 9 ) ; tree [ 9 ] . Add ( 4 ) ; tree [ 4 ] . Add ( 10 ) ; tree [ 10 ] . Add ( 4 ) ; Console . WriteLine ( minEdge ( n ) ) ; } }


"Convert all substrings of length ' k ' from base ' b ' to decimal | Simple C # program to convert all substrings from decimal to given base . ; Saving substring in sub ; Evaluating decimal for current substring and printing it . ; Driver code"
Solution in C#:

using System ; class GFG { static void substringConversions ( String str , int k , int b ) { for ( int i = 0 ; i + k <= str . Length ; i ++ ) { String sub = str . Substring ( i , k ) ; int sum = 0 , counter = 0 ; for ( int j = sub . Length - 1 ; j >= 0 ; j -- ) { sum = ( int ) ( sum + ( ( sub [ j ] - '0' ) * Math . Pow ( b , counter ) ) ) ; counter ++ ; } Console . Write ( sum + " ▁ " ) ; } } public static void Main ( String [ ] args ) { String str = "12212" ; int b = 3 , k = 3 ; substringConversions ( str , b , k ) ; } }


"Convert an Array to a Circular Doubly Linked List | C # program to convert array to circular doubly linked list ; Doubly linked list node ; Utility function to create a node in memory ; Function to display the list ; Function to convert array into list ; Declare newNode and temporary pointer ; Iterate the loop until array length ; Create new node ; Assign the array data ; If it is first element Put that node prev and next as start as it is circular ; Find the last node ; Add the last node to make them in circular fashion ; Driver Code ; Array to be converted ; Start Pointer ; Create the List ; Display the list"
Solution in C#:

using System ; class GFG { public class node { public int data ; public node next ; public node prev ; } ; static node getNode ( ) { return new node ( ) ; } static int displayList ( node temp ) { node t = temp ; if ( temp == null ) return 0 ; else { Console . Write ( " The ▁ list ▁ is : ▁ " ) ; while ( temp . next != t ) { Console . Write ( temp . data + " ▁ " ) ; temp = temp . next ; } Console . Write ( temp . data ) ; return 1 ; } } static node createList ( int [ ] arr , int n , node start ) { node newNode , temp ; int i ; for ( i = 0 ; i < n ; i ++ ) { newNode = getNode ( ) ; newNode . data = arr [ i ] ; if ( i == 0 ) { start = newNode ; newNode . prev = start ; newNode . next = start ; } else { temp = ( start ) . prev ; temp . next = newNode ; newNode . next = start ; newNode . prev = temp ; temp = start ; temp . prev = newNode ; } } return start ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . Length ; node start = null ; start = createList ( arr , n , start ) ; displayList ( start ) ; } }


"Convert an arbitrary Binary Tree to a tree that holds Children Sum Property | C # program to convert an arbitrary binary tree to a tree that holds children sum property ; A binary tree node ; This function changes a tree to hold children sum property ; If tree is empty or it 's a leaf   node then return true  ; convert left and right subtrees ; If left child is not present then 0 is used as data of left child ; If right child is not present then 0 is used as data of right child ; get the diff of node 's data   and children sum  ; If node ' s ▁ children ▁ sum ▁ is ▁ greater ▁ ▁ ▁ than ▁ the ▁ node ' s data ; THIS IS TRICKY -- > If node 's data is   greater than children sum, then increment  subtree by diff  ; - diff is used to make diff positive ; This function is used to increment subtree by diff ; IF left child is not NULL then increment it ; Recursively call to fix the descendants of node -> left ; Else increment right child ; Recursively call to fix the descendants of node -> right ; Given a binary tree , printInorder ( ) prints out its inorder traversal ; first recur on left child ; then print the data of node ; now recur on right child ; 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 convertTree ( Node node ) { int left_data = 0 , right_data = 0 , diff ; if ( node == null || ( node . left == null && node . right == null ) ) { return ; } else { convertTree ( node . left ) ; convertTree ( node . right ) ; if ( node . left != null ) { left_data = node . left . data ; } if ( node . right != null ) { right_data = node . right . data ; } diff = left_data + right_data - node . data ; if ( diff > 0 ) { node . data = node . data + diff ; } if ( diff < 0 ) { increment ( node , - diff ) ; } } } public virtual void increment ( Node node , int diff ) { if ( node . left != null ) { node . left . data = node . left . data + diff ; increment ( node . left , diff ) ; } else if ( node . right != null ) { node . right . data = node . right . data + diff ; increment ( node . right , diff ) ; } } public virtual void printInorder ( Node node ) { if ( node == null ) { return ; } printInorder ( node . left ) ; Console . Write ( node . data + " ▁ " ) ; printInorder ( node . right ) ; } public static void Main ( string [ ] args ) { GFG tree = new GFG ( ) ; tree . root = new Node ( 50 ) ; tree . root . left = new Node ( 7 ) ; tree . root . right = new Node ( 2 ) ; tree . root . left . left = new Node ( 3 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . left = new Node ( 1 ) ; tree . root . right . right = new Node ( 30 ) ; Console . WriteLine ( " Inorder ▁ traversal ▁ " + " before ▁ conversion ▁ is ▁ : " ) ; tree . printInorder ( tree . root ) ; tree . convertTree ( tree . root ) ; Console . WriteLine ( " " ) ; Console . WriteLine ( " Inorder ▁ traversal ▁ " + " after ▁ conversion ▁ is ▁ : " ) ; tree . printInorder ( tree . root ) ; } }


"Convert an array into another by repeatedly removing the last element and placing it at any arbitrary index | C # program for the above approach ; Function to count the minimum number of operations required to convert the array A [ ] into array B [ ] ; Stores the index in the first permutation A [ ] which is same as the subsequence in B [ ] ; Find the first i elements in A [ ] which is a subsequence in B [ ] ; If element A [ i ] is same as B [ j ] ; Return the count of operations required ; Driver Code"
Solution in C#:

using System ; class GFG { static int minCount ( int [ ] A , int [ ] B , int N ) { int i = 0 ; for ( int j = 0 ; j < N ; j ++ ) { if ( A [ i ] == B [ j ] ) { i ++ ; } } return N - i ; } public static void Main ( string [ ] args ) { int [ ] A = { 1 , 2 , 3 , 4 , 5 } ; int [ ] B = { 1 , 5 , 2 , 3 , 4 } ; int N = A . Length ; Console . WriteLine ( minCount ( A , B , N ) ) ; } }


"Convert an array to reduced form | Set 1 ( Simple and Hashing ) | C # Program to convert an Array to reduced form ; Create a temp array and copy contents of [ ] arr to temp ; Sort temp array ; Create a hash table . ; One by one insert elements of sorted [ ] temp and assign them values from 0 to n - 1 ; Convert array by taking positions from umap ; Driver code"
Solution in C#:

 using System ; using System . Collections . Generic ; using System . Linq ; class GFG { public static void convert ( int [ ] arr , int n ) { int [ ] temp = new int [ arr . Length ] ; Array . Copy ( arr , 0 , temp , 0 , arr . Length ) ; Array . Sort ( temp ) ; Dictionary < int , int > umap = new Dictionary < int , int > ( ) ; int val = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( umap . ContainsKey ( temp [ i ] ) ) umap [ temp [ i ] ] = val ++ ; else umap . Add ( temp [ i ] , val ++ ) ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = umap [ arr [ i ] ] ; } public static void printArr ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 10 , 20 , 15 , 12 , 11 , 50 } ; int n = arr . Length ; Console . WriteLine ( " Given ▁ Array ▁ is ▁ " ) ; printArr ( arr , n ) ; convert ( arr , n ) ; Console . WriteLine ( "

 Converted ▁ Array ▁ is ▁ " ) ; printArr ( arr , n ) ; } }


"Convert an array to reduced form | Set 3 ( Binary Search ) | C # program for the above approach ; Function to find the reduced form of the given array arr [ ] ; Stores the sorted form of the the given array arr [ ] ; Sort the array brr [ ] ; Traverse the given array arr [ ] ; Perform the Binary Search ; Calculate the value of mid ; Print the current index and break ; Update the value of l ; Update the value of r ; Driver Code"
Solution in C#:

using System ; class GFG { static void convert ( int [ ] arr , int n ) { int [ ] brr = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) brr [ i ] = arr [ i ] ; Array . Sort ( brr ) ; for ( int i = 0 ; i < n ; i ++ ) { int l = 0 , r = n - 1 , mid ; while ( l <= r ) { mid = ( l + r ) / 2 ; if ( brr [ mid ] == arr [ i ] ) { Console . Write ( mid + " ▁ " ) ; break ; } else if ( brr [ mid ] < arr [ i ] ) { l = mid + 1 ; } else { r = mid - 1 ; } } } } public static void Main ( string [ ] args ) { int [ ] arr = { 10 , 20 , 15 , 12 , 11 , 50 } ; int N = arr . Length ; convert ( arr , N ) ; } }


"Convert an unbalanced bracket sequence to a balanced sequence | C # implementation of the approach ; Function to return balancedBrackets string ; Initializing dep to 0 ; Stores maximum negative depth ; if dep is less than minDep ; if minDep is less than 0 then there is need to add ' ( ' at the front ; Reinitializing to check the updated string ; if dep is not 0 then there is need to add ' ) ' at the back ; Driver code"
Solution in C#:

using System ; class GFG { static string balancedBrackets ( string str ) { int dep = 0 ; int minDep = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( str [ i ] == ' ( ' ) dep ++ ; else dep -- ; if ( minDep > dep ) minDep = dep ; } if ( minDep < 0 ) { for ( int i = 0 ; i < Math . Abs ( minDep ) ; i ++ ) str = ' ( ' + str ; } dep = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( str [ i ] == ' ( ' ) dep ++ ; else dep -- ; } if ( dep != 0 ) { for ( int i = 0 ; i < dep ; i ++ ) str = str + ' ) ' ; } return str ; } public static void Main ( ) { String str = " ) ) ) ( ) " ; Console . WriteLine ( balancedBrackets ( str ) ) ; } }


"Convert characters of a string to opposite case | C # program to toggle all characters ; Function to toggle characters ; Driver code"
Solution in C#:

 using System ; class GFG { static char [ ] S = " GeKf @ rGeek $ " . ToCharArray ( ) ; static void toggleChars ( ) { for ( int i = 0 ; i < S . Length ; i ++ ) { if ( char . IsLetter ( S [ i ] ) ) { S [ i ] = ( char ) ( ( int ) ( S [ i ] ) ^ ( 1 << 5 ) ) ; } } } public static void Main ( String [ ] args ) { toggleChars ( ) ; Console . Write ( " String ▁ after ▁ toggle ▁ " + "
 " ) ; Console . Write ( String . Join ( " " , S ) ) ; } }


"Convert decimal fraction to binary number | C # program to convert fractional decimal to binary number ; Function to convert decimal to binary upto k - precision after decimal point ; Fetch the integral part of decimal number ; Fetch the fractional part decimal number ; Conversion of integral part to binary equivalent ; Append 0 in binary ; Reverse string to get original binary equivalent ; Append point before conversion of fractional part ; Conversion of fractional part to binary equivalent ; Find next bit in fraction ; Swap values of left and right ; Driver code"
Solution in C#:

using System ; class GFG { static String decimalToBinary ( double num , int k_prec ) { String binary = " " ; int Integral = ( int ) num ; double fractional = num - Integral ; while ( Integral > 0 ) { int rem = Integral % 2 ; binary += ( ( char ) ( rem + '0' ) ) ; Integral /= 2 ; } binary = reverse ( binary ) ; binary += ( ' . ' ) ; while ( k_prec -- > 0 ) { fractional *= 2 ; int fract_bit = ( int ) fractional ; if ( fract_bit == 1 ) { fractional -= fract_bit ; binary += ( char ) ( 1 + '0' ) ; } else { binary += ( char ) ( 0 + '0' ) ; } } return binary ; } static String reverse ( String input ) { char [ ] temparray = input . ToCharArray ( ) ; int left , right = 0 ; right = temparray . Length - 1 ; for ( left = 0 ; left < right ; left ++ , right -- ) { char temp = temparray [ left ] ; temparray [ left ] = temparray [ right ] ; temparray [ right ] = temp ; } return String . Join ( " " , temparray ) ; } public static void Main ( String [ ] args ) { double n = 4.47 ; int k = 3 ; Console . WriteLine ( decimalToBinary ( n , k ) ) ; n = 6.986 ; k = 5 ; Console . WriteLine ( decimalToBinary ( n , k ) ) ; } }


"Convert from any base to decimal and vice versa | C # program to convert a number from any base to decimal ; To return value of a char . For example , 2 is returned for '2' . 10 is returned for ' A ' , 11 for ' B ' ; Function to convert a number from given base ' b ' to decimal ; Initialize ; power of base Initialize result ; Decimal equivalent is str [ len - 1 ] * 1 + str [ len - 2 ] * base + str [ len - 3 ] * ( base ^ 2 ) + ... ; A digit in input number must be less than number 's base ; Driver code"
Solution in C#:

using System ; class GFG { static int val ( char c ) { if ( c >= '0' && c <= '9' ) return ( int ) c - '0' ; else return ( int ) c - ' A ' + 10 ; } static int toDeci ( string str , int b_ase ) { int len = str . Length ; int power = 1 ; int num = 0 ; int i ; for ( i = len - 1 ; i >= 0 ; i -- ) { if ( val ( str [ i ] ) >= b_ase ) { Console . WriteLine ( " Invalid ▁ Number " ) ; return - 1 ; } num += val ( str [ i ] ) * power ; power = power * b_ase ; } return num ; } public static void Main ( ) { string str = "11A " ; int b_ase = 16 ; Console . WriteLine ( " Decimal ▁ equivalent ▁ of ▁ " + str + " ▁ in ▁ base ▁ " + b_ase + " ▁ is ▁ " + toDeci ( str , b_ase ) ) ; } }


"Convert given Binary string S to all 1 s by changing all 0 s to 1 s in range [ i + 1 , i + K ] if S [ i ] is 1 | C # program for the above approach ; Function to check whether all 0 s in the string can be changed into 1 s ; Store the count of 0 s converted for the last occurrence of 1 ; Declere a stack ; Traverse the string , S ; If stack is empty ; There is no 1 that can change this 0 to 1 ; Push 1 into the stack ; The last 1 has reached its limit ; New 1 has been found which can now change at most K 0 s ; If flag is 1 , print " YES " else print " NO " ; Driver code ; Given Input ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void changeCharacters ( string S , int N , int K ) { int flag = 1 ; int count = 0 ; Stack < char > st = new Stack < char > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( st . Count == 0 ) { if ( S [ i ] == '0' ) { flag = 0 ; break ; } count = 0 ; st . Push ( S [ i ] ) ; } else { if ( S [ i ] == '0' ) { count ++ ; if ( count == K ) { st . Pop ( ) ; count = 0 ; } } else { count = 0 ; } } } if ( flag == 1 ) Console . Write ( " YES " ) ; else Console . Write ( " NO " ) ; } public static void Main ( ) { string S = "100100" ; int N = S . Length ; int K = 2 ; changeCharacters ( S , N , K ) ; } }


"Convert given Float value to equivalent Fraction | C # program for the above approach ; Recursive function to return gcd of a and b ; Function to convert the floating values into fraction ; Initialize variables ; Traverse the floating String ; Check if decimal part exist ; Check if recurrence sequence exist ; Retrieve decimal part and recurrence resquence ; Traverse the String ; Convert String to integer ; If no recurrence sequence exist ; Initialize numerator & denominator ; No reccuring term ; Print the result ; If reccuring term exist ; Convert reccuring term to integer ; reccu . Count is num of digit in reccur term ; eq 2 - eq 1 ; Print the result ; Driver Code ; Given String str ; Function Call"
Solution in C#:

using System ; class GFG { static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } static void findFraction ( String s ) { String be_deci = " " , af_deci = " " , reccu = " " ; bool x = true , y = false , z = false ; for ( int i = 0 ; i < s . Length ; ++ i ) { if ( s [ i ] == ' . ' ) { x = false ; y = true ; continue ; } if ( s [ i ] == ' ( ' ) { z = true ; y = false ; continue ; } if ( x ) be_deci += s [ i ] ; if ( y ) af_deci += s [ i ] ; if ( z ) { for ( ; i < s . Length && s [ i ] != ' ) ' ; ++ i ) reccu += s [ i ] ; break ; } } int num_be_deci = Int32 . Parse ( be_deci ) ; int num_af_deci = 0 ; if ( af_deci . Length != 0 ) num_af_deci = Int32 . Parse ( af_deci ) ; int numr = num_be_deci * ( int ) Math . Pow ( 10 , af_deci . Length ) + num_af_deci ; int deno = ( int ) Math . Pow ( 10 , af_deci . Length ) ; if ( reccu . Length == 0 ) { int gd = __gcd ( numr , deno ) ; Console . Write ( numr / gd + " ▁ / ▁ " + deno / gd ) ; } else { int reccu_num = Int32 . Parse ( reccu ) ; int numr1 = numr * ( int ) Math . Pow ( 10 , reccu . Length ) + reccu_num ; int deno1 = deno * ( int ) Math . Pow ( 10 , reccu . Length ) ; int res_numr = numr1 - numr , res_deno = deno1 - deno ; int gd = __gcd ( res_numr , res_deno ) ; Console . Write ( res_numr / gd + " ▁ / ▁ " + res_deno / gd ) ; } } public static void Main ( String [ ] args ) { String str = "23.98(231 ) " ; findFraction ( str ) ; } }


"Convert given Matrix into sorted Spiral Matrix | C # program to Convert given Matrix into sorted Spiral Matrix ; Function to convert the array to Spiral ; For Array pointer ; k - starting row index m - ending row index l - starting column index n - ending column index ; Print the first row from the remaining rows ; Print the last column from the remaining columns ; Print the last row from the remaining rows ; Print the first column from the remaining columns ; Function to convert 2D array to 1D array ; Store value 2D Matrix To 1D array ; Function to print the Matrix ; Print Spiral Matrix ; Function to sort the array ; Sort array Using InBuilt Function ; Function to Convert given Matrix into sorted Spiral Matrix ; Driver code"
Solution in C#:

using System ; class GFG { static int MAX = 1000 ; static void ToSpiral ( int m , int n , int [ ] Sorted , int [ , ] a ) { int index = 0 ; int k = 0 , l = 0 ; while ( k < m && l < n ) { for ( int i = l ; i < n ; ++ i ) { a [ k , i ] = Sorted [ index ] ; index ++ ; } k ++ ; for ( int i = k ; i < m ; ++ i ) { a [ i , n - 1 ] = Sorted [ index ] ; index ++ ; } n -- ; if ( k < m ) { for ( int i = n - 1 ; i >= l ; -- i ) { a [ m - 1 , i ] = Sorted [ index ] ; index ++ ; } m -- ; } if ( l < n ) { for ( int i = m - 1 ; i >= k ; -- i ) { a [ i , l ] = Sorted [ index ] ; index ++ ; } l ++ ; } } } public static int [ ] convert2Dto1D ( int [ , ] y , int m , int n ) { int index = 0 ; int [ ] x = new int [ m * n ] ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { x [ index ] = y [ i , j ] ; index ++ ; } } return x ; } public static void PrintMatrix ( int [ , ] a , int m , int n ) { for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { Console . Write ( a [ i , j ] + " ▁ " ) ; } Console . WriteLine ( ) ; } } public static int [ ] SortArray ( int [ ] x ) { Array . Sort ( x ) ; return x ; } public static void convertMatrixToSortedSpiral ( int [ , ] y , int m , int n ) { int [ , ] a = new int [ MAX , MAX ] ; int [ ] x = new int [ m * n ] ; x = convert2Dto1D ( y , m , n ) ; x = SortArray ( x ) ; ToSpiral ( m , n , x , a ) ; PrintMatrix ( a , m , n ) ; } public static void Main ( String [ ] args ) { int m = 4 , n = 3 ; int [ , ] y = { { 2 , 5 , 12 } , { 22 , 45 , 55 } , { 1 , 6 , 8 } , { 13 , 56 , 10 } } ; convertMatrixToSortedSpiral ( y , m , n ) ; } }


"Convert given array to Arithmetic Progression by adding an element | C # implementation of the approach ; Function to return the number to be added ; If difference of the current consecutive elements is different from the common difference ; If number has already been chosen then it 's not possible  to add another number ; If the current different is twice the common difference then a number can be added midway from current and previous element ; Number has been chosen ; It 's not possible to maintain  the common difference ; Return last element + common difference if no element is chosen and the array is already in AP ; Else return the chosen number ; Driver code"
Solution in C#:

using System ; public class GFG { static int getNumToAdd ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int d = arr [ 1 ] - arr [ 0 ] ; int numToAdd = - 1 ; bool numAdded = false ; for ( int i = 2 ; i < n ; i ++ ) { int diff = arr [ i ] - arr [ i - 1 ] ; if ( diff != d ) { if ( numAdded ) return - 1 ; if ( diff == 2 * d ) { numToAdd = arr [ i ] - d ; numAdded = true ; } else return - 1 ; } } if ( numToAdd == - 1 ) return ( arr [ n - 1 ] + d ) ; return numToAdd ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 5 , 7 , 11 , 13 , 15 } ; int n = arr . Length ; Console . WriteLine ( getNumToAdd ( arr , n ) ) ; } }


"Convert given string so that it holds only distinct characters | C # implementation of the approach ; Function to return the index of the character that has 0 occurrence starting from index i ; If current character has 0 occurrence ; If no character has 0 occurrence ; Function to return the modified string which consists of distinct characters ; String cannot consist of all distinct characters ; Count the occurrences for each of the character ; Index for the first character that hasn 't appeared in the string ; If current character appeared more than once then it has to be replaced with some character that hasn 't occurred yet ; Decrement current character 's occurrence by 1 ; Replace the character ; Update the new character ' s ▁ occurrence ▁ ▁ This ▁ step ▁ can ▁ also ▁ be ▁ skipped ▁ ▁ as ▁ we ' ll never encounter this character in the string because it has been added just now ; Find the next character that hasn 't occurred yet ; Return the modified string ; Driver code"
Solution in C#:

using System ; class GFG { static int nextZero ( int i , int [ ] occurrences ) { while ( i < occurrences . Length ) { if ( occurrences [ i ] == 0 ) return i ; i ++ ; } return - 1 ; } static string getModifiedString ( string str ) { int n = str . Length ; if ( n > 26 ) return " - 1" ; char [ ] ch = str . ToCharArray ( ) ; int [ ] occurrences = new int [ 26 ] ; int i ; for ( i = 0 ; i < n ; i ++ ) occurrences [ ch [ i ] - ' a ' ] ++ ; int index = nextZero ( 0 , occurrences ) ; for ( i = 0 ; i < n ; i ++ ) { if ( occurrences [ ch [ i ] - ' a ' ] > 1 ) { occurrences [ ch [ i ] - ' a ' ] -- ; ch [ i ] = ( char ) ( ' a ' + index ) ; occurrences [ index ] = 1 ; index = nextZero ( index + 1 , occurrences ) ; } } string s = new string ( ch ) ; return s ; } public static void Main ( ) { string str = " geeksforgeeks " ; Console . WriteLine ( getModifiedString ( str ) ) ; } }


"Convert given string to a valid mobile number | C # program for the above approach ; Function to print the valid and formatted phone number ; Length of given String ; Store digits in temp ; Iterate given M ; If any digit , append it to temp ; Find new length of String ; If length is not equal to 10 ; Store final result ; Make groups of 3 digits and enclose them within ( ) and separate them with " - " 0 to 2 index 1 st group ; 3 to 5 index 2 nd group ; 6 to 8 index 3 rd group ; 9 to 9 index last group ; Print final result ; Driver Code ; Given String ; Function Call"
Solution in C#:

 using System ; class GFG { static void Validate ( string M ) { int len = M . Length ; string temp = " " ; for ( int i = 0 ; i < len ; i ++ ) { if ( Char . IsDigit ( M [ i ] ) ) temp += M [ i ] ; } int nwlen = temp . Length ; if ( nwlen != 10 ) { Console . Write ( " Invalid
 " ) ; return ; } string res = " " ; string x = temp . Substring ( 0 , 3 ) ; res += " ( " + x + " ) - " ; x = temp . Substring ( 3 , 3 ) ; res += " ( " + x + " ) - " ; x = temp . Substring ( 6 , 3 ) ; res += " ( " + x + " ) - " ; x = temp . Substring ( 9 , 1 ) ; res += " ( " + x + " ) " ; Console . WriteLine ( res ) ; } public static void Main ( string [ ] args ) { string M = "91 ▁ 234rt5%34*0 ▁ 3" ; Validate ( M ) ; } }