"Insertion Sort by Swapping Elements | Recursive C # program to sort an array by swapping elements ; Utility function to print a Vector ; Function performs insertion sort on vector V ; General Case Sort V till second last element and then insert last element into V ; Insertion step ; Insert V [ i ] into list 0. . i - 1 ; Swap V [ j ] and V [ j - 1 ] ; Decrement j by 1 ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void printVector ( List < int > V ) { for ( int i = 0 ; i < V . Count ; i ++ ) { Console . Write ( V [ i ] + " ▁ " ) ; } Console . WriteLine ( ) ; } static void insertionSortRecursive ( List < int > V , int N ) { if ( N <= 1 ) return ; insertionSortRecursive ( V , N - 1 ) ; int j = N - 1 ; while ( j > 0 && V [ j ] < V [ j - 1 ] ) { int temp = V [ j ] ; V [ j ] = V [ j - 1 ] ; V [ j - 1 ] = temp ; j -= 1 ; } } public static void Main ( String [ ] args ) { List < int > A = new List < int > ( ) ; A . Insert ( 0 , 9 ) ; A . Insert ( 1 , 8 ) ; A . Insert ( 2 , 7 ) ; A . Insert ( 3 , 5 ) ; A . Insert ( 4 , 2 ) ; A . Insert ( 5 , 1 ) ; A . Insert ( 6 , 2 ) ; A . Insert ( 7 , 3 ) ; Console . Write ( " Array : ▁ " ) ; printVector ( A ) ; Console . Write ( " After ▁ Sorting ▁ : " ) ; insertionSortRecursive ( A , A . Count ) ; printVector ( A ) ; } }


"Insertion at Specific Position in a Circular Doubly Linked List | C # program to convert insert an element at a specific position in a circular doubly linked listing , end and middle ; Doubly linked list node ; Utility function to create a node in memory ; Function to display the list ; Function to count nunmber of elements in the list ; Declare temp pointer to traverse the list ; Variable to store the count ; Iterate the list and increment the count ; As the list is circular , increment the counter at last ; Function to insert a node at a given position in the circular doubly linked list ; Declare two pointers ; Create a new node in memory ; Point temp to start ; count of total elements in the list ; If list is empty or the position is not valid , return false ; Assign the data ; Iterate till the loc ; See in Image , circle 1 ; See in Image , Circle 2 ; See in Image , Circle 3 ; See in Image , Circle 4 ; Function to create circular doubly linked list from array elements ; Declare newNode and temporary pointer ; Iterate the loop until array length ; Create new node ; Assign the array data ; If it is first element Put that node prev and next as start as it is circular ; Find the last node ; Add the last node to make them in circular fashion ; Driver Code ; Array elements to create circular doubly linked list ; Start Pointer ; Create the List ; Display the list before insertion ; Inserting 8 at 3 rd position ; Display the list after insertion"
Solution in C#:

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


