"ZigZag Level Order Traversal of an N | C # program for the above approach ; Structure of a tree node ; Function to create a new node ; Function to perform zig zag traversal of the given tree ; Stores the vectors containing nodes in each level of tree respectively ; Create a queue for BFS ; Enqueue Root of the tree ; Standard Level Order Traversal code using queue ; Stores the element in the current level ; Iterate over all nodes of the current level ; Insert all children of the current node into the queue ; Insert curLevel into result ; Loop to Print the ZigZag Level order Traversal of the given tree ; If i + 1 is even reverse the order of nodes in the current level ; Print the node of ith level ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { class Node { public int val ; public List < Node > child ; public Node ( int key ) { val = key ; child = new List < Node > ( ) ; } } static Node newNode ( int key ) { Node temp = new Node ( key ) ; return temp ; } static void zigzagLevelOrder ( Node root ) { if ( root == null ) return ; List < List < int > > result = new List < List < int > > ( ) ; List < Node > q = new List < Node > ( ) ; q . Add ( root ) ; while ( q . Count > 0 ) { int size = q . Count ; List < int > curLevel = new List < int > ( ) ; for ( int i = 0 ; i < size ; i ++ ) { Node node = q [ 0 ] ; q . RemoveAt ( 0 ) ; curLevel . Add ( node . val ) ; for ( int j = 0 ; j < ( node . child ) . Count ; j ++ ) q . Add ( node . child [ j ] ) ; } result . Add ( curLevel ) ; } for ( int i = 0 ; i < result . Count ; i ++ ) { if ( ( i + 1 ) % 2 == 0 ) { result [ i ] . Reverse ( ) ; } for ( int j = 0 ; j < result [ i ] . Count ; j ++ ) Console . Write ( result [ i ] [ j ] + " ▁ " ) ; Console . WriteLine ( ) ; } } static void Main ( ) { Node root = newNode ( 1 ) ; ( root . child ) . Add ( newNode ( 2 ) ) ; ( root . child ) . Add ( newNode ( 3 ) ) ; ( root . child [ 0 ] . child ) . Add ( newNode ( 4 ) ) ; ( root . child [ 0 ] . child ) . Add ( newNode ( 5 ) ) ; ( root . child [ 1 ] . child ) . Add ( newNode ( 6 ) ) ; ( root . child [ 1 ] ) . child . Add ( newNode ( 7 ) ) ; ( root . child [ 1 ] . child ) . Add ( newNode ( 8 ) ) ; zigzagLevelOrder ( root ) ; } }


"Zigzag ( or diagonal ) traversal of Matrix |  ; we will use a 2D vector to store the diagonals of our array the 2D vector will have ( n + m - 1 ) rows that is equal to the number of diagnols ; Driver code ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { public static int R = 5 , C = 4 ; public static void diagonalOrder ( int [ , ] arr , int n , int m ) { List < List < int > > ans = new List < List < int > > ( n + m - 1 ) ; for ( int i = 0 ; i < n + m - 1 ; i ++ ) { ans . Add ( new List < int > ( ) ) ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { ( ans [ i + j ] ) . Add ( arr [ i , j ] ) ; } } for ( int i = 0 ; i < ans . Count ; i ++ ) { for ( int j = ans [ i ] . Count - 1 ; j >= 0 ; j -- ) { Console . Write ( ans [ i ] [ j ] + " ▁ " ) ; } Console . WriteLine ( ) ; } } static public void Main ( ) { int n = 5 , m = 4 ; int [ , ] arr = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } , { 17 , 18 , 19 , 20 } , } ; diagonalOrder ( arr , n , m ) ; } }


"Zuckerman Numbers | C # implementation to check if N is a Zuckerman number ; Function to get product of digits ; Function to check if N is an Zuckerman number ; Driver code"
Solution in C#:

