"Compute sum of digits in all numbers from 1 to n | C # program to compute sum of digits in numbers from 1 to n ; Function to computer sum of digits in numbers from 1 to n . Comments use example of 328 to explain the code ; base case : if n < 10 return sum of first n natural numbers ; d = number of digits minus one in n . For 328 , d is 2 ; computing sum of digits from 1 to 10 ^ d - 1 , d = 1 a [ 0 ] = 0 ; d = 2 a [ 1 ] = sum of digit from 1 to 9 = 45 d = 3 a [ 2 ] = sum of digit from 1 to 99 = a [ 1 ] * 10 + 45 * 10 ^ 1 = 900 d = 4 a [ 3 ] = sum of digit from 1 to 999 = a [ 2 ] * 10 + 45 * 10 ^ 2 = 13500 ; computing 10 ^ d ; Most significant digit ( msd ) of n , For 328 , msd is 3 which can be obtained using 328 / 100 ; EXPLANATION FOR FIRST and SECOND TERMS IN BELOW LINE OF CODE First two terms compute sum of digits from 1 to 299 ( sum of digits in range 1 - 99 stored in a [ d ] ) + ( sum of digits in range 100 - 199 , can be calculated as 1 * 100 + a [ d ] ( sum of digits in range 200 - 299 , can be calculated as 2 * 100 + a [ d ] The above sum can be written as 3 * a [ d ] + ( 1 + 2 ) * 100 EXPLANATION FOR THIRD AND FOURTH TERMS IN BELOW LINE OF CODE The last two terms compute sum of digits in number from 300 to 328. The third term adds 3 * 29 to sum as digit 3 occurs in all numbers from 300 to 328. The fourth term recursively calls for 28 ; Driver Program"
Solution in C#:

using System ; public class GFG { static int sumOfDigitsFrom1ToN ( int n ) { if ( n < 10 ) return ( n * ( n + 1 ) / 2 ) ; int d = ( int ) ( Math . Log ( n ) / Math . Log ( 10 ) ) ; int [ ] a = new int [ d + 1 ] ; a [ 0 ] = 0 ; a [ 1 ] = 45 ; for ( int i = 2 ; i <= d ; i ++ ) a [ i ] = a [ i - 1 ] * 10 + 45 * ( int ) ( Math . Ceiling ( Math . Pow ( 10 , i - 1 ) ) ) ; int p = ( int ) ( Math . Ceiling ( Math . Pow ( 10 , d ) ) ) ; int msd = n / p ; return ( msd * a [ d ] + ( msd * ( msd - 1 ) / 2 ) * p + msd * ( 1 + n % p ) + sumOfDigitsFrom1ToN ( n % p ) ) ; } public static void Main ( ) { int n = 328 ; Console . WriteLine ( " Sum ▁ of ▁ digits ▁ in ▁ numbers ▁ " + " from ▁ 1 ▁ to ▁ " + n + " ▁ is ▁ " + sumOfDigitsFrom1ToN ( n ) ) ; } }


"Compute sum of digits in all numbers from 1 to n | C # program to compute sum of digits in numbers from 1 to n ; Function to computer sum of digits in numbers from 1 to n ; Driver code"
Solution in C#:

using System ; class GFG { static int sumOfDigitsFrom1ToNUtil ( int n , int [ ] a ) { if ( n < 10 ) return ( n * ( n + 1 ) / 2 ) ; int d = ( int ) ( Math . Log10 ( n ) ) ; int p = ( int ) ( Math . Ceiling ( Math . Pow ( 10 , d ) ) ) ; int msd = n / p ; return ( msd * a [ d ] + ( msd * ( msd - 1 ) / 2 ) * p + msd * ( 1 + n % p ) + sumOfDigitsFrom1ToNUtil ( n % p , a ) ) ; } static int sumOfDigitsFrom1ToN ( int n ) { int d = ( int ) ( Math . Log10 ( n ) ) ; int [ ] a = new int [ d + 1 ] ; a [ 0 ] = 0 ; a [ 1 ] = 45 ; for ( int i = 2 ; i <= d ; i ++ ) a [ i ] = a [ i - 1 ] * 10 + 45 * ( int ) ( Math . Ceiling ( Math . Pow ( 10 , i - 1 ) ) ) ; return sumOfDigitsFrom1ToNUtil ( n , a ) ; } public static void Main ( String [ ] args ) { int n = 328 ; Console . WriteLine ( " Sum ▁ of ▁ digits ▁ in ▁ numbers ▁ " + " from ▁ 1 ▁ to ▁ " + n + " ▁ is ▁ " + sumOfDigitsFrom1ToN ( n ) ) ; } }


"Compute the maximum power with a given condition | C # program for Compute maximum power to which K can be raised so that given condition remains true ; Function to return the largest power ; If n is greater than given M ; If n == m ; Checking for the next power ; Driver Code"
Solution in C#:

using System ; class GFG { static int calculate ( int n , int k , int m , int power ) { if ( n > m ) { if ( power == 0 ) return 0 ; else return power - 1 ; } else if ( n == m ) return power ; else return calculate ( n * k , k , m , power + 1 ) ; } public static void Main ( String [ ] args ) { int N = 1 , K = 2 , M = 5 ; Console . WriteLine ( calculate ( N , K , M , 0 ) ) ; } }


"Compute the minimum or maximum of two integers without branching | C # implementation of above approach ; Function to find minimum of x and y ; Function to find maximum of x and y ; Driver code"
Solution in C#:

using System ; class GFG { static int CHAR_BIT = 8 ; static int min ( int x , int y ) { return y + ( ( x - y ) & ( ( x - y ) >> ( sizeof ( int ) * CHAR_BIT - 1 ) ) ) ; } static int max ( int x , int y ) { return x - ( ( x - y ) & ( ( x - y ) >> ( sizeof ( int ) * CHAR_BIT - 1 ) ) ) ; } static void Main ( ) { int x = 15 ; int y = 6 ; Console . WriteLine ( " Minimum ▁ of ▁ " + x + " ▁ and ▁ " + y + " ▁ is ▁ " + min ( x , y ) ) ; Console . WriteLine ( " Maximum ▁ of ▁ " + x + " ▁ and ▁ " + y + " ▁ is ▁ " + max ( x , y ) ) ; } }


"Compute the minimum or maximum of two integers without branching | C # program for the above approach ; absbit32 function ; max function ; min function ; Driver code"
Solution in C#:

using System ; class GFG { public static int absbit32 ( int x , int y ) { int sub = x - y ; int mask = ( sub >> 31 ) ; return ( sub ^ mask ) - mask ; } public static int max ( int x , int y ) { int abs = absbit32 ( x , y ) ; return ( x + y + abs ) / 2 ; } public static int min ( int x , int y ) { int abs = absbit32 ( x , y ) ; return ( x + y - abs ) / 2 ; } public static void Main ( String [ ] args ) { Console . WriteLine ( max ( 2 , 3 ) ) ; Console . WriteLine ( max ( 2 , - 3 ) ) ; Console . WriteLine ( max ( - 2 , - 3 ) ) ; Console . WriteLine ( min ( 2 , 3 ) ) ; Console . WriteLine ( min ( 2 , - 3 ) ) ; Console . WriteLine ( min ( - 2 , - 3 ) ) ; } }


"Compute the minimum or maximum of two integers without branching | C # program to Compute the minimum or maximum of two integers without branching ; Function to find minimum of x and y ; Function to find maximum of x and y ; Driver program to test above functions"
Solution in C#:

using System ; public class AWS { public static int min ( int x , int y ) { return y ^ ( ( x ^ y ) & - ( x << y ) ) ; } public static int max ( int x , int y ) { return x ^ ( ( x ^ y ) & - ( x << y ) ) ; } public static void Main ( string [ ] args ) { int x = 15 ; int y = 6 ; Console . Write ( " Minimum ▁ of ▁ " + x + " ▁ and ▁ " + y + " ▁ is ▁ " ) ; Console . WriteLine ( min ( x , y ) ) ; Console . Write ( " Maximum ▁ of ▁ " + x + " ▁ and ▁ " + y + " ▁ is ▁ " ) ; Console . WriteLine ( max ( x , y ) ) ; } }


"Concatenate suffixes of a String | C # Implementation of the above approach ; Function to print the expansion of the string ; Take sub - string from i to n - 1 ; Print the sub - string ; Driver code"
Solution in C#:

using System ; class GFG { static void printExpansion ( String str ) { String suff = " " ; for ( int i = str . Length - 1 ; i >= 0 ; i -- ) { suff = suff + str [ i ] ; Console . Write ( suff ) ; } } public static void Main ( String [ ] args ) { String str = " geeks " ; printExpansion ( str ) ; } }


"Concatenate suffixes of a String | C # implementation of the approach ; Function to print the expansion of the string ; Take sub - string from i to n - 1 ; Print the sub - string ; Driver code"
Solution in C#:

using System ; class GFG { static void printExpansion ( String str ) { for ( int i = ( int ) str . Length - 1 ; i >= 0 ; i -- ) { String subStr = str . Substring ( i ) ; Console . Write ( subStr ) ; } } static public void Main ( String [ ] args ) { String str = " geeks " ; printExpansion ( str ) ; } }


"Concatenate the Array of elements into a single element | C # program for the above approach ; Function to find the integer value obtained by joining array elements together ; Stores the resulting integer value ; Traverse the array arr [ ] ; Stores the count of digits of arr [ i ] ; Update ans ; Increment ans by arr [ i ] ; Return the ans ; Driver Code ; Input ; Function call"
Solution in C#:

using System ; class GFG { static int ConcatenateArr ( int [ ] arr , int N ) { int ans = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { int l = ( int ) Math . Floor ( Math . Log10 ( arr [ i ] ) + 1 ) ; ans = ans * ( int ) Math . Pow ( 10 , l ) ; ans += arr [ i ] ; } return ans ; } public static void Main ( ) { int [ ] arr = { 1 , 23 , 456 } ; int N = arr . Length ; Console . Write ( ConcatenateArr ( arr , N ) ) ; } }


"Concatenate the strings in an order which maximises the occurrence of subsequence " ab " | C # implementation of the approach ; Custom sort function to sort the given string in the order which maximises the final score ; To store the count of occurrences of ' a ' and ' b ' in s1 ; Count the number of occurrences of ' a ' and ' b ' in s1 ; To store the count of occurrences of ' a ' and ' b ' in s2 ; Count the number of occurrences of ' a ' and ' b ' in s2 ; Since the number of subsequences ' ab ' is more when s1 is placed before s2 we return 1 so that s1 occurs before s2 in the combined string ; Function that return the concatenated string as S [ 0 ] + S [ 1 ] + ... + S [ N - 1 ] ; To store the concatenated string ; Concatenate every string in the order of appearance ; Return the concatenated string ; Function to return the maximum required score ; Sort the strings in the order which maximizes the score that we can get ; Get the concatenated string combined string ; Calculate the score of the combined string i . e . the count of occurrences of " ab " as subsequences ; Number of ' a ' has increased by one ; There are count_a number of ' a ' that can form subsequence ' ab ' with this ' b ' ; Driver code"
Solution in C#:

using System ; class GFG { static bool customSort ( string s1 , string s2 ) { int count_a1 = 0 , count_b1 = 0 ; for ( int i = 0 ; i < s1 . Length ; i ++ ) { if ( s1 [ i ] == ' a ' ) { count_a1 ++ ; } else { count_b1 ++ ; } } int count_a2 = 0 , count_b2 = 0 ; for ( int i = 0 ; i < s1 . Length ; i ++ ) { if ( s2 [ i ] == ' a ' ) { count_a2 ++ ; } else { count_b2 ++ ; } } if ( count_a1 * count_b2 > count_b1 * count_a2 ) { return true ; } else { return false ; } } static string concatenateStrings ( string [ ] S , int N ) { string str = " " ; for ( int i = 0 ; i < N ; i ++ ) { str += S [ i ] ; } return str ; } static int getMaxScore ( string [ ] S , int N ) { Array . Sort ( S ) ; string combined_string = concatenateStrings ( S , N ) ; int final_score = 0 , count_a = 0 ; for ( int i = 0 ; i < combined_string . Length ; i ++ ) { if ( combined_string [ i ] == ' a ' ) { count_a ++ ; } else { final_score += count_a ; } } return final_score ; } static public void Main ( ) { string [ ] S = { " aa " , " bb " , " aab " , " bab " } ; int N = S . Length ; Console . WriteLine ( getMaxScore ( S , N ) - 10 ) ; } }