"Insertion in a Binary Tree in level order | C # program to insert element in binary tree ; A binary tree node has key , pointer to left child and a pointer to right child ; constructor ; Inorder traversal of a binary tree ; function to insert element in binary tree ; Do level order traversal until we find an empty place . ; Driver code"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { public class Node { public int key ; public Node left , right ; public Node ( int key ) { this . key = key ; left = null ; right = null ; } } static Node root ; static void inorder ( Node temp ) { if ( temp == null ) return ; inorder ( temp . left ) ; Console . Write ( temp . key + " ▁ " ) ; inorder ( temp . right ) ; } static void insert ( Node temp , int key ) { if ( temp == null ) { root = new Node ( key ) ; return ; } Queue < Node > q = new Queue < Node > ( ) ; q . Enqueue ( temp ) ; while ( q . Count != 0 ) { temp = q . Peek ( ) ; q . Dequeue ( ) ; if ( temp . left == null ) { temp . left = new Node ( key ) ; break ; } else q . Enqueue ( temp . left ) ; if ( temp . right == null ) { temp . right = new Node ( key ) ; break ; } else q . Enqueue ( temp . right ) ; } } public static void Main ( String [ ] args ) { root = new Node ( 10 ) ; root . left = new Node ( 11 ) ; root . left . left = new Node ( 7 ) ; root . right = new Node ( 9 ) ; root . right . left = new Node ( 15 ) ; root . right . right = new Node ( 8 ) ; Console . Write ( " Inorder ▁ traversal ▁ before ▁ insertion : " ) ; inorder ( root ) ; int key = 12 ; insert ( root , key ) ; Console . Write ( "
 Inorder ▁ traversal ▁ after ▁ insertion : " ) ; inorder ( root ) ; } }


"Insertion in a sorted circular linked list when a random pointer is given | C # implementation of the approach ; Node structure ; Function to create a node ; Function to find and return the head ; If the list is empty ; Finding the last node of the linked list the last node must have the highest value if no such element is present then all the nodes of the linked list must be same ; Return the head ; Function to insert a new_node in the list in sorted fashion . Note that this function expects a pointer to head node as this can modify the head of the input linked list ; If the list is empty ; If the node to be inserted is the smallest then it has to be the new head ; Find the last node of the list as it will be pointing to the head ; Locate the node before the point of insertion ; Return the new head ; Function to print the nodes of the linked list ; Driver code ; Start with an empty linked list ; Create linked list from the given array ; Move to a random node if it exists ; Print the contents of the created list"
Solution in C#:

using System ; class GFG { public class Node { public Node next ; public int data ; } ; static Node create ( ) { Node new_node = new Node ( ) ; new_node . next = null ; return new_node ; } static Node find_head ( Node random ) { if ( random == null ) return null ; Node var = random ; while ( ! ( var . data > var . next . data var . next == random ) ) { var = var . ; } return var . next ; } static Node sortedInsert ( Node head_ref , Node new_node ) { Node current = head_ref ; if ( current == null ) { new_node . next = new_node ; head_ref = new_node ; } else if ( current . data >= new_node . data ) { while ( current . next != head_ref ) current = current . next ; current . next = new_node ; new_node . next = head_ref ; head_ref = new_node ; } else { while ( current . next != head_ref && current . next . data < new_node . data ) { current = current . next ; } new_node . next = current . next ; current . next = new_node ; } return head_ref ; } static void printList ( Node start ) { Node temp ; if ( start != null ) { temp = start ; do { Console . Write ( temp . data + " ▁ " ) ; temp = temp . next ; } while ( temp != start ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 12 , 56 , 2 , 11 , 1 , 90 } ; int i ; Node start = null ; Node temp ; for ( i = 0 ; i < 6 ; i ++ ) { if ( start != null ) for ( int j = 0 ; j < ( new Random ( ) . Next ( ) * 10 ) ; j ++ ) start = start . next ; temp = create ( ) ; temp . data = arr [ i ] ; start = sortedInsert ( find_head ( start ) , temp ) ; } printList ( find_head ( start ) ) ; } }


"Insertion sort to sort even and odd positioned elements in different orders | C # program to sort even positioned elements in ascending order and odd positioned elements in descending order . ; Function to calculate the given problem . ; checking for odd positioned . ; Inserting even positioned elements in ascending order . ; sorting the even positioned . ; Inserting odd positioned elements in descending order . ; A utility function to print an array of size n ; Driver code"
Solution in C#:

 using System ; class GFG { static void evenOddInsertionSort ( int [ ] arr , int n ) { for ( int i = 2 ; i < n ; i ++ ) { int j = i - 2 ; int temp = arr [ i ] ; if ( ( ( i + 1 ) & 1 ) == 1 ) { while ( j >= 0 && temp >= arr [ j ] ) { arr [ j + 2 ] = arr [ j ] ; j -= 2 ; } arr [ j + 2 ] = temp ; } else { while ( j >= 0 && temp <= arr [ j ] ) { arr [ j + 2 ] = arr [ j ] ; j -= 2 ; } arr [ j + 2 ] = temp ; } } } static void printArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( " { 0 } ▁ " , arr [ i ] ) ; } Console . Write ( "
 " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 12 , 11 , 13 , 5 , 6 } ; int n = arr . Length ; evenOddInsertionSort ( arr , n ) ; printArray ( arr , n ) ; } }


"Integer part of the geometric mean of the divisors of N | C # implementation of the approach ; Function to return the integer part of the geometric mean of the divisors of n ; Driver code"
Solution in C#:

using System ; class GFG { static int geometricMean ( int n ) { return ( int ) Math . Sqrt ( n ) ; } public static void Main ( String [ ] args ) { int n = 16 ; Console . WriteLine ( geometricMean ( n ) ) ; } }


"Integers from the range that are composed of a single distinct digit | C # implementation of the approach ; Boolean function to check distinct digits of a number ; Take last digit ; Check if all other digits are same as last digit ; Remove last digit ; Function to return the count of integers that are composed of a single distinct digit only ; If i has single distinct digit ; Driver code"
Solution in C#:

using System ; class GFG { static Boolean checkDistinct ( int x ) { int last = x % 10 ; while ( x > 0 ) { if ( x % 10 != last ) return false ; x = x / 10 ; } return true ; } static int findCount ( int L , int R ) { int count = 0 ; for ( int i = L ; i <= R ; i ++ ) { if ( checkDistinct ( i ) ) count += 1 ; } return count ; } static public void Main ( String [ ] args ) { int L = 10 , R = 50 ; Console . WriteLine ( findCount ( L , R ) ) ; } }


"Integers from the range that are composed of a single distinct digit | C # implementation of the approach ; Function to return the count of digits of a number ; Function to return a number that contains only digit ' d ' repeated exactly count times ; Function to return the count of integers that are composed of a single distinct digit only ; Count of digits in L and R ; First digits of L and R ; If L has lesser number of digits than R ; If the number that starts with firstDigitL and has number of digits = countDigitsL is within the range include the number ; Exclude the number ; If the number that starts with firstDigitR and has number of digits = countDigitsR is within the range include the number ; Exclude the number ; If both L and R have equal number of digits ; Include the number greater than L upto the maximum number whose digit = coutDigitsL ; Exclude the numbers which are greater than R ; Return the count ; Driver code"
Solution in C#:

using System ; class GFG { static int countDigits ( int n ) { int count = 0 ; while ( n > 0 ) { count += 1 ; n /= 10 ; } return count ; } static int getDistinct ( int d , int count ) { int num = 0 ; count = ( int ) Math . Pow ( 10 , count - 1 ) ; while ( count > 0 ) { num += ( count * d ) ; count /= 10 ; } return num ; } static int findCount ( int L , int R ) { int count = 0 ; int countDigitsL = countDigits ( L ) ; int countDigitsR = countDigits ( R ) ; int firstDigitL = ( L / ( int ) Math . Pow ( 10 , countDigitsL - 1 ) ) ; int firstDigitR = ( R / ( int ) Math . Pow ( 10 , countDigitsR - 1 ) ) ; if ( countDigitsL < countDigitsR ) { count += ( 9 * ( countDigitsR - countDigitsL - 1 ) ) ; if ( getDistinct ( firstDigitL , countDigitsL ) >= L ) count += ( 9 - firstDigitL + 1 ) ; else count += ( 9 - firstDigitL ) ; if ( getDistinct ( firstDigitR , countDigitsR ) <= R ) count += firstDigitR ; else count += ( firstDigitR - 1 ) ; } else { if ( getDistinct ( firstDigitL , countDigitsL ) >= L ) count += ( 9 - firstDigitL + 1 ) ; else count += ( 9 - firstDigitL ) ; if ( getDistinct ( firstDigitR , countDigitsR ) <= R ) count -= ( 9 - firstDigitR ) ; else count -= ( 9 - firstDigitR + 1 ) ; } return count ; } public static void Main ( ) { int L = 10 , R = 50 ; Console . WriteLine ( findCount ( L , R ) ) ; } }


"Interactive Problems in Competitive Programming |  ; Applying Binary Search interactively ; Print the guessed number ; Input the response from the judge"
Solution in C#:

using System ; class GFG { static void Main ( ) { int lower_bound = 2 ; int upper_bound = 10 ; while ( lower_bound <= upper_bound ) { int mid = ( lower_bound + upper_bound ) / 2 ; Console . WriteLine ( mid ) ; int response = Convert . ToInt32 ( Console . ReadLine ( ) ) ; if ( response == - 1 ) { lower_bound = mid + 1 ; } else if ( response == 1 ) { upper_bound = mid - 1 ; } else if ( response == 0 ) { Console . WriteLine ( " Number ▁ guessed ▁ is ▁ : " + mid ) ; break ; } } } }


"Interactive Problems in Competitive Programming |  ; Iterating from lower_bound to upper_bound ; Input the response from the judge"
Solution in C#:

using System ; class GFG { public static void Main ( string [ ] args ) { int lower_bound = 2 ; int upper_bound = 10 ; for ( int i = lower_bound ; i <= upper_bound ; i ++ ) { Console . WriteLine ( i ) ; int response = int . Parse ( Console . ReadLine ( ) ) ; if ( response == 0 ) { Console . WriteLine ( " Number ▁ guessed ▁ is ▁ : " + i ) ; break ; } } } }


"Interchange elements of first and last rows in matrix | C # code to swap the element of first and last row and display the result ; swapping of element between first and last rows ; Driver code ; input in the array ; printing the interchanged matrix"
Solution in C#:

using System ; class GFG { public static void interchangeFirstLast ( int [ ] [ ] m ) { int rows = m . Length ; for ( int i = 0 ; i < m [ 0 ] . Length ; i ++ ) { int t = m [ 0 ] [ i ] ; m [ 0 ] [ i ] = m [ rows - 1 ] [ i ] ; m [ rows - 1 ] [ i ] = t ; } } public static void Main ( string [ ] args ) { int [ ] [ ] m = new int [ ] [ ] { new int [ ] { 8 , 9 , 7 , 6 } , new int [ ] { 4 , 7 , 6 , 5 } , new int [ ] { 3 , 2 , 1 , 8 } , new int [ ] { 9 , 9 , 7 , 7 } } ; interchangeFirstLast ( m ) ; for ( int i = 0 ; i < m . Length ; i ++ ) { for ( int j = 0 ; j < m [ 0 ] . Length ; j ++ ) { Console . Write ( m [ i ] [ j ] + " ▁ " ) ; } Console . WriteLine ( ) ; } } }


"Interchanging first and second halves of strings | C # code to create two new strings by swapping first and second halves ; Function to concatenate two different halves of given strings ; Creating new strings by exchanging the first half of a and b . ; Driver Code ; Calling function"
Solution in C#:

 using System ; class GFG { static void swapTwoHalves ( string a , string b ) { int la = a . Length ; int lb = b . Length ; string c = a . Substring ( 0 , la / 2 ) + b . Substring ( lb / 2 , lb / 2 + 1 ) ; string d = b . Substring ( 0 , lb / 2 ) + a . Substring ( la / 2 , la / 2 ) ; Console . Write ( c + "
 " + d + "
 " ) ; } static void Main ( ) { string a = " remuneration " ; string b = " day " ; swapTwoHalves ( a , b ) ; } }


"Interesting facts about Fibonacci numbers | C # program to demonstrate that Fibonacci numbers that are divisible by their indexes have indexes as either power of 5 or multiple of 12. ; storing Fibonacci numbers"
Solution in C#:

 using System ; class GFG { static int MAX = 100 ; static void Main ( ) { long [ ] arr = new long [ MAX ] ; arr [ 0 ] = 0 ; arr [ 1 ] = 1 ; for ( int i = 2 ; i < MAX ; i ++ ) arr [ i ] = arr [ i - 1 ] + arr [ i - 2 ] ; Console . Write ( " Fibonacci ▁ numbers ▁ divisible ▁ by ▁ " + " their ▁ indexes ▁ are ▁ :
 " ) ; for ( int i = 1 ; i < MAX ; i ++ ) if ( arr [ i ] % i == 0 ) System . Console . Write ( i + " ▁ " ) ; } }


"Interpolation Search | C # program to implement interpolation search ; If x is present in arr [ 0. . n - 1 ] , then returns index of it , else returns - 1. ; Since array is sorted , an element present in array must be in range defined by corner ; Probing the position with keeping uniform distribution in mind . ; Condition of target found ; If x is larger , x is in right sub array ; If x is smaller , x is in left sub array ; Driver Code ; Array of items on which search will be conducted . ; Element to be searched ; If element was found"
Solution in C#:

using System ; class GFG { static int interpolationSearch ( int [ ] arr , int lo , int hi , int x ) { int pos ; if ( lo <= hi && x >= arr [ lo ] && x <= arr [ hi ] ) { pos = lo + ( ( ( hi - lo ) / ( arr [ hi ] - arr [ lo ] ) ) * ( x - arr [ lo ] ) ) ; if ( arr [ pos ] == x ) return pos ; if ( arr [ pos ] < x ) return interpolationSearch ( arr , pos + 1 , hi , x ) ; if ( arr [ pos ] > x ) return interpolationSearch ( arr , lo , pos - 1 , x ) ; } return - 1 ; } public static void Main ( ) { int [ ] arr = new int [ ] { 10 , 12 , 13 , 16 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 33 , 35 , 42 , 47 } ; int n = arr . Length ; int x = 18 ; int index = interpolationSearch ( arr , 0 , n - 1 , x ) ; if ( index != - 1 ) Console . WriteLine ( " Element ▁ found ▁ at ▁ index ▁ " + index ) ; else Console . WriteLine ( " Element ▁ not ▁ found . " ) ; } }


"Interprime | C # program to check if a number is interprime or not ; Function to check if a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to check if the given number is interprime or not ; Smallest Interprime is 4 So the number less than 4 can not be a Interprime ; Calculate first prime number < n ; Calculate first prime number > n ; check if next_prime and prev_prime have the same average ; Driver Code"
Solution in C#:

using System ; class GFG { static bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } static bool isInterprime ( int n ) { if ( n < 4 ) return false ; int prev_prime = n ; int next_prime = n ; while ( ! isPrime ( prev_prime ) ) { prev_prime -- ; } while ( ! isPrime ( next_prime ) ) { next_prime ++ ; } if ( ( prev_prime + next_prime ) == 2 * n ) return true ; else return false ; } public static void Main ( ) { int n = 9 ; if ( isInterprime ( n ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }


"Intersection point of two Linked Lists | Set 3 | C # program for the above approach ; Structure of a node of a Linked List ; Constructor ; Function to find the intersection point of the two Linked Lists ; Traverse the first linked list and multiply all values by - 1 ; Update a -> data ; Update a ; Traverse the second Linked List and find the value of the first node having negative value ; Intersection point ; Update b ; Function to create linked lists ; Linked List L1 ; Linked List L2 ; Driver Code"
Solution in C#:

using System ; public class Node { public int data ; public Node next ; public Node ( int x ) { public Node ( int x ) { data = x ; next = null ; } } public class GFG { static Node head1 = null ; static Node head2 = null ; static Node intersectingNode ( Node headA , Node headB ) { Node a = headA ; while ( a != null ) { a . data *= - 1 ; a = a . next ; } Node b = headB ; while ( b != null ) { if ( b . data < 0 ) break ; b = b . next ; } return b ; } static void formLinkList ( ) { head1 = new Node ( 3 ) ; head1 . next = new Node ( 6 ) ; head1 . next . next = new Node ( 9 ) ; head1 . next . next . next = new Node ( 15 ) ; head1 . next . next . next . next = new Node ( 30 ) ; head2 = new Node ( 10 ) ; head2 . next = head1 . next . next . next ; return ; } static public void Main ( ) { formLinkList ( ) ; Console . WriteLine ( Math . Abs ( intersectingNode ( head1 , head2 ) . data ) ) ; } }


"Is it possible to reach N and M from 1 and 0 respectively as per given condition | C # program for the above approach ; Function that find given x and y is possible or not ; Check if x is less than 2 and y is not equal to 0 ; Perform subtraction ; Check if y is divisible by 2 and greater than equal to 0 ; Driver Code ; Given X and Y ; Function Call"
Solution in C#:

using System ; class GFG { static bool is_possible ( int x , int y ) { if ( x < 2 && y != 0 ) return false ; y = y - x + 1 ; if ( y % 2 == 0 && y >= 0 ) return true ; else return false ; } public static void Main ( string [ ] args ) { int x = 5 , y = 2 ; if ( is_possible ( x , y ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Iterated Logarithm log * ( n ) | Iterative C # function to find value of Iterated Logarithm"
Solution in C#:

static int iterativeLogStar ( double n , double b ) { int count = 0 ; while ( n >= 1 ) { n = _log ( n , b ) ; count ++ ; } return count ; }


"Iterated Logarithm log * ( n ) | Recursive C # program to find value of Iterated Logarithm ; Driver code"
Solution in C#:

using System ; public class GFG { static int _log ( double x , double baset ) { return ( int ) ( Math . Log ( x ) / Math . Log ( baset ) ) ; } static double recursiveLogStar ( double n , double b ) { if ( n > 1.0 ) return 1.0 + recursiveLogStar ( _log ( n , b ) , b ) ; else return 0 ; } static public void Main ( ) { int n = 100 , baset = 5 ; Console . WriteLine ( " Log * ( " + n + " ) ▁ = ▁ " + recursiveLogStar ( n , baset ) ) ; } }


"Iterative HeapSort | C # implementation of Iterative Heap Sort ; function build Max Heap where value of each child is always smaller than value of their parent ; if child is bigger than parent ; swap child and parent until parent is smaller ; swap value of first indexed with last indexed ; maintaining heap property after each swapping ; if left child is smaller than right child point index variable to right child ; if parent is smaller than child then swapping parent with child having higher value ; A utility function to print array of size n ; Driver Code"
Solution in C#:

using System ; class HeapSort { static void buildMaxHeap ( int [ ] arr , int n ) { for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > arr [ ( i - 1 ) / 2 ] ) { int j = i ; while ( arr [ j ] > arr [ ( j - 1 ) / 2 ] ) { swap ( arr , j , ( j - 1 ) / 2 ) ; j = ( j - 1 ) / 2 ; } } } } static void heapSort ( int [ ] arr , int n ) { buildMaxHeap ( arr , n ) ; for ( int i = n - 1 ; i > 0 ; i -- ) { swap ( arr , 0 , i ) ; int j = 0 , index ; do { index = ( 2 * j + 1 ) ; if ( index < ( i - 1 ) && arr [ index ] < arr [ index + 1 ] ) index ++ ; if ( index < i && arr [ j ] < arr [ index ] ) swap ( arr , j , index ) ; j = index ; } while ( index < i ) ; } } public static void swap ( int [ ] a , int i , int j ) { int temp = a [ i ] ; a [ i ] = a [ j ] ; a [ j ] = temp ; } static void printArray ( int [ ] arr ) { int n = arr . Length ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 10 , 20 , 15 , 17 , 9 , 21 } ; int n = arr . Length ; Console . Write ( " Given ▁ array : ▁ " ) ; printArray ( arr ) ; heapSort ( arr , n ) ; Console . Write ( " Sorted ▁ array : ▁ " ) ; printArray ( arr ) ; } }


"Iterative Preorder Traversal |  ; A binary tree node ; Iterative function to do Preorder traversal of the tree ; Start from root node ( set curr node to root node ) ; Run till stack is not empty or current is not NULL ; Print left children while exist and keep pushing right into the stack . ; We reach when curr is NULL , so We take out a right child from stack ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class BinaryTree { Node root ; void preorderIterative ( ) { preorderIterative ( root ) ; } void preorderIterative ( Node node ) { if ( node == null ) { return ; } Stack < Node > st = new Stack < Node > ( ) ; Node curr = node ; while ( curr != null st . Count != 0 ) { while ( curr != null ) { Console . Write ( curr . data + " ▁ " ) ; if ( curr . right != null ) st . Push ( curr . right ) ; curr = curr . left ; } if ( st . Count != 0 ) { curr = st . Pop ( ) ; } } } public static void Main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( 20 ) ; tree . root . right = new Node ( 30 ) ; tree . root . left . left = new Node ( 40 ) ; tree . root . left . left . left = new Node ( 70 ) ; tree . root . left . right = new Node ( 50 ) ; tree . root . right . left = new Node ( 60 ) ; tree . root . left . left . right = new Node ( 80 ) ; tree . preorderIterative ( ) ; } }


"Iterative Preorder Traversal | C # program to implement iterative preorder traversal ; A binary tree node ; An iterative process to print preorder traversal of Binary tree ; Base Case ; Create an empty stack and push root to it ; Pop all items one by one . Do following for every popped item a ) print it b ) push its right child c ) push its left child Note that right child is pushed first so that left is processed first ; Pop the top item from stack and print it ; Push right and left children of the popped node to stack ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class GFG { public Node root ; public virtual void iterativePreorder ( ) { iterativePreorder ( root ) ; } public virtual void iterativePreorder ( Node node ) { if ( node == null ) { return ; } Stack < Node > nodeStack = new Stack < Node > ( ) ; nodeStack . Push ( root ) ; while ( nodeStack . Count > 0 ) { Node mynode = nodeStack . Peek ( ) ; Console . Write ( mynode . data + " ▁ " ) ; nodeStack . Pop ( ) ; if ( mynode . right != null ) { nodeStack . Push ( mynode . right ) ; } if ( mynode . left != null ) { nodeStack . Push ( mynode . left ) ; } } } public static void Main ( string [ ] args ) { GFG tree = new GFG ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( 8 ) ; tree . root . right = new Node ( 2 ) ; tree . root . left . left = new Node ( 3 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . left = new Node ( 2 ) ; tree . iterativePreorder ( ) ; } }


"Iterative Quick Sort | C # program for implementation of QuickSort ; This function takes last element as pivot , places the pivot element at its correct position in sorted array , and places all smaller ( smaller than pivot ) to left of pivot and all greater elements to right of pivot ; A [ ] -- > Array to be sorted , l -- > Starting index , h -- > Ending index ; Create an auxiliary stack ; initialize top of stack ; push initial values of l and h to stack ; Keep popping from stack while is not empty ; Pop h and l ; Set pivot element at its correct position in sorted array ; If there are elements on left side of pivot , then push left side to stack ; If there are elements on right side of pivot , then push right side to stack ; Driver code ; Function calling"
Solution in C#:

using System ; class GFG { static int partition ( int [ ] arr , int low , int high ) { int temp ; int pivot = arr [ high ] ; int i = ( low - 1 ) ; for ( int j = low ; j <= high - 1 ; j ++ ) { if ( arr [ j ] <= pivot ) { i ++ ; temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } } temp = arr [ i + 1 ] ; arr [ i + 1 ] = arr [ high ] ; arr [ high ] = temp ; return i + 1 ; } static void quickSortIterative ( int [ ] arr , int l , int h ) { int [ ] stack = new int [ h - l + 1 ] ; int top = - 1 ; stack [ ++ top ] = l ; stack [ ++ top ] = h ; while ( top >= 0 ) { h = stack [ top -- ] ; l = stack [ top -- ] ; int p = partition ( arr , l , h ) ; if ( p - 1 > l ) { stack [ ++ top ] = l ; stack [ ++ top ] = p - 1 ; } if ( p + 1 < h ) { stack [ ++ top ] = p + 1 ; stack [ ++ top ] = h ; } } } public static void Main ( ) { int [ ] arr = { 4 , 3 , 5 , 2 , 1 , 3 , 2 , 3 } ; int n = 8 ; quickSortIterative ( arr , 0 , n - 1 ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }


"Iterative Quick Sort | C # program for implementation of QuickSort ; This function takes last element as pivot , places the pivot element at its correct position in sorted array , and places all smaller ( smaller than pivot ) to left of pivot and all greater elements to right of pivot ; The main function that implements QuickSort ( ) arr [ ] -- > Array to be sorted , low -- > Starting index , high -- > Ending index ; pi is partitioning index , arr [ pi ] is now at right place ; Driver code"
Solution in C#:

using System ; class GFG { static int partition ( int [ ] arr , int low , int high ) { int temp ; int pivot = arr [ high ] ; int i = ( low - 1 ) ; for ( int j = low ; j <= high - 1 ; j ++ ) { if ( arr [ j ] <= pivot ) { i ++ ; temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } } temp = arr [ i + 1 ] ; arr [ i + 1 ] = arr [ high ] ; arr [ high ] = temp ; return i + 1 ; } static void qSort ( int [ ] arr , int low , int high ) { if ( low < high ) { int pi = partition ( arr , low , high ) ; qSort ( arr , low , pi - 1 ) ; qSort ( arr , pi + 1 , high ) ; } } public static void Main ( ) { int n = 5 ; int [ ] arr = { 4 , 2 , 6 , 9 , 2 } ; qSort ( arr , 0 , n - 1 ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }


"Iterative Search for a key ' x ' in Binary Tree | An iterative method to search an item in Binary Tree ; A binary tree node has data , left child and right child ; Helper function that allocates a new node with the given data and null left and right pointers . ; iterative process to search an element x in a given binary tree ; Base Case ; Create an empty stack and . Push root to it ; Do iterative preorder traversal to search x ; See the top item from stack and check if it is same as x ; Push right and left children of the . Popped node to stack ; Driver Code"
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 node = new node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } static bool iterativeSearch ( node root , int x ) { if ( root == null ) return false ; Stack < node > nodeStack = new Stack < node > ( ) ; nodeStack . Push ( root ) ; while ( nodeStack . Count != 0 ) { node node = nodeStack . Peek ( ) ; if ( node . data == x ) return true ; nodeStack . Pop ( ) ; if ( node . right != null ) nodeStack . Push ( node . right ) ; if ( node . left != null ) nodeStack . Push ( node . left ) ; } return false ; } public static void Main ( String [ ] args ) { node root = newNode ( 2 ) ; root . left = newNode ( 7 ) ; root . right = newNode ( 5 ) ; root . left . right = newNode ( 6 ) ; root . left . right . left = newNode ( 1 ) ; root . left . right . right = newNode ( 11 ) ; root . right . right = newNode ( 9 ) ; root . right . right . left = newNode ( 4 ) ; if ( iterativeSearch ( root , 6 ) ) Console . WriteLine ( " Found " ) ; else Console . WriteLine ( " Not ▁ Found " ) ; if ( iterativeSearch ( root , 12 ) ) Console . WriteLine ( " Found " ) ; else Console . WriteLine ( " Not ▁ Found " ) ; } }


"Iterative Search for a key ' x ' in Binary Tree | Iterative level order traversal based method to search in Binary Tree ; A binary tree node has data , left child and right child ; Constructor that allocates a new node with the given data and null left and right pointers . ; An iterative process to search an element x in a given binary tree ; Base Case ; Create an empty queue for level order traversal ; Enqueue Root and initialize height ; Queue based level order traversal ; See if current node is same as x ; Remove current node and enqueue its children ; Driver code"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { public class node { public int data ; public node left ; public node right ; public node ( int data ) { this . data = data ; this . left = null ; this . right = null ; } } ; static Boolean iterativeSearch ( node root , int x ) { if ( root == null ) return false ; Queue < node > q = new Queue < node > ( ) ; q . Enqueue ( root ) ; while ( q . Count > 0 ) { node node = q . Peek ( ) ; if ( node . data == x ) return true ; q . Dequeue ( ) ; if ( node . left != null ) q . Enqueue ( node . left ) ; if ( node . right != null ) q . Enqueue ( node . right ) ; } return false ; } public static void Main ( String [ ] ags ) { node root = new node ( 2 ) ; root . left = new node ( 7 ) ; root . right = new node ( 5 ) ; root . left . right = new node ( 6 ) ; root . left . right . left = new node ( 1 ) ; root . left . right . right = new node ( 11 ) ; root . right . right = new node ( 9 ) ; root . right . right . left = new node ( 4 ) ; Console . WriteLine ( ( iterativeSearch ( root , 6 ) ? " Found
 " : " Not ▁ Found " ) ) ; Console . Write ( ( iterativeSearch ( root , 12 ) ? " Found
 " : " Not ▁ Found
 " ) ) ; } }


"Iterative Tower of Hanoi | C # program for iterative Tower of Hanoi ; A structure to represent a stack ; function to create a stack of given capacity . ; Stack is full when top is equal to the last index ; Stack is empty when top is equal to - 1 ; Function to add an item to stack . It increases top by 1 ; Function to remove an item from stack . It decreases top by 1 ; Function to show the movement of disks ; Function to implement legal movement between two poles ; When pole 1 is empty ; When pole2 pole is empty ; When top disk of pole1 > top disk of pole2 ; When top disk of pole1 < top disk of pole2 ; Function to implement TOH puzzle ; If number of disks is even , then interchange destination pole and auxiliary pole ; Larger disks will be pushed first ; Driver code ; Input : number of disks ; Create three stacks of size ' num _ of _ disks ' to hold the disks"
Solution in C#:

using System ; class GFG { public class Stack { public int capacity ; public int top ; public int [ ] array ; } Stack createStack ( int capacity ) { Stack stack = new Stack ( ) ; stack . capacity = capacity ; stack . top = - 1 ; stack . array = new int [ capacity ] ; return stack ; } Boolean isFull ( Stack stack ) { return ( stack . top == stack . capacity - 1 ) ; } Boolean isEmpty ( Stack stack ) { return ( stack . top == - 1 ) ; } void push ( Stack stack , int item ) { if ( isFull ( stack ) ) return ; stack . array [ ++ stack . top ] = item ; } int pop ( Stack stack ) { if ( isEmpty ( stack ) ) return int . MinValue ; return stack . array [ stack . top -- ] ; } void moveDisk ( char fromPeg , char toPeg , int disk ) { Console . WriteLine ( " Move ▁ the ▁ disk ▁ " + disk + " ▁ from ▁ " + fromPeg + " ▁ to ▁ " + toPeg ) ; } void moveDisksBetweenTwoPoles ( Stack src , Stack dest , char s , char d ) { int pole1TopDisk = pop ( src ) ; int pole2TopDisk = pop ( dest ) ; if ( pole1TopDisk == int . MinValue ) { push ( src , pole2TopDisk ) ; moveDisk ( d , s , pole2TopDisk ) ; } else if ( pole2TopDisk == int . MinValue ) { push ( dest , pole1TopDisk ) ; moveDisk ( s , d , pole1TopDisk ) ; } else if ( pole1TopDisk > pole2TopDisk ) { push ( src , pole1TopDisk ) ; push ( src , pole2TopDisk ) ; moveDisk ( d , s , pole2TopDisk ) ; } else { push ( dest , pole2TopDisk ) ; push ( dest , pole1TopDisk ) ; moveDisk ( s , d , pole1TopDisk ) ; } } void tohIterative ( int num_of_disks , Stack src , Stack aux , Stack dest ) { int i , total_num_of_moves ; char s = ' S ' , d = ' D ' , a = ' A ' ; if ( num_of_disks % 2 == 0 ) { char temp = d ; d = a ; a = temp ; } total_num_of_moves = ( int ) ( Math . Pow ( 2 , num_of_disks ) - 1 ) ; for ( i = num_of_disks ; i >= 1 ; i -- ) push ( src , i ) ; for ( i = 1 ; i <= total_num_of_moves ; i ++ ) { if ( i % 3 == 1 ) moveDisksBetweenTwoPoles ( src , dest , s , d ) ; else if ( i % 3 == 2 ) moveDisksBetweenTwoPoles ( src , aux , s , a ) ; else if ( i % 3 == 0 ) moveDisksBetweenTwoPoles ( aux ,...


"Iterative approach to print all combinations of an Array | C # implementation of the approach ; Data array for combination ; Number of elements in the combination ; The boolean array ; Starting index of the 1 st tract of trues ; Ending index of the 1 st tract of trues ; Constructor ; Set the 1 st r Booleans to true , initialize Start and End ; Generate the very first combination ; Update the starting ending indices of trues in the boolean array ; Function that returns true if another combination can still be generated ; Function to generate the next combination ; Only one true in the tract ; Move the End and reset the End ; Set all the values to false starting from index Start and ending at index End in the boolean array ; Set the beginning elements to true ; Reset the End ; Function to print the combination generated previouslt ; If current index is set to true in the boolean array then element at current index in the original array is part of the combination generated previously ; Driver code"
Solution in C#:

using System ; namespace IterativeCombination { class Combination { private int [ ] Indices ; private int R ; private bool [ ] Flags ; private int Start ; private int End ; public Combination ( int [ ] arr , int r ) { this . Indices = arr ; this . R = r ; } public void GetFirst ( ) { Flags = new bool [ this . Indices . Length ] ; for ( int i = 0 ; i < this . R ; ++ i ) { Flags [ i ] = true ; } this . Start = 0 ; this . End = this . R - 1 ; this . Output ( ) ; } public bool HasNext ( ) { return End < ( this . Indices . Length - 1 ) ; } public void Next ( ) { if ( this . Start == this . End ) { this . Flags [ this . End ] = false ; this . Flags [ this . End + 1 ] = true ; this . Start += 1 ; this . End += 1 ; while ( this . End + 1 < this . Indices . Length && this . Flags [ this . End + 1 ] ) { ++ this . End ; } } else { if ( this . Start == 0 ) { Flags [ this . End ] = false ; Flags [ this . End + 1 ] = true ; this . End -= 1 ; } else { Flags [ this . End + 1 ] = true ; for ( int i = this . Start ; i <= this . End ; ++ i ) { Flags [ i ] = false ; } for ( int i = 0 ; i < this . End - this . Start ; ++ i ) { Flags [ i ] = true ; } this . End = this . End - this . Start - 1 ; this . Start = 0 ; } } this . Output ( ) ; } private void Output ( ) { for ( int i = 0 , count = 0 ; i < Indices . Length && count < this . R ; ++ i ) { if ( Flags [ i ] ) { Console . Write ( Indices [ i ] ) ; Console . Write ( " ▁ " ) ; ++ count ; } } Console . WriteLine ( ) ; } } class AppDriver { static void Main ( ) { int [ ] arr = { 0 , 1 , 2 , 3 } ; int r = 3 ; Combination com = new Combination ( arr , r ) ; com . GetFirst ( ) ; while ( com . HasNext ( ) ) { com . Next ( ) ; } } } }


"Iterative approach to print all permutations of an Array | C # implementation of the approach ; The input array for permutation ; Index array to store indexes of input array ; The index of the first " increase " in the Index array which is the smallest i such that Indexes [ i ] < Indexes [ i + 1 ] ; Constructor ; Initialize and output the first permutation ; Allocate memory for Indexes array ; Initialize the values in Index array from 0 to n - 1 ; Set the Increase to 0 since Indexes [ 0 ] = 0 < Indexes [ 1 ] = 1 ; Output the first permutation ; Function that returns true if it is possible to generate the next permutation ; When Increase is in the end of the array , it is not possible to have next one ; Output the next permutation ; Increase is at the very beginning ; Swap Index [ 0 ] and Index [ 1 ] ; Update Increase ; Value at Indexes [ Increase + 1 ] is greater than Indexes [ 0 ] no need for binary search , just swap Indexes [ Increase + 1 ] and Indexes [ 0 ] ; Binary search to find the greatest value which is less than Indexes [ Increase + 1 ] ; Swap ; Invert 0 to Increase ; Reset Increase ; Function to output the input array ; Indexes of the input array are at the Indexes array ; Swap two values in the Indexes array ; Driver code"
Solution in C#:

using System ; namespace Permutation { class AllPermutation < T > { private readonly T [ ] Arr ; private int [ ] Indexes ; private int Increase ; public AllPermutation ( T [ ] arr ) { this . Arr = arr ; this . Increase = - 1 ; } public void GetFirst ( ) { this . Indexes = new int [ this . Arr . Length ] ; for ( int i = 0 ; i < Indexes . Length ; ++ i ) { this . Indexes [ i ] = i ; } this . Increase = 0 ; this . Output ( ) ; } public bool HasNext ( ) { return this . Increase != ( this . Indexes . Length - 1 ) ; } public void GetNext ( ) { if ( this . Increase == 0 ) { this . Swap ( this . Increase , this . Increase + 1 ) ; this . Increase += 1 ; while ( this . Increase < this . Indexes . Length - 1 && this . Indexes [ this . Increase ] > this . Indexes [ this . Increase + 1 ] ) { ++ this . Increase ; } } else { if ( this . Indexes [ this . Increase + 1 ] > this . Indexes [ 0 ] ) { this . Swap ( this . Increase + 1 , 0 ) ; } else { int start = 0 ; int end = this . Increase ; int mid = ( start + end ) / 2 ; int tVal = this . Indexes [ this . Increase + 1 ] ; while ( ! ( this . Indexes [ mid ] < tVal && this . Indexes [ mid - 1 ] > tVal ) ) { if ( this . Indexes [ mid ] < tVal ) { end = mid - 1 ; } else { start = mid + 1 ; } mid = ( start + end ) / 2 ; } this . Swap ( this . Increase + 1 , mid ) ; } for ( int i = 0 ; i <= this . Increase / 2 ; ++ i ) { this . Swap ( i , this . Increase - i ) ; } this . Increase = 0 ; } this . Output ( ) ; } private void Output ( ) { for ( int i = 0 ; i < this . Indexes . Length ; ++ i ) { Console . Write ( this . Arr [ this . Indexes [ i ] ] ) ; Console . Write ( " ▁ " ) ; } Console . WriteLine ( ) ; } private void Swap ( int p , int q ) { int tmp = this . Indexes [ p ] ; this . Indexes [ p ] = this . Indexes [ q ] ; this . Indexes [ q ] = tmp ; } } class AppDriver { static void Main ( ) { int [ ] arr = { 0 , 1 , 2 } ; AllPermutation < int > perm = new AllPermutation < int > ( arr ) ; perm . GetFirst ( ) ; while ( perm . HasNext ( ) ) { perm . GetNext ( ) ; } } } }


"Iterative diagonal traversal of binary tree | C # program to con string from binary tree ; A binary tree node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new node ; Iterative function to print diagonal view ; base case ; inbuilt queue of Treenode ; Enqueue root ; Enqueue delimiter ; if current is delimiter then insert another for next diagonal and cout nextline ; if queue is empty return ; output nextline ; Enqueue delimiter again ; if left child is present Enqueue into queue ; current equals to right child ; Driver Code"
Solution in C#:

using System ; using System . Collections ; class GFG { public class Node { public int data ; public Node left , right ; } ; static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } static void diagonalPrint ( Node root ) { if ( root == null ) return ; Queue q = new Queue ( ) ; q . Enqueue ( root ) ; q . Enqueue ( null ) ; while ( q . Count > 0 ) { Node temp = ( Node ) q . Peek ( ) ; q . Dequeue ( ) ; if ( temp == null ) { if ( q . Count == 0 ) return ; Console . WriteLine ( ) ; q . Enqueue ( null ) ; } else { while ( temp != null ) { Console . Write ( temp . data + " ▁ " ) ; if ( temp . left != null ) q . Enqueue ( temp . left ) ; temp = temp . right ; } } } } public static void Main ( String [ ] args ) { Node root = newNode ( 8 ) ; root . left = newNode ( 3 ) ; root . right = newNode ( 10 ) ; root . left . left = newNode ( 1 ) ; root . left . right = newNode ( 6 ) ; root . right . right = newNode ( 14 ) ; root . right . right . left = newNode ( 13 ) ; root . left . right . left = newNode ( 4 ) ; root . left . right . right = newNode ( 7 ) ; diagonalPrint ( root ) ; } }


"Iterative function to check if two trees are identical | Iterative C # program to check if two ; A Binary Tree Node ; Iterative method to find height of Binary Tree ; Return true if both trees are empty ; Return false if one is empty and other is not ; Create an empty queues for simultaneous traversals ; Enqueue Roots of trees in respective queues ; Get front nodes and compare them ; Remove front nodes from queues ; Enqueue left children of both nodes ; If one left child is empty and other is not ; Right child code ( Similar to left child code ) ; Utility function to create a new tree node ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GfG { class Node { public int data ; public Node left , right ; } static bool areIdentical ( Node root1 , Node root2 ) { if ( root1 == null && root2 == null ) return true ; if ( root1 == null root2 == null ) return false ; Queue < Node > q1 = new Queue < Node > ( ) ; Queue < Node > q2 = new Queue < Node > ( ) ; q1 . Enqueue ( root1 ) ; q2 . Enqueue ( root2 ) ; while ( q1 . Count != 0 && q2 . Count != 0 ) { Node n1 = q1 . Peek ( ) ; Node n2 = q2 . Peek ( ) ; if ( n1 . data != n2 . data ) return false ; q1 . Dequeue ( ) ; q2 . Dequeue ( ) ; if ( n1 . left != null && n2 . left != null ) { q1 . Enqueue ( n1 . left ) ; q2 . Enqueue ( n2 . left ) ; } else if ( n1 . left != null n2 . left != null ) return false ; if ( n1 . right != null && n2 . right != null ) { q1 . Enqueue ( n1 . right ) ; q2 . Enqueue ( n2 . right ) ; } else if ( n1 . right != null n2 . right != null ) return false ; } return true ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = null ; temp . right = null ; return temp ; } public static void Main ( String [ ] args ) { Node root1 = newNode ( 1 ) ; root1 . left = newNode ( 2 ) ; root1 . right = newNode ( 3 ) ; root1 . left . left = newNode ( 4 ) ; root1 . left . right = newNode ( 5 ) ; Node root2 = newNode ( 1 ) ; root2 . left = newNode ( 2 ) ; root2 . right = newNode ( 3 ) ; root2 . left . left = newNode ( 4 ) ; root2 . left . right = newNode ( 5 ) ; if ( areIdentical ( root1 , root2 ) == true ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Iterative method to check if two trees are mirror of each other | C # implementation to check whether the two binary tress are mirrors of each other or not ; structure of a node in binary tree ; Utility function to create and return a new node for a binary tree ; function to check whether the two binary trees are mirrors of each other or not ; iterative inorder traversal of 1 st tree and reverse inoder traversal of 2 nd tree ; if the corresponding nodes in the two traversal have different data values , then they are not mirrors of each other . ; if at any point one root becomes null and the other root is not null , then they are not mirrors . This condition verifies that structures of tree are mirrors of each other . ; we have visited the node and its left subtree . Now , it ' s ▁ right ▁ subtree ' s turn ; we have visited the node and its right subtree . Now , it ' s ▁ left ▁ subtree ' s turn ; both the trees have been completely traversed ; tress are mirrors of each other ; Driver program to test above ; 1 st binary tree formation ; 1 ; / \ ; 3 2 ; / \ ; 2 nd binary tree formation ; 1 ; / \ ; 2 3 ; / \ ; 4 5"
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 = null ; temp . right = null ; return temp ; } static String areMirrors ( Node root1 , Node root2 ) { Stack < Node > st1 = new Stack < Node > ( ) ; Stack < Node > st2 = new Stack < Node > ( ) ; while ( true ) { while ( root1 != null && root2 != null ) { if ( root1 . data != root2 . data ) return " No " ; st1 . Push ( root1 ) ; st2 . Push ( root2 ) ; root1 = root1 . left ; root2 = root2 . right ; } if ( ! ( root1 == null && root2 == null ) ) return " No " ; if ( st1 . Count != 0 && st2 . Count != 0 ) { root1 = st1 . Peek ( ) ; root2 = st2 . Peek ( ) ; st1 . Pop ( ) ; st2 . Pop ( ) ; root1 = root1 . right ; root2 = root2 . left ; } else break ; } return " Yes " ; } public static void Main ( String [ ] args ) { Node root1 = newNode ( 1 ) ; root1 . left = newNode ( 3 ) ; root1 . right = newNode ( 2 ) ; root1 . right . left = newNode ( 5 ) ; root1 . right . right = newNode ( 4 ) ; Node root2 = newNode ( 1 ) ; root2 . left = newNode ( 2 ) ; root2 . right = newNode ( 3 ) ; root2 . left . left = newNode ( 4 ) ; root2 . left . right = newNode ( 5 ) ; Console . WriteLine ( areMirrors ( root1 , root2 ) ) ; } }


"Iterative method to find ancestors of a given binary tree | C # program to print all ancestors of a given key ; Structure for a tree node ; A utility function to create a new tree node ; Iterative Function to print all ancestors of a given key ; Create a stack to hold ancestors ; Traverse the complete tree in postorder way till we find the key ; Traverse the left side . While traversing , push the nodes into the stack so that their right subtrees can be traversed later ; push current node ; move to next node ; If the node whose ancestors are to be printed is found , then break the while loop . ; Check if right sub - tree exists for the node at top If not then pop that node because we don 't   need this node any more.  ; If the popped node is right child of top , then remove the top as well . Left child of the top must have processed before . ; if stack is not empty then simply set the root as right child of top and start traversing right sub - tree . ; If stack is not empty , print contents of stack Here assumption is that the key is there in tree ; Driver code ; Let us construct a binary 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 node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return node ; } static void printAncestors ( Node root , int key ) { if ( root == null ) return ; Stack < Node > st = new Stack < Node > ( ) ; while ( 1 == 1 ) { while ( root != null && root . data != key ) { st . Push ( root ) ; root = root . left ; } if ( root != null && root . data == key ) break ; if ( st . Peek ( ) . right == null ) { root = st . Peek ( ) ; st . Pop ( ) ; while ( st . Count != 0 && st . Peek ( ) . right == root ) { root = st . Peek ( ) ; st . Pop ( ) ; } } root = st . Count == 0 ? null : st . Peek ( ) . right ; } while ( st . Count != 0 ) { Console . Write ( st . Peek ( ) . data + " ▁ " ) ; st . Pop ( ) ; } } public static void Main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 7 ) ; root . left . left = newNode ( 3 ) ; root . left . right = newNode ( 5 ) ; root . right . left = newNode ( 8 ) ; root . right . right = newNode ( 9 ) ; root . left . left . left = newNode ( 4 ) ; root . left . right . right = newNode ( 6 ) ; root . right . right . left = newNode ( 10 ) ; int key = 6 ; printAncestors ( root , key ) ; } }


"Iterative program to Calculate Size of a tree | C # programn to calculate Size of a tree ; Node Structure ; return size of tree ; if tree is empty it will return 0 ; Using level order Traversal . ; when the queue is empty : the poll ( ) method returns null . ; Increment count ; Enqueue left child ; Increment count ; Enqueue right child ; Driver code ; creating a binary tree and entering the nodes"
Solution in C#:

using System ; using System . Collections . Generic ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class BinaryTree { Node root ; public int size ( ) { if ( root == null ) return 0 ; Queue < Node > q = new Queue < Node > ( ) ; q . Enqueue ( root ) ; int count = 1 ; while ( q . Count != 0 ) { Node tmp = q . Dequeue ( ) ; if ( tmp != null ) { if ( tmp . left != null ) { count ++ ; q . Enqueue ( tmp . left ) ; } if ( tmp . right != null ) { count ++ ; q . Enqueue ( tmp . right ) ; } } } return count ; } public static void Main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; Console . WriteLine ( " The ▁ size ▁ of ▁ binary ▁ tree " + " ▁ is ▁ : ▁ " + tree . size ( ) ) ; } }


"Iterative program to count leaf nodes in a Binary Tree | C # Program for above approach ; Node class ; Program to count leaves ; If the node itself is " null " return 0 , as there are no leaves ; It the node is a leaf then both right and left children will be " null " ; Now we count the leaves in the left and right subtrees and return the sum ; Driver Code ; Get leaf count of the above created tree"
Solution in C#:

using System ; public class Node { public int data ; public Node left , right ; public Node ( int d ) { data = d ; left = right = null ; } } public class BinaryTree { public static Node root ; static int countLeaves ( Node node ) { if ( node == null ) { return 0 ; } if ( node . left == null && node . right == null ) { return 1 ; } return countLeaves ( node . left ) + countLeaves ( node . right ) ; } static public void Main ( ) { BinaryTree . root = new Node ( 1 ) ; BinaryTree . root . left = new Node ( 2 ) ; BinaryTree . root . right = new Node ( 3 ) ; BinaryTree . root . left . left = new Node ( 4 ) ; BinaryTree . root . left . right = new Node ( 5 ) ; Console . WriteLine ( countLeaves ( root ) ) ; } }


"Iterative program to count leaf nodes in a Binary Tree | C # program to count leaf nodes in a Binary Tree ; A binary tree Node has data , pointer to left child and a pointer to right child ; Function to get the count of leaf Nodes in a binary tree ; If tree is empty ; Initialize empty queue . ; Do level order traversal starting from root Initialize count of leaves ; Helper function that allocates a new Node with the given data and null left and right pointers . ; Driver Code ; 1 / \ 2 3 / \ 4 5 Let us create Binary Tree shown in above example ; get leaf count of the above created tree"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public class Node { public int data ; public Node left , right ; } static int getLeafCount ( Node node ) { if ( node == null ) { return 0 ; } Queue < Node > q = new Queue < Node > ( ) ; int count = 0 ; q . Enqueue ( node ) ; 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 . left == null && temp . right == null ) { count ++ ; } } return count ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } public static void Main ( String [ ] args ) { { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; Console . WriteLine ( getLeafCount ( root ) ) ; } } }


"Iterative selection sort for linked list |  ; Traverse the List ; Traverse the unsorted sublist ; Swap Data"
Solution in C#:

static void selectionSort ( node head ) { node temp = head ; while ( temp ) { node min = temp ; node r = temp . next ; while ( r ) { if ( min . data > r . data ) min = r ; r = r . next ; } int x = temp . data ; temp . data = min . data ; min . data = x ; temp = temp . next ; } }


"Iterative selection sort for linked list | C # implementation of the approach ; Linked List Node ; Utility function to create a new Linked List Node ; Function to sort a linked list using selection sort algorithm by swapping the next pointers ; While b is not the last node ; While d is pointing to a valid node ; If d appears immediately after b ; Case 1 : b is the head of the linked list ; Move d before b ; Swap b and d pointers ; Update the head ; Skip to the next element as it is already in order ; Case 2 : b is not the head of the linked list ; Move d before b ; Swap b and d pointers ; Skip to the next element as it is already in order ; If b and d have some non - zero number of nodes in between them ; Case 3 : b is the head of the linked list ; Swap b . next and d . next ; Swap b and d pointers ; Skip to the next element as it is already in order ; Update the head ; Case 4 : b is not the head of the linked list ; Swap b . next and d . next ; Swap b and d pointers ; Skip to the next element as it is already in order ; Update c and skip to the next element as it is already in order ; Function to print the list ; Driver Code"
Solution in C#:

using System ; class GFG { public class Node { public int data ; public Node next ; } ; static Node newNode ( int val ) { Node temp = new Node ( ) ; temp . data = val ; temp . next = null ; return temp ; } static Node selectionSort ( Node head ) { Node a , b , c , d , r ; a = b = head ; while ( b . next != null ) { c = d = b . next ; while ( d != null ) { if ( b . data > d . data ) { if ( b . next == d ) { if ( b == head ) { b . next = d . next ; d . next = b ; r = b ; b = d ; d = r ; c = d ; head = b ; d = d . next ; } else { b . next = d . next ; d . next = b ; a . next = d ; r = b ; b = d ; d = r ; c = d ; d = d . next ; } } else { if ( b == head ) { r = b . next ; b . next = d . next ; d . next = r ; c . next = b ; r = b ; b = d ; d = r ; c = d ; d = d . next ; head = b ; } else { r = b . next ; b . next = d . next ; d . next = r ; c . next = b ; a . next = d ; r = b ; b = d ; d = r ; c = d ; d = d . next ; } } } else { c = d ; d = d . next ; } } a = b ; b = b . next ; } return head ; } static void printList ( Node head ) { while ( head != null ) { Console . Write ( head . data + " ▁ " ) ; head = head . next ; } } public static void Main ( String [ ] arg ) { Node head = newNode ( 5 ) ; head . next = newNode ( 4 ) ; head . next . next = newNode ( 3 ) ; head = selectionSort ( head ) ; printList ( head ) ; } }


"Iteratively Reverse a linked list using only 2 pointers ( An Interesting Method ) | C # program to reverse a linked list using two pointers . ; Link list node ; Function to reverse the linked list using 2 pointers ; At last prev points to new head ; This expression evaluates from left to right current . next = prev , changes the link fron next to prev node prev = current , moves prev to current node for next reversal of node This example of list will clear it more 1.2 . 3.4 initially prev = 1 , current = 2 Final expression will be current = 1 ^ 2 ^ 3 ^ 2 ^ 1 , as we know that bitwise XOR of two same numbers will always be 0 i . e ; 1 ^ 1 = 2 ^ 2 = 0 After the evaluation of expression current = 3 that means it has been moved by one node from its previous position ; Function to push a node ; Allocate node ; Put in the data ; Link the old list off the new node ; Move the head to point to the new node ; Function to print linked list ; Driver code"
Solution in C#:

 using System ; using System . Collections ; using System . Collections . Generic ; class GFG { class Node { public int data ; public Node next ; } ; static Node head_ref = null ; static void reverse ( ) { Node prev = null ; Node current = head_ref ; while ( current != null ) { Node next = current . next ; current . next = prev ; prev = current ; current = next ; } head_ref = prev ; } static void push ( int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = ( head_ref ) ; ( head_ref ) = new_node ; } static void printList ( ) { Node temp = head_ref ; while ( temp != null ) { Console . Write ( temp . data + " ▁ " ) ; temp = temp . next ; } } public static void Main ( string [ ] args ) { push ( 20 ) ; push ( 4 ) ; push ( 15 ) ; push ( 85 ) ; Console . Write ( " Given ▁ linked ▁ list
 " ) ; printList ( ) ; reverse ( ) ; Console . Write ( "
 Reversed ▁ Linked ▁ list ▁
 " ) ; printList ( ) ; } }


"Jacobsthal and Jacobsthal | A DP based solution to find Jacobsthal and Jacobsthal - Lucas numbers ; Return nth Jacobsthal number . ; base case ; Return nth Jacobsthal - Lucas number . ; base case ; Driver Code"
Solution in C#:

using System ; public class GfG { public static int Jacobsthal ( int n ) { int [ ] dp = new int [ n + 1 ] ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) dp [ i ] = dp [ i - 1 ] + 2 * dp [ i - 2 ] ; return dp [ n ] ; } public static int Jacobsthal_Lucas ( int n ) { int [ ] dp = new int [ n + 1 ] ; dp [ 0 ] = 2 ; dp [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) dp [ i ] = dp [ i - 1 ] + 2 * dp [ i - 2 ] ; return dp [ n ] ; } public static void Main ( ) { int n = 5 ; Console . WriteLine ( " Jacobsthal ▁ number : ▁ " + Jacobsthal ( n ) ) ; Console . WriteLine ( " Jacobsthal - Lucas ▁ number : ▁ " + Jacobsthal_Lucas ( n ) ) ; } }


"Jaro and Jaro | C # implementation of above approach ; Function to calculate the Jaro Similarity of two Strings ; If the Strings are equal ; Length of two Strings ; Maximum distance upto which matching is allowed ; Count of matches ; Hash for matches ; Traverse through the first String ; Check if there is any matches ; If there is a match ; If there is no match ; Number of transpositions ; Count number of occurrences where two characters match but there is a third matched character in between the indices ; Find the next matched character in second String ; Return the Jaro Similarity ; Driver code ; Print jaro Similarity of two Strings"
Solution in C#:

using System ; class GFG { static double jaro_distance ( string s1 , string s2 ) { if ( s1 == s2 ) return 1.0 ; int len1 = s1 . Length ; int len2 = s2 . Length ; int max_dist = ( int ) ( Math . Floor ( ( double ) ( ( Math . Max ( len1 , len2 ) / 2 ) - 1 ) ) ) ; int match = 0 ; int [ ] hash_s1 = new int [ s1 . Length ] ; int [ ] hash_s2 = new int [ s2 . Length ] ; for ( int i = 0 ; i < len1 ; i ++ ) { for ( int j = Math . Max ( 0 , i - max_dist ) ; j < Math . Min ( len2 , i + max_dist + 1 ) ; j ++ ) if ( s1 [ i ] == s2 [ j ] && hash_s2 [ j ] == 0 ) { hash_s1 [ i ] = 1 ; hash_s2 [ j ] = 1 ; match ++ ; break ; } } if ( match == 0 ) return 0.0 ; double t = 0 ; int point = 0 ; for ( int i = 0 ; i < len1 ; i ++ ) if ( hash_s1 [ i ] == 1 ) { while ( hash_s2 [ point ] == 0 ) point ++ ; if ( s1 [ i ] != s2 [ point ++ ] ) t ++ ; } t /= 2 ; return ( ( ( double ) match ) / ( ( double ) len1 ) + ( ( double ) match ) / ( ( double ) len2 ) + ( ( double ) match - t ) / ( ( double ) match ) ) / 3.0 ; } public static void Main ( ) { string s1 = " CRATE " , s2 = " TRACE " ; Console . WriteLine ( jaro_distance ( s1 , s2 ) ) ; } }


"Jaro and Jaro | C # implementation of above approach ; Function to calculate the Jaro Similarity of two strings ; If the strings are equal ; Length of two strings ; Maximum distance upto which matching is allowed ; Count of matches ; Hash for matches ; Traverse through the first string ; Check if there is any matches ; If there is a match ; If there is no match ; Number of transpositions ; Count number of occurrences where two characters match but there is a third matched character in between the indices ; Find the next matched character in second string ; Return the Jaro Similarity ; Jaro Winkler Similarity ; If the jaro Similarity is above a threshold ; Find the length of common prefix ; If the characters match ; Else break ; Maximum of 4 characters are allowed in prefix ; Calculate jaro winkler Similarity ; Driver code ; Print Jaro - Winkler Similarity of two strings"
Solution in C#:

using System ; class GFG { static double jaro_distance ( string s1 , string s2 ) { if ( s1 == s2 ) return 1.0 ; int len1 = s1 . Length , len2 = s2 . Length ; if ( len1 == 0 len2 == 0 ) return 0.0 ; int max_dist = ( int ) Math . Floor ( ( double ) Math . Max ( len1 , len2 ) / 2 ) - 1 ; int match = 0 ; int [ ] hash_s1 = new int [ s1 . Length ] ; int [ ] hash_s2 = new int [ s2 . Length ] ; for ( int i = 0 ; i < len1 ; i ++ ) { for ( int j = Math . Max ( 0 , i - max_dist ) ; j < Math . Min ( len2 , i + max_dist + 1 ) ; j ++ ) if ( s1 [ i ] == s2 [ j ] && hash_s2 [ j ] == 0 ) { hash_s1 [ i ] = 1 ; hash_s2 [ j ] = 1 ; match ++ ; break ; } } if ( match == 0 ) return 0.0 ; double t = 0 ; int point = 0 ; for ( int i = 0 ; i < len1 ; i ++ ) if ( hash_s1 [ i ] == 1 ) { while ( hash_s2 [ point ] == 0 ) point ++ ; if ( s1 [ i ] != s2 [ point ++ ] ) t ++ ; } t /= 2 ; return ( ( ( double ) match ) / ( ( double ) len1 ) + ( ( double ) match ) / ( ( double ) len2 ) + ( ( double ) match - t ) / ( ( double ) match ) ) / 3.0 ; } static double jaro_Winkler ( string s1 , string s2 ) { double jaro_dist = jaro_distance ( s1 , s2 ) ; if ( jaro_dist > 0.7 ) { int prefix = 0 ; for ( int i = 0 ; i < Math . Min ( s1 . Length , s2 . Length ) ; i ++ ) { if ( s1 [ i ] == s2 [ i ] ) prefix ++ ; else break ; } prefix = Math . Min ( 4 , prefix ) ; jaro_dist += 0.1 * prefix * ( 1 - jaro_dist ) ; } return jaro_dist ; } public static void Main ( ) { string s1 = " TRATE " , s2 = " TRACE " ; Console . WriteLine ( " Jaro - Winkler ▁ Similarity ▁ = " + jaro_Winkler ( s1 , s2 ) ) ; } }


"Josephus Circle implementation using STL list | C # Code to find the last man Standing ; Node class to store data ; Function to find the only person left after one in every m - th node is killed in a circle of n nodes ; Create a circular linked list of size N . ; Connect last node to first ; while only one node is left in the linked list ; Find m - th node ; Remove the m - th node ; Driver program to test above functions"
Solution in C#:

using System ; public class GFG { class Node { public int data ; public Node next ; public Node ( int data ) { this . data = data ; } } static void getJosephusPosition ( int m , int n ) { Node head = new Node ( 1 ) ; Node prev = head ; for ( int i = 2 ; i <= n ; i ++ ) { prev . next = new Node ( i ) ; prev = prev . next ; } prev . next = head ; Node ptr1 = head , ptr2 = head ; while ( ptr1 . next != ptr1 ) { int count = 1 ; while ( count != m ) { ptr2 = ptr1 ; ptr1 = ptr1 . next ; count ++ ; } ptr2 . next = ptr1 . next ; ptr1 = ptr2 . next ; } Console . WriteLine ( " Last ▁ person ▁ left ▁ standing ▁ " + " ( Josephus ▁ Position ) ▁ is ▁ " + ptr1 . data ) ; } static public void Main ( String [ ] args ) { int n = 14 , m = 2 ; getJosephusPosition ( m , n ) ; } }


"Josephus Problem Using Bit Magic | C # program for Josephus Problem ; Method to find the position of the Most Significant Bit ; keeps shifting bits to the right until we are left with 0 ; method to return at which place Josephus should sit to avoid being killed ; Getting the position of the Most Significant Bit ( MSB ) . The leftmost '1' . If the number is '41' then its binary is '101001' . So msbPos ( 41 ) = 6 ; ' j ' stores the number with which to XOR the number ' n ' . Since we need '100000' We will do 1 << 6 - 1 to get '100000' ; Toggling the Most Significant Bit . Changing the leftmost '1' to '0' . 101001 ^ 100000 = 001001 ( 9 ) ; Left - shifting once to add an extra '0' to the right end of the binary number 001001 = 010010 ( 18 ) ; Toggling the '0' at the end to '1' which is essentially the same as putting the MSB at the rightmost place . 010010 | 1 = 010011 ( 19 ) ; Driver code"
Solution in C#:

using System ; public class GFG { static int msbPos ( int n ) { int pos = 0 ; while ( n != 0 ) { pos ++ ; n = n >> 1 ; } return pos ; } static int josephify ( int n ) { int position = msbPos ( n ) ; int j = 1 << ( position - 1 ) ; n = n ^ j ; n = n << 1 ; n = n | 1 ; return n ; } public static void Main ( ) { int n = 41 ; Console . WriteLine ( josephify ( n ) ) ; } }


"Jump Search | C # program to implement Jump Search . ; Finding block size to be jumped ; Finding the block where element is present ( if it is present ) ; Doing a linear search for x in block beginning with prev . ; If we reached next block or end of array , element is not present . ; If element is found ; Driver program to test function ; Find the index of ' x ' using Jump Search ; Print the index where ' x ' is located"
Solution in C#:

using System ; public class JumpSearch { public static int jumpSearch ( int [ ] arr , int x ) { int n = arr . Length ; int step = ( int ) Math . Floor ( Math . Sqrt ( n ) ) ; int prev = 0 ; while ( arr [ Math . Min ( step , n ) - 1 ] < x ) { prev = step ; step += ( int ) Math . Floor ( Math . Sqrt ( n ) ) ; if ( prev >= n ) return - 1 ; } while ( arr [ prev ] < x ) { prev ++ ; if ( prev == Math . Min ( step , n ) ) return - 1 ; } if ( arr [ prev ] == x ) return prev ; return - 1 ; } public static void Main ( ) { int [ ] arr = { 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 , 144 , 233 , 377 , 610 } ; int x = 55 ; int index = jumpSearch ( arr , x ) ; Console . Write ( " Number ▁ " + x + " ▁ is ▁ at ▁ index ▁ " + index ) ; } }


"Jump in rank of a student after updating marks | C # implementation of the approach ; Function to print the name of student who stood first after updation in rank ; Array of students ; Store the name of the student ; Update the marks of the student ; Store the current rank of the student ; Print the name and jump in rank ; Driver code ; Names of the students ; Marks of the students ; Updates that are to be done ; Number of students"
Solution in C#:

using System ; class GFG { public class Student { public string name ; public int marks , prev_rank ; public Student ( ) { this . marks = 0 ; this . prev_rank = 0 ; } } static void nameRank ( string [ ] names , int [ ] marks , int [ ] updates , int n ) { Student [ ] x = new Student [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { x [ i ] = new Student ( ) ; x [ i ] . name = names [ i ] ; x [ i ] . marks = marks [ i ] + updates [ i ] ; x [ i ] . prev_rank = i + 1 ; } Student highest = x [ 0 ] ; for ( int j = 1 ; j < n ; j ++ ) { if ( x [ j ] . marks >= highest . marks ) highest = x [ j ] ; } Console . Write ( " Name : ▁ " + highest . name + " , ▁ Jump : ▁ " + Math . Abs ( highest . prev_rank - 1 ) ) ; } public static void Main ( string [ ] args ) { string [ ] names = { " sam " , " ram " , " geek " } ; int [ ] marks = { 80 , 79 , 75 } ; int [ ] updates = { 0 , 5 , - 9 } ; int n = marks . Length ; nameRank ( names , marks , updates , n ) ; } }


"Junction Numbers | C # program for above approach ; Function to find the sum Of digits of N ; To store sum of N and sumOfdigitsof ( N ) ; extracting digit ; Function to check Junction numbers ; To store count of ways n can be represented as i + SOD ( i ) ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static int sum ( int n ) { int sum = 0 ; while ( n != 0 ) { int r = n % 10 ; sum = sum + r ; n = n / 10 ; } return sum ; } static bool isJunction ( int n ) { int count = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( i + sum ( i ) == n ) count ++ ; } return count >= 2 ; } public static void Main ( ) { int N = 111 ; if ( isJunction ( N ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"K 'th Boom Number | C # program to find K 'th Boom number ; This function uses queue data structure to K 'th Boom number ; Create an empty queue of strings ; Enqueue an empty string ; counter for K 'th element ; This loop checks the value of count to become equal to K when value of count will be equals to k we will print the Boom number ; current Boom number ; Store current Boom number before changing it ; Append "2" to string s1 and enqueue it ; Check if count == k ; K 'th Boom number ; Append "3" to string s2 and enqueue it . Note that s2 contains the previous front ; Check if count == k ; K 'th Boom number ; Driver code"
Solution in C#:

using System ; using System . Collections ; class GFG { static void boomNumber ( long k ) { Queue q = new Queue ( ) ; q . Enqueue ( " " ) ; long count = 0 ; while ( count <= k ) { string s1 = ( string ) q . Dequeue ( ) ; string s2 = s1 ; s1 += "2" ; q . Enqueue ( s1 ) ; count ++ ; if ( count == k ) { Console . Write ( s1 ) ; break ; } s2 += "3" ; q . Enqueue ( s2 ) ; count ++ ; if ( count == k ) { Console . Write ( s2 ) ; break ; } } return ; } public static void Main ( string [ ] arg ) { long k = 1000000 ; boomNumber ( k ) ; } }


"K 'th smallest element in BST using O(1) Extra Space | C # program to find k 'th largest element in BST ; A BST node ; A function to find ; Count to iterate over elements till we get the kth smallest number ; store the Kth smallest ; to store the current node ; Like Morris traversal if current does not have left child rather than printing as we did in inorder , we will just increment the count as the number will be in an increasing order ; if count is equal to K then we found the kth smallest , so store it in ksmall ; go to current 's right child ; we create links to Inorder Successor and count using these links ; building links ; link made to Inorder Successor ; While breaking the links in so made temporary threaded tree we will check for the K smallest condition ; Revert the changes made in if part ( break link from the Inorder Successor ) ; If count is equal to K then we found the kth smallest and so store it in ksmall ; return the found value ; A utility function to create a new BST node ; A utility function to insert a new node with given key in BST ; If the tree is empty , return a new node ; Otherwise , recur down the tree ; return the ( unchanged ) node pointer ; Driver Program to test above functions ; Let us create following BST 50 / \ 30 70 / \ / \ 20 40 60 80"
Solution in C#:

using System ; class GfG { public class Node { public int key ; public Node left , right ; } static int KSmallestUsingMorris ( Node root , int k ) { int count = 0 ; int ksmall = int . MinValue ; Node curr = root ; while ( curr != null ) { if ( curr . left == null ) { count ++ ; if ( count == k ) ksmall = curr . key ; curr = curr . right ; } else { Node pre = curr . left ; while ( pre . right != null && pre . right != curr ) pre = pre . right ; if ( pre . right == null ) { pre . right = curr ; curr = curr . left ; } else { pre . right = null ; count ++ ; if ( count == k ) ksmall = curr . key ; curr = curr . right ; } } } return ksmall ; } static Node newNode ( int item ) { Node temp = new Node ( ) ; temp . key = item ; temp . left = null ; temp . right = null ; return temp ; } static Node insert ( Node node , int key ) { if ( node == null ) return newNode ( key ) ; if ( key < node . key ) node . left = insert ( node . left , key ) ; else if ( key > node . key ) node . right = insert ( node . right , key ) ; return node ; } public static void Main ( String [ ] args ) { Node root = null ; root = insert ( root , 50 ) ; insert ( root , 30 ) ; insert ( root , 20 ) ; insert ( root , 40 ) ; insert ( root , 70 ) ; insert ( root , 60 ) ; insert ( root , 80 ) ; for ( int k = 1 ; k <= 7 ; k ++ ) Console . Write ( KSmallestUsingMorris ( root , k ) + " ▁ " ) ; } }


"K length words that can be formed from given characters without repetition | C # implementation of the approach ; Function to return the required count ; To store the count of distinct characters in str ; Traverse str character by character ; If current character is appearing for the first time in str ; Increment the distinct character count ; Update the appearance of the current character ; Since P ( n , r ) = n ! / ( n - r ) ! ; Return the answer ; Driver code"
Solution in C#:

using System ; class solution { static int findPermutation ( string str , int k ) { bool [ ] has = new bool [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) has [ i ] = false ; int cnt = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( ! has [ str [ i ] - ' a ' ] ) { cnt ++ ; has [ str [ i ] - ' a ' ] = true ; } } int ans = 1 ; for ( int i = 2 ; i <= cnt ; i ++ ) ans *= i ; for ( int i = cnt - k ; i > 1 ; i -- ) ans /= i ; return ans ; } public static void Main ( ) { string str = " geeksforgeeks " ; int k = 4 ; Console . WriteLine ( findPermutation ( str , k ) ) ; } }


"K | C # Program to find kth element from two sorted arrays ; Driver Code"
Solution in C#:

class GFG { static int kth ( int [ ] arr1 , int [ ] arr2 , int m , int n , int k ) { int [ ] sorted1 = new int [ m + n ] ; int i = 0 , j = 0 , d = 0 ; while ( i < m && j < n ) { if ( arr1 [ i ] < arr2 [ j ] ) sorted1 [ d ++ ] = arr1 [ i ++ ] ; else sorted1 [ d ++ ] = arr2 [ j ++ ] ; } while ( i < m ) sorted1 [ d ++ ] = arr1 [ i ++ ] ; while ( j < n ) sorted1 [ d ++ ] = arr2 [ j ++ ] ; return sorted1 [ k - 1 ] ; } static void Main ( ) { int [ ] arr1 = { 2 , 3 , 6 , 7 , 9 } ; int [ ] arr2 = { 1 , 4 , 8 , 10 } ; int k = 5 ; System . Console . WriteLine ( kth ( arr1 , arr2 , 5 , 4 , k ) ) ; } }


"K | C # Program to find kth element from two sorted arrays ; In case we have reached end of array 1 ; In case we have reached end of array 2 ; k should never reach 0 or exceed sizes of arrays ; Compare first elements of arrays and return ; Size of array 1 is less than k / 2 ; Last element of array 1 is not kth We can directly return the ( k - m ) th element in array 2 ; Size of array 2 is less than k / 2 ; Normal comparison , move starting index of one array k / 2 to the right ; Driver code"
Solution in C#:

using System ; class GFG { static int kth ( int [ ] arr1 , int [ ] arr2 , int m , int n , int k , int st1 , int st2 ) { if ( st1 == m ) { return arr2 [ st2 + k - 1 ] ; } if ( st2 == n ) { return arr1 [ st1 + k - 1 ] ; } if ( k == 0 || k > ( m - st1 ) + ( n - st2 ) ) { return - 1 ; } if ( k == 1 ) { return ( arr1 [ st1 ] < arr2 [ st2 ] ) ? arr1 [ st1 ] : arr2 [ st2 ] ; } int curr = k / 2 ; if ( curr - 1 >= m - st1 ) { if ( arr1 [ m - 1 ] < arr2 [ st2 + curr - 1 ] ) { return arr2 [ st2 + ( k - ( m - st1 ) - 1 ) ] ; } else { return kth ( arr1 , arr2 , m , n , k - curr , st1 , st2 + curr ) ; } } if ( curr - 1 >= n - st2 ) { if ( arr2 [ n - 1 ] < arr1 [ st1 + curr - 1 ] ) { return arr1 [ st1 + ( k - ( n - st2 ) - 1 ) ] ; } else { return kth ( arr1 , arr2 , m , n , k - curr , st1 + curr , st2 ) ; } } else if ( arr1 [ curr + st1 - 1 ] < arr2 [ curr + st2 - 1 ] ) { return kth ( arr1 , arr2 , m , n , k - curr , st1 + curr , st2 ) ; } else { return kth ( arr1 , arr2 , m , n , k - curr , st1 , st2 + curr ) ; } } public static void Main ( String [ ] args ) { int [ ] arr1 = { 2 , 3 , 6 , 7 , 9 } ; int [ ] arr2 = { 1 , 4 , 8 , 10 } ; int k = 5 ; int st1 = 0 , st2 = 0 ; Console . WriteLine ( kth ( arr1 , arr2 , 5 , 4 , k , st1 , st2 ) ) ; } }


"K | C # implementation for the above approach ; Function to calculate K - th smallest solution ( Y ) of equation X + Y = X | Y ; Initialize the variable to store the answer ; The i - th bit of X is off ; The i - bit of K is on ; Divide K by 2 ; If K becomes 0 then break ; Driver Code"
Solution in C#:

using System ; class GFG { static long KthSolution ( long X , long K ) { long ans = 0 ; for ( int i = 0 ; i < 64 ; i ++ ) { if ( ( X & ( 1L L << i ) ) == 0 ) { if ( ( K & 1 ) > 0 ) { ans |= ( 1L L << i ) ; } K >>= 1 ; if ( K == 0 ) { break ; } } } return ans ; } public static void Main ( ) { long X = 5 , K = 5 ; Console . Write ( KthSolution ( X , K ) ) ; } }


"K | C # implementation of above approach ; Function that finds the Nth element of K - Fibonacci series ; If N is less than K then the element is '1' ; first k elements are 1 ; ( K + 1 ) th element is K ; find the elements of the K - Fibonacci series ; subtract the element at index i - k - 1 and add the element at index i - i from the sum ( sum contains the sum of previous ' K ' elements ) ; set the new sum ; Main Method ; get the Nth value of K - Fibonacci series"
Solution in C#:

using System ; class GFG { public static void solve ( int N , int K ) { int [ ] Array = new int [ N + 1 ] ; if ( N <= K ) { Console . WriteLine ( "1" ) ; return ; } int i = 0 ; int sum = K ; for ( i = 1 ; i <= K ; ++ i ) { Array [ i ] = 1 ; } Array [ i ] = sum ; for ( i = K + 2 ; i <= N ; ++ i ) { Array [ i ] = sum - Array [ i - K - 1 ] + Array [ i - 1 ] ; sum = Array [ i ] ; } Console . WriteLine ( Array [ N ] ) ; } public static void Main ( string [ ] args ) { int N = 4 , K = 2 ; solve ( N , K ) ; } }


"K | C # implementation of the above approach ; Set to store the unique substring ; String to create each substring ; Adding to set ; Converting set into a list ; Sorting the strings int the list into lexicographical order ; Printing kth substring ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; using System . Collections ; class GFG { public static void kThLexString ( string st , int k , int n ) { HashSet < string > z = new HashSet < string > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { string pp = " " ; for ( int j = i ; j < i + k ; j ++ ) { if ( j >= n ) break ; pp += st [ j ] ; z . Add ( pp ) ; } } ArrayList fin = new ArrayList ( ) ; foreach ( string s in z ) { fin . Add ( s ) ; } fin . Sort ( ) ; Console . Write ( fin [ k - 1 ] ) ; } public static void Main ( string [ ] args ) { string s = " geeksforgeeks " ; int k = 5 ; int n = s . Length ; kThLexString ( s , k , n ) ; } }


"K | C # program for finding k - th digit in a ^ b ; To compute k - th digit in a ^ b ; Computing a ^ b ; Getting last digit ; Increasing count by 1 ; If current number is required digit ; Remove last digit ; Driver Code"
Solution in C#:

using System ; public class GfG { public static int kthdigit ( int a , int b , int k ) { int p = ( int ) Math . Pow ( a , b ) ; int count = 0 ; while ( p > 0 && count < k ) { int rem = p % 10 ; count ++ ; if ( count == k ) return rem ; p = p / 10 ; } return 0 ; } public static void Main ( ) { int a = 5 , b = 2 ; int k = 1 ; Console . WriteLine ( kthdigit ( a , b , k ) ) ; } }


"K | C # program for the above approach ; Function to find the kth digit from last in an integer n ; If k is less than equal to 0 ; Convert integer into string ; If k is greater than length of the string temp ; Print the k digit from last ; Driver code ; Given Input ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void kthDigitFromLast ( int n , int k ) { if ( k <= 0 ) { Console . Write ( - 1 ) ; return ; } string temp = n . ToString ( ) ; if ( k > temp . Length ) { Console . WriteLine ( - 1 ) ; } else { Console . Write ( temp [ temp . Length - k ] - 48 ) ; } } public static void Main ( ) { int n = 2354 ; int k = 2 ; kthDigitFromLast ( n , k ) ; } }


"K | C # program for the above approach ; Function to find the kth digit from last in an integer n ; If k is less than equal to 0 ; Divide the number n by 10 upto k - 1 times ; If the number n is equal 0 ; Print the right most digit ; Driver code ; Given input ; Function call"
Solution in C#:

using System ; class GFG { public static void kthDigitFromLast ( int n , int k ) { if ( k <= 0 ) { Console . Write ( - 1 ) ; return ; } while ( ( k - 1 ) > 0 && n > 0 ) { n = n / 10 ; k -- ; } if ( n == 0 ) { Console . Write ( - 1 ) ; } else { Console . Write ( n % 10 ) ; } } public static void Main ( String [ ] args ) { int n = 2354 ; int k = 2 ; kthDigitFromLast ( n , k ) ; } }


"K | C # program for the above approach ; Recursive function to find the Kth smallest binary string ; Base Case ; Return string of all 1 's  of length B ; Return string of all 0 's  of length A ; Function to find the Kth lexicographically smallest binary string with exactly A zeroes and B ones ; Stores the recurring states ; Calculate the dp values iteratively ; The last character was '0' ; The last character was '1' ; Print the binary string obtained ; Driver Code"
Solution in C#:

using System ; class GFG { static string KthString ( int A , int B , long K , int [ , ] dp ) { if ( A == 0 ) { string ans = " " ; for ( int i = 0 ; i < B ; i ++ ) { ans += '1' ; } return ans ; } if ( B == 0 ) { string ans = " " ; for ( int i = 0 ; i < A ; i ++ ) { ans += '0' ; } return ans ; } if ( K <= dp [ A - 1 , B ] ) { return "0" + KthString ( A - 1 , B , K , dp ) ; } else { return "1" + KthString ( A , B - 1 , K - dp [ A - 1 , B ] , dp ) ; } } static int KthStringUtil ( int A , int B , int K ) { int [ , ] dp = new int [ A + 1 , B + 1 ] ; dp [ 0 , 0 ] = 1 ; for ( int i = 0 ; i <= A ; ++ i ) { for ( int j = 0 ; j <= B ; ++ j ) { if ( i > 0 ) { dp [ i , j ] += dp [ i - 1 , j ] ; } if ( j > 0 ) { dp [ i , j ] += dp [ i , j - 1 ] ; } } } Console . WriteLine ( KthString ( A , B , K , dp ) ) ; return 0 ; } public static void Main ( string [ ] args ) { int A = 3 , B = 3 , K = 7 ; KthStringUtil ( A , B , K ) ; } }


"K | C # program to count all those numbers in given range whose count of prime factors is k ; Returns the sum of first n odd numbers ; Count prime factors of all numbers till B . ; Print all numbers with k prime factors ; Driver code"
Solution in C#:

using System ; class GFG { static void printKPFNums ( int A , int B , int K ) { bool [ ] prime = new bool [ B + 1 ] ; for ( int i = 0 ; i < B + 1 ; i ++ ) prime [ i ] = true ; int [ ] p_factors = new int [ B + 1 ] ; for ( int i = 0 ; i < B + 1 ; i ++ ) p_factors [ i ] = 0 ; for ( int p = 2 ; p <= B ; p ++ ) if ( p_factors [ p ] == 0 ) for ( int i = p ; i <= B ; i += p ) p_factors [ i ] ++ ; for ( int i = A ; i <= B ; i ++ ) if ( p_factors [ i ] == K ) Console . Write ( i + " ▁ " ) ; } public static void Main ( ) { int A = 14 , B = 18 , K = 2 ; printKPFNums ( A , B , K ) ; } }


"K | C # program to find k - th term of N merged Arithmetic Progressions ; Function to count and return the number of values less than equal to N present in the set ; Check whether j - th bit is set bit or not ; Function to implement Binary Search to find K - th element ; Find middle index of the array ; Search in the left half ; Search in the right half ; If exactly K elements are present ; Driver Code"
Solution in C#:

 using System ; class GFG { static readonly int maxm = 1000000000 ; static int count ( int [ ] v , int n ) { int i , odd = 0 , even = 0 ; int j , d , count ; int t = ( int ) 1 << v . Length ; int size = v . Length ; for ( i = 1 ; i < t ; i ++ ) { d = 1 ; count = 0 ; for ( j = 0 ; j < size ; j ++ ) { if ( ( i & ( 1 << j ) ) > 0 ) { d *= v [ j ] ; count ++ ; } } if ( count % 2 == 1 ) odd += n / d ; else even += n / d ; } return ( odd - even ) ; } static int BinarySearch ( int l , int r , int [ ] v , int key ) { int mid ; while ( r - l > 1 ) { mid = ( l + r ) / 2 ; if ( key <= count ( v , mid ) ) { r = mid ; } else { l = mid ; } } if ( key == count ( v , l ) ) return l ; else return r ; } public static void Main ( String [ ] args ) { int K = 10 ; int [ ] v = { 2 , 3 } ; Console . Write ( BinarySearch ( 1 , maxm , v , K ) + "
 " ) ; } }


"K | C # program to find the K - th lexicographical String of length N ; Initialize the array to store the base 26 representation of K with all zeroes , that is , the initial String consists of N a 's ; Start filling all the N slots for the base 26 representation of K ; Store the remainder ; Reduce K ; If K is greater than the possible number of Strings that can be represented by a String of length N ; Store the Kth lexicographical String ; Driver Code ; Reducing k value by 1 because our stored value starts from 0"
Solution in C#:

using System ; class GFG { static String find_kth_String_of_n ( int n , int k ) { int [ ] d = new int [ n ] ; for ( int i = n - 1 ; i > - 1 ; i -- ) { d [ i ] = k % 26 ; k /= 26 ; } if ( k > 0 ) return " - 1" ; string s = " " ; for ( int i = 0 ; i < n ; i ++ ) s += ( char ) ( d [ i ] + ( ' a ' ) ) ; return s ; } public static void Main ( ) { int n = 3 ; int k = 10 ; k -= 1 ; Console . Write ( find_kth_String_of_n ( n , k ) ) ; } }


"K | C # program to find the K - th smallest element after removing some integers from natural number . ; Return the K - th smallest element . ; Making an array , and mark all number as unmarked . ; Marking the number present in the given array . ; If j is unmarked , reduce k by 1. ; If k is 0 return j . ; Driven code"
Solution in C#:

using System ; class GFG { static int MAX = 1000000 ; static int ksmallest ( int [ ] arr , int n , int k ) { int [ ] b = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { b [ arr [ i ] ] = 1 ; } for ( int j = 1 ; j < MAX ; j ++ ) { if ( b [ j ] != 1 ) { k -- ; } if ( k != 1 ) { return j ; } } return int . MaxValue ; } public static void Main ( ) { int k = 1 ; int [ ] arr = { 1 } ; int n = arr . Length ; Console . WriteLine ( ksmallest ( arr , n , k ) ) ; } }


"K | C # program to find the Kth smallest element after removing some integer from first n natural number . ; Return the K - th smallest element . ; sort ( arr , arr + n ) ; ; Checking if k lies before 1 st element ; If k is the first element of array arr [ ] . ; If k is more than last element ; If first element of array is 1. ; Reducing k by numbers before arr [ 0 ] . ; Finding k 'th smallest  element after removing  array elements. ; Finding count of element between i - th and ( i - 1 ) - th element . ; Driver Code"
Solution in C#:

using System ; class GFG { static int ksmallest ( int [ ] arr , int n , int k ) { Array . Sort ( arr ) ; if ( k < arr [ 0 ] ) return k ; if ( k == arr [ 0 ] ) return arr [ 0 ] + 1 ; if ( k > arr [ n - 1 ] ) return k + n ; if ( arr [ 0 ] == 1 ) k -- ; else k -= ( arr [ 0 ] - 1 ) ; for ( int i = 1 ; i < n ; i ++ ) { int c = arr [ i ] - arr [ i - 1 ] - 1 ; if ( k <= c ) return arr [ i - 1 ] + k ; else k -= c ; } return arr [ n - 1 ] + k ; } static public void Main ( ) { int k = 1 ; int [ ] arr = { 1 } ; int n = arr . Length ; Console . WriteLine ( ksmallest ( arr , n , k ) ) ; } }


"Kaprekar Constant | C # program to demonstrate working of Kaprekar constant ; This function checks validity of kaprekar ' s ▁ constant . ▁ It ▁ returns ▁ kaprekar ' s constant for any four digit number " n " such that all digits of n are not same . ; Store current n as previous number ; Get four digits of given number ; Sort all four dgits in ascending order And giet in the form of number " asc " ; Get all four dgits in descending order in the form of number " desc " ; Get the difference of two numbers ; If difference is same as previous , we have reached kaprekar 's constant ; Else recur ; A wrapper over kaprekarRec ( ) ; Driver code ; Trying few four digit numbers , we always get 6174"
Solution in C#:

using System ; class GFG { static int kaprekarRec ( int n , int prev ) { if ( n == 0 ) return 0 ; prev = n ; int [ ] digits = new int [ 4 ] ; for ( int i = 0 ; i < 4 ; i ++ ) { digits [ i ] = n % 10 ; n = n / 10 ; } Array . Sort ( digits ) ; int asc = 0 ; for ( int i = 0 ; i < 4 ; i ++ ) asc = asc * 10 + digits [ i ] ; Array . Sort ( digits ) ; int desc = 0 ; for ( int i = 3 ; i >= 0 ; i -- ) desc = desc * 10 + digits [ i ] ; int diff = Math . Abs ( asc - desc ) ; if ( diff == prev ) return diff ; return kaprekarRec ( diff , prev ) ; } static int kaprekar ( int n ) { int prev = 0 ; return kaprekarRec ( n , prev ) ; } public static void Main ( ) { System . Console . WriteLine ( kaprekar ( 1000 ) ) ; System . Console . WriteLine ( kaprekar ( 1112 ) ) ; System . Console . WriteLine ( kaprekar ( 9812 ) ) ; } }


"Kaprekar Number | C # program to check if a number is Kaprekar number or not ; Returns true if n is a Kaprekar number , else false ; Count number of digits in square ; Recompute square as it was changed ; Split the square at different poitns and see if sum of any pair of splitted numbers is equal to n . ; To avoid numbers like 10 , 100 , 1000 These are not Karprekar numbers ; Find sum of current parts and compare with n ; compare with original number ; Driver method"
Solution in C#:

using System ; class GFG { static bool iskaprekar ( int n ) { if ( n == 1 ) return true ; int sq_n = n * n ; int count_digits = 0 ; while ( sq_n != 0 ) { count_digits ++ ; sq_n /= 10 ; } sq_n = n * n ; for ( int r_digits = 1 ; r_digits < count_digits ; r_digits ++ ) { int eq_parts = ( int ) Math . Pow ( 10 , r_digits ) ; if ( eq_parts == n ) continue ; int sum = sq_n / eq_parts + sq_n % eq_parts ; if ( sum == n ) return true ; } return false ; } public static void Main ( ) { Console . WriteLine ( " Printing ▁ first ▁ few ▁ " + " Kaprekar ▁ Numbers ▁ using ▁ iskaprekar ( ) " ) ; for ( int i = 1 ; i < 10000 ; i ++ ) if ( iskaprekar ( i ) ) Console . Write ( i + " ▁ " ) ; } }


"Karp 's minimum mean (or average) weight cycle algorithm | C # program to find minimum average weight of a cycle in connected and directed graph . ; a struct to represent edges ; vector to store edges ; calculates the shortest path ; initializing all distances as - 1 ; shortest distance from first vertex to in tself consisting of 0 edges ; filling up the dp table ; Returns minimum value of average weight of a cycle in graph . ; array to store the avg values ; Compute average values for all vertices using weights of shortest paths store in dp . ; Find minimum value in avg [ ] ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int V = 4 ; public class Edge { public int from , weight ; public Edge ( int from , int weight ) { this . from = from ; this . weight = weight ; } } static List < Edge > [ ] edges = new List < Edge > [ V ] ; static void addedge ( int u , int v , int w ) { edges [ v ] . Add ( new Edge ( u , w ) ) ; } static void shortestpath ( int [ , ] dp ) { for ( int i = 0 ; i <= V ; i ++ ) for ( int j = 0 ; j < V ; j ++ ) dp [ i , j ] = - 1 ; dp [ 0 , 0 ] = 0 ; for ( int i = 1 ; i <= V ; i ++ ) { for ( int j = 0 ; j < V ; j ++ ) { for ( int k = 0 ; k < edges [ j ] . Count ; k ++ ) { if ( dp [ i - 1 , edges [ j ] [ k ] . from ] != - 1 ) { int curr_wt = dp [ i - 1 , edges [ j ] [ k ] . from ] + edges [ j ] [ k ] . weight ; if ( dp [ i , j ] == - 1 ) dp [ i , j ] = curr_wt ; else dp [ i , j ] = Math . Min ( dp [ i , j ] , curr_wt ) ; } } } } } static double minAvgWeight ( ) { int [ , ] dp = new int [ V + 1 , V ] ; shortestpath ( dp ) ; double [ ] avg = new double [ V ] ; for ( int i = 0 ; i < V ; i ++ ) avg [ i ] = - 1 ; for ( int i = 0 ; i < V ; i ++ ) { if ( dp [ V , i ] != - 1 ) { for ( int j = 0 ; j < V ; j ++ ) if ( dp [ j , i ] != - 1 ) avg [ i ] = Math . Max ( avg [ i ] , ( ( double ) dp [ V , i ] - dp [ j , i ] ) / ( V - j ) ) ; } } double result = avg [ 0 ] ; for ( int i = 0 ; i < V ; i ++ ) if ( avg [ i ] != - 1 && avg [ i ] < result ) result = avg [ i ] ; return result ; } public static void Main ( String [ ] args ) { for ( int i = 0 ; i < V ; i ++ ) edges [ i ] = new List < Edge > ( ) ; addedge ( 0 , 1 , 1 ) ; addedge ( 0 , 2 , 10 ) ; addedge ( 1 , 2 , 3 ) ; addedge ( 2 , 3 , 2 ) ; addedge ( 3 , 1 , 0 ) ; addedge ( 3 , 0 , 8 ) ; Console . Write ( " { 0 : F5 } " , minAvgWeight ( ) ) ; } }


"Keith Number | C # program to check if a number is Keith or not ; Returns true if x is Keith , else false . ; Store all digits of x in a vector " terms " Also find number of digits and store in " n " . ; int temp = x , n = 0 ; n is number of digits in x ; To get digits in right order ( from MSB to LSB ) ; Keep finding next trms of a sequence generated using digits of x until we either reach x or a number greate than x ; Next term is sum of previous n terms ; When the control comes out of the while loop , either the next_term is equal to the number or greater than it . If next_term is equal to x , then x is a Keith number , else not ; Driver program"
Solution in C#:

using System ; using System . Collections ; class GFG { static bool isKeith ( int x ) { ArrayList terms = new ArrayList ( ) ; while ( temp > 0 ) { terms . Add ( temp % 10 ) ; temp = temp / 10 ; n ++ ; } terms . Reverse ( ) ; int next_term = 0 , i = n ; while ( next_term < x ) { next_term = 0 ; for ( int j = 1 ; j <= n ; j ++ ) next_term += ( int ) terms [ i - j ] ; terms . Add ( next_term ) ; i ++ ; } return ( next_term == x ) ; } public static void Main ( ) { if ( isKeith ( 14 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; if ( isKeith ( 12 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; if ( isKeith ( 197 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Keyword Cipher | C # program for encoding the string using classical cipher ; Function generates the encoded text ; This array represents the 26 letters of alphabets ; This loop inserts the keyword at the start of the encoded string ; To check whether the character is inserted earlier in the encoded string or not ; This loop inserts the remaining characters in the encoded string . ; Function that generates encodes ( cipher ) the message ; This loop ciphered the message . Spaces , special characters and numbers remain same . ; Driver code ; Hold the Keyword ; Function call to generate encoded text ; Message that need to encode ; Function call to print ciphered text"
Solution in C#:

using System ; class GFG { static String encoder ( char [ ] key ) { String encoded = " " ; Boolean [ ] arr = new Boolean [ 26 ] ; for ( int i = 0 ; i < key . Length ; i ++ ) { if ( key [ i ] >= ' A ' && key [ i ] <= ' Z ' ) { if ( arr [ key [ i ] - 65 ] == false ) { encoded += ( char ) key [ i ] ; arr [ key [ i ] - 65 ] = true ; } } else if ( key [ i ] >= ' a ' && key [ i ] <= ' z ' ) { if ( arr [ key [ i ] - 97 ] == false ) { encoded += ( char ) ( key [ i ] - 32 ) ; arr [ key [ i ] - 97 ] = true ; } } } for ( int i = 0 ; i < 26 ; i ++ ) { if ( arr [ i ] == false ) { arr [ i ] = true ; encoded += ( char ) ( i + 65 ) ; } } return encoded ; } static String cipheredIt ( String msg , String encoded ) { String cipher = " " ; for ( int i = 0 ; i < msg . Length ; i ++ ) { if ( msg [ i ] >= ' a ' && msg [ i ] <= ' z ' ) { int pos = msg [ i ] - 97 ; cipher += encoded [ pos ] ; } else if ( msg [ i ] >= ' A ' && msg [ i ] <= ' Z ' ) { int pos = msg [ i ] - 65 ; cipher += encoded [ pos ] ; } else { cipher += msg [ i ] ; } } return cipher ; } public static void Main ( String [ ] args ) { String key ; key = " Computer " ; Console . WriteLine ( " Keyword ▁ : ▁ " + key ) ; String encoded = encoder ( key . ToCharArray ( ) ) ; String message = " GeeksforGeeks " ; Console . WriteLine ( " Message ▁ before ▁ Ciphering ▁ : ▁ " + message ) ; Console . WriteLine ( " Ciphered ▁ Text ▁ : ▁ " + cipheredIt ( message , encoded ) ) ; } }