using System ; class GFG { static int getProduct ( int n ) { int product = 1 ; while ( n != 0 ) { product = product * ( n % 10 ) ; n = n / 10 ; } return product ; } static bool isZuckerman ( int n ) { return n % getProduct ( n ) == 0 ; } public static void Main ( String [ ] args ) { int n = 115 ; if ( isZuckerman ( n ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }


"Zygodrome Number | C # implementation to check if N is a zygodrome number ; Function to check if N is an zygodrome number ; convert N to string ; Adding a space at the beginning and end of the string ; Traverse the string ; If any character is not same as prev and next then return false ; Driver code"
Solution in C#:

using System ; class GFG { static bool iszygodromeNum ( int N ) { String s = N . ToString ( ) ; s = ' ▁ ' + s + ' ▁ ' ; for ( int i = 1 ; i < s . Length - 1 ; i ++ ) { if ( s [ i ] != s [ i - 1 ] && s [ i ] != s [ i + 1 ] ) { return false ; } } return true ; } public static void Main ( String [ ] args ) { int n = 1122 ; if ( iszygodromeNum ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"a | C # program to remove consecutive duplicates from a given string . ; A iterative function that removes consecutive duplicates from string S ; We don 't need to do anything for  empty or single character string. ; j is used to store index is result string ( or index of current distinct character ) ; Traversing string ; If current character S [ i ] is different from S [ j ] ; Driver code"
Solution in C#:

using System ; class GFG { static void removeDuplicates ( char [ ] S ) { int n = S . Length ; if ( n < 2 ) { return ; } int j = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( S [ j ] != S [ i ] ) { j ++ ; S [ j ] = S [ i ] ; } } char [ ] A = new char [ j + 1 ] ; Array . Copy ( S , 0 , A , 0 , j + 1 ) ; Console . WriteLine ( A ) ; } public static void Main ( String [ ] args ) { char [ ] S1 = " geeksforgeeks " . ToCharArray ( ) ; removeDuplicates ( S1 ) ; char [ ] S2 = " aabcca " . ToCharArray ( ) ; removeDuplicates ( S2 ) ; } }


"k smallest elements in same order using O ( 1 ) extra space | C # for printing smallest k numbers in order ; Function to print smallest k numbers in arr [ 0. . n - 1 ] ; For each arr [ i ] find whether it is a part of n - smallest with insertion sort concept ; Find largest from top n - element ; If largest is greater than arr [ i ] shift all element one place left ; make arr [ k - 1 ] = arr [ i ] ; print result ; Driver function"
Solution in C#:

using System ; public class GfG { public static void printSmall ( int [ ] arr , int n , int k ) { for ( int i = k ; i < n ; ++ i ) { int max_var = arr [ k - 1 ] ; int pos = k - 1 ; for ( int j = k - 2 ; j >= 0 ; j -- ) { if ( arr [ j ] > max_var ) { max_var = arr [ j ] ; pos = j ; } } if ( max_var > arr [ i ] ) { int j = pos ; while ( j < k - 1 ) { arr [ j ] = arr [ j + 1 ] ; j ++ ; } arr [ k - 1 ] = arr [ i ] ; } } for ( int i = 0 ; i < k ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } public static void Main ( ) { int [ ] arr = { 1 , 5 , 8 , 9 , 6 , 7 , 3 , 4 , 2 , 0 } ; int n = 10 ; int k = 5 ; printSmall ( arr , n , k ) ; } }


"kth smallest / largest in a small range unsorted array | C # program of kth smallest / largest in a small range unsorted array ; Storing counts of elements ; Traverse hash array build above until we reach k - th smallest element . ; Driver code"
Solution in C#:

using System ; using System . Linq ; class GFG { static int maxs = 1000001 ; static int kthSmallestLargest ( int [ ] arr , int n , int k ) { int max_val = arr . Max ( ) ; int [ ] hash = new int [ max_val + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { hash [ arr [ i ] ] ++ ; } int count = 0 ; for ( int i = 0 ; i <= max_val ; i ++ ) { while ( hash [ i ] > 0 ) { count ++ ; if ( count == k ) { return i ; } hash [ i ] -- ; } } return - 1 ; } public static void Main ( ) { int [ ] arr = { 11 , 6 , 2 , 9 , 4 , 3 , 16 } ; int n = arr . Length , k = 3 ; Console . WriteLine ( " kth ▁ smallest ▁ number ▁ is : ▁ " + kthSmallestLargest ( arr , n , k ) ) ; } }


"m Coloring Problem | Backtracking |  ; Number of vertices in the graph ; A utility function to print solution ; check if the colored graph is safe or not ; check for every edge ; This function solves the m Coloring problem using recursion . It returns false if the m colours cannot be assigned , otherwise , return true and prints assignments of colours to all vertices . Please note that there may be more than one solutions , this function prints one of the feasible solutions . ; if current index reached end ; if coloring is safe ; Print the solution ; Assign each color from 1 to m ; Recur of the rest vertices ; Driver code ; Create following graph and test whether it is 3 colorable ( 3 ) -- - ( 2 ) | / | | / | | / | ( 0 ) -- - ( 1 ) ; Number of colors ; Initialize all color values as 0. This initialization is needed correct functioning of isSafe ( )"
Solution in C#:

using System ; class GFG { static int V = 4 ; static void printSolution ( int [ ] color ) { Console . WriteLine ( " Solution ▁ Exists : " + " ▁ Following ▁ are ▁ the ▁ assigned ▁ colors ▁ " ) ; for ( int i = 0 ; i < V ; i ++ ) Console . Write ( " ▁ " + color [ i ] ) ; Console . WriteLine ( ) ; } static bool isSafe ( bool [ , ] graph , int [ ] color ) { for ( int i = 0 ; i < V ; i ++ ) for ( int j = i + 1 ; j < V ; j ++ ) if ( graph [ i , j ] && color [ j ] == color [ i ] ) return false ; return true ; } static bool graphColoring ( bool [ , ] graph , int m , int i , int [ ] color ) { if ( i == V ) { if ( isSafe ( graph , color ) ) { printSolution ( color ) ; return true ; } return false ; } for ( int j = 1 ; j <= m ; j ++ ) { color [ i ] = j ; if ( graphColoring ( graph , m , i + 1 , color ) ) return true ; color [ i ] = 0 ; } return false ; } static void Main ( ) { bool [ , ] graph = { { false , true , true , true } , { true , false , true , false } , { true , true , false , true } , { true , false , true , false } , } ; int m = 3 ; int [ ] color = new int [ V ] ; for ( int i = 0 ; i < V ; i ++ ) color [ i ] = 0 ; if ( ! graphColoring ( graph , m , 0 , color ) ) Console . WriteLine ( " Solution ▁ does ▁ not ▁ exist " ) ; } }


"m Coloring Problem | Backtracking | C # program for the above approach ; A node class which stores the color and the edges connected to the node ; Create a visited array of n nodes , initialized to zero ; maxColors used till now are 1 as all nodes are painted color 1 ; Do a full BFS traversal from all unvisited starting points ; If the starting point is unvisited , mark it visited and push it in queue ; BFS Travel starts here ; Checking all adjacent nodes to " top " edge in our queue ; IMPORTANT : If the color of the adjacent node is same , increase it by 1 ; If number of colors used shoots m , return 0 ; If the adjacent node is not visited , mark it visited and push it in queue ; Driver code ; Create a vector of n + 1 nodes of type " node " The zeroth position is just dummy ( 1 to n to be used ) ; Add edges to each node as per given input ; Connect the undirected graph ; Display final answer"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class GFG { class node { public int color = 1 ; public HashSet < int > edges = new HashSet < int > ( ) ; } ; static int canPaint ( List < node > nodes , int n , int m ) { List < int > visited = new List < int > ( ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) { visited . Add ( 0 ) ; } int maxColors = 1 ; for ( int sv = 1 ; sv <= n ; sv ++ ) { if ( visited [ sv ] > 0 ) continue ; visited [ sv ] = 1 ; Queue q = new Queue ( ) ; q . Enqueue ( sv ) ; while ( q . Count != 0 ) { int top = ( int ) q . Peek ( ) ; q . Dequeue ( ) ; foreach ( int it in nodes [ top ] . edges ) { if ( nodes [ top ] . color == nodes [ it ] . color ) nodes [ it ] . color += 1 ; maxColors = Math . Max ( maxColors , Math . Max ( nodes [ top ] . color , nodes [ it ] . color ) ) ; if ( maxColors > m ) return 0 ; if ( visited [ it ] == 0 ) { visited [ it ] = 1 ; q . Enqueue ( it ) ; } } } } return 1 ; } public static void Main ( ) { int n = 4 ; int [ , ] graph = { { 0 , 1 , 1 , 1 } , { 1 , 0 , 1 , 0 } , { 1 , 1 , 0 , 1 } , { 1 , 0 , 1 , 0 } } ; List < node > nodes = new List < node > ( ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) { nodes . Add ( new node ( ) ) ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( graph [ i , j ] > 0 ) { nodes [ i ] . edges . Add ( i ) ; nodes [ j ] . edges . Add ( j ) ; } } } Console . WriteLine ( canPaint ( nodes , n , m ) ) ; } }


"m Coloring Problem | Backtracking | C # program for the above approach ; A node class which stores the color and the edges connected to the node ; Create a visited array of n nodes , initialized to zero ; maxColors used till now are 1 as all nodes are painted color 1 ; Do a full BFS traversal from all unvisited starting points ; If the starting point is unvisited , mark it visited and push it in queue ; BFS Travel starts here ; Checking all adjacent nodes to " top " edge in our queue ; IMPORTANT : If the color of the adjacent node is same , increase it by 1 ; If number of colors used shoots m , return 0 ; If the adjacent node is not visited , mark it visited and push it in queue ; Driver code ; Number of colors ; Create a vector of n + 1 nodes of type " node " The zeroth position is just dummy ( 1 to n to be used ) ; Add edges to each node as per given input ; Connect the undirected graph ; Display final answer"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class GFG { class node { public int color = 1 ; public HashSet < int > edges = new HashSet < int > ( ) ; } ; static int canPaint ( List < node > nodes , int n , int m ) { List < int > visited = new List < int > ( ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) { visited . Add ( 0 ) ; } int maxColors = 1 ; for ( int sv = 1 ; sv <= n ; sv ++ ) { if ( visited [ sv ] > 0 ) continue ; visited [ sv ] = 1 ; Queue q = new Queue ( ) ; q . Enqueue ( sv ) ; while ( q . Count != 0 ) { int top = ( int ) q . Peek ( ) ; q . Dequeue ( ) ; foreach ( int it in nodes [ top ] . edges ) { if ( nodes [ top ] . color == nodes [ it ] . color ) nodes [ it ] . color += 1 ; maxColors = Math . Max ( maxColors , Math . Max ( nodes [ top ] . color , nodes [ it ] . color ) ) ; if ( maxColors > m ) return 0 ; if ( visited [ it ] == 0 ) { visited [ it ] = 1 ; q . Enqueue ( it ) ; } } } } return 1 ; } public static void Main ( ) { int n = 4 ; int [ , ] graph = { { 0 , 1 , 1 , 1 } , { 1 , 0 , 1 , 0 } , { 1 , 1 , 0 , 1 } , { 1 , 0 , 1 , 0 } } ; int m = 3 ; List < node > nodes = new List < node > ( ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) { nodes . Add ( new node ( ) ) ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( graph [ i , j ] > 0 ) { nodes [ i ] . edges . Add ( i ) ; nodes [ j ] . edges . Add ( j ) ; } } } Console . WriteLine ( canPaint ( nodes , n , m ) ) ; } }


"n | C # code to find nth number with digits 0 , 1 , 2 , 3 , 4 , 5 ; If the Number is less than 6 return the number as it is . ; Call the function again and again the get the desired result . And convert the number to base 6. ; Decrease the Number by 1 and Call ans function to convert N to base 6 ; Driver code"
Solution in C#:

using System ; public class GFG { static int ans ( int n ) { if ( n < 6 ) { return n ; } return n % 6 + 10 * ( ans ( n / 6 ) ) ; } static int getSpecialNumber ( int N ) { return ans ( -- N ) ; } public static void Main ( String [ ] args ) { int N = 17 ; int answer = getSpecialNumber ( N ) ; Console . WriteLine ( answer ) ; } }


"n | C # code to find nth number with digits 0 , 1 , 2 , 3 , 4 , 5 ; function to convert num to base 6 ; Driver code ; initialize an array to 0 ; function calling to convert number n to base 6 ; if size is zero then return zero"
Solution in C#:

using System ; class GFG { static int max = 100000 ; static int baseconversion ( int [ ] arr , int num , int bas ) { int i = 0 , rem ; if ( num == 0 ) { return 0 ; } while ( num > 0 ) { rem = num % bas ; arr [ i ++ ] = rem ; num /= bas ; } return i ; } public static void Main ( ) { int [ ] arr = new int [ max ] ; int n = 10 ; int size = baseconversion ( arr , n - 1 , 6 ) ; if ( size == 0 ) Console . Write ( size ) ; for ( int i = size - 1 ; i >= 0 ; i -- ) { Console . Write ( arr [ i ] ) ; } } }


"n | C # program to find n - th number with sum of digits as 10. ; Find sum of digits in current no . ; If sum is 10 , we increment count ; If count becomes n , we return current number . ; Driver Code"
Solution in C#:

using System ; class GFG { public static int findNth ( int n ) { int count = 0 ; for ( int curr = 19 ; ; curr += 9 ) { int sum = 0 ; for ( int x = curr ; x > 0 ; x = x / 10 ) sum = sum + x % 10 ; if ( sum == 10 ) count ++ ; if ( count == n ) return curr ; } } static public void Main ( ) { Console . WriteLine ( findNth ( 5 ) ) ; } }


"n | C # program to find n - th term of series ; Function to find the nth term of series ; Loop to add 4 th powers ; Driver code"
Solution in C#:

using System ; class GFG { static int sumOfSeries ( int n ) { int ans = 0 ; for ( int i = 1 ; i <= n ; i ++ ) ans += i * i * i * i ; return ans ; } public static void Main ( ) { int n = 4 ; Console . WriteLine ( sumOfSeries ( n ) ) ; } }


"n | C # program to find n - th term of the series 2 , 12 , 36 , 80 , 150 , . . ; Returns n - th term of the series 2 , 12 , 36 , 80 , 150 ; Driver code"
Solution in C#:

using System ; class GFG { public static int nthTerm ( int n ) { return ( n * n ) + ( n * n * n ) ; } public static void Main ( ) { int n = 4 ; Console . WriteLine ( nthTerm ( n ) ) ; } }


"n | C # program to find the n - th term in series ; Function to find nth term ; Driver Code"
Solution in C#:

using System ; class Series { static int sumOfSeries ( int n ) { return n * ( n + 1 ) * ( 6 * n * n * n + 9 * n * n + n - 1 ) / 30 ; } public static void Main ( ) { int n = 4 ; Console . WriteLine ( sumOfSeries ( n ) ) ; } }


"s | C # program for the above approach ; To sort the array and return the answer ; Sort the array ; Fill all stated with - 1 when only one element ; As dp [ 0 ] = 0 ( base case ) so min no of elements to be removed are n - 1 elements ; Iterate from 1 to n - 1 ; Driver code"
Solution in C#:

using System ; class GFG { static int removals ( int [ ] arr , int n , int k ) { Array . Sort ( arr ) ; int [ ] dp = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) dp [ i ] = - 1 ; int ans = n - 1 ; dp [ 0 ] = 0 ; for ( int i = 1 ; i < n ; i ++ ) { dp [ i ] = i ; int j = dp [ i - 1 ] ; while ( j != i && arr [ i ] - arr [ j ] > k ) { j ++ ; } dp [ i ] = Math . Min ( dp [ i ] , j ) ; ans = Math . Min ( ans , ( n - ( i - j + 1 ) ) ) ; } return ans ; } static public void Main ( ) { int [ ] a = { 1 , 3 , 4 , 9 , 10 , 11 , 12 , 17 , 20 } ; int n = a . Length ; int k = 4 ; Console . Write ( removals ( a , n , k ) ) ; } }