"Concatenated string with uncommon characters of two strings | C # program Find concatenated string with uncommon characters of given strings ; Result ; creating a hashMap to add characters in string s2 ; Find characters of s1 that are not present in s2 and append to result ; Find characters of s2 that are not present in s1 . ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static String concatenatedString ( String s1 , String s2 ) { String res = " " ; int i ; Dictionary < char , int > m = new Dictionary < char , int > ( ) ; for ( i = 0 ; i < s2 . Length ; i ++ ) if ( ! m . ContainsKey ( s2 [ i ] ) ) m . Add ( s2 [ i ] , 1 ) ; for ( i = 0 ; i < s1 . Length ; i ++ ) if ( ! m . ContainsKey ( s1 [ i ] ) ) res += s1 [ i ] ; else m [ s1 [ i ] ] = 2 ; for ( i = 0 ; i < s2 . Length ; i ++ ) if ( m [ s2 [ i ] ] == 1 ) res += s2 [ i ] ; return res ; } public static void Main ( String [ ] args ) { String s1 = " abcs " ; String s2 = " cxzca " ; Console . WriteLine ( concatenatedString ( s1 , s2 ) ) ; } }


"Concentration of juice after mixing n glasses in equal proportion | C # implementation of the approach ; Function to return the concentration of the resultant mixture ; Driver code"
Solution in C#:

using System ; class GFG { static double mixtureConcentration ( int n , int [ ] p ) { double res = 0 ; for ( int i = 0 ; i < n ; i ++ ) res += p [ i ] ; res /= n ; return Math . Round ( res , 4 ) ; } static void Main ( ) { int [ ] arr = { 0 , 20 , 20 } ; int n = arr . Length ; Console . WriteLine ( mixtureConcentration ( n , arr ) ) ; } }


"Concentric Hexagonal Numbers | C # program to find nth concentric hexagon number ; Function to find nth concentric hexagon number ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { static int concentric_Hexagon ( int n ) { return 3 * ( int ) Math . Pow ( n , 2 ) / 2 ; } public static void Main ( ) { int n = 3 ; Console . WriteLine ( concentric_Hexagon ( n ) ) ; } }


