"Create loops of even and odd values in a binary tree | C # implementation to create odd and even loops in a binary tree ; structure of a node ; Utility function to create a new node ; preorder traversal to place the node pointer in the respective even_ptrs or odd_ptrs list ; place node ptr in even_ptrs list if node contains even number ; else place node ptr in odd_ptrs list ; function to create the even and odd loops ; forming even loop ; for the last element ; Similarly forming odd loop ; traversing the loop from any random node in the loop ; Driver code ; Binary tree formation ; traversing odd loop from any random odd node ; traversing even loop from any random even node"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { public class Node { public int data ; public Node left , right , abtr ; } ; static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = node . abtr = null ; return node ; } static List < Node > even_ptrs = new List < Node > ( ) ; static List < Node > odd_ptrs = new List < Node > ( ) ; static void preorderTraversal ( Node root ) { if ( root == null ) return ; if ( root . data % 2 == 0 ) ( even_ptrs ) . Add ( root ) ; else ( odd_ptrs ) . Add ( root ) ; preorderTraversal ( root . left ) ; preorderTraversal ( root . right ) ; } static void createLoops ( Node root ) { preorderTraversal ( root ) ; int i ; for ( i = 1 ; i < even_ptrs . Count ; i ++ ) even_ptrs [ i - 1 ] . abtr = even_ptrs [ i ] ; even_ptrs [ i - 1 ] . abtr = even_ptrs [ 0 ] ; for ( i = 1 ; i < odd_ptrs . Count ; i ++ ) odd_ptrs [ i - 1 ] . abtr = odd_ptrs [ i ] ; odd_ptrs [ i - 1 ] . abtr = odd_ptrs [ 0 ] ; } static void traverseLoop ( Node start ) { Node curr = start ; do { Console . Write ( curr . data + " ▁ " ) ; curr = curr . abtr ; } while ( curr != start ) ; } public static void Main ( String [ ] args ) { Node root = null ; root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; root . right . left = newNode ( 6 ) ; root . right . right = newNode ( 7 ) ; createLoops ( root ) ; Console . Write ( " Odd ▁ nodes : ▁ " ) ; traverseLoop ( root . right ) ; Console . Write ( "
 Even ▁ nodes : ▁ " ) ; traverseLoop ( root . left ) ; } }


"Create matrix whose sum of diagonals in each sub matrix is even | C # program for the above approach ; Function to print N * N order matrix with all sub - matrix of even order is sum of its diagonal also even ; Even index ; Odd index ; Iterate two nested loop ; For even index the element should be consecutive odd ; For odd index the element should be consecutive even ; Driver code ; Given order of matrix ; Function call"
Solution in C#:

using System ; class GFG { static void evenSubMatrix ( int N ) { int even = 1 ; int odd = 2 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( ( i + j ) % 2 == 0 ) { Console . Write ( even + " ▁ " ) ; even += 2 ; } else { Console . Write ( odd + " ▁ " ) ; odd += 2 ; } } Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { int N = 4 ; evenSubMatrix ( N ) ; } }


"Create new linked list from two given linked list with greater element at each node | C # program to create a new linked list from two given linked list of the same size with the greater element among the two at each node ; Representation of node ; Function to insert node in a linked list ; Function which returns new linked list ; Compare for greater node ; Driver code ; First linked list ; Second linked list"
Solution in C#:

using System ; class GFG { public class Node { public int data ; public Node next ; } ; static Node insert ( Node root , int item ) { Node ptr , temp ; temp = new Node ( ) ; temp . data = item ; temp . next = null ; if ( root == null ) root = temp ; else { ptr = root ; while ( ptr . next != null ) ptr = ptr . next ; ptr . next = temp ; } return root ; } static Node newList ( Node root1 , Node root2 ) { Node ptr1 = root1 , ptr2 = root2 , ptr ; Node root = null , temp ; while ( ptr1 != null ) { temp = new Node ( ) ; temp . next = null ; if ( ptr1 . data < ptr2 . data ) temp . data = ptr2 . data ; else temp . data = ptr1 . data ; if ( root == null ) root = temp ; else { ptr = root ; while ( ptr . next != null ) ptr = ptr . next ; ptr . next = temp ; } ptr1 = ptr1 . next ; ptr2 = ptr2 . next ; } return root ; } static void display ( Node root ) { while ( root != null ) { Console . Write ( root . data + " - > " ) ; root = root . next ; } Console . WriteLine ( ) ; } public static void Main ( String [ ] args ) { Node root1 = null , root2 = null , root = null ; root1 = insert ( root1 , 5 ) ; root1 = insert ( root1 , 2 ) ; root1 = insert ( root1 , 3 ) ; root1 = insert ( root1 , 8 ) ; Console . Write ( " First ▁ List : ▁ " ) ; display ( root1 ) ; root2 = insert ( root2 , 1 ) ; root2 = insert ( root2 , 7 ) ; root2 = insert ( root2 , 4 ) ; root2 = insert ( root2 , 5 ) ; Console . Write ( " Second ▁ List : ▁ " ) ; display ( root2 ) ; root = newList ( root1 , root2 ) ; Console . Write ( " New ▁ List : ▁ " ) ; display ( root ) ; } }


"Creating a tree with Left | C # program to create a tree with left child right sibling representation . ; Creating new Node ; Adds a sibling to a list with starting with n ; Add child Node to a Node ; Check if child is not empty . ; Traverses tree in depth first order ; Driver code"
Solution in C#:

using System ; class GFG { public class NodeTemp { public int data ; public NodeTemp next , child ; public NodeTemp ( int data ) { this . data = data ; next = child = null ; } } public static NodeTemp addSibling ( NodeTemp node , int data ) { if ( node == null ) { return null ; } while ( node . next != null ) { node = node . next ; } return ( node . next = new NodeTemp ( data ) ) ; } public static NodeTemp addChild ( NodeTemp node , int data ) { if ( node == null ) { return null ; } if ( node . child != null ) { return ( addSibling ( node . child , data ) ) ; } else { return ( node . child = new NodeTemp ( data ) ) ; } } public static void traverseTree ( NodeTemp root ) { if ( root == null ) { return ; } while ( root != null ) { Console . Write ( root . data + " ▁ " ) ; if ( root . child != null ) { traverseTree ( root . child ) ; } root = root . next ; } } public static void Main ( string [ ] args ) { NodeTemp root = new NodeTemp ( 10 ) ; NodeTemp n1 = addChild ( root , 2 ) ; NodeTemp n2 = addChild ( root , 3 ) ; NodeTemp n3 = addChild ( root , 4 ) ; NodeTemp n4 = addChild ( n3 , 6 ) ; NodeTemp n5 = addChild ( root , 5 ) ; NodeTemp n6 = addChild ( n5 , 7 ) ; NodeTemp n7 = addChild ( n5 , 8 ) ; NodeTemp n8 = addChild ( n5 , 9 ) ; traverseTree ( root ) ; } }


"Creating a tree with Left | C # program to create a tree with left child right sibling representation . ; Creating new Node ; Adds a sibling to a list with starting with n ; Add child Node to a Node ; Check if child list is not empty . ; Traverses tree in level order ; Corner cases ; Create a queue and enque root ; Take out an item from the queue ; Print next level of taken out item and enque next level 's children ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public class Node { public int data ; public Node next ; public Node child ; } ; static Node newNode ( int data ) { Node newNode = new Node ( ) ; newNode . next = newNode . child = null ; newNode . data = data ; return newNode ; } static Node addSibling ( Node n , int data ) { if ( n == null ) return null ; while ( n . next != null ) n = n . next ; return ( n . next = newNode ( data ) ) ; } static Node addChild ( Node n , int data ) { if ( n == null ) return null ; if ( n . child != null ) return addSibling ( n . child , data ) ; else return ( n . child = newNode ( data ) ) ; } static void traverseTree ( Node root ) { if ( root == null ) return ; Console . Write ( root . data + " ▁ " ) ; if ( root . child == null ) return ; Queue < Node > q = new Queue < Node > ( ) ; Node curr = root . child ; q . Enqueue ( curr ) ; while ( q . Count != 0 ) { curr = q . Peek ( ) ; q . Dequeue ( ) ; while ( curr != null ) { Console . Write ( curr . data + " ▁ " ) ; if ( curr . child != null ) { q . Enqueue ( curr . child ) ; } curr = curr . next ; } } } public static void Main ( String [ ] args ) { Node root = newNode ( 10 ) ; Node n1 = addChild ( root , 2 ) ; Node n2 = addChild ( root , 3 ) ; Node n3 = addChild ( root , 4 ) ; Node n4 = addChild ( n3 , 6 ) ; Node n5 = addChild ( root , 5 ) ; Node n6 = addChild ( n5 , 7 ) ; Node n7 = addChild ( n5 , 8 ) ; Node n8 = addChild ( n5 , 9 ) ; traverseTree ( root ) ; } }


"Cube Free Numbers smaller than n | C # Program to print all cube free numbers smaller than or equal to n . ; Returns true if n is a cube free number , else returns false . ; check for all possible divisible cubes ; Print all cube free numbers smaller than n . ; Driver program"
Solution in C#:

using System ; class GFG { static bool isCubeFree ( int n ) { if ( n == 1 ) return false ; for ( int i = 2 ; i * i * i <= n ; i ++ ) if ( n % ( i * i * i ) == 0 ) return false ; return true ; } static void printCubeFree ( int n ) { for ( int i = 2 ; i <= n ; i ++ ) { if ( isCubeFree ( i ) ) { Console . Write ( i + " ▁ " ) ; } } } public static void Main ( ) { int n = 20 ; printCubeFree ( n ) ; } }


"Cube Free Numbers smaller than n | Efficient C # Program to print all cube free numbers smaller than or equal to n . ; Initialize all numbers as not cube free ; Traverse through all possible cube roots ; If i itself is cube free ; Mark all multiples of i as not cube free ; Print all cube free numbers ; Driver Code"
Solution in C#:

using System ; class GFG { public static void printCubeFree ( int n ) { bool [ ] cubFree = new bool [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) cubFree [ i ] = true ; for ( int i = 2 ; i * i * i <= n ; i ++ ) { if ( cubFree [ i ] ) { for ( int multiple = 1 ; i * i * i * multiple <= n ; multiple ++ ) { cubFree [ i * i * i * multiple ] = false ; } } } for ( int i = 2 ; i <= n ; i ++ ) { if ( cubFree [ i ] == true ) Console . Write ( i + " ▁ " ) ; } } public static void Main ( ) { printCubeFree ( 20 ) ; } }


"Cuckoo Hashing | C # program to demonstrate working of Cuckoo hashing . ; upper bound on number of elements in our set ; choices for position ; Auxiliary space bounded by a small multiple of MAXN , minimizing wastage ; Array to store possible positions for a key ; function to fill hash table with dummy value * dummy value : INT_MIN * number of hashtables : ver ; return hashed value for a key * function : ID of hash function according to which key has to hashed * key : item to be hashed ; function to place a key in one of its possible positions * tableID : table in which key has to be placed , also equal to function according to which key must be hashed * cnt : number of times function has already been called in order to place the first input key * n : maximum number of times function can be recursively called before stopping and declaring presence of cycle ; if function has been recursively called max number of times , stop and declare cycle . Rehash . ; calculate and store possible positions * for the key . Check if key already present at any of the positions . If YES , return . ; check if another key is already present at the position for the new key in the table * If YES : place the new key in its position * and place the older key in an alternate position for it in the next table ; else : place the new key in its position ; function to print hash table contents ; function for Cuckoo - hashing keys * keys [ ] : input array of keys * n : size of input array ; initialize hash tables to a dummy value ( INT - MIN ) indicating empty position ; start with placing every key at its position in the first hash table according to first hash function ; print the final hash tables ; Driver Code ; following array doesn 't have  any cycles and hence all keys  will be inserted without any rehashing  ; following array has a cycle and hence we will have to rehash to position every key"
Solution in C#:

 using System ; class GFG { static int MAXN = 11 ; static int ver = 2 ; static int [ , ] hashtable = new int [ ver , MAXN ] ; static int [ ] pos = new int [ ver ] ; static void initTable ( ) { for ( int j = 0 ; j < MAXN ; j ++ ) for ( int i = 0 ; i < ver ; i ++ ) hashtable [ i , j ] = int . MinValue ; } static int hash ( int function , int key ) { switch ( function ) { case 1 : return key % MAXN ; case 2 : return ( key / MAXN ) % MAXN ; } return int . MinValue ; } static void place ( int key , int tableID , int cnt , int n ) { if ( cnt == n ) { Console . Write ( " { 0 } ▁ unpositioned
 " , key ) ; Console . Write ( " Cycle ▁ present . ▁ REHASH .
 " ) ; return ; } for ( int i = 0 ; i < ver ; i ++ ) { pos [ i ] = hash ( i + 1 , key ) ; if ( hashtable [ i , pos [ i ] ] == key ) return ; } if ( hashtable [ tableID , pos [ tableID ] ] != int . MinValue ) { int dis = hashtable [ tableID , pos [ tableID ] ] ; hashtable [ tableID , pos [ tableID ] ] = key ; place ( dis , ( tableID + 1 ) % ver , cnt + 1 , n ) ; } else hashtable [ tableID , pos [ tableID ] ] = key ; } static void printTable ( ) { Console . Write ( " Final ▁ hash ▁ tables :
 " ) ; for ( int i = 0 ; i < ver ; i ++ , Console . Write ( "
 " ) ) for ( int j = 0 ; j < MAXN ; j ++ ) if ( hashtable [ i , j ] == int . MinValue ) Console . Write ( " - ▁ " ) ; else Console . Write ( " { 0 } ▁ " , hashtable [ i , j ] ) ; Console . Write ( "
 " ) ; } static void cuckoo ( int [ ] keys , int n ) { initTable ( ) ; for ( int i = 0 , cnt = 0 ; i < n ; i ++ , cnt = 0 ) place ( keys [ i ] , 0 , cnt , n ) ; printTable ( ) ; } public static void Main ( String [ ] args ) { int [ ] keys_1 = { 20 , 50 , 53 , 75 , 100 , 67 , 105 , 3 , 36 , 39 } ; int n = keys_1 . Length ; cuckoo ( keys_1 , n ) ; int [ ] keys_2 = { 20 , 50 , 53 , 75 , 100 , 67 , 105 , 3 , 36 , 39 , 6 } ; int m = keys_2 . Length ; cuckoo ( keys_2 , m ) ; } }


"Cumulative frequency of count of each element in an unsorted array | C # program to print the cumulative frequency according to the order given ; Function to print the cumulative frequency according to the order given ; Insert elements and their frequencies in hash map . ; traverse in the array ; add the frequencies ; if the element has not been visited previously ; mark the hash 0 as the element 's cumulative frequency  has been printed ; Driver Code"
Solution in C#:

using System ; class GFG { static void countFreq ( int [ ] a , int n ) { int [ ] hm = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) hm [ a [ i ] ] ++ ; int cumul = 0 ; for ( int i = 0 ; i < n ; i ++ ) { cumul += hm [ a [ i ] ] ; if ( hm [ a [ i ] ] != 0 ) { Console . WriteLine ( a [ i ] + " - > " + cumul ) ; } hm [ a [ i ] ] = 0 ; } } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 3 , 2 , 4 , 2 , 1 } ; int n = a . Length ; countFreq ( a , n ) ; } }


"Cumulative product of digits of all numbers in the given range | C # program to print the product of all numbers in range L and R ; Function to get product of digits ; Function to find the product of digits of all natural numbers in range L to R ; Iterate between L to R ; 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 int productinRange ( int l , int r ) { if ( r - l > 9 ) return 0 ; else { int p = 1 ; for ( int i = l ; i <= r ; i ++ ) p *= getProduct ( i ) ; return p ; } } public static void Main ( String [ ] args ) { int l = 11 , r = 15 ; Console . Write ( productinRange ( l , r ) + "
 " ) ; l = 1 ; r = 15 ; Console . Write ( productinRange ( l , r ) ) ; } }


"Cunningham Numbers | C # implementation for the above approach ; Function to check if a number can be expressed as a ^ b . ; Function to check if N is a Cunningham number ; Driver Code ; Given number ; Function Call"
Solution in C#:

using System ; class GFG { static bool isPower ( int a ) { if ( a == 1 ) return true ; for ( int i = 2 ; i * i <= a ; i ++ ) { double val = Math . Log ( a ) / Math . Log ( i ) ; if ( ( val - ( int ) val ) < 0.00000001 ) return true ; } return false ; } static bool isCunningham ( int n ) { return isPower ( n - 1 ) || isPower ( n + 1 ) ; } public static void Main ( string [ ] args ) { int n = 126 ; if ( isCunningham ( n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Cunningham chain | C # Program to print the series of first kind ; Function to print Cunningham chain of the first kind ; Iterate till all elements are printed ; check prime or not ; Driver Code"
Solution in C#:

using System ; class GFG { static void print ( int p0 ) { int p1 , i = 0 , x , flag , k ; while ( true ) { flag = 1 ; x = ( int ) ( Math . Pow ( 2 , i ) ) ; p1 = x * p0 + ( x - 1 ) ; for ( k = 2 ; k < p1 ; k ++ ) { if ( p1 % k == 0 ) { flag = 0 ; break ; } } if ( flag == 0 ) break ; Console . Write ( " ▁ " + p1 ) ; i ++ ; } } public static void Main ( ) { int p0 = 2 ; print ( p0 ) ; } }


"Cunningham chain | C # program for cunningham chain Function to print the series of second kind ; Function to print Cunningham chain of the second kind ; Iterate till all elements are printed ; check prime or not ; Driver Code"
Solution in C#:

using System ; class GFG { static void print ( int p0 ) { int p1 , i = 0 , x , flag , k ; while ( true ) { flag = 1 ; x = ( int ) ( Math . Pow ( 2 , i ) ) ; p1 = x * p0 - ( x - 1 ) ; for ( k = 2 ; k < p1 ; k ++ ) { if ( p1 % k == 0 ) { flag = 0 ; break ; } } if ( flag == 0 ) break ; Console . Write ( p1 + " ▁ " ) ; i ++ ; } } static void Main ( ) { int p0 = 19 ; print ( p0 ) ; } }


"Custom Tree Problem | C # program to create a custom tree from a given set of links . The main class that represents tree and has main method ; Returns an array of trees from links input . Links are assumed to be Strings of the form " < s > ▁ < e > " where < s > and < e > are starting and ending points for the link . The returned array is of size 26 and has non - null values at indexes corresponding to roots of trees in output ; Create two arrays for nodes and forest ; Process each link ; Find the two ends of current link ; Start node ; End node ; If start of link not seen before , add it two both arrays ; Note that it may be removed later when this character is last character of a link . For example , let we first see a -- -> b , then c -- -> a . We first add ' a ' to array of trees and when we see link c -- -> a , we remove it from trees array . ; If end of link is not seen before , add it to the nodes array ; If end of link is seen before , remove it from forest if it exists there . ; Establish Parent - Child Relationship between Start and End ; Constructor ; Driver Code ; Class to represent a tree node ; Adds a child ' n ' to this node ; Constructor ; Recursive method to print indented tree rooted with this node ."
Solution in C#:

using System ; class Tree { public TreeNode root ; public Tree [ ] buildFromLinks ( String [ ] links ) { TreeNode [ ] nodes = new TreeNode [ 26 ] ; Tree [ ] forest = new Tree [ 26 ] ; foreach ( String link in links ) { char [ ] sep = { ' ▁ ' , ' ▁ ' } ; String [ ] ends = link . Split ( sep ) ; int start = ( int ) ( ends [ 0 ] [ 0 ] - ' a ' ) ; int end = ( int ) ( ends [ 1 ] [ 0 ] - ' a ' ) ; if ( nodes [ start ] == null ) { nodes [ start ] = new TreeNode ( ( char ) ( start + ' a ' ) ) ; forest [ start ] = new Tree ( nodes [ start ] ) ; } if ( nodes [ end ] == null ) nodes [ end ] = new TreeNode ( ( char ) ( end + ' a ' ) ) ; else forest [ end ] = null ; nodes [ start ] . addChild ( nodes [ end ] , end ) ; } return forest ; } public Tree ( TreeNode root ) { this . root = root ; } public static void printForest ( String [ ] links ) { Tree t = new Tree ( new TreeNode ( ' \0' ) ) ; foreach ( Tree t1 in t . buildFromLinks ( links ) ) { if ( t1 != null ) { t1 . root . printTreeIdented ( " " ) ; Console . WriteLine ( " " ) ; } } } public static void Main ( String [ ] args ) { String [ ] links1 = { " a ▁ b " , " b ▁ c " , " b ▁ d " , " a ▁ e " } ; Console . WriteLine ( " - - - - - - - - - - - - ▁ Forest ▁ 1 ▁ - - - - - - - - - - - - - - - - " ) ; printForest ( links1 ) ; String [ ] links2 = { " a ▁ b " , " a ▁ g " , " b ▁ c " , " c ▁ d " , " d ▁ e " , " c ▁ f " , " z ▁ y " , " y ▁ x " , " x ▁ w " } ; Console . WriteLine ( " - - - - - - - - - - - - ▁ Forest ▁ 2 ▁ - - - - - - - - - - - - - - - - " ) ; printForest ( links2 ) ; } } public class TreeNode { TreeNode [ ] children ; char c ; public void addChild ( TreeNode n , int index ) { this . children [ index ] = n ; } public TreeNode ( char c ) { this . c = c ; this . children = new TreeNode [ 26 ] ; } public void printTreeIdented ( String indent ) { Console . WriteLine ( indent + " - - > " + c ) ; foreach ( TreeNode child in children ) { if ( child != null ) child . printTreeIdented ( indent + " ▁ " ) ; } } }


"Cycle Sort | C # program to implement cycle sort ; Function sort the array using Cycle sort ; count number of memory writes ; traverse array elements and put it to on the right place ; initialize item as starting point ; Find position where we put the item . We basically count all smaller elements on right side of item . ; If item is already in correct position ; ignore all duplicate elements ; put the item to it 's right position ; Rotate rest of the cycle ; Find position where we put the element ; ignore all duplicate elements ; put the item to it 's right position ; Driver program to test above function"
Solution in C#:

using System ; class GFG { public static void cycleSort ( int [ ] arr , int n ) { int writes = 0 ; for ( int cycle_start = 0 ; cycle_start <= n - 2 ; cycle_start ++ ) { int item = arr [ cycle_start ] ; int pos = cycle_start ; for ( int i = cycle_start + 1 ; i < n ; i ++ ) if ( arr [ i ] < item ) pos ++ ; if ( pos == cycle_start ) continue ; while ( item == arr [ pos ] ) pos += 1 ; if ( pos != cycle_start ) { int temp = item ; item = arr [ pos ] ; arr [ pos ] = temp ; writes ++ ; } while ( pos != cycle_start ) { pos = cycle_start ; for ( int i = cycle_start + 1 ; i < n ; i ++ ) if ( arr [ i ] < item ) pos += 1 ; while ( item == arr [ pos ] ) pos += 1 ; if ( item != arr [ pos ] ) { int temp = item ; item = arr [ pos ] ; arr [ pos ] = temp ; writes ++ ; } } } } public static void Main ( ) { int [ ] arr = { 1 , 8 , 3 , 9 , 10 , 10 , 2 , 4 } ; int n = arr . Length ; cycleSort ( arr , n ) ; Console . Write ( " After ▁ sort ▁ : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }


"Cycles of length n in an undirected and connected graph | C # program to calculate cycles of length n in a given graph ; Number of vertices ; mark the vertex vert as visited ; if the path of length ( n - 1 ) is found ; mark vert as un - visited to make it usable again ; Check if vertex vert end with vertex start ; For searching every possible path of length ( n - 1 ) ; DFS for searching path by decreasing length by 1 ; marking vert as unvisited to make it usable again ; Count cycles of length N in an undirected and connected graph . ; all vertex are marked un - visited initially . ; Searching for cycle by using v - n + 1 vertices ; ith vertex is marked as visited and will not be visited again ; Driver code"
Solution in C#:

using System ; class GFG { public static int V = 5 ; static int count = 0 ; static void DFS ( int [ , ] graph , bool [ ] marked , int n , int vert , int start ) { marked [ vert ] = true ; if ( n == 0 ) { marked [ vert ] = false ; if ( graph [ vert , start ] == 1 ) { count ++ ; return ; } else return ; } for ( int i = 0 ; i < V ; i ++ ) if ( ! marked [ i ] && graph [ vert , i ] == 1 ) DFS ( graph , marked , n - 1 , i , start ) ; marked [ vert ] = false ; } static int countCycles ( int [ , ] graph , int n ) { bool [ ] marked = new bool [ V ] ; for ( int i = 0 ; i < V - ( n - 1 ) ; i ++ ) { DFS ( graph , marked , n - 1 , i , i ) ; marked [ i ] = true ; } return count / 2 ; } public static void Main ( ) { int [ , ] graph = { { 0 , 1 , 0 , 1 , 0 } , { 1 , 0 , 1 , 0 , 1 } , { 0 , 1 , 0 , 1 , 0 } , { 1 , 0 , 1 , 0 , 1 } , { 0 , 1 , 0 , 1 , 0 } } ; int n = 4 ; Console . WriteLine ( " Total ▁ cycles ▁ of ▁ length ▁ " + n + " ▁ are ▁ " + countCycles ( graph , n ) ) ; } }


"DFA that begins with ' a ' but does not contain substring ' aab ' | C # code for the above DFA ; Function for state A transition ; If at index 0 ' a ' if found then call stateB function with passing n [ 1 : ] to it ; If at index 0 ' b ' if found then call stateQ function with passing n to it ; Function for transition state B ; Length of string become 0 then print Accepted ; If at index 0 ' a ' if found then call stateC function with passing n [ 1 : ] to it ; If at index 0 ' b ' if found then call stateD function with passing n [ 1 : ] to it ; Function for transition state C ; Length of string become 0 then print Accepted ; If at index 0 ' a ' if found then call stateC function with passing n [ 1 : ] to it ; If at index 0 ' b ' if found then call stateQ function with passing n to it ; Function for transition state D ; Length of string become 0 then print Accepted ; If at index 0 ' a ' if found then call stateC function with passing n [ 1 : ] to it ; If at index 0 ' b ' if found then call stateQ function with passing n to it ; Function for state Q transition ; In dead state it shows string not accepted ; Driver code ; Take string input ; Call stateA to check the input"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static void stateA ( string n ) { if ( n [ 0 ] == ' a ' ) { stateB ( n . Substring ( 1 ) ) ; } else { stateQ ( n ) ; } } static void stateB ( string n ) { if ( n . Length == 0 ) { Console . Write ( " Accepted " ) ; } else { if ( n [ 0 ] == ' a ' ) stateC ( n . Substring ( 1 ) ) ; else stateD ( n . Substring ( 1 ) ) ; } } static void stateC ( string n ) { if ( n . Length == 0 ) Console . Write ( " Accepted " ) ; else { if ( n [ 0 ] == ' a ' ) stateC ( n . Substring ( 1 ) ) ; else stateQ ( n ) ; } } static void stateD ( string n ) { if ( n . Length == 0 ) Console . Write ( " Accepted " ) ; else { if ( n [ 0 ] == ' a ' ) { stateB ( n . Substring ( 1 ) ) ; } else { stateD ( n . Substring ( 1 ) ) ; } } } static void stateQ ( string n ) { Console . Write ( " Not ▁ Accepted " ) ; } public static void Main ( string [ ] args ) { string n = " aaaba " ; stateA ( n ) ; } }


"DFS for a n | C # Code For DFS for a n - ary tree ( acyclic graph ) represented as adjacency list ; DFS on tree ; Printing traversed node ; Traversing adjacent edges ; Not traversing the parent node ; Driver program to test above function ; Number of nodes ; Adjacency list ; Designing the tree ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { public static void dfs ( List < int > [ ] list , int node , int arrival ) { Console . WriteLine ( node ) ; for ( int i = 0 ; i < list [ node ] . Count ; i ++ ) { if ( list [ node ] [ i ] != arrival ) dfs ( list , list [ node ] [ i ] , node ) ; } } public static void Main ( String [ ] args ) { int nodes = 5 ; List < int > [ ] list = new List < int > [ nodes + 1 ] ; for ( int i = 0 ; i < list . Length ; i ++ ) { list [ i ] = new List < int > ( ) ; } list [ 1 ] . Add ( 2 ) ; list [ 2 ] . Add ( 1 ) ; list [ 1 ] . Add ( 3 ) ; list [ 3 ] . Add ( 1 ) ; list [ 2 ] . Add ( 4 ) ; list [ 4 ] . Add ( 2 ) ; list [ 3 ] . Add ( 5 ) ; list [ 5 ] . Add ( 3 ) ; dfs ( list , 1 , 0 ) ; } }


"DP on Trees | Set | C # program to find diameter of a tree using DFS . ; Function to find the diameter of the tree using Dynamic Programming ; Store the first maximum and secondmax ; Traverse for all children of node ; Call DFS function again ; Find first max ; Secondmaximum ; Find secondmaximum ; Base case for every node ; if ( firstmax != - 1 ) Add ; Find dp [ 2 ] ; Return maximum of both ; Constructed tree is 1 / \ 2 3 / \ 4 5 ; create undirected edges ; Find diameter by calling function"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int dfs ( int node , int parent , int [ ] dp1 , int [ ] dp2 , List < List < int > > adj ) { int firstmax = - 1 ; int secondmax = - 1 ; for ( int i = 0 ; i < adj [ node ] . Count ; ++ i ) { if ( adj [ node ] [ i ] == parent ) continue ; dfs ( adj [ node ] [ i ] , node , dp1 , dp2 , adj ) ; if ( firstmax == - 1 ) { firstmax = dp1 [ adj [ node ] [ i ] ] ; } else if ( dp1 [ adj [ node ] [ i ] ] >= firstmax ) { secondmax = firstmax ; firstmax = dp1 [ adj [ node ] [ i ] ] ; } else if ( dp1 [ adj [ node ] [ i ] ] > secondmax ) { secondmax = dp1 [ adj [ node ] [ i ] ] ; } } dp1 [ node ] = 1 ; dp1 [ node ] += firstmax ; if ( secondmax != - 1 ) dp2 [ node ] = 1 + firstmax + secondmax ; return Math . Max ( dp1 [ node ] , dp2 [ node ] ) ; } static void Main ( ) { int n = 5 ; List < List < int > > adj = new List < List < int > > ( ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) { adj . Add ( new List < int > ( ) ) ; } adj [ 1 ] . Add ( 2 ) ; adj [ 2 ] . Add ( 1 ) ; adj [ 1 ] . Add ( 3 ) ; adj [ 3 ] . Add ( 1 ) ; adj [ 2 ] . Add ( 4 ) ; adj [ 4 ] . Add ( 2 ) ; adj [ 2 ] . Add ( 5 ) ; adj [ 5 ] . Add ( 2 ) ; int [ ] dp1 = new int [ n + 1 ] ; int [ ] dp2 = new int [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { dp1 [ i ] = 0 ; dp2 [ i ] = 0 ; } Console . WriteLine ( " Diameter ▁ of ▁ the ▁ given ▁ tree ▁ is ▁ " + dfs ( 1 , 1 , dp1 , dp2 , adj ) ) ; } }


"Date after adding given number of days to the given date | C # program to find date after adding given number of days . ; Find values of day and month from offset of result year . ; Return if year is leap year or not . ; Given a date , returns number of days elapsed from the beginning of the current year ( 1 stjan ) . ; Given a year and days elapsed in it , finds date by storing results in d and m . ; Add x days to the given date . ; y2 is going to store result year and offset2 is going to store offset days in result year . ; x may store thousands of days . We find correct year and offset in the year . ; Driven Program"
Solution in C#:

using System ; class GFG { static int m2 , d2 ; static bool isLeap ( int y ) { if ( y % 100 != 0 && y % 4 == 0 y % 400 == 0 ) return true ; return false ; } static int offsetDays ( int d , int m , int y ) { int offset = d ; if ( m - 1 == 11 ) offset += 335 ; if ( m - 1 == 10 ) offset += 304 ; if ( m - 1 == 9 ) offset += 273 ; if ( m - 1 == 8 ) offset += 243 ; if ( m - 1 == 7 ) offset += 212 ; if ( m - 1 == 6 ) offset += 181 ; if ( m - 1 == 5 ) offset += 151 ; if ( m - 1 == 4 ) offset += 120 ; if ( m - 1 == 3 ) offset += 90 ; if ( m - 1 == 2 ) offset += 59 ; if ( m - 1 == 1 ) offset += 31 ; if ( isLeap ( y ) && m > 2 ) offset += 1 ; return offset ; } static void revoffsetDays ( int offset , int y ) { int [ ] month = { 0 , 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 } ; if ( isLeap ( y ) ) month [ 2 ] = 29 ; int i ; for ( i = 1 ; i <= 12 ; i ++ ) { if ( offset <= month [ i ] ) break ; offset = offset - month [ i ] ; } d2 = offset ; m2 = i ; } static void addDays ( int d1 , int m1 , int y1 , int x ) { int offset1 = offsetDays ( d1 , m1 , y1 ) ; int remDays = isLeap ( y1 ) ? ( 366 - offset1 ) : ( 365 - offset1 ) ; int y2 , offset2 = 0 ; if ( x <= remDays ) { y2 = y1 ; offset2 = offset1 + x ; } else { x -= remDays ; y2 = y1 + 1 ; int y2days = isLeap ( y2 ) ? 366 : 365 ; while ( x >= y2days ) { x -= y2days ; y2 ++ ; y2days = isLeap ( y2 ) ? 366 : 365 ; } offset2 = x ; } revoffsetDays ( offset2 , y2 ) ; Console . WriteLine ( " d2 ▁ = ▁ " + d2 + " , ▁ m2 ▁ = ▁ " + m2 + " , ▁ y2 ▁ = ▁ " + y2 ) ; } static void Main ( ) { int d = 14 , m = 3 , y = 2015 ; int x = 366 ; addDays ( d , m , y , x ) ; } }


"De Bruijn sequence | Set 1 | C # implementation of the above approach ; Modified DFS in which no edge is traversed twice ; Function to find a de Bruijn sequence of order n on k characters ; Clearing global variables ; Number of edges ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static HashSet < String > seen = new HashSet < String > ( ) ; static List < int > edges = new List < int > ( ) ; static void dfs ( String node , int k , String A ) { for ( int i = 0 ; i < k ; ++ i ) { String str = node + A [ i ] ; if ( ! seen . Contains ( str ) ) { seen . Add ( str ) ; dfs ( str . Substring ( 1 ) , k , A ) ; edges . Add ( i ) ; } } } static String deBruijn ( int n , int k , String A ) { seen . Clear ( ) ; edges . Clear ( ) ; String startingNode = strings ( n - 1 , A [ 0 ] ) ; dfs ( startingNode , k , A ) ; String S = " " ; int l = ( int ) Math . Pow ( k , n ) ; for ( int i = 0 ; i < l ; ++ i ) S += A [ edges [ i ] ] ; S += startingNode ; return S ; } private static String strings ( int n , char charAt ) { String str = " " ; for ( int i = 0 ; i < n ; i ++ ) str += charAt ; return str ; } public static void Main ( String [ ] args ) { int n = 3 , k = 2 ; String A = "01" ; Console . Write ( deBruijn ( n , k , A ) ) ; } }


"Decakismyriagon Number | C # program for the above approach ; Function to find the N - th Decakismyriagon Number ; Driver code ; Given Number N ; Function Call"
Solution in C#:

using System ; class GFG { static int DecakismyriagonNum ( int N ) { return ( 99998 * N * N - 99996 * N ) / 2 ; } public static void Main ( ) { int N = 3 ; Console . Write ( DecakismyriagonNum ( N ) ) ; } }


"Decimal Equivalent of Gray Code and its Inverse | C # Program to convert given decimal number of gray code into its inverse in decimal form ; Function to convert given decimal number of gray code into its inverse in decimal form ; Taking xor until n becomes zero ; Driver code"
Solution in C#:

using System ; class GFG { static int inversegrayCode ( int n ) { int inv = 0 ; for ( ; n != 0 ; n = n >> 1 ) inv ^= n ; return inv ; } public static void Main ( ) { int n = 15 ; Console . Write ( inversegrayCode ( n ) ) ; } }


"Decimal equivalent of concatenation of absolute difference of floor and rounded | C # program for the above approach ; Function to find the decimal equivalent of the new binary array constructed from absolute decimal of floor and the round - off values ; Traverse the givenarray from the end ; Stores the absolute difference between floor and round - off each array element ; If bit / difference is 1 , then calculate the bit by proper power of 2 and add it to result ; Increment the value of power ; Print the result ; Driver Code"
Solution in C#:

using System ; class GFG { static void findDecimal ( double [ ] arr , int N ) { int bit , power = 0 , result = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { bit = Math . Abs ( ( int ) Math . Floor ( arr [ i ] ) - ( int ) Math . Round ( arr [ i ] ) ) ; if ( bit != 0 ) result += ( int ) Math . Pow ( 2 , power ) ; power ++ ; } Console . WriteLine ( result ) ; } public static void Main ( ) { double [ ] arr = { 1.2 , 2.6 , 4.2 , 6.9 , 3.1 , 21.6 , 91.2 } ; int N = arr . Length ; findDecimal ( arr , N ) ; } }


"Decimal representation of given binary string is divisible by 10 or not | C # implementation to check whether decimal representation of given binary number is divisible by 10 or not ; function to check whether decimal representation of given binary number is divisible by 10 or not ; if last digit is '1' , then number is not divisible by 10 ; to accumulate the sum of last digits in perfect powers of 2 ; traverse from the 2 nd last up to 1 st digit in ' bin ' ; if digit in '1' ; calculate digit 's position  from the right ; according to the digit 's  position, obtain the last  digit of the applicable  perfect power of 2 ; if last digit is 0 , then divisible by 10 ; not divisible by 10 ; Driver program to test above function"
Solution in C#:

using System ; class GFG { static bool isDivisibleBy10 ( String bin ) { int n = bin . Length ; if ( bin [ n - 1 ] == '1' ) return false ; int sum = 0 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( bin [ i ] == '1' ) { int posFromRight = n - i - 1 ; if ( posFromRight % 4 == 1 ) sum = sum + 2 ; else if ( posFromRight % 4 == 2 ) sum = sum + 4 ; else if ( posFromRight % 4 == 3 ) sum = sum + 8 ; else if ( posFromRight % 4 == 0 ) sum = sum + 6 ; } } if ( sum % 10 == 0 ) return true ; return false ; } public static void Main ( ) { String bin = "11000111001110" ; if ( isDivisibleBy10 ( bin ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Decimal representation of given binary string is divisible by 20 or not | C # implementation to check whether decimal representation of given binary number is divisible by 20 or not ; function to check whether decimal representation of given binary number is divisible by 10 or not ; if last digit is '1' , then number is not divisible by 10 ; to accumulate the sum of last digits in perfect powers of 2 ; traverse from the 2 nd last up to 1 st digit in ' bin ' ; if digit in '1' ; calculate digit 's position from  the right ; according to the digit 's position,  obtain the last digit of the  applicable perfect power of 2 ; if last digit is 0 , then divisible by 10 ; not divisible by 10 ; function to check whether decimal representation of given binary number is divisible by 20 or not ; if ' bin ' is an odd number ; check if bin ( 0. . n - 2 ) is divisible by 10 or not ; Driver program to test above"
Solution in C#:

using System ; class GFG { static bool isDivisibleBy10 ( string bin , int n ) { if ( bin [ n - 1 ] == '1' ) return false ; int sum = 0 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( bin [ i ] == '1' ) { int posFromRight = n - i - 1 ; if ( posFromRight % 4 == 1 ) sum = sum + 2 ; else if ( posFromRight % 4 == 2 ) sum = sum + 4 ; else if ( posFromRight % 4 == 3 ) sum = sum + 8 ; else if ( posFromRight % 4 == 0 ) sum = sum + 6 ; } } if ( sum % 10 == 0 ) return true ; return false ; } static bool isDivisibleBy20 ( string bin , int n ) { if ( bin [ n - 1 ] == '1' ) return false ; return isDivisibleBy10 ( bin , n - 1 ) ; } public static void Main ( ) { string bin = "101000" ; int n = bin . Length ; if ( isDivisibleBy20 ( bin , n - 1 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Decimal to binary conversion without using arithmetic operators | C # implementation of decimal to binary conversion without using arithmetic operators ; function for decimal to binary conversion without using arithmetic operators ; to store the binary equivalent of decimal ; to get the last binary digit of the number ' n ' and accumulate it at the beginning of ' bin ' ; right shift ' n ' by 1 ; required binary number ; Driver program to test above"
Solution in C#:

using System ; class GFG { static String decToBin ( int n ) { if ( n == 0 ) return "0" ; String bin = " " ; while ( n > 0 ) { bin = ( ( n & 1 ) == 0 ? '0' : '1' ) + bin ; n >>= 1 ; } return bin ; } public static void Main ( ) { int n = 38 ; Console . WriteLine ( decToBin ( n ) ) ; } }


"Decimal to octal conversion with minimum use of arithmetic operators | C # implementation of decimal to octal conversion with minimum use of arithmetic operators ; Function for decimal to binary conversion without using arithmetic operators ; To store the binary equivalent of decimal ; To get the last binary digit of the number ' n ' and accumulate it at the beginning of ' bin ' ; Right shift ' n ' by 1 ; Required binary number ; Function to find octal equivalent of binary ; Add min 0 's in the beginning to make  string length divisible by 3 ; Create map between binary and its equivalent octal code ; One by one extract from left , substring of size 3 and add its octal code ; Required octal number ; Function to find octal equivalent of decimal ; Convert decimal to binary ; Convert binary to octal required octal equivalent of decimal ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static string decToBin ( int n ) { if ( n == 0 ) return "0" ; string bin = " " ; while ( n > 0 ) { bin = ( ( n & 1 ) == 0 ? '0' : '1' ) + bin ; n >>= 1 ; } return bin ; } static string convertBinToOct ( string bin ) { int l = bin . Length ; for ( int j = 1 ; j <= ( 3 - l % 3 ) % 3 ; j ++ ) bin = '0' + bin ; Dictionary < string , char > bin_oct_map = new Dictionary < string , char > ( ) ; bin_oct_map . Add ( "000" , '0' ) ; bin_oct_map . Add ( "001" , '1' ) ; bin_oct_map . Add ( "010" , '2' ) ; bin_oct_map . Add ( "011" , '3' ) ; bin_oct_map . Add ( "100" , '4' ) ; bin_oct_map . Add ( "101" , '5' ) ; bin_oct_map . Add ( "110" , '6' ) ; bin_oct_map . Add ( "111" , '7' ) ; int i = 0 ; string octal = " " ; while ( true ) { octal += bin_oct_map [ bin . Substring ( i , 3 ) ] ; i += 3 ; if ( i == bin . Length ) break ; } return octal ; } static string decToOctal ( int n ) { string bin = decToBin ( n ) ; return convertBinToOct ( bin ) ; } public static void Main ( string [ ] args ) { int n = 151 ; Console . Write ( decToOctal ( n ) ) ; } }


"Decode a given string by removing duplicate occurrences | C # program for the above approach ; Function to count the appearances of each character ; If the character is lower case ; If the character is uppercase ; If the character is a punctuation mark ; Function to decode the given encoded string ; Iterate the given string str ; Find the index of the next character to be printed ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int findRepitition ( char a ) { if ( a <= ' z ' && a >= ' a ' ) { return ( int ) a - 97 ; } else if ( a <= ' Z ' && a >= ' A ' ) { return ( int ) a - 65 ; } return 0 ; } static void decodeString ( string str ) { string output = " " ; for ( int i = 0 ; i < str . Length ; i ++ ) { output += str [ i ] ; i += findRepitition ( str [ i ] ) ; } Console . Write ( " Decrypted ▁ code ▁ is ▁ { " + output + " } " ) ; } public static void Main ( ) { string str = " abbbb ▁ acccdddd " ; decodeString ( str ) ; } }


"Decode a median string to the original string | C # program to decode a median string to the original string ; function to calculate the median back string ; length of string ; initialize a blank string ; Flag to check if length is even or odd ; traverse from first to last ; if len is even then add first character to beginning of new string and second character to end ; if current length is odd and is greater than 1 ; add first character to end and second character to beginning ; if length is 1 , add character to end ; Driver code"
Solution in C#:

using System ; class GFG { static string decodeMedianString ( string s ) { int l = s . Length ; string s1 = " " ; bool isEven = ( l % 2 == 0 ) ? true : false ; for ( int i = 0 ; i < l ; i += 2 ) { if ( isEven ) { s1 = s [ i ] + s1 ; s1 += s [ i + 1 ] ; } else { if ( l - i > 1 ) { s1 += s [ i ] ; s1 = s [ i + 1 ] + s1 ; } else { s1 += s [ i ] ; } } } return s1 ; } public static void Main ( ) { string s = " eekgs " ; Console . WriteLine ( decodeMedianString ( s ) ) ; } }


"Decode a string recursively encoded as count followed by substring | C # program to decode a string recursively encoded as count followed substring ; Returns decoded string for ' str ' ; Traversing the string ; If number , convert it into number and push it into integerstack . ; If closing bracket ' ] ' , pop elemment until ' [ ' opening bracket is not found in the character stack . ; Repeating the popped string ' temo ' count number of times . ; Push it in the character stack . ; If ' [ ' opening bracket , push it into character stack . ; Pop all the elmenet , make a string and return . ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static string decode ( string str ) { Stack < int > integerstack = new Stack < int > ( ) ; Stack < char > stringstack = new Stack < char > ( ) ; string temp = " " , result = " " ; for ( int i = 0 ; i < str . Length ; i ++ ) { int count = 0 ; if ( char . IsDigit ( str [ i ] ) ) { while ( char . IsDigit ( str [ i ] ) ) { count = count * 10 + str [ i ] - '0' ; i ++ ; } i -- ; integerstack . Push ( count ) ; } else if ( str [ i ] == ' ] ' ) { temp = " " ; count = 0 ; if ( integerstack . Count > 0 ) { count = integerstack . Peek ( ) ; integerstack . Pop ( ) ; } while ( stringstack . Count > 0 && stringstack . Peek ( ) != ' [ ' ) { temp = stringstack . Peek ( ) + temp ; stringstack . Pop ( ) ; } if ( stringstack . Count > 0 && stringstack . Peek ( ) == ' [ ' ) { stringstack . Pop ( ) ; } for ( int j = 0 ; j < count ; j ++ ) { result = result + temp ; } for ( int j = 0 ; j < result . Length ; j ++ ) { stringstack . Push ( result [ j ] ) ; } result = " " ; } else if ( str [ i ] == ' [ ' ) { if ( char . IsDigit ( str [ i - 1 ] ) ) { stringstack . Push ( str [ i ] ) ; } else { stringstack . Push ( str [ i ] ) ; integerstack . Push ( 1 ) ; } } else { stringstack . Push ( str [ i ] ) ; } } while ( stringstack . Count > 0 ) { result = stringstack . Peek ( ) + result ; stringstack . Pop ( ) ; } return result ; } public static void Main ( string [ ] args ) { string str = "3 [ b2 [ ca ] ] " ; Console . WriteLine ( decode ( str ) ) ; } }


"Decode an Encoded Base 64 String to ASCII String | C # Program to decode a base64 Encoded String back to ASCII String ; char_set = " ABCDEFGHIJKLMNOPQRSTUVWXYZ ▁ abcdefghijklmnopqrstuvwxyz0123456789 + / " ; stores the bitstream . ; count_bits stores current number of bits in num . ; selects 4 characters from encoded String at a time . find the position of each encoded character in char_set and stores in num . ; make space for 6 bits . ; encoded [ i + j ] = ' E ' , ' E ' - ' A ' = 5 ' E ' has 5 th position in char_set . ; encoded [ i + j ] = ' e ' , ' e ' - ' a ' = 5 , 5 + 26 = 31 , ' e ' has 31 st position in char_set . ; encoded [ i + j ] = '8' , '8' - '0' = 8 8 + 52 = 60 , '8' has 60 th position in char_set . ; ' + ' occurs in 62 nd position in char_set . ; ' / ' occurs in 63 rd position in char_set . ; ( str [ i + j ] == ' = ' ) remove 2 bits to delete appended bits during encoding . ; 255 in binary is 11111111 ; Driver code ; Do not count last null character ."
Solution in C#:

 using System ; class GFG { static readonly int SIZE = 100 ; static String base64Decoder ( char [ ] encoded , int len_str ) { char [ ] decoded_String ; decoded_String = new char [ SIZE ] ; int i , j , k = 0 ; int num = 0 ; int count_bits = 0 ; for ( i = 0 ; i < len_str ; i += 4 ) { num = 0 ; count_bits = 0 ; for ( j = 0 ; j < 4 ; j ++ ) { if ( encoded [ i + j ] != ' = ' ) { num = num << 6 ; count_bits += 6 ; } if ( encoded [ i + j ] >= ' A ' && encoded [ i + j ] <= ' Z ' ) num = num | ( encoded [ i + j ] - ' A ' ) ; else if ( encoded [ i + j ] >= ' a ' && encoded [ i + j ] <= ' z ' ) num = num | ( encoded [ i + j ] - ' a ' + 26 ) ; else if ( encoded [ i + j ] >= '0' && encoded [ i + j ] <= '9' ) num = num | ( encoded [ i + j ] - '0' + 52 ) ; else if ( encoded [ i + j ] == ' + ' ) num = num | 62 ; else if ( encoded [ i + j ] == ' / ' ) num = num | 63 ; else { num = num >> 2 ; count_bits -= 2 ; } } while ( count_bits != 0 ) { count_bits -= 8 ; decoded_String [ k ++ ] = ( char ) ( ( num >> count_bits ) & 255 ) ; } } return String . Join ( " " , decoded_String ) ; } public static void Main ( String [ ] args ) { char [ ] encoded_String = " TUVOT04 = " . ToCharArray ( ) ; int len_str = encoded_String . Length ; len_str -= 1 ; Console . Write ( " Encoded ▁ String ▁ : ▁ { 0 }
 " , String . Join ( " " , encoded_String ) ) ; Console . Write ( " Decoded _ String ▁ : ▁ { 0 }
 " , base64Decoder ( encoded_String , len_str ) ) ; } }


"Decrypt the String according to given algorithm | C # implementation of the approach ; Function that returns ( num % 26 ) ; Initialize result ; One by one process all digits of ' num ' ; Function to return the decrypted String ; To store the readonly decrypted answer ; One by one check for each character if it is a numeric character ; Modulo the number found in the String by 26 ; Driver code ; Print the decrypted string"
Solution in C#:

using System ; class GFG { static int MOD = 26 ; static int modulo_by_26 ( String num ) { int res = 0 ; for ( int i = 0 ; i < num . Length ; i ++ ) { res = ( ( res * 10 + ( int ) num [ i ] - '0' ) % MOD ) ; } return res ; } static String decrypt_message ( String s ) { String decrypted_str = " " ; String num_found_so_far = " " ; for ( int i = 0 ; i < s . Length ; ++ i ) { if ( s [ i ] >= '0' && s [ i ] <= '9' ) { num_found_so_far += s [ i ] ; } else if ( num_found_so_far . Length > 0 ) { decrypted_str += ( char ) ( ' a ' + modulo_by_26 ( num_found_so_far ) ) ; num_found_so_far = " " ; } } if ( num_found_so_far . Length > 0 ) { decrypted_str += ( char ) ( ' a ' + modulo_by_26 ( num_found_so_far ) ) ; } return decrypted_str ; } public static void Main ( String [ ] args ) { String s = "32ytAAcV4ui30hf10hj18" ; Console . Write ( decrypt_message ( s ) ) ; } }


"Deepest left leaf node in a binary tree | A C # program to find the deepest left leaf in a binary tree ; A Binary Tree node ; Constructor ; Class to evaluate pass by reference ; maxlevel : gives the value of level of maximum left leaf ; Node to store resultant node after left traversal ; A utility function to find deepest leaf node . lvl : level of current node . isLeft : A bool indicate that this node is left child ; Base case ; Update result if this node is left leaf and its level is more than the maxl level of the current result ; Recur for left and right subtrees ; A wrapper over deepestLeftLeafUtil ( ) . ; Driver program to test above functions"
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 Level { public int maxlevel = 0 ; } public class BinaryTree { public Node root ; public Node result ; public virtual void deepestLeftLeafUtil ( Node node , int lvl , Level level , bool isLeft ) { if ( node == null ) { return ; } if ( isLeft != false && node . left == null && node . right == null && lvl > level . maxlevel ) { result = node ; level . maxlevel = lvl ; } deepestLeftLeafUtil ( node . left , lvl + 1 , level , true ) ; deepestLeftLeafUtil ( node . right , lvl + 1 , level , false ) ; } public virtual void deepestLeftLeaf ( Node node ) { Level level = new Level ( ) ; deepestLeftLeafUtil ( node , 0 , level , false ) ; } public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . right . left = new Node ( 5 ) ; tree . root . right . right = new Node ( 6 ) ; tree . root . right . left . right = new Node ( 7 ) ; tree . root . right . right . right = new Node ( 8 ) ; tree . root . right . left . right . left = new Node ( 9 ) ; tree . root . right . right . right . right = new Node ( 10 ) ; tree . deepestLeftLeaf ( tree . root ) ; if ( tree . result != null ) { Console . WriteLine ( " The ▁ deepest ▁ left ▁ child ▁ is ▁ " + tree . result . data ) ; } else { Console . WriteLine ( " There ▁ is ▁ no ▁ left ▁ leaf ▁ in ▁ the ▁ given ▁ tree " ) ; } } }


"Deepest left leaf node in a binary tree | iterative approach | C # program to find deepest left leaf node of binary tree ; tree node ; returns a new tree Node ; return the deepest left leaf node of binary tree ; create a queue for level order traversal ; traverse until the queue is empty ; Since we go level by level , the last stored left leaf node is deepest one , ; Driver Code ; construct a tree"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { class Node { public int data ; public Node left , right ; } ; static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } static Node getDeepestLeftLeafNode ( Node root ) { if ( root == null ) return null ; Queue < Node > q = new Queue < Node > ( ) ; q . Enqueue ( root ) ; Node result = null ; while ( q . Count != 0 ) { Node temp = q . Peek ( ) ; q . Dequeue ( ) ; if ( temp . left != null ) { q . Enqueue ( temp . left ) ; if ( temp . left . left == null && temp . left . right == null ) result = temp . left ; } if ( temp . right != null ) q . Enqueue ( temp . right ) ; } return result ; } public static void Main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . right . left = newNode ( 5 ) ; root . right . right = newNode ( 6 ) ; root . right . left . right = newNode ( 7 ) ; root . right . right . right = newNode ( 8 ) ; root . right . left . right . left = newNode ( 9 ) ; root . right . right . right . right = newNode ( 10 ) ; Node result = getDeepestLeftLeafNode ( root ) ; if ( result != null ) Console . WriteLine ( " Deepest ▁ Left ▁ Leaf ▁ Node ▁ : : ▁ " + result . data ) ; else Console . WriteLine ( " No ▁ result , ▁ " + " left ▁ leaf ▁ not ▁ found " ) ; } }


"Deepest right leaf node in a binary tree | Iterative approach | C # program to find deepest right leaf node of binary tree ; tree node ; returns a new tree Node ; return the deepest right leaf node of binary tree ; Create a queue for level order traversal ; Traverse until the queue is empty ; Since we go level by level , the last stored right leaf node is deepest one ; Driver code ; construct a tree"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { public class Node { public int data ; public Node left , right ; } ; static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } static Node getDeepestRightLeafNode ( Node root ) { if ( root == null ) return null ; Queue < Node > q = new Queue < Node > ( ) ; q . Enqueue ( root ) ; Node result = null ; while ( q . Count != 0 ) { Node temp = q . Peek ( ) ; q . Dequeue ( ) ; if ( temp . left != null ) { q . Enqueue ( temp . left ) ; } if ( temp . right != null ) { q . Enqueue ( temp . right ) ; if ( temp . right . left == null && temp . right . right == null ) result = temp . right ; } } return result ; } public static void Main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . right = newNode ( 4 ) ; root . right . left = newNode ( 5 ) ; root . right . right = newNode ( 6 ) ; root . right . left . right = newNode ( 7 ) ; root . right . right . right = newNode ( 8 ) ; root . right . left . right . left = newNode ( 9 ) ; root . right . right . right . right = newNode ( 10 ) ; Node result = getDeepestRightLeafNode ( root ) ; if ( result != null ) Console . WriteLine ( " Deepest ▁ Right ▁ Leaf ▁ Node ▁ : : ▁ " + result . data ) ; else Console . WriteLine ( " No ▁ result , ▁ right ▁ leaf ▁ not ▁ found
 " ) ; } }


"Defanged Version of Internet Protocol Address | C # implementation to find the defanged version of the IP address ; Function to generate a defanged version of IP address . ; Loop to iterate over the characters of the string ; Driver Code"
Solution in C#:

using System ; class GFG { static String GeberateDefangIP ( string str ) { string defangIP = " " ; for ( int i = 0 ; i < str . Length ; i ++ ) { char c = str [ i ] ; if ( c == ' . ' ) { defangIP += " [ . ] " ; } else { defangIP += c ; } } return defangIP ; } public static void Main ( ) { string str = "255.100.50.0" ; Console . Write ( GeberateDefangIP ( str ) ) ; } }


"Deficient Number | C # program to implement an Optimized Solution to check Deficient Number ; Function to calculate sum of divisors ; Initialize sum of prime factors ; Note that this loop runs till square root of n ; If divisors are equal , take only one of them ; else Otherwise take both ; Function to check Deficient Number ; Check if sum ( n ) < 2 * n ; Driver program to test above function"
Solution in C#:

using System ; class GFG { static int divisorsSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= ( Math . Sqrt ( n ) ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) { sum = sum + i ; } { sum = sum + i ; sum = sum + ( n / i ) ; } } } return sum ; } static bool isDeficient ( int n ) { return ( divisorsSum ( n ) < ( 2 * n ) ) ; } public static void Main ( ) { string var = isDeficient ( 12 ) ? " YES " : " NO " ; Console . WriteLine ( var ) ; string var1 = isDeficient ( 15 ) ? " YES " : " NO " ; Console . WriteLine ( var1 ) ; } }


"Delannoy Number | C # Program for finding nth Delannoy Number . ; Return the nth Delannoy Number . ; Base case ; Recursive step . ; driver function"
Solution in C#:

using System ; public class GfG { public static int dealnnoy ( int n , int m ) { if ( m == 0 n == 0 ) return 1 ; return dealnnoy ( m - 1 , n ) + dealnnoy ( m - 1 , n - 1 ) + dealnnoy ( m , n - 1 ) ; } public static void Main ( ) { int n = 3 , m = 4 ; Console . WriteLine ( dealnnoy ( n , m ) ) ; } }


"Delannoy Number | C # Program of finding nth Delannoy Number . ; Return the nth Delannoy Number . ; Base cases ; Driven Program"
Solution in C#:

using System ; class GFG { static int dealnnoy ( int n , int m ) { int [ , ] dp = new int [ m + 1 , n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) dp [ i , 0 ] = 1 ; for ( int i = 0 ; i < m ; i ++ ) dp [ 0 , i ] = 1 ; for ( int i = 1 ; i <= m ; i ++ ) for ( int j = 1 ; j <= n ; j ++ ) dp [ i , j ] = dp [ i - 1 , j ] + dp [ i - 1 , j - 1 ] + dp [ i , j - 1 ] ; return dp [ m , n ] ; } public static void Main ( ) { int n = 3 , m = 4 ; Console . WriteLine ( dealnnoy ( n , m ) ) ; } }


"Delete all odd nodes of a Circular Linked List | C # program to delete all prime node from a Circular singly linked list ; Structure for a node ; Function to insert a node at the beginning of a Circular linked list ; If linked list is not null then set the next of last node ; For the first node ; Delete the node if it is odd ; If node to be deleted is head node ; Traverse list till not found delete node ; copy address of node ; Function to delete all odd nodes from the singly circular linked list ; Traverse list till the end if the node is odd then delete it ; If node is odd ; Point to next node ; Function to print nodes ; Driver code ; Initialize lists as empty ; Created linked list will be 56 -> 61 -> 57 -> 11 -> 12 -> 2"
Solution in C#:

 using System ; class GFG { public class Node { public int data ; public Node next ; } ; static Node push ( Node head_ref , int data ) { Node ptr1 = new Node ( ) ; Node temp = head_ref ; ptr1 . data = data ; ptr1 . next = head_ref ; if ( head_ref != null ) { while ( temp . next != head_ref ) temp = temp . next ; temp . next = ptr1 ; return head_ref ; } else ptr1 . next = ptr1 ; head_ref = ptr1 ; return head_ref ; } static Node deleteNode ( Node head_ref , Node del ) { Node temp = head_ref ; if ( head_ref == del ) head_ref = del . next ; while ( temp . next != del ) { temp = temp . next ; } temp . next = del . next ; return head_ref ; } static Node deleteoddNodes ( Node head ) { Node ptr = head ; Node next ; do { if ( ptr . data % 2 == 1 ) deleteNode ( head , ptr ) ; next = ptr . next ; ptr = next ; } while ( ptr != head ) ; return head ; } static void printList ( Node head ) { Node temp = head ; if ( head != null ) { do { Console . Write ( " { 0 } ▁ " , temp . data ) ; temp = temp . next ; } while ( temp != head ) ; } } public static void Main ( String [ ] args ) { Node head = null ; head = push ( head , 2 ) ; head = push ( head , 12 ) ; head = push ( head , 11 ) ; head = push ( head , 57 ) ; head = push ( head , 61 ) ; head = push ( head , 56 ) ; Console . WriteLine ( "
 List ▁ after ▁ deletion ▁ : ▁ " ) ; head = deleteoddNodes ( head ) ; printList ( head ) ; } }


"Delete all odd or even positioned nodes from Circular Linked List | C # program to delete all even and odd position nodes from Singly Circular Linked list ; structure for a node ; Function return number of nodes present in list ; if list is empty simply return length zero ; traverse forst to last node ; Function print data of list ; if list is empty simply show message ; traverse forst to last node ; Function to insert a node at the end of a Circular linked list ; Create a new node ; check node is created or not ; insert data into newly created node ; check list is empty if not have any node then make first node it ; if list have already some node ; move firt node to last node ; put first or head node address in new node link ; put new node address into last node link ( next ) ; Utitlity function to delete a Node ; If node to be deleted is head node ; traverse list till not found delete node ; copy address of node ; Function to delete First node of Circular Linked List ; check list have any node if not then return ; check list have single node if yes then delete it and return ; traverse second to first ; now previous is last node and next is first node of list first node ( next ) link address put in last node ( previous ) link ; make second node as head node ; Function to delete odd position nodes ; check list have any node if not then return ; if list have single node means odd position then delete it ; traverse first to last if list have more than one node ; delete first position node which is odd position ; Function to delete first node ; check position is odd or not if yes then delete node Note : Considered 1 based indexing ; Function to delete all even position nodes ; Take size of list ; Check list is empty if empty simply return ; if list have single node then return ; make first node is previous ; make second node is current ; check node number is even if node is even then delete that node ; Driver Code ; Deleting Odd positioned nodes ; Deleting Even positioned nodes"
Solution in C#:

 using System ; class GFG { class Node { public int data ; public Node next ; } ; static int Length ( Node head ) { Node current = head ; int count = 0 ; if ( head == null ) { return 0 ; } else { do { current = current . next ; count ++ ; } while ( current != head ) ; } return count ; } static void Display ( Node head ) { Node current = head ; if ( head == null ) { Console . Write ( "
 Display ▁ List ▁ is ▁ empty
 " ) ; return ; } else { do { Console . Write ( " { 0 } ▁ " , current . data ) ; current = current . next ; } while ( current != head ) ; } } static Node Insert ( Node head , int data ) { Node current = head ; Node newNode = new Node ( ) ; if ( newNode == null ) { Console . Write ( "
 Memory ▁ Error
 " ) ; return null ; } newNode . data = data ; if ( head == null ) { newNode . next = newNode ; head = newNode ; return head ; } else { while ( current . next != head ) { current = current . next ; } newNode . next = head ; current . next = newNode ; } return head ; } static Node deleteNode ( Node head_ref , Node del ) { Node temp = head_ref ; if ( head_ref == del ) { head_ref = del . next ; } while ( temp . next != del ) { temp = temp . next ; } temp . next = del . next ; return head_ref ; } static Node DeleteFirst ( Node head ) { Node previous = head , next = head ; if ( head == null ) { Console . Write ( " List is " return head ; } if ( previous . next == previous ) { head = null ; return head ; } while ( previous . next != head ) { previous = previous . next ; next = previous . next ; } previous . next = next . next ; head = previous . next ; return head ; } static Node DeleteAllOddNode ( Node head ) { int len = Length ( head ) ; int count = 0 ; Node previous = head , next = head ; if ( head == null ) { Console . Write ( "
 Delete ▁ Last ▁ List ▁ is ▁ empty
 " ) ; return null ; } if ( len == 1 ) { head = DeleteFirst ( head ) ; return head ; } while ( len > 0 ) { if ( count == 0 ) { head = DeleteFirst ( head ) ; } if ( count % 2 == 0 && count != 0 ) { head = deleteNode ( head , previous ) ; }...


"Delete all odd or even positioned nodes from Circular Linked List | Function to delete all even position nodes ; Take size of list ; Check list is empty if empty simply return ; If list have single node then return ; Make first node is previous ; Make second node is current ; Check node number is even if node is even then delete that node"
Solution in C#:

static Node DeleteAllEvenNode ( Node head ) { int len = Length ( head ) ; int count = 1 ; Node previous = head , next = head ; if ( head == null ) { Console . Write ( " List is " return null ; } if ( len < 2 ) { return null ; } previous = head ; next = previous . next ; while ( len > 0 ) { if ( count % 2 == 0 ) { previous . next = next . next ; previous = next . next ; next = previous . next ; } len -- ; count ++ ; } return head ; }


"Delete all the even nodes of a Circular Linked List | C # program to delete all prime node from a Circular singly linked list ; Structure for a node ; Function to insert a node at the beginning of a Circular linked list ; If linked list is not null then set the next of last node ; For the first node ; Delete the node if it is even ; If node to be deleted is head node ; traverse list till not found delete node ; copy address of node ; Function to delete all even nodes from the singly circular linked list ; traverse list till the end if the node is even then delete it ; if node is even ; point to next node ; Function to print nodes ; Driver code ; Initialize lists as empty ; Created linked list will be 57.11 .2 .56 .12 .61"
Solution in C#:

 using System ; class GFG { public class Node { public int data ; public Node next ; } ; static Node push ( Node head_ref , int data ) { Node ptr1 = new Node ( ) ; Node temp = head_ref ; ptr1 . data = data ; ptr1 . next = head_ref ; if ( head_ref != null ) { while ( temp . next != head_ref ) temp = temp . next ; temp . next = ptr1 ; return head_ref ; } else ptr1 . next = ptr1 ; head_ref = ptr1 ; return head_ref ; } static Node deleteNode ( Node head_ref , Node del ) { Node temp = head_ref ; if ( head_ref == del ) head_ref = del . next ; while ( temp . next != del ) { temp = temp . next ; } temp . next = del . next ; return head_ref ; } static Node deleteEvenNodes ( Node head ) { Node ptr = head ; Node next ; do { if ( ptr . data % 2 == 0 ) deleteNode ( head , ptr ) ; next = ptr . next ; ptr = next ; } while ( ptr != head ) ; return head ; } static void printList ( Node head ) { Node temp = head ; if ( head != null ) { do { Console . Write ( " { 0 } ▁ " , temp . data ) ; temp = temp . next ; } while ( temp != head ) ; } } public static void Main ( String [ ] args ) { Node head = null ; head = push ( head , 61 ) ; head = push ( head , 12 ) ; head = push ( head , 56 ) ; head = push ( head , 2 ) ; head = push ( head , 11 ) ; head = push ( head , 57 ) ; Console . WriteLine ( "
 List ▁ after ▁ deletion ▁ : ▁ " ) ; head = deleteEvenNodes ( head ) ; printList ( head ) ; } }


"Delete alternate nodes of a Linked List | deletes alternate nodes of a list starting with head ; Change the next link of head ; Recursively call for the new next of head"
Solution in C#:

static Node deleteAlt ( Node head ) { if ( head == null ) return ; Node node = head . next ; if ( node == null ) return ; head . next = node . next ; head . next = deleteAlt ( head . next ) ; }


"Delete an element from array ( Using two traversals and one traversal ) | C # program to remove a given element from an array ; This function removes an element x from arr [ ] and returns new size after removal ( size is reduced only when x is present in arr [ ] ; Search x in array ; If x found in array ; reduce size of array and move all elements on space ahead ; Driver program to test above function ; Delete x from arr [ ]"
Solution in C#:

using System ; class GFG { static int deleteElement ( int [ ] arr , int n , int x ) { int i ; for ( i = 0 ; i < n ; i ++ ) if ( arr [ i ] == x ) break ; if ( i < n ) { n = n - 1 ; for ( int j = i ; j < n ; j ++ ) arr [ j ] = arr [ j + 1 ] ; } return n ; } public static void Main ( ) { int [ ] arr = { 11 , 15 , 6 , 8 , 9 , 10 } ; int n = arr . Length ; int x = 6 ; n = deleteElement ( arr , n , x ) ; Console . WriteLine ( " Modified ▁ array ▁ is " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }


"Delete an element from array ( Using two traversals and one traversal ) | C # program to remove a given element from an array ; This function removes an element x from arr [ ] and returns new size after removal . Returned size is n - 1 when element is present . Otherwise 0 is returned to indicate failure . ; If x is last element , nothing to do ; Start from rightmost element and keep moving elements one position ahead . ; If element was not found ; Else move the next element in place of x ; Driver Code ; Delete x from arr [ ]"
Solution in C#:

using System ; class GFG { static int deleteElement ( int [ ] arr , int n , int x ) { if ( arr [ n - 1 ] == x ) return ( n - 1 ) ; int prev = arr [ n - 1 ] , i ; for ( i = n - 2 ; i >= 0 && arr [ i ] != x ; i -- ) { int curr = arr [ i ] ; arr [ i ] = prev ; prev = curr ; } if ( i < 0 ) return 0 ; arr [ i ] = prev ; return ( n - 1 ) ; } public static void Main ( ) { int [ ] arr = { 11 , 15 , 6 , 8 , 9 , 10 } ; int n = arr . Length ; int x = 6 ; n = deleteElement ( arr , n , x ) ; Console . WriteLine ( " Modified ▁ array ▁ is " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }


"Delete array element in given index range [ L | C # code to delete element in given range ; Delete L to R elements ; Return size of Array after delete element ; Driver Code"
Solution in C#:

using System ; class GFG { static int deleteElement ( int [ ] A , int L , int R , int N ) { int i , j = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( i <= L i >= R ) { A [ j ] = A [ i ] ; j ++ ; } } return j ; } public static void Main ( ) { int [ ] A = new int [ ] { 5 , 8 , 11 , 15 , 26 , 14 , 19 , 17 , 10 , 14 } ; int L = 2 , R = 7 ; int n = A . Length ; int res_size = deleteElement ( A , L , R , n ) ; for ( int i = 0 ; i < res_size ; i ++ ) Console . Write ( A [ i ] + " ▁ " ) ; } }


"Delete consecutive same words in a sequence | C # implementation of above method ; Method to find the size of manipulated sequence ; Start traversing the sequence ; Push the current string if the stack is empty ; compare the current string with stack top if equal , pop the top ; Otherwise push the current string ; Return stack size ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static int removeConsecutiveSame ( List < string > v ) { Stack < string > st = new Stack < string > ( ) ; for ( int i = 0 ; i < v . Count ; i ++ ) { if ( st . Count == 0 ) { st . Push ( v [ i ] ) ; } else { string str = st . Peek ( ) ; if ( str . Equals ( v [ i ] ) ) { st . Pop ( ) ; } else { st . Push ( v [ i ] ) ; } } } return st . Count ; } public static void Main ( string [ ] args ) { List < string > v = new List < string > ( ) ; v . Add ( " ab " ) ; v . Add ( " aa " ) ; v . Add ( " aa " ) ; v . Add ( " bcd " ) ; v . Add ( " ab " ) ; Console . WriteLine ( removeConsecutiveSame ( v ) ) ; } }


"Delete consecutive same words in a sequence | C # program to remove consecutive same words ; Method to find the size of manipulated sequence ; Start traversing the sequence ; Compare the current string with next one Erase both if equal ; Erase function delete the element and also shifts other element that 's why   i is not updated  ; Update i , as to check from previous element again ; Reduce sequence size ; Increment i , if not equal ; Return modified size ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static int removeConsecutiveSame ( List < string > v ) { int n = v . Count ; for ( int i = 0 ; i < n - 1 ; ) { if ( v [ i ] . Equals ( v [ i + 1 ] ) ) { v . RemoveAt ( i ) ; v . RemoveAt ( i ) ; if ( i > 0 ) { i -- ; } n = n - 2 ; } else { i ++ ; } } return v . Count ; } public static void Main ( string [ ] args ) { List < string > v = new List < string > ( ) ; v . Add ( " tom " ) ; v . Add ( " jerry " ) ; v . Add ( " jerry " ) ; v . Add ( " tom " ) ; Console . WriteLine ( removeConsecutiveSame ( v ) ) ; } }


"Delete every Kth node from circular linked list | C # program to delete every kth Node from circular linked list . ; structure for a Node ; Utility function to print the circular linked list ; Function to delete every kth Node ; If list is empty , simply return . ; take two pointers - current and previous ; Check if Node is the only Node \ If yes , we reached the goal , therefore return . ; Print intermediate list . ; If more than one Node present in the list , Make previous pointer point to current Iterate current pointer k times , i . e . current Node is to be deleted . ; If Node to be deleted is head ; If Node to be deleted is last Node . ; Function to insert a Node at the end of a Circular linked list ; Create a new Node ; if the list is empty , make the new Node head Also , it will point to itself . ; traverse the list to reach the last Node and insert the Node ; Driver code ; insert Nodes in the circular linked list ; Delete every kth Node from the circular linked list ."
Solution in C#:

using System ; class GFG { public class Node { public int data ; public Node next ; public Node ( int x ) { data = x ; next = null ; } } ; static void printList ( Node head ) { if ( head == null ) return ; Node temp = head ; do { Console . Write ( temp . data + " - > " ) ; temp = temp . next ; } while ( temp != head ) ; Console . WriteLine ( head . data ) ; } static Node deleteK ( Node head_ref , int k ) { Node head = head_ref ; if ( head == null ) return null ; Node curr = head , prev = null ; while ( true ) { if ( curr . next == head && curr == head ) break ; printList ( head ) ; for ( int i = 0 ; i < k ; i ++ ) { prev = curr ; curr = curr . next ; } if ( curr == head ) { prev = head ; while ( prev . next != head ) prev = prev . next ; head = curr . next ; prev . next = head ; head_ref = head ; } else if ( curr . next == head ) { prev . next = head ; } else { prev . next = curr . next ; } } return head ; } static Node insertNode ( Node head_ref , int x ) { Node head = head_ref ; Node temp = new Node ( x ) ; if ( head == null ) { temp . next = temp ; head_ref = temp ; return head_ref ; } else { Node temp1 = head ; while ( temp1 . next != head ) temp1 = temp1 . next ; temp1 . next = temp ; temp . next = head ; } return head ; } public static void Main ( String [ ] args ) { Node head = null ; head = insertNode ( head , 1 ) ; head = insertNode ( head , 2 ) ; head = insertNode ( head , 3 ) ; head = insertNode ( head , 4 ) ; head = insertNode ( head , 5 ) ; head = insertNode ( head , 6 ) ; head = insertNode ( head , 7 ) ; head = insertNode ( head , 8 ) ; head = insertNode ( head , 9 ) ; int k = 4 ; head = deleteK ( head , k ) ; } }


"Delete last occurrence of an item from linked list | C # program to demonstrate deletion of last Node in singly linked list ; A linked list Node ; Function to delete the last occurrence ; If found key , update ; If the last occurrence is the last node ; If it is not the last node ; Utility function to create a new node with given key ; This function prints contents of linked list starting from the given Node ; Driver code"
Solution in C#:

 using System ; class GFG { public class Node { public int data ; public Node next ; } ; static void deleteLast ( Node head , int x ) { Node temp = head , ptr = null ; while ( temp != null ) { if ( temp . data == x ) ptr = temp ; temp = temp . next ; } if ( ptr != null && ptr . next == null ) { temp = head ; while ( temp . next != ptr ) temp = temp . next ; temp . next = null ; } if ( ptr != null && ptr . next != null ) { ptr . data = ptr . next . data ; temp = ptr . next ; ptr . next = ptr . next . next ; } } static Node newNode ( int x ) { Node node = new Node ( ) ; node . data = x ; node . next = null ; return node ; } static void display ( Node head ) { Node temp = head ; if ( head == null ) { Console . Write ( " null
 " ) ; return ; } while ( temp != null ) { Console . Write ( " { 0 } ▁ - - > ▁ " , temp . data ) ; temp = temp . next ; } Console . Write ( " null
 " ) ; } public static void Main ( String [ ] args ) { Node head = newNode ( 1 ) ; head . next = newNode ( 2 ) ; head . next . next = newNode ( 3 ) ; head . next . next . next = newNode ( 4 ) ; head . next . next . next . next = newNode ( 5 ) ; head . next . next . next . next . next = newNode ( 4 ) ; head . next . next . next . next . next . next = newNode ( 4 ) ; Console . Write ( " Created ▁ Linked ▁ list : ▁ " ) ; display ( head ) ; deleteLast ( head , 4 ) ; Console . Write ( " List ▁ after ▁ deletion ▁ of ▁ 4 : ▁ " ) ; display ( head ) ; } }


"Delete leaf nodes with value as x | C # code to delete all leaves with given value . ; A binary tree node ; A utility function to allocate a new node ; deleteleaves ( ) ; inorder ( ) ; Driver code"
Solution in C#:

using System ; class GfG { class Node { public int data ; public Node left , right ; } static Node newNode ( int data ) { Node newNode = new Node ( ) ; newNode . data = data ; newNode . left = null ; newNode . right = null ; return ( newNode ) ; } static Node deleteLeaves ( Node root , int x ) { if ( root == null ) return null ; root . left = deleteLeaves ( root . left , x ) ; root . right = deleteLeaves ( root . right , x ) ; if ( root . data == x && root . left == null && root . right == null ) { return null ; } return root ; } static void inorder ( Node root ) { if ( root == null ) return ; inorder ( root . left ) ; Console . Write ( root . data + " ▁ " ) ; inorder ( root . right ) ; } public static void Main ( String [ ] args ) { Node root = newNode ( 10 ) ; root . left = newNode ( 3 ) ; root . right = newNode ( 10 ) ; root . left . left = newNode ( 3 ) ; root . left . right = newNode ( 1 ) ; root . right . right = newNode ( 3 ) ; root . right . right . left = newNode ( 3 ) ; root . right . right . right = newNode ( 3 ) ; deleteLeaves ( root , 3 ) ; Console . Write ( " Inorder ▁ traversal ▁ after ▁ deletion ▁ : ▁ " ) ; inorder ( root ) ; } }


"Delete middle element of a stack | C # code to delete middle of a stack without using additional data structure . ; Deletes middle of stack of size n . Curr is current item number ; If stack is empty or all items are traversed ; Remove current item ; Remove other items ; Put all items back except middle ; Driver Code ; push elements into the stack ; Printing stack after deletion of middle ."
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void deleteMid ( Stack < char > st , int n , int curr = 0 ) { if ( st . Count == 0 curr == n ) return ; char x = st . Peek ( ) ; st . Pop ( ) ; deleteMid ( st , n , curr + 1 ) ; if ( curr != n / 2 ) st . Push ( x ) ; } public static void Main ( ) { Stack < char > st = new Stack < char > ( ) ; st . Push ( '1' ) ; st . Push ( '2' ) ; st . Push ( '3' ) ; st . Push ( '4' ) ; st . Push ( '5' ) ; st . Push ( '6' ) ; st . Push ( '7' ) ; deleteMid ( st , st . Count ) ; while ( st . Count != 0 ) { char p = st . Peek ( ) ; st . Pop ( ) ; Console . Write ( p + " ▁ " ) ; } } }


"Deletion at different positions in a Circular Linked List | C # program to delete node at different positions from a circular linked list ; structure for a node ; Function to insert a node at the end of a Circular linked list ; Create a new node ; check node is created or not ; insert data into newly created node ; check list is empty if not have any node then make first node it ; if list have already some node ; move first node to last node ; put first or head node address in new node link ; put new node address into last node link ( next ) ; Function print data of list ; if list is empty , simply show message ; traverse first to last node ; Function return number of nodes present in list ; if list is empty simply return length zero ; traverse forst to last node ; Function delete First node of Circular Linked List ; check list have any node if not then return ; check list have single node if yes then delete it and return ; traverse second to first ; now previous is last node and next is first node of list first node ( next ) link address put in last node ( previous ) link ; make second node as head node ; Function to delete last node of Circular Linked List ; check if list doesn 't have any node  if not then return ; check if list have single node if yes then delete it and return ; move first node to last previous ; Function delete node at a given poisition of Circular Linked List ; Find length of list ; check list have any node if not then return ; given index is in list or not ; delete first node ; traverse first to last node ; if index found delete that node ; Driver Code ; Deleting Node at position ; Deleting first Node ; Deleting last Node"
Solution in C#:

 using System ; class GFG { class Node { public int data ; public Node next ; } ; static Node Insert ( Node head , int data ) { Node current = head ; Node newNode = new Node ( ) ; if ( newNode == null ) { Console . Write ( "
 Memory ▁ Error
 " ) ; return null ; } newNode . data = data ; if ( head == null ) { newNode . next = newNode ; head = newNode ; return head ; } else { while ( current . next != head ) { current = current . next ; } newNode . next = head ; current . next = newNode ; } return head ; } static void Display ( Node head ) { Node current = head ; if ( head == null ) { Console . Write ( "
 Display ▁ List ▁ is ▁ empty
 " ) ; return ; } else { do { Console . Write ( " { 0 } ▁ " , current . data ) ; current = current . next ; } while ( current != head ) ; } } static int Length ( Node head ) { Node current = head ; int count = 0 ; if ( head == null ) { return 0 ; } else { do { current = current . next ; count ++ ; } while ( current != head ) ; } return count ; } static Node DeleteFirst ( Node head ) { Node previous = head , next = head ; if ( head == null ) { Console . Write ( " List is " return null ; } if ( previous . next == previous ) { head = null ; return null ; } while ( previous . next != head ) { previous = previous . next ; next = previous . next ; } previous . next = next . next ; head = previous . next ; return head ; } static Node DeleteLast ( Node head ) { Node current = head , temp = head , previous = null ; if ( head == null ) { Console . Write ( " List is " return null ; } if ( current . next == current ) { head = null ; return null ; } while ( current . next != head ) { previous = current ; current = current . next ; } previous . next = current . next ; head = previous . next ; return head ; } static Node DeleteAtPosition ( Node head , int index ) { int len = Length ( head ) ; int count = 1 ; Node previous = head , next = head ; if ( head == null ) { Console . Write ( "
 Delete ▁ Last ▁ List ▁ is ▁ empty
 " ) ; return null ; } if ( index >= len index < 0 ) { Console . Write (...


"Deletions of "01" or "10" in binary string to make it free from "01" or "10" | C # implementation of the approach ; Function to return the count of deletions of sub - strings "01" or "10" ; To store the count of 0 s and 1 s ; Driver code"
Solution in C#:

using System ; class GFG { static int substrDeletion ( string str , int len ) { int count0 = 0 , count1 = 0 ; for ( int i = 0 ; i < len ; i ++ ) { if ( str [ i ] == '0' ) count0 ++ ; else count1 ++ ; } return Math . Min ( count0 , count1 ) ; } public static void Main ( ) { string str = "010" ; int len = str . Length ; Console . Write ( substrDeletion ( str , len ) ) ; } }


"Demlo number ( Square of 11. . .1 ) | C # program to print DemloNumber ; To return demlo number . This function assumes that the length of str is smaller than 10. ; Add numbers to res upto size of str and then add number reverse to it ; Driver program to test printDemlo ( )"
Solution in C#:

using System ; public class GFG { static String printDemlo ( String str ) { int len = str . Length ; String res = " " ; for ( int i = 1 ; i <= len ; i ++ ) res += i . ToString ( ) ; for ( int i = len - 1 ; i >= 1 ; i -- ) res += i . ToString ( ) ; return res ; } public static void Main ( ) { String str = "111111" ; Console . WriteLine ( printDemlo ( str ) ) ; } }


"Density of Binary Tree in One Traversal | C # program to find density of Binary Tree ; A binary tree node ; Class to implement pass by reference of size ; Function to compute height and size of a binary tree ; compute height of each subtree ; increase size by 1 ; return larger of the two ; function to calculate density of a binary tree ; Finds height and size ; Driver code"
Solution in C#:

using System ; class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } class Size { public int size = 0 ; } class BinaryTree { Node root ; int heighAndSize ( Node node , Size size ) { if ( node == null ) return 0 ; int l = heighAndSize ( node . left , size ) ; int r = heighAndSize ( node . right , size ) ; size . size ++ ; return ( l > r ) ? l + 1 : r + 1 ; } float density ( Node root ) { Size size = new Size ( ) ; if ( root == null ) return 0 ; int _height = heighAndSize ( root , size ) ; return ( float ) size . size / _height ; } static public void Main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; Console . WriteLine ( " Density ▁ of ▁ given ▁ " + " Binary ▁ Tree ▁ is ▁ : ▁ " + tree . density ( tree . root ) ) ; } }


"Depth of the deepest odd level node in Binary Tree | C # program to find depth of the deepest odd level node ; A Tree node ; Utility function to create a new node ; Utility function which returns whether the current node is a leaf or not ; function to return the longest odd level depth if it exists otherwise 0 ; Base case return from here ; increment current level ; if curr_level is odd and its a leaf node ; A wrapper over deepestOddLevelDepth ( ) ; 10 / \ 28 13 / \ 14 15 / \ 23 24 Let us create Binary Tree shown in above example"
Solution in C#:

using System ; class GfG { class Node { public int key ; public Node left , right ; } static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . left = null ; temp . right = null ; return ( temp ) ; } static bool isleaf ( Node curr_node ) { return ( curr_node . left == null && curr_node . right == null ) ; } static int deepestOddLevelDepthUtil ( Node curr_node , int curr_level ) { if ( curr_node == null ) return 0 ; curr_level += 1 ; if ( curr_level % 2 != 0 && isleaf ( curr_node ) ) return curr_level ; return Math . Max ( deepestOddLevelDepthUtil ( curr_node . left , curr_level ) , deepestOddLevelDepthUtil ( curr_node . right , curr_level ) ) ; } static int deepestOddLevelDepth ( Node curr_node ) { return deepestOddLevelDepthUtil ( curr_node , 0 ) ; } public static void Main ( String [ ] args ) { Node root = newNode ( 10 ) ; root . left = newNode ( 28 ) ; root . right = newNode ( 13 ) ; root . right . left = newNode ( 14 ) ; root . right . right = newNode ( 15 ) ; root . right . right . left = newNode ( 23 ) ; root . right . right . right = newNode ( 24 ) ; Console . WriteLine ( deepestOddLevelDepth ( root ) ) ; } }


"Descartes ' Circle Theorem with implementation | C # implementation of the above formulae ; Function to find the fourth circle 's  when three radius are given ; Driver code ; Radius of three circles ; Calculation of r4 using formula given above"
Solution in C#:

using System ; class GFG { static double findRadius ( double r1 , double r2 , double r3 ) { double r4 = ( r1 * r2 * r3 ) / ( r1 * r2 + r2 * r3 + r1 * r3 + 2.0 * Math . Sqrt ( r1 * r2 * r3 * ( r1 + r2 + r3 ) ) ) ; return r4 ; } public static void Main ( String [ ] args ) { double r1 = 1 ; double r2 = 1 ; double r3 = 1 ; double r4 = Math . Round ( findRadius ( r1 , r2 , r3 ) , 6 ) ; Console . Write ( " The ▁ radius ▁ of ▁ fourth ▁ circle : ▁ " + r4 ) ; } }


"Deserium Number | C # program to check whether a number is Deserium number or not ; Returns count of digits in n . ; Returns true if x is Diserium ; Compute powers of digits from right to left . ; If sum of powers is same as given number . ; Driver code"
Solution in C#:

using System ; class Deserium { static int countDigits ( int n ) { int c = 0 ; do { c ++ ; n = n / 10 ; } while ( n != 0 ) ; return c ; } static bool isDeserium ( int x ) { int temp = x ; int p = countDigits ( x ) ; int sum = 0 ; while ( x != 0 ) { int digit = x % 10 ; sum += ( int ) Math . Pow ( digit , p ) ; p -- ; x = x / 10 ; } return ( sum == temp ) ; } public static void Main ( ) { int x = 135 ; if ( isDeserium ( x ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Detect a negative cycle in a Graph using Shortest Path Faster Algorithm | C # program for the above approach ; Stores the adjacency list of the given graph ; Create Adjacency List ; Stores the distance of all reachable vertex from source ; Check if vertex is present in queue or not ; Counts the relaxation for each vertex ; Distance from src to src is 0 ; Create a queue ; Push source in the queue ; Mark source as visited ; Front vertex of Queue ; Relaxing all edges of vertex from the Queue ; Update the dist [ v ] to minimum distance ; If vertex v is in Queue ; Negative cycle ; No cycle found ; Driver Code ; Number of vertices ; Given source node src ; Number of Edges ; Given Edges with weight ; If cycle is present"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static bool sfpa ( int V , int src , int [ , ] Edges , int M ) { List < pair > [ ] g = new List < pair > [ V ] ; for ( int i = 0 ; i < V ; i ++ ) { g [ i ] = new List < pair > ( ) ; } for ( int i = 0 ; i < M ; i ++ ) { int u = Edges [ i , 0 ] ; int v = Edges [ i , 1 ] ; int w = Edges [ i , 2 ] ; g [ u ] . Add ( new pair ( v , w ) ) ; } int [ ] dist = new int [ V ] ; for ( int i = 0 ; i < V ; i ++ ) dist [ i ] = int . MaxValue ; bool [ ] inQueue = new bool [ V ] ; int [ ] cnt = new int [ V ] ; dist [ src ] = 0 ; Queue < int > q = new Queue < int > ( ) ; q . Enqueue ( src ) ; inQueue [ src ] = true ; while ( q . Count != 0 ) { int u = q . Peek ( ) ; q . Dequeue ( ) ; inQueue [ u ] = false ; foreach ( pair x in g [ u ] ) { int v = x . first ; int cost = x . second ; if ( dist [ v ] > dist [ u ] + cost ) { dist [ v ] = dist [ u ] + cost ; if ( ! inQueue [ v ] ) { q . Enqueue ( v ) ; inQueue [ v ] = true ; cnt [ v ] ++ ; if ( cnt [ v ] >= V ) return true ; } } } } return false ; } public static void Main ( String [ ] args ) { int N = 4 ; int src = 0 ; int M = 4 ; int [ , ] Edges = { { 0 , 1 , 1 } , { 1 , 2 , - 1 } , { 2 , 3 , - 1 } , { 3 , 0 , - 1 } } ; if ( sfpa ( N , src , Edges , M ) == true ) Console . Write ( " Yes " + "
 " ) ; else Console . Write ( " No " + "
 " ) ; } }


"Detect cycle in Directed Graph using Topological Sort | C # program to implement the above approach ; Stack to store the visited vertices in the Topological Sort ; Store Topological Order ; Adjacency list to store edges ; To ensure visited vertex ; Function to perform DFS ; Set the vertex as visited ; Visit connected vertices ; Push into the stack on complete visit of vertex ; Function to check and return if a cycle exists or not ; Stores the position of vertex in topological order ; Pop all elements from stack ; Push element to get Topological Order ; Pop from the stack ; If parent vertex does not appear first ; Cycle exists ; Return false if cycle does not exist ; Function to add edges from u to v ; Driver code ; Insert edges ; If cycle exist"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int n ; static Stack < int > s ; static ArrayList tsort ; static ArrayList adj ; static int [ ] visited = new int [ 100001 ] ; static void dfs ( int u ) { visited [ u ] = 1 ; foreach ( int it in ( ArrayList ) adj [ u ] ) { if ( visited [ it ] == 0 ) dfs ( it ) ; } s . Push ( u ) ; } static bool check_cycle ( ) { Dictionary < int , int > pos = new Dictionary < int , int > ( ) ; int ind = 0 ; while ( s . Count != 0 ) { pos . Add ( s . Peek ( ) , ind ) ; tsort . Add ( s . Peek ( ) ) ; ind += 1 ; s . Pop ( ) ; } for ( int i = 0 ; i < n ; i ++ ) { foreach ( int it in ( ArrayList ) adj [ i ] ) { if ( pos [ i ] > pos [ it ] ) { return true ; } } } return false ; } static void addEdge ( int u , int v ) { ( ( ArrayList ) adj [ u ] ) . Add ( v ) ; } public static void Main ( string [ ] args ) { n = 4 ; s = new Stack < int > ( ) ; adj = new ArrayList ( ) ; tsort = new ArrayList ( ) ; for ( int i = 0 ; i < 4 ; i ++ ) adj . Add ( new ArrayList ( ) ) ; addEdge ( 0 , 1 ) ; addEdge ( 0 , 2 ) ; addEdge ( 1 , 2 ) ; addEdge ( 2 , 0 ) ; addEdge ( 2 , 3 ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( visited [ i ] == 0 ) { dfs ( i ) ; } } if ( check_cycle ( ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Detect if two integers have opposite signs | C # Program to Detect if two integers have opposite signs . ; Function to detect signs ; Driver Code"
Solution in C#:

using System ; class GFG { static bool oppositeSigns ( int x , int y ) { return ( ( x ^ y ) < 0 ) ; } public static void Main ( ) { int x = 100 , y = - 100 ; if ( oppositeSigns ( x , y ) == true ) Console . Write ( " Signs ▁ are ▁ opposite " ) ; else Console . Write ( " Signs ▁ are ▁ not ▁ opposite " ) ; } }


"Detect loop in a linked list | C # program to detect loop in a linked list ; Link list node ; Allocate node ; Put in the data ; Link the old list off the new node ; Move the head to point to the new node ; Returns true if there is a loop in linked list else returns false . ; If this node is already traverse it means there is a cycle ( Because you we encountering the node for the second time ) . ; If we are seeing the node for the first time , mark its flag as 1 ; Driver code ; Start with the empty list ; Create a loop for testing"
Solution in C#:

using System ; class GFG { class Node { public int data ; public Node next ; public int flag ; } ; static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . flag = 0 ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } static bool detectLoop ( Node h ) { while ( h != null ) { if ( h . flag == 1 ) return true ; h . flag = 1 ; h = h . next ; } return false ; } public static void Main ( string [ ] args ) { Node head = null ; head = push ( head , 20 ) ; head = push ( head , 4 ) ; head = push ( head , 15 ) ; head = push ( head , 10 ) ; head . next . next . next . next = head ; if ( detectLoop ( head ) ) Console . Write ( " Loop ▁ found " ) ; else Console . Write ( " No ▁ Loop " ) ; } }


"Detect loop in a linked list | C # program to detect loop in a linked list ; head of list ; Linked list Node ; Inserts a new Node at front of the list . ; 1 & 2 : Allocate the Node & Put in the data ; 3. Make next of new Node as head ; 4. Move the head to point to new Node ; Driver code ; Start with the empty list ; Create loop for testing"
Solution in C#:

using System ; public class LinkedList { Node head ; public class Node { public int data ; public Node next ; public Node ( int d ) { data = d ; next = null ; } } public void push ( int new_data ) { Node new_node = new Node ( new_data ) ; new_node . next = head ; head = new_node ; } Boolean detectLoop ( ) { Node slow_p = head , fast_p = head ; while ( slow_p != null && fast_p != null && fast_p . next != null ) { slow_p = slow_p . next ; fast_p = fast_p . next . next ; if ( slow_p == fast_p ) { return true ; } } return false ; } public static void Main ( String [ ] args ) { LinkedList llist = new LinkedList ( ) ; llist . push ( 20 ) ; llist . push ( 4 ) ; llist . push ( 15 ) ; llist . push ( 10 ) ; llist . head . next . next . next . next = llist . head ; Boolean found = llist . detectLoop ( ) ; if ( found ) { Console . WriteLine ( " Loop ▁ Found " ) ; } else { Console . WriteLine ( " No ▁ Loop " ) ; } } }


"Detect loop in a linked list | C # program to return first node of loop ; A utility function to print a linked list ; Function to detect first node of loop in a linked list that may contain loop ; Create a temporary node ; This condition is for the case when there is no loop ; Check if next is already pointing to temp ; Store the pointer to the next node in order to get to it in the next step ; Make next point to temp ; Get to the next node in the list ; Driver code ; Create a loop for testing ( 5 is pointing to 3 )"
Solution in C#:

using System ; public class GFG { public class Node { public int key ; public Node next ; } ; static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . next = null ; return temp ; } static void printList ( Node head ) { while ( head != null ) { Console . Write ( head . key + " ▁ " ) ; head = head . next ; } Console . WriteLine ( ) ; } static Boolean detectLoop ( Node head ) { Node temp = new Node ( ) ; while ( head != null ) { if ( head . next == null ) { return false ; } if ( head . next == temp ) { return true ; } Node nex = head . next ; head . next = temp ; head = nex ; } return false ; } public static void Main ( String [ ] args ) { Node head = newNode ( 1 ) ; head . next = newNode ( 2 ) ; head . next . next = newNode ( 3 ) ; head . next . next . next = newNode ( 4 ) ; head . next . next . next . next = newNode ( 5 ) ; head . next . next . next . next . next = head . next . next ; Boolean found = detectLoop ( head ) ; if ( found ) { Console . WriteLine ( " Loop ▁ Found " ) ; } else { Console . WriteLine ( " No ▁ Loop " ) ; } } }