"Connect Nodes at same Level ( Level Order Traversal ) | Connect nodes at same level using level order traversal . ; Node class ; Sets nextRight of all nodes of a tree ; null marker to represent end of current level ; Do Level order of tree using NULL markers ; next element in queue represents next node at current Level ; push left and right children of current node ; if queue is not empty , push NULL to mark nodes at this level are visited ; Driver code ; Constructed binary tree is 10 / \ 8 2 / \ 3 90 ; Populates nextRight pointer in all nodes ; Let us check the values of nextRight pointers"
Solution in C#:

 using System ; using System . Collections . Generic ; public class Connect_node_same_level { class Node { public int data ; public Node left , right , nextRight ; public Node ( int data ) { this . data = data ; left = null ; right = null ; nextRight = null ; } } ; static void connect ( Node root ) { Queue < Node > q = new Queue < Node > ( ) ; q . Enqueue ( root ) ; q . Enqueue ( null ) ; while ( q . Count != 0 ) { Node p = q . Peek ( ) ; q . Dequeue ( ) ; if ( p != null ) { p . nextRight = q . Peek ( ) ; if ( p . left != null ) q . Enqueue ( p . left ) ; if ( p . right != null ) q . Enqueue ( p . right ) ; } else if ( q . Count != 0 ) q . Enqueue ( null ) ; } } public static void Main ( ) { Node root = new Node ( 10 ) ; root . left = new Node ( 8 ) ; root . right = new Node ( 2 ) ; root . left . left = new Node ( 3 ) ; root . right . right = new Node ( 90 ) ; connect ( root ) ; Console . WriteLine ( " Following ▁ are ▁ populated ▁ nextRight ▁ pointers ▁ in ▁
 " + " the ▁ tree ▁ ( -1 ▁ is ▁ printed ▁ if ▁ there ▁ is ▁ no ▁ nextRight ) " ) ; Console . WriteLine ( " nextRight ▁ of ▁ " + root . data + " ▁ is ▁ " + ( ( root . nextRight != null ) ? root . nextRight . data : - 1 ) ) ; Console . WriteLine ( " nextRight ▁ of ▁ " + root . left . data + " ▁ is ▁ " + ( ( root . left . nextRight != null ) ? root . left . nextRight . data : - 1 ) ) ; Console . WriteLine ( " nextRight ▁ of ▁ " + root . right . data + " ▁ is ▁ " + ( ( root . right . nextRight != null ) ? root . right . nextRight . data : - 1 ) ) ; Console . WriteLine ( " nextRight ▁ of ▁ " + root . left . left . data + " ▁ is ▁ " + ( ( root . left . left . nextRight != null ) ? root . left . left . nextRight . data : - 1 ) ) ; Console . WriteLine ( " nextRight ▁ of ▁ " + root . right . right . data + " ▁ is ▁ " + ( ( root . right . right . nextRight != null ) ? root . right . right . nextRight . data : - 1 ) ) ; } }


"Connect a graph by M edges such that the graph does not contain any cycle and Bitwise AND of connected vertices is maximum | C # program for the above approach ; Function to find the maximum Bitwise AND of connected components possible by connecting a graph using M edges ; Stores total number of ways to connect the graph ; Stores the maximum Bitwise AND ; Iterate over the range [ 0 , 2 ^ n ] ; Store the Bitwise AND of the connected vertices ; Store the count of the connected vertices ; Check for all the bits ; If i - th bit is set ; If the first vertex is added ; Set andans equal to arr [ i ] ; Calculate Bitwise AND of arr [ i ] with andans ; Increase the count of connected vertices ; If number of connected vertices is ( m + 1 ) , no cycle is formed ; Find the maximum Bitwise AND value possible ; Return the maximum Bitwise AND possible ; Driver Code"
Solution in C#:

using System ; class GFG { static int maximumAND ( int [ ] arr , int n , int m ) { int tot = 1 << n ; int mx = 0 ; for ( int bm = 0 ; bm < tot ; bm ++ ) { int andans = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( ( ( bm >> i ) & 1 ) != 0 ) { if ( count == 0 ) { andans = arr [ i ] ; } else { andans = andans & arr [ i ] ; } count ++ ; } } if ( count == ( m + 1 ) ) { mx = Math . Max ( mx , andans ) ; } } return mx ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int N = arr . Length ; int M = 2 ; Console . WriteLine ( maximumAND ( arr , N , M ) ) ; } }


"Connect n ropes with minimum cost | C # program to connect n ropes with minimum cost ; A class for Min Heap ; Array of elements in heap ; Current number of elements in min heap ; maximum possible size of min heap ; Constructor : Builds a heap from a given array a [ ] of given size ; A recursive method to heapify a subtree with the root at given index This method assumes that the subtrees are already heapified ; to get index of left child of node at index i ; to get index of right child of node at index i ; Method to remove minimum element ( or root ) from min heap ; Store the minimum value , and remove it from heap ; Inserts a new key ' k ' ; First insert the new key at the end ; Fix the min heap property if it is violated ; A utility function to check if size of heap is 1 or not ; A utility function to swap two elements ; The main function that returns the minimum cost to connect n ropes of lengths stored in len [ 0. . n - 1 ] ; Initialize result ; Create a min heap of capacity equal to n and put all ropes in it ; Iterate while size of heap doesn 't become 1 ; Extract two minimum length ropes from min heap ; Update total cost ; Insert a new rope in min heap with length equal to sum of two extracted minimum lengths ; Finally return total minimum cost for connecting all ropes ; Driver code"
Solution in C#:

using System ; class MinHeap { int [ ] harr ; int heap_size ; int capacity ; public MinHeap ( int [ ] a , int size ) { heap_size = size ; capacity = size ; harr = a ; int i = ( heap_size - 1 ) / 2 ; while ( i >= 0 ) { MinHeapify ( i ) ; i -- ; } } void MinHeapify ( int i ) { int l = left ( i ) ; int r = right ( i ) ; int smallest = i ; if ( l < heap_size && harr [ l ] < harr [ i ] ) smallest = l ; if ( r < heap_size && harr [ r ] < harr [ smallest ] ) smallest = r ; if ( smallest != i ) { swap ( i , smallest ) ; MinHeapify ( smallest ) ; } } int parent ( int i ) { return ( i - 1 ) / 2 ; } int left ( int i ) { return ( 2 * i + 1 ) ; } int right ( int i ) { return ( 2 * i + 2 ) ; } int extractMin ( ) { if ( heap_size <= 0 ) return int . MaxValue ; if ( heap_size == 1 ) { heap_size -- ; return harr [ 0 ] ; } int root = harr [ 0 ] ; harr [ 0 ] = harr [ heap_size - 1 ] ; heap_size -- ; MinHeapify ( 0 ) ; return root ; } void insertKey ( int k ) { if ( heap_size == capacity ) { Console . WriteLine ( " Overflow : ▁ Could ▁ not ▁ insertKey " ) ; return ; } heap_size ++ ; int i = heap_size - 1 ; harr [ i ] = k ; while ( i != 0 && harr [ parent ( i ) ] > harr [ i ] ) { swap ( i , parent ( i ) ) ; i = parent ( i ) ; } } Boolean isSizeOne ( ) { return ( heap_size == 1 ) ; } void swap ( int x , int y ) { int temp = harr [ x ] ; harr [ x ] = harr [ y ] ; harr [ y ] = temp ; } static int minCost ( int [ ] len , int n ) { int cost = 0 ; MinHeap minHeap = new MinHeap ( len , n ) ; while ( ! minHeap . isSizeOne ( ) ) { int min = minHeap . extractMin ( ) ; int sec_min = minHeap . extractMin ( ) ; cost += ( min + sec_min ) ; minHeap . insertKey ( min + sec_min ) ; } return cost ; } public static void Main ( String [ ] args ) { int [ ] len = { 4 , 3 , 2 , 6 } ; int size = len . Length ; Console . WriteLine ( " Total ▁ cost ▁ for ▁ connecting ▁ ropes ▁ is ▁ " + minCost ( len , size ) ) ; } } ;


"Connect n ropes with minimum cost | C # program to connect n ropes with minimum cost ; Create a priority queue ; Initialize result ; While size of priority queue is more than 1 ; Extract shortest two ropes from pq ; Connect the ropes : update result and insert the new rope to pq ; Driver program to test above function"
Solution in C#:

using System ; using System . Collections . Generic ; public class ConnectRopes { static int minCost ( int [ ] arr , int n ) { List < int > pq = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { pq . Add ( arr [ i ] ) ; } int res = 0 ; while ( pq . Count > 1 ) { pq . Sort ( ) ; int first = pq [ 0 ] ; int second = pq [ 1 ] ; pq . RemoveRange ( 0 , 2 ) ; res += first + second ; pq . Add ( first + second ) ; } return res ; } public static void Main ( String [ ] args ) { int [ ] len = { 4 , 3 , 2 , 6 } ; int size = len . Length ; Console . WriteLine ( " Total ▁ cost ▁ for ▁ connecting " + " ▁ ropes ▁ is ▁ " + minCost ( len , size ) ) ; } }


"Connect nodes at same level using constant extra space | Iterative c # program to connect nodes at same level using constant extra space ; A binary tree node ; This function returns the leftmost child of nodes at the same level as p . This function is used to getNExt right of p 's right child   If right child of is NULL then this can also be used for the   left child  ; Traverse nodes at p ' s ▁ level ▁ and ▁ find ▁ and ▁ return ▁ ▁ ▁ the ▁ first ▁ node ' s first child ; If all the nodes at p 's level are leaf nodes then return NULL  ; Sets nextRight of all nodes of a tree with root as p ; Set nextRight for root ; set nextRight of all levels one by one ; Connect all childrem nodes of p and children nodes of all other nodes at same level as p ; Set the nextRight pointer for p 's left child  ; If q has right child , then right child is nextRight of p and we also need to set nextRight of right child ; Set nextRight for other nodes in pre order fashion ; start from the first node of next level ; Driver program to test above functions ; Constructed binary tree is 10 / \ 8 2 / \ 3 90 ; Populates nextRight pointer in all nodes ; Let us check the values of nextRight pointers"
Solution in C#:

using System ; public class Node { public int data ; public Node left , right , nextRight ; public Node ( int item ) { data = item ; left = right = nextRight = null ; } } public class BinaryTree { public Node root ; public virtual Node getNextRight ( Node p ) { Node temp = p . nextRight ; while ( temp != null ) { if ( temp . left != null ) { return temp . left ; } if ( temp . right != null ) { return temp . right ; } temp = temp . nextRight ; } return null ; } public virtual void connect ( Node p ) { Node temp = null ; if ( p == null ) { return ; } p . nextRight = null ; while ( p != null ) { Node q = p ; while ( q != null ) { if ( q . left != null ) { if ( q . right != null ) { q . left . nextRight = q . right ; } else { q . left . nextRight = getNextRight ( q ) ; } } if ( q . right != null ) { q . right . nextRight = getNextRight ( q ) ; } q = q . nextRight ; } if ( p . left != null ) { p = p . left ; } else if ( p . right != null ) { p = p . right ; } else { p = getNextRight ( p ) ; } } } public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( 8 ) ; tree . root . right = new Node ( 2 ) ; tree . root . left . left = new Node ( 3 ) ; tree . root . right . right = new Node ( 90 ) ; tree . connect ( tree . root ) ; int a = tree . root . nextRight != null ? tree . root . nextRight . data : - 1 ; int b = tree . root . left . nextRight != null ? tree . root . left . nextRight . data : - 1 ; int c = tree . root . right . nextRight != null ? tree . root . right . nextRight . data : - 1 ; int d = tree . root . left . left . nextRight != null ? tree . root . left . left . nextRight . data : - 1 ; int e = tree . root . right . right . nextRight != null ? tree . root . right . right . nextRight . data : - 1 ; Console . WriteLine ( " Following ▁ are ▁ populated ▁ nextRight ▁ pointers ▁ in ▁ " + " ▁ the ▁ tree ( -1 ▁ is ▁ printed ▁ if ▁ there ▁ is ▁ no ▁ nextRight ) " ) ; Console . WriteLine ( " nextRight ▁ of ▁ " + tree . root ....


"Connect nodes at same level | C # program to connect nodes at same level ; initialize queue to hold nodes at same level ; adding nodes to tehe queue ; initializing prev to null ; i > 0 because when i is 0 prev points the last node of previous level , so we skip it ; pointing last node of the nth level to null ; Driver code ; Constructed binary tree is 10 / \ 8 2 / 3 ; Populates nextRight pointer in all nodes ; Let us check the values of nextRight pointers"
Solution in C#:

using System ; using System . Collections . Generic ; class Node { public int data ; public Node left , right , nextRight ; public Node ( int item ) { data = item ; left = right = nextRight = null ; } } public class BinaryTree { Node root ; void connect ( Node p ) { Queue < Node > q = new Queue < Node > ( ) ; q . Enqueue ( root ) ; Node temp = null ; while ( q . Count > 0 ) { int n = q . Count ; for ( int i = 0 ; i < n ; i ++ ) { Node prev = temp ; temp = q . Dequeue ( ) ; if ( i > 0 ) prev . nextRight = temp ; if ( temp . left != null ) q . Enqueue ( temp . left ) ; if ( temp . right != null ) q . Enqueue ( temp . right ) ; } temp . nextRight = null ; } } public static void Main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( 8 ) ; tree . root . right = new Node ( 2 ) ; tree . root . left . left = new Node ( 3 ) ; tree . connect ( tree . root ) ; Console . WriteLine ( " Following ▁ are ▁ populated ▁ nextRight ▁ pointers ▁ in ▁ " + " the ▁ tree " + " ( -1 ▁ is ▁ printed ▁ if ▁ there ▁ is ▁ no ▁ nextRight ) " ) ; int a = tree . root . nextRight != null ? tree . root . nextRight . data : - 1 ; Console . WriteLine ( " nextRight ▁ of ▁ " + tree . root . data + " ▁ is ▁ " + a ) ; int b = tree . root . left . nextRight != null ? tree . root . left . nextRight . data : - 1 ; Console . WriteLine ( " nextRight ▁ of ▁ " + tree . root . left . data + " ▁ is ▁ " + b ) ; int c = tree . root . right . nextRight != null ? tree . root . right . nextRight . data : - 1 ; Console . WriteLine ( " nextRight ▁ of ▁ " + tree . root . right . data + " ▁ is ▁ " + c ) ; int d = tree . root . left . left . nextRight != null ? tree . root . left . left . nextRight . data : - 1 ; Console . WriteLine ( " nextRight ▁ of ▁ " + tree . root . left . left . data + " ▁ is ▁ " + d ) ; Console . ReadKey ( ) ; } }


"Connect nodes at same level | C # program to connect nodes at same level using extended pre - order traversal ; A binary tree node ; Sets the nextRight of root and calls connectRecur ( ) for other nodes ; Set the nextRight for root ; Set the next right for rest of the nodes ( other than root ) ; Set next right of all descendents of p . Assumption : p is a compete binary tree ; Base case ; Set the nextRight pointer for p 's left child ; Set the nextRight pointer for p 's right child  p->nextRight will be NULL if p is the right most child  at its level ; Set nextRight for other nodes in pre order fashion ; Driver program to test above functions ; Constructed binary tree is 10 / \ 8 2 / 3 ; Populates nextRight pointer in all nodes ; Let us check the values of nextRight pointers"
Solution in C#:

using System ; public class Node { public int data ; public Node left , right , nextRight ; public Node ( int item ) { data = item ; left = right = nextRight = null ; } } public class BinaryTree { public Node root ; public virtual void connect ( Node p ) { p . nextRight = null ; connectRecur ( p ) ; } public virtual void connectRecur ( Node p ) { if ( p == null ) { return ; } if ( p . left != null ) { p . left . nextRight = p . right ; } if ( p . right != null ) { p . right . nextRight = ( p . nextRight != null ) ? p . nextRight . left : null ; } connectRecur ( p . left ) ; connectRecur ( p . right ) ; } public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( 8 ) ; tree . root . right = new Node ( 2 ) ; tree . root . left . left = new Node ( 3 ) ; tree . connect ( tree . root ) ; Console . WriteLine ( " Following ▁ are ▁ populated ▁ nextRight ▁ pointers ▁ in ▁ " + " the ▁ tree " + " ( -1 ▁ is ▁ printed ▁ if ▁ there ▁ is ▁ no ▁ nextRight ) " ) ; int a = tree . root . nextRight != null ? tree . root . nextRight . data : - 1 ; Console . WriteLine ( " nextRight ▁ of ▁ " + tree . root . data + " ▁ is ▁ " + a ) ; int b = tree . root . left . nextRight != null ? tree . root . left . nextRight . data : - 1 ; Console . WriteLine ( " nextRight ▁ of ▁ " + tree . root . left . data + " ▁ is ▁ " + b ) ; int c = tree . root . right . nextRight != null ? tree . root . right . nextRight . data : - 1 ; Console . WriteLine ( " nextRight ▁ of ▁ " + tree . root . right . data + " ▁ is ▁ " + c ) ; int d = tree . root . left . left . nextRight != null ? tree . root . left . left . nextRight . data : - 1 ; Console . WriteLine ( " nextRight ▁ of ▁ " + tree . root . left . left . data + " ▁ is ▁ " + d ) ; } }


"Connell Sequence | C # code to generate first ' n ' terms of Connell Sequence ; Function to generate a fixed number of even or odd terms . The size of r decides whether numbers to be generated even or odd . ; Generating the first ' n ' terms of Connell Sequence ; A dummy 0 is inserted at the beginning for consistency ; Calling function gen ( ) to generate ' k ' number of terms ; Checking if ' n ' terms are already generated ; Removing the previously inserted dummy 0 ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static List < long > gen ( long n , List < long > r ) { long a = r [ r . Count - 1 ] ; a ++ ; for ( int i = 1 ; i <= n ; a += 2 , i ++ ) r . Add ( a ) ; return r ; } static List < long > conell ( long n ) { List < long > res = new List < long > ( ) ; long k = 1 ; res . Add ( 0 ) ; while ( true ) { res = gen ( k , res ) ; k ++ ; int j = res . Count - 1 ; while ( j != n && j + k > n ) k -- ; if ( j >= n ) break ; } res . RemoveAt ( 0 ) ; return res ; } static void Main ( ) { long n = 10 ; Console . WriteLine ( " The ▁ first ▁ " + n + " ▁ terms ▁ are " ) ; List < long > res = conell ( n ) ; for ( int i = 0 ; i < res . Count ; i ++ ) Console . Write ( res [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } }


"Consecutive Prime numbers greater than equal to given number . | C # program for the above approach ; Function to check prime . ; It means it is not a prime ; No factor other than 1 therefore prime number ; Function to find out the required consecutive primes . ; Finding first prime just less than sqrt ( n ) . ; Finding prime just greater than sqrt ( n ) . ; Product of both prime is greater than n then print it ; Finding prime greater than second ; Driver Program"
Solution in C#:

using System ; class GFG { static bool is_prime ( long n ) { if ( n == 1 ) { return false ; } for ( long i = 2 ; i <= ( long ) Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { return false ; } } return true ; } static void consecutive_primes ( long n ) { long first = - 1 , second = - 1 ; for ( long i = ( long ) Math . Sqrt ( n ) ; i >= 2 ; i -- ) { if ( is_prime ( i ) ) { first = i ; break ; } } for ( long i = ( long ) Math . Sqrt ( n ) + 1 ; i <= n / 2 ; i ++ ) { if ( is_prime ( i ) ) { second = i ; break ; } } if ( first * second >= n ) { Console . WriteLine ( first + " ▁ " + second ) ; } else { for ( long i = second + 1 ; i <= n ; i ++ ) { if ( is_prime ( i ) ) { Console . WriteLine ( second + " ▁ " + i ) ; return ; } } } } public static void Main ( ) { long n = 14 ; consecutive_primes ( n ) ; } }


"Consecutive sequenced numbers in a string | C # Program to check whether a String contains consecutive sequential numbers or not ; function to check consecutive sequential number ; variable to store starting number ; length of the input String ; find the number till half of the String ; new String containing the starting substring of input String ; converting starting substring into number ; backing up the starting number in start ; while loop until the new_String is smaller than input String ; next number ; concatenate the next number ; check if new String becomes equal to input String ; if String doesn 't contains consecutive numbers ; Driver Code"
Solution in C#:

 using System ; class GFG { static int isConsecutive ( String str ) { int start ; int length = str . Length ; for ( int i = 0 ; i < length / 2 ; i ++ ) { String new_str = str . Substring ( 0 , i + 1 ) ; int num = int . Parse ( new_str ) ; start = num ; while ( new_str . Length < length ) { num ++ ; new_str = new_str + String . Join ( " " , num ) ; } if ( new_str . Equals ( str ) ) return start ; } return - 1 ; } public static void Main ( String [ ] args ) { String str = "99100" ; Console . WriteLine ( " String : ▁ " + str ) ; int start = isConsecutive ( str ) ; if ( start != - 1 ) Console . WriteLine ( " Yes ▁
 " + start ) ; else Console . WriteLine ( " No " ) ; String str1 = "121315" ; Console . WriteLine ( " String : " start = isConsecutive ( str1 ) ; if ( start != - 1 ) Console . WriteLine ( " Yes ▁
 " + start ) ; else Console . WriteLine ( " No " ) ; } }


"Consecutive steps to roof top | C # code to find maximum number of consecutive steps . ; Function to count consecutive steps ; count the number of consecutive increasing height building ; Driver code"
Solution in C#:

using System ; class GFG { static int find_consecutive_steps ( int [ ] arr , int len ) { int count = 0 ; int maximum = 0 ; for ( int index = 1 ; index < len ; index ++ ) { if ( arr [ index ] > arr [ index - 1 ] ) count ++ ; else { maximum = Math . Max ( maximum , count ) ; count = 0 ; } } return Math . Max ( maximum , count ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int len = arr . Length ; Console . WriteLine ( find_consecutive_steps ( arr , len ) ) ; } }


"Constant time range add operation on an array | C # program to get updated array after many array range add operation ; Utility method to add value val , to range [ lo , hi ] ; Utility method to get actual array from operation array ; convert array into prefix sum array ; method to print final updated array ; Driver code ; Range add Queries"
Solution in C#:

 using System ; class GFG { static void add ( int [ ] arr , int N , int lo , int hi , int val ) { arr [ lo ] += val ; if ( hi != N - 1 ) arr [ hi + 1 ] -= val ; } static void updateArray ( int [ ] arr , int N ) { for ( int i = 1 ; i < N ; i ++ ) arr [ i ] += arr [ i - 1 ] ; } static void printArr ( int [ ] arr , int N ) { updateArray ( arr , N ) ; for ( int i = 0 ; i < N ; i ++ ) Console . Write ( " " + arr [ i ] + " ▁ " ) ; Console . Write ( "
 " ) ; } public static void Main ( ) { int N = 6 ; int [ ] arr = new int [ N ] ; add ( arr , N , 0 , 2 , 100 ) ; add ( arr , N , 1 , 5 , 100 ) ; add ( arr , N , 2 , 3 , 100 ) ; printArr ( arr , N ) ; } }


"Construct BST from given preorder traversal | Set 1 | Construct a BST from given pre - order traversal for example if the given traversal is { 10 , 5 , 1 , 7 , 40 , 50 } , then the output should be the root of the following tree . 10 / \ 5 40 / \ \ 1 7 50 ; This will create the BST ; A wrapper function of createNode ; A function to print BST in inorder ; Driver Code"
Solution in C#:

using System ; public class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; this . left = this . right = null ; } } public class CreateBSTFromPreorder { private static Node node ; public static Node createNode ( Node node , int data ) { if ( node == null ) node = new Node ( data ) ; if ( node . data > data ) node . left = createNode ( node . left , data ) ; if ( node . data < data ) node . right = createNode ( node . right , data ) ; return node ; } public static void create ( int data ) { node = createNode ( node , data ) ; } public static void inorderRec ( Node root ) { if ( root != null ) { inorderRec ( root . left ) ; Console . WriteLine ( root . data ) ; inorderRec ( root . right ) ; } } public static void Main ( String [ ] args ) { int [ ] nodeData = { 10 , 5 , 1 , 7 , 40 , 50 } ; for ( int i = 0 ; i < nodeData . Length ; i ++ ) { create ( nodeData [ i ] ) ; } inorderRec ( node ) ; } }


"Construct BST from its given level order traversal | C # implementation to construct a BST from its level order traversal ; node of a BST ; function to get a new node ; Allocate memory ; put in the data ; function to construct a BST from its level order traversal ; function to print the inorder traversal ; Driver code"
Solution in C#:

using System ; class GFG { public class Node { public int data ; public Node left , right ; } ; static Node getNode ( int data ) { Node newNode = new Node ( ) ; newNode . data = data ; newNode . left = newNode . right = null ; return newNode ; } static Node LevelOrder ( Node root , int data ) { if ( root == null ) { root = getNode ( data ) ; return root ; } if ( data <= root . data ) root . left = LevelOrder ( root . left , data ) ; else root . right = LevelOrder ( root . right , data ) ; return root ; } static Node constructBst ( int [ ] arr , int n ) { if ( n == 0 ) return null ; Node root = null ; for ( int i = 0 ; i < n ; i ++ ) root = LevelOrder ( root , arr [ i ] ) ; return root ; } static void inorderTraversal ( Node root ) { if ( root == null ) return ; inorderTraversal ( root . left ) ; Console . Write ( root . data + " ▁ " ) ; inorderTraversal ( root . right ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 7 , 4 , 12 , 3 , 6 , 8 , 1 , 5 , 10 } ; int n = arr . Length ; Node root = constructBst ( arr , n ) ; Console . Write ( " Inorder ▁ Traversal : ▁ " ) ; inorderTraversal ( root ) ; } }


"Construct Binary Tree from String with bracket representation | C # program to cona binary tree from the given String ; A binary tree node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new node ; This funtcion is here just to test ; function to return the index of close parenthesis ; Inbuilt stack ; if open parenthesis , push it ; if close parenthesis ; if stack is empty , this is the required index ; if not found return - 1 ; function to contree from String ; Base case ; new root ; if next char is ' ( ' find the index of its complement ' ) ' ; if index found ; call for left subtree ; call for right subtree ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { public 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 void preOrder ( Node node ) { if ( node == null ) return ; Console . Write ( " { 0 } ▁ " , node . data ) ; preOrder ( node . left ) ; preOrder ( node . right ) ; } static int findIndex ( String str , int si , int ei ) { if ( si > ei ) return - 1 ; Stack < char > s = new Stack < char > ( ) ; for ( int i = si ; i <= ei ; i ++ ) { if ( str [ i ] == ' ( ' ) s . Push ( str [ i ] ) ; else if ( str [ i ] == ' ) ' ) { if ( s . Peek ( ) == ' ( ' ) { s . Pop ( ) ; if ( s . Count == 0 ) return i ; } } } return - 1 ; } static Node treeFromString ( String str , int si , int ei ) { if ( si > ei ) return null ; Node root = newNode ( str [ si ] - '0' ) ; int index = - 1 ; if ( si + 1 <= ei && str [ si + 1 ] == ' ( ' ) index = findIndex ( str , si + 1 , ei ) ; if ( index != - 1 ) { root . left = treeFromString ( str , si + 2 , index - 1 ) ; root . right = treeFromString ( str , index + 2 , ei - 1 ) ; } return root ; } public static void Main ( String [ ] args ) { String str = "4(2(3 ) ( 1 ) ) (6(5 ) ) " ; Node root = treeFromString ( str , 0 , str . Length - 1 ) ; preOrder ( root ) ; } }


"Construct Binary Tree from given Parent Array representation | C # program to construct a binary tree from parent array ; A binary tree node ; Creates a node with key as ' i ' . If i is root , then it changes root . If parent of i is not created , then it creates parent first ; If this node is already created ; Create a new node and set created [ i ] ; If ' i ' is root , change root pointer and return ; If parent is not created , then create parent first ; Find parent pointer ; If this is first child of parent ; If second child ; Creates tree from parent [ 0. . n - 1 ] and returns root of the created tree ; Create an array created [ ] to keep track of created nodes , initialize all entries as NULL ; For adding new line in a program ; Utility function to do inorder traversal ; Driver Code"
Solution in C#:

using System ; public class Node { public int key ; public Node left , right ; public Node ( int key ) { this . key = key ; left = right = null ; } } class GFG { public Node root ; public virtual void createNode ( int [ ] parent , int i , Node [ ] created ) { if ( created [ i ] != null ) { return ; } created [ i ] = new Node ( i ) ; if ( parent [ i ] == - 1 ) { root = created [ i ] ; return ; } if ( created [ parent [ i ] ] == null ) { createNode ( parent , parent [ i ] , created ) ; } Node p = created [ parent [ i ] ] ; if ( p . left == null ) { p . left = created [ i ] ; } else { p . right = created [ i ] ; } } public virtual Node createTree ( int [ ] parent , int n ) { Node [ ] created = new Node [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { created [ i ] = null ; } for ( int i = 0 ; i < n ; i ++ ) { createNode ( parent , i , created ) ; } return root ; } public virtual void newLine ( ) { Console . WriteLine ( " " ) ; } public virtual void inorder ( Node node ) { if ( node != null ) { inorder ( node . left ) ; Console . Write ( node . key + " ▁ " ) ; inorder ( node . right ) ; } } public static void Main ( string [ ] args ) { GFG tree = new GFG ( ) ; int [ ] parent = new int [ ] { - 1 , 0 , 0 , 1 , 1 , 3 , 5 } ; int n = parent . Length ; Node node = tree . createTree ( parent , n ) ; Console . WriteLine ( " Inorder ▁ traversal ▁ of ▁ " + " constructed ▁ tree ▁ " ) ; tree . inorder ( node ) ; tree . newLine ( ) ; } }


"Construct Full Binary Tree from given preorder and postorder traversals | C # program for construction of full binary tree ; A binary tree node has data , pointer to left child and a pointer to right child ; A recursive function to construct Full from pre [ ] and post [ ] . preIndex is used to keep track of index in pre [ ] . l is low index and h is high index for the current subarray in post [ ] ; Base case ; The first node in preorder traversal is root . So take the node at preIndex from preorder and make it root , and increment preIndex ; If the current subarry has only one element , no need to recur or preIndex > size after incrementing ; Search the next element of pre [ ] in post [ ] ; Use the index of element found in postorder to divide postorder array in two parts . Left subtree and right subtree ; The main function to construct Full Binary Tree from given preorder and postorder traversals . This function mainly uses constructTreeUtil ( ) ; A utility function to print inorder traversal of a Binary Tree ; Driver Code"
Solution in C#:

using System ; class GFG { public static int preindex ; public class node { public int data ; public node left , right ; public node ( int data ) { this . data = data ; } } public static node constructTreeUtil ( int [ ] pre , int [ ] post , int l , int h , int size ) { if ( preindex >= size l > h ) { return null ; } node root = new node ( pre [ preindex ] ) ; preindex ++ ; if ( l == h preindex >= size ) { return root ; } int i ; for ( i = l ; i <= h ; i ++ ) { if ( post [ i ] == pre [ preindex ] ) { break ; } } if ( i <= h ) { root . left = constructTreeUtil ( pre , post , l , i , size ) ; root . right = constructTreeUtil ( pre , post , i + 1 , h , size ) ; } return root ; } public static node constructTree ( int [ ] pre , int [ ] post , int size ) { preindex = 0 ; return constructTreeUtil ( pre , post , 0 , size - 1 , size ) ; } public static void printInorder ( node root ) { if ( root == null ) { return ; } printInorder ( root . left ) ; Console . Write ( root . data + " ▁ " ) ; printInorder ( root . right ) ; } public static void Main ( string [ ] args ) { int [ ] pre = new int [ ] { 1 , 2 , 4 , 8 , 9 , 5 , 3 , 6 , 7 } ; int [ ] post = new int [ ] { 8 , 9 , 4 , 5 , 2 , 6 , 7 , 3 , 1 } ; int size = pre . Length ; node root = constructTree ( pre , post , size ) ; Console . WriteLine ( " Inorder ▁ traversal ▁ of ▁ " + " the ▁ constructed ▁ tree : " ) ; printInorder ( root ) ; } }


"Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree | C # program to construct full binary tree using its preorder traversal and preorder traversal of its mirror tree ; A Binary Tree Node ; Utility function to create a new tree node ; A utility function to print inorder traversal of a Binary Tree ; A recursive function to con Full binary tree from pre [ ] and preM [ ] . preIndex is used to keep track of index in pre [ ] . l is low index and h is high index for the current subarray in preM [ ] ; Base case ; The first node in preorder traversal is root . So take the node at preIndex from preorder and make it root , and increment preIndex ; If the current subarry has only one element , no need to recur ; Search the next element of pre [ ] in preM [ ] ; con left and right subtrees recursively ; return root ; function to con full binary tree using its preorder traversal and preorder traversal of its mirror tree ; Driver code"
Solution in C#:

using System ; class GFG { public class Node { public int data ; public Node left , right ; } ; public class INT { public int a ; public INT ( int a ) { this . a = a ; } } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } static void printInorder ( Node node ) { if ( node == null ) return ; printInorder ( node . left ) ; Console . Write ( " { 0 } ▁ " , node . data ) ; printInorder ( node . right ) ; } static Node conBinaryTreeUtil ( int [ ] pre , int [ ] preM , INT preIndex , int l , int h , int size ) { if ( preIndex . a >= size l > h ) return null ; Node root = newNode ( pre [ preIndex . a ] ) ; ++ ( preIndex . a ) ; if ( l == h ) return root ; int i ; for ( i = l ; i <= h ; ++ i ) if ( pre [ preIndex . a ] == preM [ i ] ) break ; if ( i <= h ) { root . left = conBinaryTreeUtil ( pre , preM , preIndex , i , h , size ) ; root . right = conBinaryTreeUtil ( pre , preM , preIndex , l + 1 , i - 1 , size ) ; } return root ; } static void conBinaryTree ( Node root , int [ ] pre , int [ ] preMirror , int size ) { INT preIndex = new INT ( 0 ) ; int preMIndex = 0 ; root = conBinaryTreeUtil ( pre , preMirror , preIndex , 0 , size - 1 , size ) ; printInorder ( root ) ; } public static void Main ( String [ ] args ) { int [ ] preOrder = { 1 , 2 , 4 , 5 , 3 , 6 , 7 } ; int [ ] preOrderMirror = { 1 , 3 , 7 , 6 , 2 , 5 , 4 } ; int size = preOrder . Length ; Node root = new Node ( ) ; conBinaryTree ( root , preOrder , preOrderMirror , size ) ; } }


"Construct MEX array from the given array | C # program for the above approach ; Function to construct array B [ ] that stores MEX of array A [ ] excluding A [ i ] ; Stores elements present in arr [ ] ; Mark all values 1 , if present ; Initialize variable to store MEX ; Find MEX of arr [ ] ; Stores MEX for all indices ; Traverse the given array ; Update MEX ; MEX default ; Print the array B ; Driver Code ; Given array arr [ ] ; Size of array ; Function call"
Solution in C#:

using System ; class GFG { static int MAXN = 100001 ; static void constructMEX ( int [ ] arr , int N ) { int [ ] hash = new int [ MAXN ] ; for ( int i = 0 ; i < N ; i ++ ) { hash [ i ] = 0 ; } for ( int i = 0 ; i < N ; i ++ ) { hash [ arr [ i ] ] = 1 ; } int MexOfArr = 0 ; for ( int i = 1 ; i < MAXN ; i ++ ) { if ( hash [ i ] == 0 ) { MexOfArr = i ; break ; } } int [ ] B = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < MexOfArr ) B [ i ] = arr [ i ] ; else B [ i ] = MexOfArr ; } for ( int i = 0 ; i < N ; i ++ ) Console . Write ( B [ i ] + " ▁ " ) ; } public static void Main ( ) { int [ ] arr = { 2 , 1 , 5 , 3 } ; int N = arr . Length ; constructMEX ( arr , N ) ; } }


"Construct Special Binary Tree from given Inorder traversal | C # program to construct tree from inorder traversal ; A binary tree node has data , pointer to left child and a pointer to right child ; Recursive function to construct binary of size len from Inorder traversal inorder [ ] . Initial values of start and end should be 0 and len - 1. ; Find index of the maximum element from Binary Tree ; Pick the maximum value and make it root ; If this is the only element in inorder [ start . . end ] , then return it ; Using index in Inorder traversal , construct left and right subtress ; Function to find index of the maximum value in arr [ start ... end ] ; This funtcion is here just to test buildTree ( ) ; first recur on left child ; then print the data of node ; now recur on right child ; Driver Code ; Assume that inorder traversal of following tree is given 40 / \ 10 30 / \ 5 28 ; Let us test the built tree by printing Inorder traversal"
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 Node buildTree ( int [ ] inorder , int start , int end , Node node ) { if ( start > end ) { return null ; } int i = max ( inorder , start , end ) ; node = new Node ( inorder [ i ] ) ; if ( start == end ) { return node ; } node . left = buildTree ( inorder , start , i - 1 , node . left ) ; node . right = buildTree ( inorder , i + 1 , end , node . right ) ; return node ; } public virtual int max ( int [ ] arr , int strt , int end ) { int i , max = arr [ strt ] , maxind = strt ; for ( i = strt + 1 ; i <= end ; i ++ ) { if ( arr [ i ] > max ) { max = arr [ i ] ; maxind = i ; } } return maxind ; } 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 ( ) ; int [ ] inorder = new int [ ] { 5 , 10 , 40 , 30 , 28 } ; int len = inorder . Length ; Node mynode = tree . buildTree ( inorder , 0 , len - 1 , tree . root ) ; Console . WriteLine ( " Inorder ▁ traversal ▁ of ▁ " + " the ▁ constructed ▁ tree ▁ is ▁ " ) ; tree . printInorder ( mynode ) ; } }


"Construct Tree from given Inorder and Preorder traversals | C # program to construct a tree using inorder and preorder traversal ; A binary tree node has data , pointer to left child and a pointer to right child ; Recursive function to construct binary of size len from Inorder traversal in [ ] and Preorder traversal pre [ ] . Initial values of inStrt and inEnd should be 0 and len - 1. The function doesn 't do any error checking for cases where inorder and preorder do not form a tree  ; Pick current node from Preorder traversal using preIndex and increment preIndex ; If this node has no children then return ; Else find the index of this node in Inorder traversal ; Using index in Inorder traversal , construct left and right subtress ; Function to find index of value in arr [ start ... end ] The function assumes that value is present in in [ ] ; This funtcion is here just to test buildTree ( ) ; first recur on left child ; then print the data of node ; now recur on right child ; Driver Code ; building the tree by printing inorder traversal"
Solution in C#:

using System ; public class Node { public char data ; public Node left , right ; public Node ( char item ) { data = item ; left = right = null ; } } class GFG { public Node root ; public static int preIndex = 0 ; public virtual Node buildTree ( char [ ] arr , char [ ] pre , int inStrt , int inEnd ) { if ( inStrt > inEnd ) { return null ; } Node tNode = new Node ( pre [ preIndex ++ ] ) ; if ( inStrt == inEnd ) { return tNode ; } int inIndex = search ( arr , inStrt , inEnd , tNode . data ) ; tNode . left = buildTree ( arr , pre , inStrt , inIndex - 1 ) ; tNode . right = buildTree ( arr , pre , inIndex + 1 , inEnd ) ; return tNode ; } public virtual int search ( char [ ] arr , int strt , int end , char value ) { int i ; for ( i = strt ; i <= end ; i ++ ) { if ( arr [ i ] == value ) { return i ; } } return i ; } 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 ( ) ; char [ ] arr = new char [ ] { ' D ' , ' B ' , ' E ' , ' A ' , ' F ' , ' C ' } ; char [ ] pre = new char [ ] { ' A ' , ' B ' , ' D ' , ' E ' , ' C ' , ' F ' } ; int len = arr . Length ; Node root = tree . buildTree ( arr , pre , 0 , len - 1 ) ; Console . WriteLine ( " Inorder ▁ traversal ▁ of ▁ " + " constructed ▁ tree ▁ is ▁ : ▁ " ) ; tree . printInorder ( root ) ; } }


"Construct Tree from given Inorder and Preorder traversals | C # program to construct a tree using inorder and preorder traversal ; Function to build tree using given traversal ; Function to print tree in Inorder ; first recur on left child ; then print the data of node ; now recur on right child ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; public class TreeNode { public int val ; public TreeNode left ; public TreeNode right ; public TreeNode ( int x ) { val = x ; } } class GFG { static HashSet < TreeNode > set = new HashSet < TreeNode > ( ) ; static Stack < TreeNode > stack = new Stack < TreeNode > ( ) ; public TreeNode buildTree ( int [ ] preorder , int [ ] inorder ) { TreeNode root = null ; for ( int pre = 0 , iN = 0 ; pre < preorder . Length ; ) { TreeNode node = null ; do { node = new TreeNode ( preorder [ pre ] ) ; if ( root == null ) { root = node ; } if ( stack . Count != 0 ) { if ( set . Contains ( stack . Peek ( ) ) ) { set . Remove ( stack . Peek ( ) ) ; stack . Pop ( ) . right = node ; } else { stack . Peek ( ) . left = node ; } } stack . Push ( node ) ; } while ( preorder [ pre ++ ] != inorder [ iN ] && pre < preorder . Length ) ; node = null ; while ( stack . Count != 0 && iN < inorder . Length && stack . Peek ( ) . val == inorder [ iN ] ) { node = stack . Pop ( ) ; iN ++ ; } if ( node != null ) { set . Add ( node ) ; stack . Push ( node ) ; } } return root ; } void printInorder ( TreeNode node ) { if ( node == null ) return ; printInorder ( node . left ) ; Console . Write ( node . val + " ▁ " ) ; printInorder ( node . right ) ; } public static void Main ( String [ ] args ) { GFG tree = new GFG ( ) ; int [ ] iN = new int [ ] { 9 , 8 , 4 , 2 , 10 , 5 , 10 , 1 , 6 , 3 , 13 , 12 , 7 } ; int [ ] pre = new int [ ] { 1 , 2 , 4 , 8 , 9 , 5 , 10 , 10 , 3 , 6 , 7 , 12 , 13 } ; int len = iN . Length ; TreeNode root = tree . buildTree ( pre , iN ) ; tree . printInorder ( root ) ; } }


"Construct Tree from given Inorder and Preorder traversals | C # program to construct tree using inorder and preorder traversals ; A binary tree node has data , pointer to left child and a pointer to right child ; Store indexes of all items so that we we can quickly find later ; Recursive function to construct binary of size len from Inorder traversal in [ ] and Preorder traversal pre [ ] . Initial values of inStrt and inEnd should be 0 and len - 1. The function doesn 't do any error  checking for cases where inorder and preorder  do not form a tree  ; Pick current node from Preorder traversal using preIndex and increment preIndex ; If this node has no children then return ; Else find the index of this node in Inorder traversal ; Using index in Inorder traversal , construct left and right subtress ; This function mainly creates an unordered_map , then calls buildTree ( ) ; This funtcion is here just to test buildTree ( ) ; Driver code ; Let us test the built tree by printing Insorder traversal"
Solution in C#:

using System ; using System . Collections . Generic ; public class Node { public char data ; public Node left , right ; public Node ( char d ) { data = d ; left = right = null ; } } public class Tree { public static Node root ; static Dictionary < char , int > mp = new Dictionary < char , int > ( ) ; static int preIndex = 0 ; static Node buildTree ( char [ ] In , char [ ] pre , int inStrt , int inEnd ) { if ( inStrt > inEnd ) { return null ; } char curr = pre [ preIndex ++ ] ; Node tNode ; tNode = new Node ( curr ) ; if ( inStrt == inEnd ) { return tNode ; } int inIndex = mp [ curr ] ; tNode . left = buildTree ( In , pre , inStrt , inIndex - 1 ) ; tNode . right = buildTree ( In , pre , inIndex + 1 , inEnd ) ; return tNode ; } public static Node buldTreeWrap ( char [ ] In , char [ ] pre , int len ) { for ( int i = 0 ; i < len ; i ++ ) { mp . Add ( In [ i ] , i ) ; } return buildTree ( In , pre , 0 , len - 1 ) ; } static void printInorder ( Node node ) { if ( node == null ) { return ; } printInorder ( node . left ) ; Console . Write ( node . data + " ▁ " ) ; printInorder ( node . right ) ; } static public void Main ( ) { char [ ] In = { ' D ' , ' B ' , ' E ' , ' A ' , ' F ' , ' C ' } ; char [ ] pre = { ' A ' , ' B ' , ' D ' , ' E ' , ' C ' , ' F ' } ; int len = In . Length ; Tree . root = buldTreeWrap ( In , pre , len ) ; Console . WriteLine ( " Inorder ▁ traversal ▁ of ▁ the ▁ constructed ▁ tree ▁ is " ) ; printInorder ( Tree . root ) ; } }


"Construct XOR tree by Given leaf nodes of Perfect Binary Tree | C # implementation of the above approach ; Maximum size for xor tree ; Allocating space to xor tree ; A recursive function that constructs xor tree for vector array [ start ... . . end ] . x is index of current node in XOR tree ; If there is one element in vector array , store it in current node of XOR tree ; Console . Write ( xor_tree [ x ] + " ▁ x " ; ; for left subtree ; for right subtree ; for getting the middle index from corner indexes . ; Build the left and the right subtrees by xor operation ; merge the left and right subtrees by XOR operation ; Function to conXOR tree from the given vector array . This function calls construct_Xor_Tree_Util ( ) to fill the allocated memory of xor_tree vector array ; Driver Code ; leaf nodes of Perfect Binary Tree ; Build the xor tree ; Height of xor tree ; Maximum size of xor tree ; Root node is at index 0 considering 0 - based indexing in XOR Tree ; print value at root node"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static int maxsize = 100005 ; static int [ ] xor_tree = new int [ maxsize ] ; static void construct_Xor_Tree_Util ( int [ ] current , int start , int end , int x ) { if ( start == end ) { xor_tree [ x ] = current [ start ] ; return ; } int left = x * 2 + 1 ; int right = x * 2 + 2 ; int mid = start + ( end - start ) / 2 ; construct_Xor_Tree_Util ( current , start , mid , left ) ; construct_Xor_Tree_Util ( current , mid + 1 , end , right ) ; xor_tree [ x ] = ( xor_tree [ left ] ^ xor_tree [ right ] ) ; } static void construct_Xor_Tree ( int [ ] arr , int n ) { construct_Xor_Tree_Util ( arr , 0 , n - 1 , 0 ) ; } public static void Main ( String [ ] args ) { int [ ] leaf_nodes = { 40 , 32 , 12 , 1 , 4 , 3 , 2 , 7 } ; int n = leaf_nodes . Length ; construct_Xor_Tree ( leaf_nodes , n ) ; int x = ( int ) ( Math . Ceiling ( Math . Log ( n ) ) ) ; int max_size = 2 * ( int ) Math . Pow ( 2 , x ) - 1 ; Console . Write ( " Nodes ▁ of ▁ the ▁ XOR ▁ Tree :
 " ) ; for ( int i = 0 ; i < max_size ; i ++ ) { Console . Write ( xor_tree [ i ] + " ▁ " ) ; } int root = 0 ; Console . Write ( " Root : " } }


"Construct a Binary Tree from Postorder and Inorder | C # program for above approach ; Node class ; Constructor ; Tree building function ; Create Stack of type Node * ; Create HashSet of type Node * ; Initialise postIndex with n - 1 ; Initialise root with null ; Initialise node with NULL ; Run do - while loop ; Initialise node with new Node ( post [ p ] ) ; ; Check is root is equal to NULL ; If size of set is greater than 0 ; If st . Peek ( ) is present in the set s ; If the stack is not empty and st . top ( ) . data is equal to in [ i ] ; Pop elements from stack ; If node not equal to NULL ; Return root ; For print preOrder Traversal ; Driver Code ; Function Call ; Function Call for preOrder"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { public class Node { public int data ; public Node left , right ; public Node ( int x ) { data = x ; left = right = null ; } } static Node buildTree ( int [ ] init , int [ ] post , int n ) { Stack < Node > st = new Stack < Node > ( ) ; HashSet < Node > s = new HashSet < Node > ( ) ; int postIndex = n - 1 ; Node root = null ; for ( int p = n - 1 , i = n - 1 ; p >= 0 ; ) { Node node = null ; do { node = new Node ( post [ p ] ) ; if ( root == null ) { root = node ; } if ( st . Count > 0 ) { if ( s . Contains ( st . Peek ( ) ) ) { s . Remove ( st . Peek ( ) ) ; st . Peek ( ) . left = node ; st . Pop ( ) ; } else { st . Peek ( ) . right = node ; } } st . Push ( node ) ; } while ( post [ p -- ] != init [ i ] && p >= 0 ) ; node = null ; while ( st . Count > 0 && i >= 0 && st . Peek ( ) . data == init [ i ] ) { node = st . Peek ( ) ; st . Pop ( ) ; i -- ; } if ( node != null ) { s . Add ( node ) ; st . Push ( node ) ; } } return root ; } static void preOrder ( Node node ) { if ( node == null ) return ; Console . Write ( node . data + " ▁ " ) ; preOrder ( node . left ) ; preOrder ( node . right ) ; } public static void Main ( String [ ] args ) { int [ ] init = { 4 , 8 , 2 , 5 , 1 , 6 , 3 , 7 } ; int [ ] post = { 8 , 4 , 5 , 2 , 6 , 7 , 3 , 1 } ; int n = init . Length ; Node root = buildTree ( init , post , n ) ; Console . Write ( " Preorder ▁ of ▁ the ▁ constructed ▁ tree ▁ : ▁
 " ) ; preOrder ( root ) ; } }


"Construct a Binary Tree from Postorder and Inorder | C # program to construct a tree using inorder and postorder traversals ; A binary tree node has data , pointer to left child and a pointer to right child ; Class Index created to implement pass by reference of Index ; Recursive function to construct binary of size n from Inorder traversal in [ ] and Postrder traversal post [ ] . Initial values of inStrt and inEnd should be 0 and n - 1. The function doesn 't do any error checking for cases where inorder and postorder do not form a tree  ; Base case ; Pick current node from Postrder traversal using postIndex and decrement postIndex ; If this node has no children then return ; Else find the index of this node in Inorder traversal ; Using index in Inorder traversal , construct left and right subtress ; This function mainly initializes index of root and calls buildUtil ( ) ; Function to find index of value in arr [ start ... end ] . The function assumes that value is postsent in in [ ] ; This funtcion is here just to test ; Driver Code"
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 ; } } public class Index { public int index ; } class GFG { public virtual Node buildUtil ( int [ ] @in , int [ ] post , int inStrt , int inEnd , Index pIndex ) { if ( inStrt > inEnd ) { return null ; } Node node = new Node ( post [ pIndex . index ] ) ; ( pIndex . index ) -- ; if ( inStrt == inEnd ) { return node ; } int iIndex = search ( @in , inStrt , inEnd , node . data ) ; node . right = buildUtil ( @in , post , iIndex + 1 , inEnd , pIndex ) ; node . left = buildUtil ( @in , post , inStrt , iIndex - 1 , pIndex ) ; return node ; } public virtual Node buildTree ( int [ ] @in , int [ ] post , int n ) { Index pIndex = new Index ( ) ; pIndex . index = n - 1 ; return buildUtil ( @in , post , 0 , n - 1 , pIndex ) ; } public virtual int search ( int [ ] arr , int strt , int end , int value ) { int i ; for ( i = strt ; i <= end ; i ++ ) { if ( arr [ i ] == value ) { break ; } } return i ; } 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 ) { GFG tree = new GFG ( ) ; int [ ] @in = new int [ ] { 4 , 8 , 2 , 5 , 1 , 6 , 3 , 7 } ; int [ ] post = new int [ ] { 8 , 4 , 5 , 2 , 6 , 7 , 3 , 1 } ; int n = @in . Length ; Node root = tree . buildTree ( @in , post , n ) ; Console . WriteLine ( " Preorder ▁ of ▁ the ▁ constructed ▁ tree ▁ : ▁ " ) ; tree . preOrder ( root ) ; } }


"Construct a Binary Tree from Postorder and Inorder | C # program to contree using inorder and postorder traversals ; A binary tree node has data , pointer to left child and a pointer to right child ; Recursive function to conbinary of size n from Inorder traversal in [ ] and Postorder traversal post [ ] . Initial values of inStrt and inEnd should be 0 and n - 1. The function doesn 't do any error checking for cases where inorder and postorder do not form a tree  ; Base case ; Pick current node from Postorder traversal using postIndex and decrement postIndex ; If this node has no children then return ; Else find the index of this node in Inorder traversal ; Using index in Inorder traversal , con left and right subtress ; This function mainly creates an unordered_map , then calls buildTreeUtil ( ) ; Store indexes of all items so that we we can quickly find later ; Index in postorder ; Helper function that allocates a new node ; This funtcion is here just to test ; Driver code"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { public class Node { public int data ; public Node left , right ; } ; static Node buildUtil ( int [ ] init , int [ ] post , int inStrt , int inEnd ) { if ( inStrt > inEnd ) return null ; int curr = post [ index ] ; Node node = newNode ( curr ) ; ( index ) -- ; if ( inStrt == inEnd ) return node ; int iIndex = mp [ curr ] ; node . right = buildUtil ( init , post , iIndex + 1 , inEnd ) ; node . left = buildUtil ( init , post , inStrt , iIndex - 1 ) ; return node ; } static Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; static int index ; static Node buildTree ( int [ ] init , int [ ] post , int len ) { for ( int i = 0 ; i < len ; i ++ ) mp . Add ( init [ i ] , i ) ; index = len - 1 ; return buildUtil ( init , post , 0 , len - 1 ) ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } static void preOrder ( Node node ) { if ( node == null ) return ; Console . Write ( node . data + " ▁ " ) ; preOrder ( node . left ) ; preOrder ( node . right ) ; } public static void Main ( String [ ] args ) { int [ ] init = { 4 , 8 , 2 , 5 , 1 , 6 , 3 , 7 } ; int [ ] post = { 8 , 4 , 5 , 2 , 6 , 7 , 3 , 1 } ; int n = init . Length ; Node root = buildTree ( init , post , n ) ; Console . Write ( " Preorder ▁ of ▁ the ▁ constructed ▁ tree ▁ : ▁
 " ) ; preOrder ( root ) ; } }


"Construct a Doubly linked linked list from 2D Matrix | C # program to construct a Doubly linked linked list from 2D Matrix ; define dimension of matrix ; struct node of doubly linked list with four pointer next , prev , up , down ; function to create a new node ; function to construct the doubly linked list ; Create Node with value contain in matrix at index ( i , j ) ; Assign address of curr into the prev pointer of temp ; Assign address of curr into the up pointer of temp ; Recursive call for next pointer ; Recursive call for down pointer ; Return newly constructed node whose all four node connected at it 's appropriate position ; Function to construct the doubly linked list ; function call for construct the doubly linked list ; function for displaying doubly linked list data ; pointer to move right ; pointer to move down ; loop till node . down is not null ; loop till node . right is not null ; driver code ; initialise matrix"
Solution in C#:

 using System ; class GFG { static int dim = 3 ; public class Node { public int data ; public Node next , prev , up , down ; } ; static Node createNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . next = null ; temp . prev = null ; temp . up = null ; temp . down = null ; return temp ; } static Node constructDoublyListUtil ( int [ , ] mtrx , int i , int j , Node curr ) { if ( i >= dim j >= dim ) { return null ; } Node temp = createNode ( mtrx [ i , j ] ) ; temp . prev = curr ; temp . up = curr ; temp . next = constructDoublyListUtil ( mtrx , i , j + 1 , temp ) ; temp . down = constructDoublyListUtil ( mtrx , i + 1 , j , temp ) ; return temp ; } static Node constructDoublyList ( int [ , ] mtrx ) { return constructDoublyListUtil ( mtrx , 0 , 0 , null ) ; } static void display ( Node head ) { Node rPtr ; Node dPtr = head ; while ( dPtr != null ) { rPtr = dPtr ; while ( rPtr != null ) { Console . Write ( rPtr . data + " ▁ " ) ; rPtr = rPtr . next ; } Console . Write ( "
 " ) ; dPtr = dPtr . down ; } } public static void Main ( ) { int [ , ] mtrx = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; Node list = constructDoublyList ( mtrx ) ; display ( list ) ; } }


"Construct a List using the given Q XOR queries | C # program for the above approach ; Function to return required list after performing all the queries ; Store cumulative Bitwise XOR ; Initialize readonly list to return ; Perform each query ; The initial value of 0 ; Sort the list ; Return readonly list ; Driver Code ; Given Queries ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static List < int > ConstructList ( int [ , ] Q ) { int xor = 0 ; List < int > ans = new List < int > ( ) ; for ( int i = Q . GetLength ( 0 ) - 1 ; i >= 0 ; i -- ) { if ( Q [ i , 0 ] == 0 ) ans . Add ( Q [ i , 1 ] ^ xor ) ; else xor ^= Q [ i , 1 ] ; } ans . Add ( xor ) ; ans . Sort ( ) ; return ans ; } public static void Main ( String [ ] args ) { int [ , ] Q = { { 0 , 6 } , { 0 , 3 } , { 0 , 2 } , { 1 , 4 } , { 1 , 5 } } ; foreach ( int i in ConstructList ( Q ) ) Console . Write ( i + " , ▁ " ) ; } }


"Construct a Matrix N x N with first N ^ 2 natural numbers for an input N | C # program for the above approach ; Function to print the desired matrix ; Iterate ove all [ 0 , N ] ; If is even ; If row number is even print the row in forward order ; If row number is odd print the row in reversed order ; Driver code ; Given matrix size ; Function call"
Solution in C#:

using System ; class GFG { static void UniqueMatrix ( int N ) { int element_value = 1 ; int i = 0 ; while ( i < N ) { if ( i % 2 == 0 ) { for ( int f = element_value ; f < element_value + N ; f ++ ) { Console . Write ( f + " ▁ " ) ; } element_value += N ; } else { for ( int k = element_value + N - 1 ; k > element_value - 1 ; k -- ) { Console . Write ( k + " ▁ " ) ; } element_value += N ; } Console . WriteLine ( ) ; i = i + 1 ; } } static public void Main ( ) { int N = 4 ; UniqueMatrix ( N ) ; } }


"Construct a Matrix such that each cell consists of sum of adjacent elements of respective cells in given Matrix | C # program for the above approach ; Initialize rows and columns ; Store all 8 directions ; Function to check if a cell ( i , j ) is valid or not ; Function to find sum of adjacent cells for cell ( i , j ) ; Initialize sum ; Visit all 8 directions ; Check if cell is valid ; Return sum ; Function to print sum of adjacent elements ; Stores the resultant matrix ; Iterate each elements of matrix ; Find adjacent sum ; Driver code ; Given matrix ; Size of matrix ; Function call"
Solution in C#:

using System ; public class GFG { private static int r , c ; static int [ , ] dir = { { 1 , 0 } , { - 1 , 0 } , { 0 , 1 } , { 0 , - 1 } , { - 1 , - 1 } , { - 1 , 1 } , { 1 , 1 } , { 1 , - 1 } } ; public static bool valid ( int i , int j ) { if ( i >= 0 && j >= 0 && i < r && j < c ) return true ; return false ; } static int find ( int i , int j , int [ , ] v ) { int s = 0 ; for ( int k = 0 ; k < 8 ; k ++ ) { int ni = i + dir [ k , 0 ] , nj = j + dir [ k , 1 ] ; if ( valid ( ni , nj ) ) s += v [ ni , nj ] ; } return s ; } static void findsumofneighbors ( int [ , ] M ) { int [ , ] v = new int [ r , c ] ; for ( int i = 0 ; i < r ; i ++ ) { for ( int j = 0 ; j < c ; j ++ ) { v [ i , j ] = find ( i , j , M ) ; Console . Write ( v [ i , j ] + " ▁ " ) ; } Console . WriteLine ( " " ) ; } } public static void Main ( String [ ] args ) { int [ , ] M = { { 1 , 4 , 1 } , { 2 , 4 , 5 } , { 3 , 1 , 2 } } ; r = M . GetLength ( 0 ) ; c = M . GetLength ( 1 ) ; findsumofneighbors ( M ) ; } }


"Construct a Matrix with no element exceeding X and sum of two adjacent elements not exceeding Y | C # implementation of the above approach ; Function to print the required matrix ; For 1 * 1 matrix ; Greater number ; Smaller number ; Sets / Resets for alternate filling of the matrix ; Print the matrix ; If end of row is reached ; Driver Code"
Solution in C#:

 using System ; class GFG { static void FindMatrix ( int n , int m , int x , int y ) { int a , b , i , j ; if ( n * m == 1 ) { if ( x > y ) { Console . Write ( y + "
 " ) ; } else { Console . Write ( x + "
 " ) ; } return ; } a = Math . Min ( x , y ) ; b = Math . Min ( 2 * x , y ) - a ; bool flag = true ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < m ; j ++ ) { if ( flag ) Console . Write ( a + " ▁ " ) ; else Console . Write ( b + " ▁ " ) ; flag = ! flag ; } if ( ( ( n % 2 != 0 && m % 2 == 0 ) || ( n % 2 == 0 && m % 2 == 0 ) ) ) flag = ! flag ; Console . Write ( "
 " ) ; } } public static void Main ( String [ ] args ) { int N , M , X , Y ; N = 3 ; M = 3 ; X = 5 ; Y = 3 ; FindMatrix ( N , M , X , Y ) ; } }


"Construct a Maximum Binary Tree from two given Binary Trees | C # program to find the Maximum Binary Tree from two Binary Trees ; A binary tree node has data , pointer to left child and a pointer to right child ; Helper method that allocates a new node with the given data and NULL left and right pointers . ; Given a binary tree , print its nodes in inorder ; first recur on left child ; then print the data of node ; now recur on right child ; Method to find the maximum binary tree from two binary trees ; Driver Code ; First Binary Tree 3 / \ 2 6 / 20 ; Second Binary Tree 5 / \ 1 8 \ \ 2 8"
Solution in C#:

using System ; class Node { public int data ; public Node left , right ; public Node ( int data , Node left , Node right ) { this . data = data ; this . left = left ; this . right = right ; } static Node newNode ( int data ) { return new Node ( data , null , null ) ; } static void inorder ( Node node ) { if ( node == null ) return ; inorder ( node . left ) ; Console . Write ( " { 0 } ▁ " , node . data ) ; inorder ( node . right ) ; } static Node MaximumBinaryTree ( Node t1 , Node t2 ) { if ( t1 == null ) return t2 ; if ( t2 == null ) return t1 ; t1 . data = Math . Max ( t1 . data , t2 . data ) ; t1 . left = MaximumBinaryTree ( t1 . left , t2 . left ) ; t1 . right = MaximumBinaryTree ( t1 . right , t2 . right ) ; return t1 ; } public static void Main ( String [ ] args ) { Node root1 = newNode ( 3 ) ; root1 . left = newNode ( 2 ) ; root1 . right = newNode ( 6 ) ; root1 . left . left = newNode ( 20 ) ; Node root2 = newNode ( 5 ) ; root2 . left = newNode ( 1 ) ; root2 . right = newNode ( 8 ) ; root2 . left . right = newNode ( 2 ) ; root2 . right . right = newNode ( 8 ) ; Node root3 = MaximumBinaryTree ( root1 , root2 ) ; inorder ( root3 ) ; } }


"Construct a Perfect Binary Tree from Preorder Traversal | C # program for the above approach ; Structure of the tree ; Function to create a new node with the value val ; Return the newly created node ; Function to create the Perfect Binary Tree ; If preStart > preEnd return NULL ; Initialize root as pre [ preStart ] ; If the only node is left , then return node ; Parameters for further recursion ; Recursive Call to build the subtree of root node ; Return the created root ; Function to build Perfect Binary Tree ; Function to print the Inorder of the given Tree ; Base Case ; Left Recursive Call ; Print the data ; Right Recursive Call ; Driver Code ; Function Call ; Print Inorder Traversal"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { class Node { public int data ; public Node left , right ; public Node ( int val ) { data = val ; left = right = null ; } } static Node getNewNode ( int val ) { Node newNode = new Node ( val ) ; return newNode ; } static Node buildPerfectBT_helper ( int preStart , int preEnd , int [ ] pre ) { if ( preStart > preEnd ) return null ; Node root = getNewNode ( pre [ preStart ] ) ; if ( preStart == preEnd ) return root ; int leftPreStart = preStart + 1 ; int rightPreStart = leftPreStart + ( preEnd - leftPreStart + 1 ) / 2 ; int leftPreEnd = rightPreStart - 1 ; int rightPreEnd = preEnd ; root . left = buildPerfectBT_helper ( leftPreStart , leftPreEnd , pre ) ; root . right = buildPerfectBT_helper ( rightPreStart , rightPreEnd , pre ) ; return root ; } static Node buildPerfectBT ( int [ ] pre , int size ) { return buildPerfectBT_helper ( 0 , size - 1 , pre ) ; } static void printInorder ( Node root ) { if ( root == null ) return ; printInorder ( root . left ) ; Console . Write ( root . data + " ▁ " ) ; printInorder ( root . right ) ; } static void Main ( ) { int [ ] pre = { 1 , 2 , 4 , 5 , 3 , 6 , 7 } ; int N = pre . Length ; Node root = buildPerfectBT ( pre , N ) ; Console . Write ( " Inorder ▁ traversal ▁ of ▁ the ▁ tree : ▁ " ) ; printInorder ( root ) ; } }


"Construct a Tree whose sum of nodes of all the root to leaf path is not divisible by the count of nodes in that path | C # program for the above approach ; Function to assign values to nodes of the tree s . t . sum of values of nodes of path between any 2 nodes is not divisible by length of path ; Stores the adjacency list ; Create a adjacency list ; Stores whether node is visited or not ; Stores the node values ; Variable used to assign values to the nodes alternatively to the parent child ; Declare a queue ; Push the 1 st node ; Assign K value to this node ; Dequeue the node ; Mark it as visited ; Upgrade the value of K ; Assign K to the child nodes ; If the child is unvisited ; Enqueue the child ; Assign K to the child ; Print the value assigned to the nodes ; Driver code ; Function Call"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; public class GFG { static void assignValues ( int [ , ] Edges , int n ) { LinkedList < int > [ ] tree = new LinkedList < int > [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) tree [ i ] = new LinkedList < int > ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int u = Edges [ i , 0 ] ; int v = Edges [ i , 1 ] ; tree [ u ] . AddLast ( v ) ; tree [ v ] . AddLast ( u ) ; } bool [ ] visited = new bool [ n + 1 ] ; int [ ] answer = new int [ n + 1 ] ; int K = 1 ; Queue q = new Queue ( ) ; q . Enqueue ( 1 ) ; answer [ 1 ] = K ; while ( q . Count > 0 ) { int node = ( int ) q . Peek ( ) ; q . Dequeue ( ) ; visited [ node ] = true ; K = ( ( answer [ node ] == 1 ) ? 2 : 1 ) ; foreach ( var child in tree [ node ] ) { if ( ! visited [ child ] ) { q . Enqueue ( child ) ; answer [ child ] = K ; } } } for ( int i = 1 ; i <= n ; i ++ ) { Console . Write ( answer [ i ] + " ▁ " ) ; } } static public void Main ( ) { int N = 11 ; int [ , ] Edges = { { 1 , 2 } , { 1 , 3 } , { 1 , 4 } , { 1 , 5 } , { 2 , 6 } , { 2 , 10 } , { 10 , 11 } , { 3 , 7 } , { 4 , 8 } , { 5 , 9 } } ; assignValues ( Edges , N ) ; } }


"Construct a binary string following the given constraints | C # implementation of the approach ; Function to print a binary string which has ' a ' number of 0 ' s , ▁ ' b ' ▁ number ▁ of ▁ 1' s and there are at least ' x ' indices such that s [ i ] != s [ i + 1 ] ; Divide index value by 2 and store it into d ; If index value x is even and x / 2 is not equal to a ; Loop for d for each d print 10 ; subtract d from a and b ; Loop for b to print remaining 1 's ; Loop for a to print remaining 0 's ; Driver code"
Solution in C#:

using System ; class GFG { static void constructBinString ( int a , int b , int x ) { int d , i ; d = x / 2 ; if ( x % 2 == 0 && x / 2 != a ) { d -- ; Console . Write ( "0" ) ; a -- ; } for ( i = 0 ; i < d ; i ++ ) Console . Write ( "10" ) ; a = a - d ; b = b - d ; for ( i = 0 ; i < b ; i ++ ) { Console . Write ( "1" ) ; } for ( i = 0 ; i < a ; i ++ ) { Console . Write ( "0" ) ; } } public static void Main ( ) { int a = 4 , b = 3 , x = 2 ; constructBinString ( a , b , x ) ; } }


"Construct a complete binary tree from given array in level order fashion | C # program to construct binary tree from given array in level order fashion ; Tree Node ; Function to insert nodes in level order ; Base case for recursion ; insert left child ; insert right child ; Function to print tree nodes in InOrder fashion ; Driver code"
Solution in C#:

using System ; public class Tree { Node root ; public class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; this . left = null ; this . right = null ; } } public Node insertLevelOrder ( int [ ] arr , Node root , int i ) { if ( i < arr . Length ) { Node temp = new Node ( arr [ i ] ) ; root = temp ; root . left = insertLevelOrder ( arr , root . left , 2 * i + 1 ) ; root . right = insertLevelOrder ( arr , root . right , 2 * i + 2 ) ; } return root ; } public void inOrder ( Node root ) { if ( root != null ) { inOrder ( root . left ) ; Console . Write ( root . data + " ▁ " ) ; inOrder ( root . right ) ; } } public static void Main ( String [ ] args ) { Tree t2 = new Tree ( ) ; int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 6 , 6 , 6 } ; t2 . root = t2 . insertLevelOrder ( arr , t2 . root , 0 ) ; t2 . inOrder ( t2 . root ) ; } }


"Construct a graph from given degrees of all vertices | C # program to generate a graph for a given fixed degrees ; A function to print the adjacency matrix . ; n is number of vertices ; For each pair of vertex decrement the degree of both vertex . ; Print the result in specified format ; Driver Code"
Solution in C#:

 using System ; class GFG { static void printMat ( int [ ] degseq , int n ) { int [ , ] mat = new int [ n , n ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( degseq [ i ] > 0 && degseq [ j ] > 0 ) { degseq [ i ] -- ; degseq [ j ] -- ; mat [ i , j ] = 1 ; mat [ j , i ] = 1 ; } } } Console . Write ( "
 " + setw ( 3 ) + " ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( setw ( 3 ) + " ( " + i + " ) " ) ; Console . Write ( "

 " ) ; for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( setw ( 4 ) + " ( " + i + " ) " ) ; for ( int j = 0 ; j < n ; j ++ ) Console . Write ( setw ( 5 ) + mat [ i , j ] ) ; Console . Write ( "
 " ) ; } } static String setw ( int n ) { String space = " " ; while ( n -- > 0 ) space += " ▁ " ; return space ; } public static void Main ( String [ ] args ) { int [ ] degseq = { 2 , 2 , 1 , 1 , 1 } ; int n = degseq . Length ; printMat ( degseq , n ) ; } }


"Construct a graph using N vertices whose shortest distance between K pair of vertices is 2 | C # program to implement the above approach ; Function to construct the simple and connected graph such that the distance between exactly K pairs of vertices is 2 ; Stores maximum possible count of edges in a graph ; Base Case ; Stores edges of a graph ; Connect all vertices of pairs ( i , j ) ; Print first ( ( N - 1 ) + Max - K ) elements of edges [ ] ; Driver Code"
Solution in C#:

 using System ; using System . Collections . Generic ; public class GFG { class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void constGraphWithCon ( int N , int K ) { int Max = ( ( N - 1 ) * ( N - 2 ) ) / 2 ; if ( K > Max ) { Console . Write ( - 1 + "
 " ) ; return ; } List < pair > ans = new List < pair > ( ) ; for ( int i = 1 ; i < N ; i ++ ) { for ( int j = i + 1 ; j <= N ; j ++ ) { ans . Add ( new pair ( i , j ) ) ; } } for ( int i = 0 ; i < ( N - 1 ) + Max - K ; i ++ ) { Console . Write ( ans [ i ] . first + " ▁ " + ans [ i ] . second + "
 " ) ; } } public static void Main ( String [ ] args ) { int N = 5 , K = 3 ; constGraphWithCon ( N , K ) ; } }


"Construct a graph which does not contain any pair of adjacent nodes with same value | C # program for the above approach ; Function that prints the edges of the generated graph ; First print connections stored in store [ ] ; Check if there is more than one occurrence of 1 st unique element ; Print all other occurrence of 1 st unique element with second unique element ; Function to construct the graph such that the every adjacent nodes have different value ; Stores pair of edges formed ; Stores first unique occurrence ; Check for the second unique occurrence ; Store indices of 2 nd unique occurrence ; To check if arr has only 1 unique element or not ; Store the connections of all unique elements with Node 1 ; If value at node ( i + 1 ) is same as value at Node 1 then store its indices ; If count is zero then it 's not  possible to construct the graph ; If more than 1 unique element is present ; Print the edges ; Driver Code ; Given array having node values ; Function Call"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { public class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void printConnections ( List < pair > store , List < int > ind , int ind1 ) { foreach ( pair pr in store ) { Console . Write ( pr . first + " ▁ " + pr . second + "
 " ) ; } if ( ind . Count != 0 ) { foreach ( int x in ind ) { Console . Write ( ind1 + " ▁ " + ( x + 1 ) + "
 " ) ; } } } static void constructGraph ( char [ ] arr , int N ) { List < int > ind = new List < int > ( ) ; List < pair > store = new List < pair > ( ) ; char x = arr [ 0 ] ; int count = 0 ; int ind1 = - 1 ; for ( int i = 1 ; i <= N - 1 ; ++ i ) { if ( arr [ i ] != x ) { ind1 = i + 1 ; count ++ ; store . Add ( new pair ( 1 , i + 1 ) ) ; } else { ind . Add ( i ) ; } } if ( count == 0 ) { Console . Write ( " Not ▁ Possible " ) ; } else { Console . Write ( " Possible " + "
 " ) ; printConnections ( store , ind , ind1 ) ; } } public static void Main ( String [ ] args ) { int N = 5 ; char [ ] arr = { ' a ' , ' b ' , ' a ' , ' b ' , ' c ' } ; constructGraph ( arr , N ) ; } }


"Construct a matrix such that union of ith row and ith column contains every element from 1 to 2 N | C # implementation of the above approach ; Function to find the square matrix ; For Matrix of order 1 , it will contain only 1 ; For Matrix of odd order , it is not possible ; For Matrix of even order ; All diagonal elements of the matrix can be N itself . ; Assign values at desired place in the matrix ; Loop to add N in the lower half of the matrix such that it contains elements from 1 to 2 * N - 1 ; Loop to print the matrix ; Driver Code"
Solution in C#:

using System ; class GFG { static int [ , ] matrix = new int [ 100 , 100 ] ; static void printRequiredMatrix ( int n ) { if ( n == 1 ) { Console . WriteLine ( "1" ) ; } else if ( n % 2 != 0 ) { Console . WriteLine ( " - 1" ) ; } else { for ( int i = 0 ; i < n ; i ++ ) { matrix [ i , i ] = n ; } int u = n - 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { matrix [ i , u ] = i + 1 ; for ( int j = 1 ; j < n / 2 ; j ++ ) { int a = ( i + j ) % ( n - 1 ) ; int b = ( i - j + n - 1 ) % ( n - 1 ) ; if ( a < b ) { int temp = a ; a = b ; b = temp ; } matrix [ b , a ] = i + 1 ; } } for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) matrix [ i , j ] = matrix [ j , i ] + n ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) Console . Write ( matrix [ i , j ] + " ▁ " ) ; Console . WriteLine ( ) ; } } Console . WriteLine ( ) ; } public static void Main ( String [ ] args ) { int n = 1 ; printRequiredMatrix ( n ) ; n = 3 ; printRequiredMatrix ( n ) ; n = 6 ; printRequiredMatrix ( n ) ; } }


"Construct a sequence from given frequencies of N consecutive integers with unit adjacent difference | C # program for the above approach ; Function generates the sequence ; Map to store the frequency of numbers ; Sum of all frequencies ; Try all possibilities for the starting element ; If the frequency of current element is non - zero ; vector to store the answer ; Copy of the map for every possible starting element ; Decrement the frequency ; Push the starting element to the vector ; The last element inserted is i ; Try to fill the rest of the positions if possible ; If the frequency of last - 1 is non - zero ; Decrement the frequency of last - 1 ; Insert it into the sequence ; Update last number added to sequence ; Break from the inner loop ; If the size of the sequence vector is equal to sum of total frequqncies ; Return sequence ; If no such sequence if found return empty sequence ; Function call to print the sequence ; The required sequence ; If the size of sequence if zero it means no such sequence was found ; Otherwise print the sequence ; Driver Code ; Frequency of all elements from 0 to n - 1 ; Number of elements whose frequencies are given ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static List < int > generateSequence ( int [ ] freq , int n ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; int total = 0 ; for ( int i = 0 ; i < n ; i ++ ) { m . Add ( i , freq [ i ] ) ; total += freq [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { if ( m . ContainsKey ( i ) ) { List < int > sequence = new List < int > ( ) ; Dictionary < int , int > mcopy = new Dictionary < int , int > ( m ) ; if ( mcopy . ContainsKey ( i ) && mcopy [ i ] > 0 ) mcopy [ i ] = mcopy [ i ] - 1 ; sequence . Add ( i ) ; int last = i ; for ( int i1 = 0 ; i1 < total - 1 ; i1 ++ ) { if ( mcopy . ContainsKey ( last - 1 ) && mcopy [ last - 1 ] > 0 ) { mcopy [ last - 1 ] = mcopy [ last - 1 ] - 1 ; sequence . Add ( last - 1 ) ; last -- ; } else if ( mcopy . ContainsKey ( last + 1 ) ) { mcopy [ last + 1 ] = mcopy [ last + 1 ] - 1 ; sequence . Add ( last + 1 ) ; last ++ ; } else break ; } if ( sequence . Count == total ) { return sequence ; } } } List < int > empty = new List < int > ( ) ; return empty ; } static void PrintSequence ( int [ ] freq , int n ) { List < int > sequence = generateSequence ( freq , n ) ; if ( sequence . Count == 0 ) { Console . Write ( " - 1" ) ; } else { for ( int i = 0 ; i < sequence . Count ; i ++ ) { Console . Write ( sequence [ i ] + " ▁ " ) ; } } } public static void Main ( String [ ] args ) { int [ ] freq = { 2 , 2 , 2 , 3 , 1 } ; int N = 5 ; PrintSequence ( freq , N ) ; } }


"Construct a special tree from given preorder traversal | C # program to construct a binary tree from preorder traversal ; A Binary Tree node ; A recursive function to create a Binary Tree from given pre [ ] preLN [ ] arrays . The function returns root of tree . index_ptr is used to update index values in recursive calls . index must be initially passed as 0 ; store the current value of index in pre [ ] ; Base Case : All nodes are constructed ; Allocate memory for this node and increment index for subsequent recursive calls ; If this is an internal node , construct left and right subtrees and link the subtrees ; A wrapper over constructTreeUtil ( ) ; Initialize index as 0. Value of index is used in recursion to maintain the current index in pre [ ] and preLN [ ] arrays . ; This function is used only for testing ; 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 ; } } public class Index { public int index = 0 ; } class GFG { public Node root ; public Index myindex = new Index ( ) ; public virtual Node constructTreeUtil ( int [ ] pre , char [ ] preLN , Index index_ptr , int n , Node temp ) { int index = index_ptr . index ; if ( index == n ) { return null ; } temp = new Node ( pre [ index ] ) ; ( index_ptr . index ) ++ ; if ( preLN [ index ] == ' N ' ) { temp . left = constructTreeUtil ( pre , preLN , index_ptr , n , temp . left ) ; temp . right = constructTreeUtil ( pre , preLN , index_ptr , n , temp . right ) ; } return temp ; } public virtual Node constructTree ( int [ ] pre , char [ ] preLN , int n , Node node ) { int index = 0 ; return constructTreeUtil ( pre , preLN , myindex , n , node ) ; } 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 ( ) ; int [ ] pre = new int [ ] { 10 , 30 , 20 , 5 , 15 } ; char [ ] preLN = new char [ ] { ' N ' , ' N ' , ' L ' , ' L ' , ' L ' } ; int n = pre . Length ; Node mynode = tree . constructTree ( pre , preLN , n , tree . root ) ; Console . WriteLine ( " Following ▁ is ▁ Inorder ▁ Traversal ▁ of ▁ the " + " Constructed ▁ Binary ▁ Tree : ▁ " ) ; tree . printInorder ( mynode ) ; } }


"Construct a string of length L such that each substring of length X has exactly Y distinct letters | C # implementation to construct a string of length L such that each substring of length X has exactly Y distinct letters . ; Initialize p equal to the ASCII value of a ; Iterate till the length of the string ; Driver code"
Solution in C#:

using System ; class GFG { static void String ( int l , int x , int y ) { int p = 97 ; for ( int j = 0 ; j < l ; j ++ ) { char ans = ( char ) ( p + ( j % y ) ) ; Console . Write ( ans ) ; } } public static void Main ( string [ ] args ) { int l = 6 ; int x = 5 ; int y = 3 ; String ( l , x , y ) ; } }


"Construct a string that has exactly K subsequences from given string | C # program for the above approach ; Function that computes the String s ; Length of the given String str ; List that stores all the prime factors of given k ; Find the prime factors ; Initialize the count of each character position as 1 ; Loop until the list becomes empty ; Increase the character count by multiplying it with the prime factor ; If we reach end then again start from beginning ; Store the output ; Print the String ; Driver code ; Given String ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void printSubsequenceString ( String str , int k ) { int n = str . Length ; int i ; List < int > factors = new List < int > ( ) ; for ( i = 2 ; i <= Math . Sqrt ( k ) ; i ++ ) { while ( k % i == 0 ) { factors . Add ( i ) ; k /= i ; } } if ( k > 1 ) factors . Add ( k ) ; int [ ] count = new int [ n ] ; for ( i = 0 ; i < n ; i ++ ) count [ i ] = 1 ; int index = 0 ; while ( factors . Count > 0 ) { count [ index ++ ] *= factors [ factors . Count - 1 ] ; factors . Remove ( factors [ factors . Count - 1 ] ) ; if ( index == n ) index = 0 ; } String s = " " ; for ( i = 0 ; i < n ; i ++ ) { while ( count [ i ] -- > 0 ) { s += str [ i ] ; } } Console . Write ( s ) ; } public static void Main ( String [ ] args ) { String str = " code " ; int k = 20 ; printSubsequenceString ( str , k ) ; } }


"Construct an AP series consisting of A and B having minimum possible Nth term | C # program for the above approach ; Function to check if both a and b are present in the AP series or not ; Iterate over the array arr [ ] ; If a is present ; If b is present ; If both are present ; Otherwise ; Function to print all the elements of the Arithmetic Progression ; Function to construct AP series consisting of A and B with minimum Nth term ; Stores the resultant series ; Initialise ans [ i ] as INT_MAX ; Maintain a smaller than b ; Swap a and b ; Difference between a and b ; Check for all possible combination of start and common difference d ; Initialise arr [ 0 ] as start ; Check if both a and b are present or not and the Nth term is the minimum or not ; Update the answer ; Print the resultant array ; Driver Code ; Function call"
Solution in C#:

using System ; class GFG { static bool check_both_present ( int [ ] arr , int N , int a , int b ) { bool f1 = false , f2 = false ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == a ) { f1 = true ; } if ( arr [ i ] == b ) { f2 = true ; } } if ( f1 && f2 ) { return true ; } else { return false ; } } static void print_array ( int [ ] ans , int N ) { for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( ans [ i ] + " ▁ " ) ; } } static void build_AP ( int N , int a , int b ) { int [ ] arr = new int [ N ] ; int [ ] ans = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) ans [ i ] = int . MaxValue ; if ( a > b ) { a += ( b - ( b = a ) ) ; } int diff = b - a ; for ( int start = 1 ; start <= a ; start ++ ) { for ( int d = 1 ; d <= diff ; d ++ ) { arr [ 0 ] = start ; for ( int i = 1 ; i < N ; i ++ ) { arr [ i ] = arr [ i - 1 ] + d ; } if ( check_both_present ( arr , N , a , b ) && arr [ N - 1 ] < ans [ N - 1 ] ) { for ( int i = 0 ; i < N ; i ++ ) { ans [ i ] = arr [ i ] ; } } } } print_array ( ans , N ) ; } static public void Main ( ) { int N = 5 , A = 20 , B = 50 ; build_AP ( N , A , B ) ; } }


"Construct an Array of Strings having Longest Common Prefix specified by the given Array | C # Program to implement the above approach ; Function to find the array of strings ; Marks the ( N + 1 ) th string ; To generate remaining N strings ; Find i - th string using ( i + 1 ) - th string ; Check if current character is b ; Otherwise ; Insert the string ; Return the answer ; Driver Code ; Print the strings"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static List < string > solve ( int n , int [ ] arr ) { string s = " aaa " ; List < string > ans = new List < string > ( ) ; ans . Add ( s ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( s . Length - 1 >= arr [ i ] ) { char ch = s [ arr [ i ] ] ; if ( ch == ' b ' ) ch = ' a ' ; else ch = ' b ' ; char [ ] chr = s . ToCharArray ( ) ; chr [ arr [ i ] ] = ch ; s = new string ( chr ) ; } ans . Add ( s ) ; } return ans ; } public static void Main ( ) { int [ ] arr = { 2 , 0 , 3 } ; int n = arr . Length ; List < string > ans = solve ( n , arr ) ; for ( int i = ans . Count - 1 ; i >= 0 ; i -- ) { Console . WriteLine ( ans [ i ] ) ; } } }


"Construct an Array of size N in which sum of odd elements is equal to sum of even elements | C # program to Create an array of size N consisting of distinct elements where sum of odd elements is equal to sum of even elements ; Function to construct the required array ; To confirst half , distinct even numbers ; To consecond half , distinct odd numbers ; Calculate the last number of second half so as to make both the halves equal ; Function to construct the required array ; check if size is multiple of 4 then array exist ; function call to conarray ; Driver code"
Solution in C#:

 using System ; class GFG { static void arrayConstruct ( int N ) { for ( int i = 2 ; i <= N ; i = i + 2 ) Console . Write ( i + " ▁ " ) ; for ( int i = 1 ; i < N - 1 ; i = i + 2 ) Console . Write ( i + " ▁ " ) ; Console . Write ( N - 1 + ( N / 2 ) + "
 " ) ; } static void createArray ( int N ) { if ( N % 4 == 0 ) arrayConstruct ( N ) ; else Console . Write ( - 1 + "
 " ) ; } public static void Main ( String [ ] args ) { int N = 8 ; createArray ( N ) ; } }


"Construct an Array of size N whose sum of cube of all elements is a perfect square | C # implementation of the above approach ; Function to construct an array of size N ; Prints the first N natural numbers ; Driver Code"
Solution in C#:

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