"Print all integers that are sum of powers of two given numbers | C # implementation of the approach ; Function to print powerful integers ; Set is used to store distinct numbers in sorted order ; Store all the powers of y < bound in a vector to avoid calculating them again and again ; x ^ i ; If num is within limits insert it into the set ; Break out of the inner loop ; Adding any number to it will be out of bounds ; Increment i ; Print the contents of the set ; Driver code ; Print powerful integers"
Solution in C#:

using System ; using System . Linq ; using System . Collections . Generic ; using System . Collections ; class GFG { static void powerfulIntegers ( int x , int y , int bound ) { HashSet < int > s = new HashSet < int > ( ) ; ArrayList powersOfY = new ArrayList ( ) ; int i ; powersOfY . Add ( 1 ) ; for ( i = y ; i < bound && y != 1 ; i = i * y ) powersOfY . Add ( i ) ; i = 0 ; while ( true ) { int xPowI = ( int ) Math . Pow ( x , i ) ; for ( int j = 0 ; j != powersOfY . Count ; ++ j ) { int num = xPowI + ( int ) powersOfY [ j ] ; if ( num <= bound ) s . Add ( num ) ; else break ; } if ( xPowI >= bound x == 1 ) break ; i ++ ; } int [ ] ar = s . ToArray ( ) ; Array . Sort ( ar ) ; s . Clear ( ) ; s . UnionWith ( ar ) ; foreach ( int t in s ) { Console . Write ( t + " ▁ " ) ; } } static void Main ( ) { int x = 2 , y = 3 , bound = 10 ; powerfulIntegers ( x , y , bound ) ; } }


"Print all k | C # program to print all paths with sum k . ; utility function to print contents of a vector from index i to it 's end  ; binary tree node ; This function prints all paths that have sum k ; empty node ; add current node to the path ; check if there 's any k sum path   in the left sub-tree.  ; check if there 's any k sum path   in the right sub-tree.  ; check if there 's any k sum path that   terminates at this node   Traverse the entire path as   there can be negative elements too  ; If path sum is k , print the path ; Remove the current element from the path ; A wrapper over printKPathUtil ( ) ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void printList ( List < int > v , int i ) { for ( int j = i ; j < v . Count ; j ++ ) Console . Write ( v [ j ] + " ▁ " ) ; Console . WriteLine ( ) ; } public class Node { public int data ; public Node left , right ; public Node ( int x ) { data = x ; left = right = null ; } } ; static List < int > path = new List < int > ( ) ; static void printKPathUtil ( Node root , int k ) { if ( root == null ) return ; path . Add ( root . data ) ; printKPathUtil ( root . left , k ) ; printKPathUtil ( root . right , k ) ; int f = 0 ; for ( int j = path . Count - 1 ; j >= 0 ; j -- ) { f += path [ j ] ; if ( f == k ) printList ( path , j ) ; } path . RemoveAt ( path . Count - 1 ) ; } static void printKPath ( Node root , int k ) { path = new List < int > ( ) ; printKPathUtil ( root , k ) ; } public static void Main ( String [ ] args ) { Node root = new Node ( 1 ) ; root . left = new Node ( 3 ) ; root . left . left = new Node ( 2 ) ; root . left . right = new Node ( 1 ) ; root . left . right . left = new Node ( 1 ) ; root . right = new Node ( - 1 ) ; root . right . left = new Node ( 4 ) ; root . right . left . left = new Node ( 1 ) ; root . right . left . right = new Node ( 2 ) ; root . right . right = new Node ( 5 ) ; root . right . right . right = new Node ( 2 ) ; int k = 5 ; printKPath ( root , k ) ; } }


"Print all leaf nodes of a Binary Tree from left to right | C # program to print leaf nodes from left to right ; A Binary Tree Node ; Function to print leaf nodes from left to right ; If node is null , return ; If node is leaf node , print its data ; If left child exists , check for leaf recursively ; If right child exists , check for leaf recursively ; Utility function to create a new tree node ; Driver code ; Let us create binary tree shown in above diagram ; Print leaf nodes of the given tree"
Solution in C#:

using System ; class GFG { class Node { public int data ; public Node left , right ; } ; static void printLeafNodes ( Node root ) { if ( root == null ) return ; if ( root . left == null && root . right == null ) { Console . Write ( root . data + " ▁ " ) ; return ; } if ( root . left != null ) printLeafNodes ( root . left ) ; if ( root . right != null ) printLeafNodes ( root . right ) ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = null ; temp . right = null ; return temp ; } public static void Main ( ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . right . left = newNode ( 5 ) ; root . right . right = newNode ( 8 ) ; root . right . left . left = newNode ( 6 ) ; root . right . left . right = newNode ( 7 ) ; root . right . right . left = newNode ( 9 ) ; root . right . right . right = newNode ( 10 ) ; printLeafNodes ( root ) ; } }


"Print all maximal increasing contiguous sub | C # Implementation to print all the Maximal Increasing Sub - array of array ; Function to print each of maximal contiguous increasing subarray ; Loop to iterate through the array and print the maximal contiguous increasing subarray . ; Condition to check whether the element at i , is greater than its next neighbouring element or not . ; Driver code"
Solution in C#:

using System ; class GFG { static void printmaxSubseq ( int [ ] arr , int n ) { int i ; for ( i = 0 ; i < n ; i ++ ) { if ( i + 1 < n && arr [ i ] < arr [ i + 1 ] ) Console . Write ( arr [ i ] + " ▁ " ) ; else Console . WriteLine ( arr [ i ] ) ; } } public static void Main ( ) { int [ ] arr = { 9 , 8 , 11 , 13 , 10 , 15 , 14 , 16 , 20 , 5 } ; int n = arr . Length ; printmaxSubseq ( arr , n ) ; } }


"Print all multiplicative primes <= N | C # implementation of the approach ; Function to return the digit product of n ; Function to print all multiplicative primes <= n ; Create a boolean array " prime [ 0 . . n + 1 ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; If i is prime and its digit sum is also prime i . e . i is a multiplicative prime ; Driver code"
Solution in C#:

class GFG { static int digitProduct ( int n ) { int prod = 1 ; while ( n > 0 ) { prod = prod * ( n % 10 ) ; n = n / 10 ; } return prod ; } static void printMultiplicativePrimes ( int n ) { bool [ ] prime = new bool [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) prime [ i ] = true ; prime [ 0 ] = prime [ 1 ] = false ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } for ( int i = 2 ; i <= n ; i ++ ) { if ( prime [ i ] && prime [ digitProduct ( i ) ] ) System . Console . Write ( i + " ▁ " ) ; } } static void Main ( ) { int n = 10 ; printMultiplicativePrimes ( n ) ; } }


"Print all n | C # program to print all n - digit numbers with absolute difference between sum of even and odd digits is 1 ; n -- > value of input out -- > output array index -- > index of next digit to be filled in output array evenSum , oddSum -- > sum of even and odd digits so far ; Base case ; If number becomes n - digit ; if absolute difference between sum of even and odd digits is 1 , print the number ; If current index is odd , then add it to odd sum and recurse ; else else add to even sum and recurse ; This is mainly a wrapper over findNDigitNumsUtil . It explicitly handles leading digit and calls findNDigitNumsUtil ( ) for remaining indexes ; output array to store n - digit numbers ; Initialize number index considered so far ; Initialize even and odd sums ; Explicitly handle first digit and call recursive function findNDigitNumsUtil for remaining indexes . Note that the first digit is considered to be present in even position ; Driver program"
Solution in C#:

using System ; class GFG { static void findNDigitNumsUtil ( int n , char [ ] ou , int index , int evenSum , int oddSum ) { if ( index > n ) return ; if ( index == n ) { if ( Math . Abs ( evenSum - oddSum ) == 1 ) { ou [ index ] = ' \0' ; Console . Write ( ou ) ; Console . Write ( " ▁ " ) ; } return ; } if ( index % 2 != 0 ) { for ( int i = 0 ; i <= 9 ; i ++ ) { ou [ index ] = ( char ) ( i + '0' ) ; findNDigitNumsUtil ( n , ou , index + 1 , evenSum , oddSum + i ) ; } } { for ( int i = 0 ; i <= 9 ; i ++ ) { ou [ index ] = ( char ) ( i + '0' ) ; findNDigitNumsUtil ( n , ou , index + 1 , evenSum + i , oddSum ) ; } } } static void findNDigitNums ( int n ) { char [ ] ou = new char [ n + 1 ] ; int index = 0 ; int evenSum = 0 , oddSum = 0 ; for ( int i = 1 ; i <= 9 ; i ++ ) { ou [ index ] = ( char ) ( i + '0' ) ; findNDigitNumsUtil ( n , ou , index + 1 , evenSum + i , oddSum ) ; } } public static void Main ( ) { int n = 3 ; findNDigitNums ( n ) ; } }


"Print all n | C # recursive program to print all n - digit numbers whose sum of digits equals to given sum ; n , sum -- > value of inputs out -- > output array index -- > index of next digit to be filled in output array ; Base case ; If number becomes N - digit ; if sum of its digits is equal to given sum , print it ; Traverse through every digit . Note that here we ' re ▁ considering ▁ leading ▁ ▁ 0' s as digits ; append current digit to number ; recurse for next digit with reduced sum ; This is mainly a wrapper over findNDigitNumsUtil . It explicitly handles leading digit ; output array to store N - digit numbers ; fill 1 st position by every digit from 1 to 9 and calls findNDigitNumsUtil ( ) for remaining positions ; driver program to test above function"
Solution in C#:

using System ; class GFG { static void findNDigitNumsUtil ( int n , int sum , char [ ] ou , int index ) { if ( index > n sum < 0 ) return ; if ( index == n ) { if ( sum == 0 ) { ou [ index ] = ' \0' ; Console . Write ( ou ) ; Console . Write ( " ▁ " ) ; } return ; } for ( int i = 0 ; i <= 9 ; i ++ ) { ou [ index ] = ( char ) ( i + '0' ) ; findNDigitNumsUtil ( n , sum - i , ou , index + 1 ) ; } } static void findNDigitNums ( int n , int sum ) { char [ ] ou = new char [ n + 1 ] ; for ( int i = 1 ; i <= 9 ; i ++ ) { ou [ 0 ] = ( char ) ( i + '0' ) ; findNDigitNumsUtil ( n , sum - i , ou , 1 ) ; } } public static void Main ( ) { int n = 2 , sum = 3 ; findNDigitNums ( n , sum ) ; } }


"Print all nodes at distance k from a given node | C # program to print all nodes at a distance k from given node ; A binary tree node ; Recursive function to print all the nodes at distance k in tree ( or subtree ) rooted with given root . ; Base Case ; If we reach a k distant node , print it ; Recur for left and right subtrees ; Prints all nodes at distance k from a given target node . The k distant nodes may be upward or downward . This function Returns distance of root from target node , it returns - 1 if target node is not present in tree rooted with root . ; Base Case 1 : If tree is empty , return - 1 ; If target is same as root . Use the downward function to print all nodes at distance k in subtree rooted with target or root ; Recur for left subtree ; Check if target node was found in left subtree ; If root is at distance k from target , print root Note that dl is Distance of root 's left child from   target ; Else go to right subtree and print all k - dl - 2 distant nodes Note that the right child is 2 edges away from left child ; Add 1 to the distance and return value for parent calls ; MIRROR OF ABOVE CODE FOR RIGHT SUBTREE Note that we reach here only when node was not found in left subtree ; If target was neither present in left nor in right subtree ; Driver program to test the above functions ; Let us construct the tree shown in above diagram"
Solution in C#:

using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class BinaryTree { public Node root ; public virtual void printkdistanceNodeDown ( Node node , int k ) { if ( node == null k < 0 ) { return ; } if ( k == 0 ) { Console . Write ( node . data ) ; Console . WriteLine ( " " ) ; return ; } printkdistanceNodeDown ( node . left , k - 1 ) ; printkdistanceNodeDown ( node . right , k - 1 ) ; } public virtual int printkdistanceNode ( Node node , Node target , int k ) { if ( node == null ) { return - 1 ; } if ( node == target ) { printkdistanceNodeDown ( node , k ) ; return 0 ; } int dl = printkdistanceNode ( node . left , target , k ) ; if ( dl != - 1 ) { if ( dl + 1 == k ) { Console . Write ( node . data ) ; Console . WriteLine ( " " ) ; } else { printkdistanceNodeDown ( node . right , k - dl - 2 ) ; } return 1 + dl ; } int dr = printkdistanceNode ( node . right , target , k ) ; if ( dr != - 1 ) { if ( dr + 1 == k ) { Console . Write ( node . data ) ; Console . WriteLine ( " " ) ; } else { printkdistanceNodeDown ( node . left , k - dr - 2 ) ; } return 1 + dr ; } return - 1 ; } public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 20 ) ; tree . root . left = new Node ( 8 ) ; tree . root . right = new Node ( 22 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 12 ) ; tree . root . left . right . left = new Node ( 10 ) ; tree . root . left . right . right = new Node ( 14 ) ; Node target = tree . root . left . right ; tree . printkdistanceNode ( tree . root , target , 2 ) ; } }


"Print all nodes that are at distance k from a leaf node |  ; C # program to print all nodes at a distance k from leaf A binary tree node ; This function prints all nodes that are distance k from a leaf node path [ ] -- > Store ancestors of a node visited [ ] -- > Stores true if a node is printed as output . A node may be k distance away from many leaves , we want to print it once ; Base case ; append this Node to the path array ; it 's a leaf, so print the ancestor at distance k only  if the ancestor is not already printed  ; If not leaf node , recur for left and right subtrees ; Given a binary tree and a nuber k , print all nodes that are k distant from a leaf ; Driver program to test the above functions ; Let us construct the tree shown in above diagram"
Solution in C#:

using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class BinaryTree { public Node root ; public virtual void kDistantFromLeafUtil ( Node node , int [ ] path , bool [ ] visited , int pathLen , int k ) { if ( node == null ) { return ; } path [ pathLen ] = node . data ; visited [ pathLen ] = false ; pathLen ++ ; if ( node . left == null && node . right == null && pathLen - k - 1 >= 0 && visited [ pathLen - k - 1 ] == false ) { Console . Write ( path [ pathLen - k - 1 ] + " ▁ " ) ; visited [ pathLen - k - 1 ] = true ; return ; } kDistantFromLeafUtil ( node . left , path , visited , pathLen , k ) ; kDistantFromLeafUtil ( node . right , path , visited , pathLen , k ) ; } public virtual void printKDistantfromLeaf ( Node node , int k ) { int [ ] path = new int [ 1000 ] ; bool [ ] visited = new bool [ 1000 ] ; kDistantFromLeafUtil ( node , path , visited , 0 , k ) ; } public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . left = new Node ( 6 ) ; tree . root . right . right = new Node ( 7 ) ; tree . root . right . left . right = new Node ( 8 ) ; Console . WriteLine ( " ▁ Nodes ▁ at ▁ distance ▁ 2 ▁ are ▁ : " ) ; tree . printKDistantfromLeaf ( tree . root , 2 ) ; } }


"Print all nodes that are at distance k from a leaf node | A binary tree node ; Given a binary tree and a nuber k , print all nodes that are k distant from a leaf ; leaf node ; parent of left leaf ; parent of right leaf ; Driver program to test the above functions ; Let us construct the tree shown in above diagram"
Solution in C#:

using System ; class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; int printKDistantfromLeaf ( Node node , int k ) { if ( node == null ) return - 1 ; int lk = printKDistantfromLeaf ( node . left , k ) ; int rk = printKDistantfromLeaf ( node . right , k ) ; bool isLeaf = lk == - 1 && lk == rk ; if ( lk == 0 || rk == 0 || ( isLeaf && k == 0 ) ) Console . Write ( " ▁ " + node . data ) ; if ( isLeaf && k > 0 ) return k - 1 ; if ( lk > 0 && lk < k ) return lk - 1 ; if ( rk > 0 && rk < k ) return rk - 1 ; return - 2 ; } public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . left = new Node ( 6 ) ; tree . root . right . right = new Node ( 7 ) ; tree . root . right . left . right = new Node ( 8 ) ; Console . Write ( " Nodes ▁ at ▁ distance ▁ 2 ▁ are ▁ : " ) ; tree . printKDistantfromLeaf ( tree . root , 2 ) ; } }


"Print all nodes that don 't have sibling | C # program to print all nodes that don 't have sibling ; A binary tree node ; Function to print all non - root nodes that don 't have a sibling ; Base case ; If this is an internal node , recur for left and right subtrees ; If left child is NULL and right is not , print right child and recur for right child ; If right child is NULL and left is not , print left child and recur for left child ; Driver program to test the above functions ; Let us construct the tree shown in above diagram"
Solution in C#:

using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class BinaryTree { public Node root ; public virtual void printSingles ( Node node ) { if ( node == null ) { return ; } if ( node . left != null && node . right != null ) { printSingles ( node . left ) ; printSingles ( node . right ) ; } else if ( node . right != null ) { Console . Write ( node . right . data + " ▁ " ) ; printSingles ( node . right ) ; } else if ( node . left != null ) { Console . Write ( node . left . data + " ▁ " ) ; printSingles ( node . left ) ; } } 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 . right = new Node ( 4 ) ; tree . root . right . left = new Node ( 5 ) ; tree . root . right . left . right = new Node ( 6 ) ; tree . printSingles ( tree . root ) ; } }


"Print all non | C # program to generate all non - increasing sequences of sum equals to x ; Utility function to print array arr [ 0. . n - 1 ] ; Recursive Function to generate all non - increasing sequences with sum x arr [ ] -- > Elements of current sequence curr_sum -- > Current Sum curr_idx -- > Current index in arr [ ] ; If current sum is equal to x , then we found a sequence ; Try placing all numbers from 1 to x - curr_sum at current index ; The placed number must also be smaller than previously placed numbers and it may be equal to the previous stored value , i . e . , arr [ curr_idx - 1 ] if there exists a previous number ; Place number at curr_idx ; Recur ; Try next number ; A wrapper over generateUtil ( ) ; Array to store sequences on by one ; Driver program"
Solution in C#:

using System ; class GFG { static void printArr ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] ) ; Console . WriteLine ( ) ; } static void generateUtil ( int x , int [ ] arr , int curr_sum , int curr_idx ) { if ( curr_sum == x ) { printArr ( arr , curr_idx ) ; return ; } int num = 1 ; while ( num <= x - curr_sum && ( curr_idx == 0 num <= arr [ curr_idx - 1 ] ) ) { arr [ curr_idx ] = num ; generateUtil ( x , arr , curr_sum + num , curr_idx + 1 ) ; num ++ ; } } static void generate ( int x ) { int [ ] arr = new int [ x ] ; generateUtil ( x , arr , 0 , 0 ) ; } public static void Main ( ) { int x = 5 ; generate ( x ) ; } }


"Print all numbers from a given range that are made up of consecutive digits | C # program for the above approach ; Function to find the consecutive digit numbers in the given range ; Initialize num as empty String ; Stores the resultant number ; Iterate over the range [ 1 , 9 ] ; Check if the current number is within range ; Iterate on the digits starting from i ; Checking the consecutive digits numbers starting from i and ending at j is within range or not ; Sort the numbers in the increasing order ; Print the required numbers ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class Program { static void solve ( int start , int end ) { string num = " " ; List < int > ans = new List < int > ( ) ; for ( int i = 1 ; i <= 9 ; i ++ ) { num = i . ToString ( ) ; int value = i ; if ( value >= start && value <= end ) { ans . Add ( value ) ; } for ( int j = i + 1 ; j <= 9 ; j ++ ) { num += j . ToString ( ) ; value = Int32 . Parse ( num ) ; if ( value >= start && value <= end ) { ans . Add ( value ) ; } } } ans . Sort ( ) ; foreach ( int it in ans ) Console . Write ( it + " ▁ " ) ; } static void Main ( ) { int L = 12 , R = 87 ; solve ( L , R ) ; } }


"Print all numbers less than N with at | C # program to print all the numbers less than N which have at most 2 unique digits ; Function to generate all possible numbers ; If the number is less than n ; If the number exceeds ; Check if it is not the same number ; Function to print all numbers ; All combination of digits ; Print all numbers ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static SortedSet < int > st = new SortedSet < int > ( ) ; static void generateNumbers ( int n , int num , int a , int b ) { if ( num > 0 && num < n ) st . Add ( num ) ; if ( num >= n ) return ; if ( num * 10 + a > num ) generateNumbers ( n , num * 10 + a , a , b ) ; generateNumbers ( n , num * 10 + b , a , b ) ; } static void printNumbers ( int n ) { for ( int i = 0 ; i <= 9 ; i ++ ) for ( int j = i + 1 ; j <= 9 ; j ++ ) generateNumbers ( n , 0 , i , j ) ; Console . Write ( " The ▁ numbers ▁ are : ▁ " ) ; foreach ( int obj in st ) Console . Write ( obj + " ▁ " ) ; st . Clear ( ) ; } public static void Main ( String [ ] args ) { int n = 12 ; printNumbers ( n ) ; } }


"Print all numbers that are divisors of N and are co | C # program for the above approach ; Function to print all numbers that are divisors of N and are co - prime with the quotient of their division ; Iterate upto square root of N ; If divisors are equal and gcd is 1 , then print only one of them ; Otherwise print both ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int gcd ( int a , int b ) { return b == 0 ? a : gcd ( b , a % b ) ; } static void printUnitaryDivisors ( int n ) { for ( int i = 1 ; i <= ( int ) Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i && gcd ( i , n / i ) == 1 ) { Console . Write ( i + " ▁ " ) ; } else { if ( gcd ( i , n / i ) == 1 ) { Console . Write ( i + " ▁ " + n / i + " ▁ " ) ; } } } } } public static void Main ( ) { int N = 12 ; printUnitaryDivisors ( N ) ; } }


"Print all numbers that can be obtained by adding A or B to N exactly M times | C # program for tha above approach ; Function to find all possible numbers that can be obtained by adding A or B to N exactly M times ; For maintaining increasing order ; Smallest number that can be achieved ; If A and B are equal , the only number that can be onbtained is N + M * A ; For finding other numbers , subtract A from number 1 time and add B to number 1 time ; Driver Code ; Given Input ; Function Call"
Solution in C#:

using System ; class GFG { static void possibleNumbers ( int N , int M , int A , int B ) { if ( A > B ) { int temp = A ; A = B ; B = temp ; } int number = N + M * A ; Console . Write ( number + " ▁ " ) ; if ( A != B ) { for ( int i = 0 ; i < M ; i ++ ) { number = number - A + B ; Console . Write ( number + " ▁ " ) ; } } } public static void Main ( String [ ] args ) { int N = 5 , M = 3 , A = 4 , B = 6 ; possibleNumbers ( N , M , A , B ) ; } }


"Print all numbers that can be obtained by adding A or B to N exactly M times | C # program for tha above approach ; Function to find all possible numbers that can be obtained by adding A or B to N exactly M times ; For maintaining increasing order ; Smallest number that can be obtained ; If A and B are equal , then only one number can be obtained , i . e . N + M * A ; For finding others numbers , subtract A from number once and add B to number once ; Driver Code ; Given Input ; Function Call"
Solution in C#:


"Print all numbers that can be obtained by adding A or B to N exactly M times | C # program for the above approach ; Function to find all possible numbers that can be obtained by adding A or B to N exactly N times ; If number of steps is 0 and only possible number is N ; Add A to N and make a recursive call for M - 1 steps ; Add B to N and make a recursive call for M - 1 steps . ; Driver Code ; Given Inputs ; Stores all possible numbers ; Function call ; Print all possible numbers"
Solution in C#:

using System ; using System . Collections . Generic ; public class MyClass { static void possibleNumbers ( HashSet < int > numbers , int N , int M , int A , int B ) { if ( M == 0 ) { numbers . Add ( N ) ; return ; } possibleNumbers ( numbers , N + A , M - 1 , A , B ) ; possibleNumbers ( numbers , N + B , M - 1 , A , B ) ; } public static void Main ( String [ ] args ) { int N = 5 , M = 3 , A = 4 , B = 6 ; HashSet < int > numbers = new HashSet < int > ( ) ; possibleNumbers ( numbers , N , M , A , B ) ; foreach ( int i in numbers ) Console . Write ( i + " ▁ " ) ; } }


"Print all numbers up to N having product of digits equal to K | C # program for the above approach ; Function to find the product of digits of a number ; Stores the product of digits of a number ; Return the product ; Function to print all numbers upto N having product of digits equal to K ; Stores whether any number satisfying the given conditions exists or not ; Iterate over the range [ 1 , N ] ; If product of digits of arr [ i ] is equal to K or not ; Print that number ; If no numbers are found ; Driver Code ; Given value of N & K ; Function call to print all numbers from [ 1 , N ] with product of digits K"
Solution in C#:

using System ; class GFG { static int productOfDigits ( int N ) { int product = 1 ; while ( N != 0 ) { product = product * ( N % 10 ) ; N = N / 10 ; } return product ; } static void productOfDigitsK ( int N , int K ) { int flag = 0 ; for ( int i = 1 ; i <= N ; ++ i ) { if ( K == productOfDigits ( i ) ) { Console . Write ( i + " ▁ " ) ; flag = 1 ; } } if ( flag == 0 ) Console . WriteLine ( - 1 ) ; } static public void Main ( ) { int N = 500 , K = 10 ; productOfDigitsK ( N , K ) ; } }


"Print all numbers up to N in words in lexicographical order | C # program for the above approach ; Function to convert a number to words ; Stores the digits ; Base cases ; Stores strings of unit place ; Stores strings for corner cases ; Stores strings for ten 's place digits ; Stores strings for powers of 10 ; If given number contains a single digit ; Iterate over all the digits ; Represent first 2 digits in words ; Represent last 2 digits in words ; Explicitly handle corner cases [ 10 , 19 ] ; Explicitly handle corner case 20 ; For rest of the two digit numbers i . e . , 21 to 99 ; Function to print all the numbers up to n in lexicographical order ; Convert all numbers in words ; Sort all strings ; Print answer ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static String convert_to_words ( int n ) { char [ ] num = String . Join ( " " , n ) . ToCharArray ( ) ; int len = num . Length ; String ans = " " ; if ( len == 0 ) { ans += " Empty ▁ String " ; return ans ; } String [ ] single_digits = new String [ ] { " zero " , " one " , " two " , " three " , " four " , " five " , " six " , " seven " , " eight " , " nine " } ; String [ ] two_digits = new String [ ] { " " , " ten " , " eleven " , " twelve " , " thirteen " , " fourteen " , " fifteen " , " sixteen " , " seventeen " , " eighteen " , " nineteen " } ; String [ ] tens_multiple = new String [ ] { " " , " " , " twenty " , " thirty " , " forty " , " fifty " , " sixty " , " seventy " , " eighty " , " ninety " } ; String [ ] tens_power = new String [ ] { " hundred " , " thousand " } ; if ( len == 1 ) { ans += single_digits [ num [ 0 ] - '0' ] ; return ans ; } int x = 0 ; while ( x < num . Length ) { if ( len >= 3 ) { if ( num [ x ] - '0' != 0 ) { ans += single_digits [ num [ x ] - '0' ] ; ans += " ▁ " ; ans += tens_power [ len - 3 ] ; ans += " ▁ " ; } -- len ; } else { if ( num [ x ] - '0' == 1 ) { int sum = num [ x ] - '0' + num [ x ] - '0' ; ans += two_digits [ sum ] ; return ans ; } else if ( num [ x ] - '0' == 2 && num [ x + 1 ] - '0' == 0 ) { ans += " twenty " ; return ans ; } else { int i = ( num [ x ] - '0' ) ; if ( i > 0 ) { ans += tens_multiple [ i ] ; ans += " ▁ " ; } else ans += " " ; ++ x ; if ( num [ x ] - '0' != 0 ) ans += single_digits [ num [ x ] - '0' ] ; } } ++ x ; } return " " ; } static void lexNumbers ( int n ) { List < String > s = new List < String > ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { s . Add ( convert_to_words ( i ) ) ; } s . Sort ( ) ; List < String > ans = new List < String > ( ) ; for ( int i = 0 ; i < n ; i ++ ) ans . Add ( s [ i ] ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) Console . Write ( ans [ i ] + " , ▁ " ) ; Console . Write ( ans [ n - 1 ] ) ; } public static void Main ( String [ ] args ) { int n = 5 ; lexNumbers (...


"Print all numbers whose set of prime factors is a subset of the set of the prime factors of X | C # program to implement the above approach ; Function to print all the numbers ; Iterate for every element in the array ; Find the gcd ; Iterate till gcd is 1 of number and x ; Divide the number by gcd ; Find the new gcdg ; If the number is 1 at the end then print the number ; If no numbers have been there ; Driver code"
Solution in C#:

using System ; class GFG { static void printNumbers ( int [ ] a , int n , int x ) { bool flag = false ; for ( int i = 0 ; i < n ; i ++ ) { int num = a [ i ] ; int g = __gcd ( num , x ) ; while ( g != 1 ) { num /= g ; g = __gcd ( num , x ) ; } if ( num == 1 ) { flag = true ; Console . Write ( a [ i ] + " ▁ " ) ; } } if ( ! flag ) Console . WriteLine ( " There ▁ are ▁ no ▁ such ▁ numbers " ) ; } static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } public static void Main ( String [ ] args ) { int x = 60 ; int [ ] a = { 2 , 5 , 10 , 7 , 17 } ; int n = a . Length ; printNumbers ( a , n , x ) ; } }


"Print all pairs of anagrams in a given array of strings | C # program to Print all pairs of anagrams in a given array of strings ; function to check whether two strings are anagram of each other ; Create two count arrays and initialize all values as 0 ; For each character in input strings , increment count in the corresponding count array ; If both strings are of different length . Removing this condition will make the program fail for strings like " aaca " and " aca " ; See if there is any non - zero value in count array ; This function prints all anagram pairs in a given array of strings ; Driver program to test to pront printDups"
Solution in C#:

using System ; class GFG { static int NO_OF_CHARS = 256 ; static bool areAnagram ( String str1 , String str2 ) { int [ ] count = new int [ NO_OF_CHARS ] ; int i ; for ( i = 0 ; i < str1 . Length && i < str2 . Length ; i ++ ) { count [ str1 [ i ] ] ++ ; count [ str2 [ i ] ] -- ; } if ( str1 . Length != str2 . Length ) return false ; for ( i = 0 ; i < NO_OF_CHARS ; i ++ ) if ( count [ i ] != 0 ) return false ; return true ; } static void findAllAnagrams ( String [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( areAnagram ( arr [ i ] , arr [ j ] ) ) Console . WriteLine ( arr [ i ] + " ▁ is ▁ anagram ▁ of ▁ " + arr [ j ] ) ; } public static void Main ( ) { String [ ] arr = { " geeksquiz " , " geeksforgeeks " , " abcd " , " forgeeksgeeks " , " zuiqkeegs " } ; int n = arr . Length ; findAllAnagrams ( arr , n ) ; } }


"Print all pairs with given sum | C # implementation of simple method to find count of pairs with given sum . ; Returns number of pairs in arr [ 0. . n - 1 ] with sum equal to ' sum ' ; Store counts of all elements in map m ; Traverse through all elements ; Search if a pair can be formed with arr [ i ] . ; Driver code"
Solution in C#:

 using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static void printPairs ( int [ ] arr , int n , int sum ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int rem = sum - arr [ i ] ; if ( m . ContainsKey ( rem ) ) { int count = m [ rem ] ; for ( int j = 0 ; j < count ; j ++ ) { Console . Write ( " ( " + rem + " , ▁ " + arr [ i ] + " ) " + "
 " ) ; } } if ( m . ContainsKey ( arr [ i ] ) ) { m [ arr [ i ] ] ++ ; } else { m [ arr [ i ] ] = 1 ; } } } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 5 , 7 , - 1 , 5 } ; int n = arr . Length ; int sum = 6 ; printPairs ( arr , n , sum ) ; } }


"Print all pairs with given sum | C # implementation of simple method to find print pairs with given sum . ; Returns number of pairs in arr [ 0. . n - 1 ] with sum equal to ' sum ' ; Consider all possible pairs and check their sums ; Driver Code"
Solution in C#:

 using System ; class GFG { static void printPairs ( int [ ] arr , int n , int sum ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] + arr [ j ] == sum ) Console . Write ( " ( " + arr [ i ] + " , ▁ " + arr [ j ] + " ) " + "
 " ) ; } public static void Main ( ) { int [ ] arr = { 1 , 5 , 7 , - 1 , 5 } ; int n = arr . Length ; int sum = 6 ; printPairs ( arr , n , sum ) ; } }


"Print all pairs with given sum | C # program to find triplets in a given array whose sum is equal to given sum . ; Driver code"
Solution in C#:

using System ; public class SumOfPairs { public void pairedElements ( int [ ] arr , int sum ) { int low = 0 ; int high = arr . Length - 1 ; while ( low < high ) { if ( arr [ low ] + arr [ high ] == sum ) { Console . WriteLine ( " The ▁ pair ▁ is ▁ : ▁ ( " + arr [ low ] + " , ▁ " + arr [ high ] + " ) " ) ; } if ( arr [ low ] + arr [ high ] > sum ) { high -- ; } else { low ++ ; } } } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 4 , - 2 , 6 , 8 , 9 , 11 } ; Array . Sort ( arr ) ; SumOfPairs sp = new SumOfPairs ( ) ; sp . pairedElements ( arr , 6 ) ; } }


"Print all palindrome dates between the given years | C # implementation of the approach ; Returns true if given year is valid ; Return true if year is a multiple pf 4 and not multiple of 100. OR year is multiple of 400. ; Returns true if given year is valid or not . ; If year , month and day are not in given range ; Handle February month with leap year ; Months of April , June , Sept and Nov must have number of days less than or equal to 30. ; Function to print the palindrome dates between the given years ; For every year ; Current year as a String ; To store the reverse of year ; Get the day and the month ; If the current palindrome date is valid ; Driver code"
Solution in C#:

 using System ; class GFG { static int MAX_VALID_YR = 9999 ; static int MIN_VALID_YR = 1800 ; static bool isLeap ( int year ) { return ( ( ( year % 4 == 0 ) && ( year % 100 != 0 ) ) || ( year % 400 == 0 ) ) ; } static bool isValidDate ( int d , int m , int y ) { if ( y > MAX_VALID_YR y < MIN_VALID_YR ) return false ; if ( m < 1 m > 12 ) return false ; if ( d < 1 d > 31 ) return false ; if ( m == 2 ) { if ( isLeap ( y ) ) return ( d <= 29 ) ; else return ( d <= 28 ) ; } if ( m == 4 m == 6 m == 9 m == 11 ) return ( d <= 30 ) ; return true ; } static void printPalindromeDates ( int y1 , int y2 ) { for ( int year = y1 ; year <= y2 ; year ++ ) { String str = String . Join ( " " , year ) ; String rev = str ; rev = reverse ( rev ) ; int day = Int32 . Parse ( rev . Substring ( 0 , 2 ) ) ; int month = Int32 . Parse ( rev . Substring ( 2 , 2 ) ) ; if ( isValidDate ( day , month , year ) ) { Console . Write ( rev + str + "
 " ) ; } } } static String reverse ( String input ) { char [ ] a = input . ToCharArray ( ) ; int l , r = a . Length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . Join ( " " , a ) ; } public static void Main ( String [ ] args ) { int y1 = 2001 , y2 = 2005 ; printPalindromeDates ( y1 , y2 ) ; } }


"Print all palindromic paths from top left to bottom right in a matrix | C # program to print all palindromic paths from top left to bottom right in a grid . ; i and j are row and column indexes of current cell ( initially these are 0 and 0 ) . ; If we have not reached bottom right corner , keep exlporing ; If we reach bottom right corner , we check if the path used is palindrome or not . ; Driver code"
Solution in C#:

using System ; class GFG { public static bool isPalin ( string str ) { int len = str . Length / 2 ; for ( int i = 0 ; i < len ; i ++ ) { if ( str [ i ] != str [ str . Length - i - 1 ] ) { return false ; } } return true ; } public static void palindromicPath ( string str , char [ ] [ ] a , int i , int j , int m , int n ) { if ( j < m - 1 i < n - 1 ) { if ( i < n - 1 ) { palindromicPath ( str + a [ i ] [ j ] , a , i + 1 , j , m , n ) ; } if ( j < m - 1 ) { palindromicPath ( str + a [ i ] [ j ] , a , i , j + 1 , m , n ) ; } } else { str = str + a [ n - 1 ] [ m - 1 ] ; if ( isPalin ( str ) ) { Console . WriteLine ( str ) ; } } } public static void Main ( string [ ] args ) { char [ ] [ ] arr = new char [ ] [ ] { new char [ ] { ' a ' , ' a ' , ' a ' , ' b ' } , new char [ ] { ' b ' , ' a ' , ' a ' , ' a ' } , new char [ ] { ' a ' , ' b ' , ' b ' , ' a ' } } ; string str = " " ; palindromicPath ( str , arr , 0 , 0 , 4 , 3 ) ; } }


"Print all perfect squares from the given range | C # implementation of the approach ; Function to print all the perfect squares from the given range ; For every element from the range ; If current element is a perfect square ; Driver code"
Solution in C#:

using System ; class GFG { static void perfectSquares ( int l , int r ) { for ( int i = l ; i <= r ; i ++ ) { if ( Math . Sqrt ( i ) == ( int ) Math . Sqrt ( i ) ) Console . Write ( i + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int l = 2 , r = 24 ; perfectSquares ( l , r ) ; } }


"Print all perfect squares from the given range | C # implementation of the approach ; Function to print all the perfect squares from the given range ; Getting the very first number ; First number 's square ; Next number is at the difference of ; While the perfect squares are from the range ; Print the perfect square ; Get the next perfect square ; Next odd number to be added ; Driver code"
Solution in C#:

using System ; class GFG { static void perfectSquares ( float l , float r ) { int number = ( int ) Math . Ceiling ( Math . Sqrt ( l ) ) ; int n2 = number * number ; number = ( number * 2 ) + 1 ; while ( ( n2 >= l && n2 <= r ) ) { Console . Write ( n2 + " ▁ " ) ; n2 = n2 + number ; number += 2 ; } } public static void Main ( String [ ] args ) { int l = 2 , r = 24 ; perfectSquares ( l , r ) ; } }


"Print all permutation of a string using ArrayList | C # implementation of the approach ; Utility function to print the contents of the ArrayList ; Function to returns the arraylist which contains all the permutation of str ; If string is empty ; Return an empty arraylist ; Take first character of str ; Take sub - string starting from the second character ; Recurvise call ; Store the generated permutations into the resultant arraylist ; Return the resultant arraylist ; Driver code"
Solution in C#:

using System . Collections . Generic ; using System ; class GFG { static void printArrayList ( List < String > arrL ) { arrL . Remove ( " " ) ; for ( int i = 0 ; i < arrL . Count ; i ++ ) Console . Write ( arrL [ i ] + " ▁ " ) ; } public static List < String > getPermutation ( String str ) { if ( str . Length == 0 ) { List < String > empty = new List < String > ( ) ; empty . Add ( " " ) ; return empty ; } char ch = str [ 0 ] ; String subStr = str . Substring ( 1 ) ; List < String > prevResult = getPermutation ( subStr ) ; List < String > Res = new List < String > ( ) ; foreach ( String val in prevResult ) { for ( int i = 0 ; i <= val . Length ; i ++ ) { Res . Add ( val . Substring ( 0 , i ) + ch + val . Substring ( i ) ) ; } } return Res ; } public static void Main ( String [ ] args ) { String str = " abc " ; printArrayList ( getPermutation ( str ) ) ; } }


"Print all permutations of a number N greater than itself | C # implementation to print all the permutation greater than the integer N ; Function to print all the permutation which are greater than N itself ; Iterate and count the number of digits in N ; vector to print the permutations of N ; Store digits of N in the vector num ; Iterate over every permutation of N which is greater than N ; Print the current permutation of N ; Function to print all the permutation which are greater than N itself ; Driver Code"
Solution in C#:

 using System ; class GFG { static void printPermutation ( int N ) { int temp = N , count = 0 ; while ( temp > 0 ) { count ++ ; temp /= 10 ; } int [ ] num = new int [ count ] ; while ( N > 0 ) { num [ count -- - 1 ] = N % 10 ; N = N / 10 ; } while ( next_permutation ( num ) ) { for ( int i = 0 ; i < num . Length ; i ++ ) Console . Write ( num [ i ] ) ; Console . Write ( "
 " ) ; } } static bool next_permutation ( int [ ] p ) { for ( int a = p . Length - 2 ; a >= 0 ; -- a ) if ( p [ a ] < p [ a + 1 ] ) for ( int b = p . Length - 1 ; ; -- b ) if ( p [ b ] > p [ a ] ) { int t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; for ( ++ a , b = p . Length - 1 ; a < b ; ++ a , -- b ) { t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; } return true ; } return false ; } public static void Main ( String [ ] args ) { int N = 324 ; printPermutation ( N ) ; } }


"Print all positions of a given string having count of smaller characters equal on both sides | C # program to implement the above approach ; Function to find indexes of the given string that satisfy the condition ; Stores length of given string ; Stores frequency of each character of str ; Update frequency of current character ; cntLeftFreq [ i ] Stores frequency of characters present on the left side of index i . ; Traverse the given string ; Stores count of smaller characters on left side of i . ; Stores count of smaller characters on Right side of i . ; Traverse smaller characters on left side of index i . ; Update cntLeft ; Update cntRight ; Update cntLeftFreq [ str [ i ] ] ; If count of smaller elements on both sides equal ; Print current index ; ; Driver Code"
Solution in C#:

using System ; class GFG { static void printIndexes ( char [ ] str ) { int N = str . Length ; int [ ] cntFreq = new int [ 256 ] ; for ( int i = 0 ; i < N ; i ++ ) { cntFreq [ str [ i ] ] ++ ; } int [ ] cntLeftFreq = new int [ 256 ] ; for ( int i = 0 ; i < N ; i ++ ) { int cntLeft = 0 ; int cntRight = 0 ; for ( int j = str [ i ] - 1 ; j >= 0 ; j -- ) { cntLeft += cntLeftFreq [ j ] ; cntRight += cntFreq [ j ] - cntLeftFreq [ j ] ; } cntLeftFreq [ str [ i ] ] ++ ; if ( cntLeft == cntRight && cntLeft != 0 ) { Console . Write ( i + " ▁ " ) ; } } } public static void Main ( ) { string str = " aabacdabbb " ; printIndexes ( str . ToCharArray ( ) ) ; } }


"Print all possible combinations of words from Dictionary using Trie | C # implementation of the approach ; Trie node ; isEndOfWord is true if node represents the end of the word ; Returns new trie node ; If not present , inserts key into trie If the key is prefix of trie node , marks the node as leaf node ; Mark node as leaf ; Returns true if the key is present in the trie ; Result stores the current prefix with spaces between words ; Process all prefixes one by one ; Extract subString from 0 to i in prefix ; If trie conatins this prefix then check for the remaining String . Otherwise ignore this prefix ; If no more elements are there then print ; Add this element to the previous prefix ; If ( result == word ) then return If you don 't want to print last word ; Driver code"
Solution in C#:

 using System ; class GFG { static int ALPHABET_SIZE = 26 ; class TrieNode { public TrieNode [ ] children = new TrieNode [ ALPHABET_SIZE ] ; public bool isEndOfWord ; public TrieNode ( ) { } } ; static TrieNode getNode ( ) { TrieNode pNode = new TrieNode ( ) ; pNode . isEndOfWord = false ; for ( int i = 0 ; i < ALPHABET_SIZE ; i ++ ) pNode . children [ i ] = null ; return pNode ; } static void insert ( TrieNode root , String key ) { TrieNode pCrawl = root ; for ( int i = 0 ; i < key . Length ; i ++ ) { int index = key [ i ] - ' a ' ; if ( pCrawl . children [ index ] == null ) pCrawl . children [ index ] = getNode ( ) ; pCrawl = pCrawl . children [ index ] ; } pCrawl . isEndOfWord = true ; } static bool search ( TrieNode root , String key ) { TrieNode pCrawl = root ; for ( int i = 0 ; i < key . Length ; i ++ ) { int index = key [ i ] - ' a ' ; if ( pCrawl . children [ index ] == null ) return false ; pCrawl = pCrawl . children [ index ] ; } return ( pCrawl != null && pCrawl . isEndOfWord ) ; } static void wordBreakAll ( TrieNode root , String word , int n , String result ) { for ( int i = 1 ; i <= n ; i ++ ) { String prefix = word . Substring ( 0 , i ) ; if ( search ( root , prefix ) ) { if ( i == n ) { result += prefix ; Console . Write ( " 	 " + result + "
 " ) ; return ; } wordBreakAll ( root , word . Substring ( i , n - i ) , n - i , result + prefix + " ▁ " ) ; } } } public static void Main ( String [ ] args ) { new TrieNode ( ) ; TrieNode root = getNode ( ) ; String [ ] dictionary = { " sam " , " sung " , " samsung " } ; int n = dictionary . Length ; for ( int i = 0 ; i < n ; i ++ ) { insert ( root , dictionary [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( dictionary [ i ] + " : ▁
 " ) ; wordBreakAll ( root , dictionary [ i ] , dictionary [ i ] . Length , " " ) ; } } }


"Print all possible pair with prime XOR in the Array | C # implementation of the above approach ; Function for Sieve of Eratosthenes ; If i is prime , then make all multiples of i false ; Function to print all Pairs whose XOR is prime ; if A [ i ] ^ A [ j ] is prime , then print this pair ; Driver Code ; Generate all the prime number ; Function Call"
Solution in C#:

using System ; class GFG { static int sz = ( int ) 1e5 ; static bool [ ] isPrime = new bool [ sz + 1 ] ; static void generatePrime ( ) { int i , j ; for ( i = 2 ; i <= sz ; i ++ ) isPrime [ i ] = true ; for ( i = 2 ; i * i <= sz ; i ++ ) { if ( isPrime [ i ] ) { for ( j = i * i ; j < sz ; j += i ) { isPrime [ j ] = false ; } } } } static void Pair_of_PrimeXor ( int [ ] A , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( isPrime [ ( A [ i ] ^ A [ j ] ) ] ) { Console . Write ( " ( " + A [ i ] + " , ▁ " + A [ j ] + " ) ▁ " ) ; } } } } public static void Main ( String [ ] args ) { int [ ] A = { 1 , 3 , 6 , 11 } ; int n = A . Length ; generatePrime ( ) ; Pair_of_PrimeXor ( A , n ) ; } }


"Print all possible paths from top left to bottom right of a mXn matrix | C # program to Print all possible paths from top left to bottom right of a mXn matrix ; If we reach the bottom of maze , we can only move right ; path . append ( maze [ i ] [ k ] ) ; If we hit this block , it means one path is completed . Add it to paths list and print ; If we reach to the right most corner , we can only move down ; path . append ( maze [ j ] [ k ] ) If we hit this block , it means one path is completed . Add it to paths list and print ; Add current element to the path list path . append ( maze [ i ] [ j ] ) ; Move down in y direction and call findPathsUtil recursively ; Move down in y direction and call findPathsUtil recursively ;  ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static List < List < int > > allPaths = new List < List < int > > ( ) ; static void findPathsUtil ( List < List < int > > maze , int m , int n , int i , int j , List < int > path , int indx ) { if ( i == m - 1 ) { for ( int k = j ; k < n ; k ++ ) { path [ indx + k - j ] = maze [ i ] [ k ] ; } Console . Write ( " [ " + path [ 0 ] + " , ▁ " ) ; for ( int z = 1 ; z < path . Count - 1 ; z ++ ) { Console . Write ( path [ z ] + " , ▁ " ) ; } Console . WriteLine ( path [ path . Count - 1 ] + " ] " ) ; allPaths . Add ( path ) ; return ; } if ( j == n - 1 ) { for ( int k = i ; k < m ; k ++ ) { path [ indx + k - i ] = maze [ k ] [ j ] ; } Console . Write ( " [ " + path [ 0 ] + " , ▁ " ) ; for ( int z = 1 ; z < path . Count - 1 ; z ++ ) { Console . Write ( path [ z ] + " , ▁ " ) ; } Console . WriteLine ( path [ path . Count - 1 ] + " ] " ) ; allPaths . Add ( path ) ; return ; } path [ indx ] = maze [ i ] [ j ] ; findPathsUtil ( maze , m , n , i + 1 , j , path , indx + 1 ) ; findPathsUtil ( maze , m , n , i , j + 1 , path , indx + 1 ) ; } static void findPaths ( List < List < int > > maze , int m , int n ) { List < int > path = new List < int > ( ) ; for ( int i = 0 ; i < m + n - 1 ; i ++ ) { path . Add ( 0 ) ; } findPathsUtil ( maze , m , n , 0 , 0 , path , 0 ) ; } static void Main ( ) { List < List < int > > maze = new List < List < int > > ( ) ; maze . Add ( new List < int > { 1 , 2 , 3 } ) ; maze . Add ( new List < int > { 4 , 5 , 6 } ) ; maze . Add ( new List < int > { 7 , 8 , 9 } ) ; findPaths ( maze , 3 , 3 ) ; } }


"Print all possible strings that can be made by placing spaces | C # program to print all strings that can be made by placing spaces ; Function to print all subsequences ; Driver code"
Solution in C#:

using System ; class GFG { static void printSubsequences ( String s ) { char [ ] str = s . ToCharArray ( ) ; int n = str . Length ; int opsize = ( int ) ( Math . Pow ( 2 , n - 1 ) ) ; for ( int counter = 0 ; counter < opsize ; counter ++ ) { for ( int j = 0 ; j < n ; j ++ ) { Console . Write ( str [ j ] ) ; if ( ( counter & ( 1 << j ) ) > 0 ) Console . Write ( " ▁ " ) ; } Console . WriteLine ( ) ; } } public static void Main ( ) { String str = " ABC " ; printSubsequences ( str ) ; } }


"Print all possible strings that can be made by placing spaces | C # program to print permutations of a given string with spaces ; Function recursively prints the strings having space pattern i and j are indices in ' String ▁ ▁ str ' and ' buf [ ] ' respectively ; Either put the character ; Or put a space followed by next character ; Function creates buf [ ] to store individual output string and uses printPatternUtil ( ) to print all permutations ; Buffer to hold the string containing spaces 2 n - 1 characters and 1 string terminator ; Copy the first character as it is , since it will be always at first position ; Driver program"
Solution in C#:

using System ; class GFG { static void printPatternUtil ( string str , char [ ] buf , int i , int j , int n ) { if ( i == n ) { buf [ j ] = ' \0' ; Console . WriteLine ( buf ) ; return ; } buf [ j ] = str [ i ] ; printPatternUtil ( str , buf , i + 1 , j + 1 , n ) ; buf [ j ] = ' ▁ ' ; buf [ j + 1 ] = str [ i ] ; printPatternUtil ( str , buf , i + 1 , j + 2 , n ) ; } static void printPattern ( string str ) { int len = str . Length ; char [ ] buf = new char [ 2 * len ] ; buf [ 0 ] = str [ 0 ] ; printPatternUtil ( str , buf , 1 , 1 , len ) ; } public static void Main ( ) { string str = " ABCD " ; printPattern ( str ) ; } }


"Print all possible ways to split an array into K subsets | C # program for the above approach ; Utility function to find all possible ways to split array into K subsets ; If count of elements in K subsets are greater than or equal to N ; If count of subsets formed is equal to K ; Print K subsets by splitting array into K subsets ; Print current subset ; If current element is the last element of the subset ; Otherwise ; If any subset is occupied , then push the element in that first ; Recursively do the same for remaining elements ; Backtrack ; Otherwise , push it in an empty subset and increase the subset count by 1 ; Break to avoid the case of going in other empty subsets , if available , and forming the same combination ; Function to to find all possible ways to split array into K subsets ; Stores K subset by splitting array into K subsets ; Size of each subset must be less than the number of elements ; Driver Code ; Given array ; Given K ; Size of the array ; Prints all possible splits into subsets"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static void PartitionSub ( int [ ] arr , int i , int N , int K , int nos , List < List < int > > v ) { if ( i >= N ) { if ( nos == K ) { for ( int x = 0 ; x < v . Count ; x ++ ) { Console . Write ( " { ▁ " ) ; for ( int y = 0 ; y < v [ x ] . Count ; y ++ ) { Console . Write ( v [ x ] [ y ] ) ; if ( y == v [ x ] . Count - 1 ) { Console . Write ( " ▁ " ) ; } else { Console . Write ( " , ▁ " ) ; } } if ( x == v . Count - 1 ) { Console . Write ( " } " ) ; } else { Console . Write ( " } , ▁ " ) ; } } Console . Write ( "
 " ) ; } return ; } for ( int j = 0 ; j < K ; j ++ ) { if ( v [ j ] . Count > 0 ) { v [ j ] . Add ( arr [ i ] ) ; PartitionSub ( arr , i + 1 , N , K , nos , v ) ; v [ j ] . RemoveAt ( v [ j ] . Count - 1 ) ; } else { v [ j ] . Add ( arr [ i ] ) ; PartitionSub ( arr , i + 1 , N , K , nos + 1 , v ) ; v [ j ] . RemoveAt ( v [ j ] . Count - 1 ) ; break ; } } } static void partKSubsets ( int [ ] arr , int N , int K ) { List < List < int > > v = new List < List < int > > ( ) ; for ( int i = 0 ; i < K ; i ++ ) v . Add ( new List < int > ( ) ) ; if ( K == 0 K > N ) { Console . WriteLine ( " Not ▁ Possible " ) ; } else { Console . WriteLine ( " The ▁ Subset ▁ Combinations ▁ are : ▁ " ) ; PartitionSub ( arr , 0 , N , K , 0 , v ) ; } } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int K = 2 ; int N = arr . Length ; partKSubsets ( arr , N , K ) ; } }


"Print all proper fractions with denominators less than equal to N | C # program to implement the above approach ; Function to print all proper fractions ; If the numerator and the denominator are coprime ; Driver code"
Solution in C#:

using System ; class GFG { static void printFractions ( int n ) { for ( int i = 1 ; i < n ; i ++ ) { for ( int j = i + 1 ; j <= n ; j ++ ) { if ( __gcd ( i , j ) == 1 ) { string a = i . ToString ( ) ; string b = j . ToString ( ) ; Console . Write ( a + " / " + b + " , ▁ " ) ; } } } } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void Main ( string [ ] args ) { int n = 3 ; printFractions ( n ) ; } }


"Print all root to leaf paths of an N |  ; Structure of an N ary tree node ; Parameterized Constructor ; Function to print the root to leaf path of the given N - ary Tree ; Print elements in the vector ; Utility function to print all root to leaf paths of an Nary Tree ; If root is null ; Insert current node 's  data into the vector ; If current node is a leaf node ; Print the path ; Pop the leaf node and return ; Recur for all children of the current node ; Recursive Function Call ; Function to print root to leaf path ; If root is null , return ; Stores the root to leaf path ; Utility function call ; Driver Code ; Given N - Ary tree ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; public class Node { public int data ; public List < Node > child ; public Node ( int x ) { this . data = x ; this . child = new List < Node > ( ) ; } } public class GFG { static void printPath ( List < int > vec ) { foreach ( int ele in vec ) { Console . Write ( ele + " ▁ " ) ; } Console . WriteLine ( ) ; } static void printAllRootToLeafPaths ( Node root , List < int > vec ) { if ( root == null ) return ; vec . Add ( root . data ) ; if ( root . child . Count == 0 ) { printPath ( vec ) ; vec . RemoveAt ( vec . Count - 1 ) ; return ; } for ( int i = 0 ; i < root . child . Count ; i ++ ) { printAllRootToLeafPaths ( root . child [ i ] , vec ) ; } vec . RemoveAt ( vec . Count - 1 ) ; } static void printAllRootToLeafPaths ( Node root ) { if ( root == null ) return ; List < int > vec = new List < int > ( ) ; printAllRootToLeafPaths ( root , vec ) ; } static public void Main ( ) { Node root = new Node ( 1 ) ; ( root . child ) . Add ( new Node ( 2 ) ) ; ( root . child ) . Add ( new Node ( 3 ) ) ; ( root . child [ 0 ] . child ) . Add ( new Node ( 4 ) ) ; ( root . child [ 1 ] . child ) . Add ( new Node ( 5 ) ) ; ( root . child [ 1 ] . child ) . Add ( new Node ( 6 ) ) ; ( root . child [ 1 ] . child [ 1 ] . child ) . Add ( new Node ( 7 ) ) ; ( root . child [ 1 ] . child [ 1 ] . child ) . Add ( new Node ( 8 ) ) ; printAllRootToLeafPaths ( root ) ; } }


"Print all root to leaf paths with there relative positions | C # program to print all root to leaf paths with there relative position ; tree structure ; Function create new node ; Store path information ; Prints given root to leaf path with underscores ; Find the minimum horizontal distance value in current root to leaf path ; Find minimum horizontal distance ; Print the root to leaf path with " _ " that indicate the related position ; Current tree node ; Print underscore ; Print current key ; A utility function print all path from root to leaf working of this function is similar to function of " Print _ vertical _ order " : Print paths of binary tree in vertical order www . geeksforgeeks . org / print - binary - tree - vertical - order - set - 2 / https : ; Base case ; Leaf node ; Add leaf node and then print path ; Store current path information ; Call left sub_tree ; Call left sub_tree ; Base case ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; public class Graph { static readonly int MAX_PATH_SIZE = 1000 ; public class Node { public char data ; public Node left , right ; } ; static Node newNode ( char data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } public class PATH { public int Hd ; public char key ; public PATH ( int Hd , char key ) { this . Hd = Hd ; this . key = key ; } public PATH ( ) { } } ; static void printPath ( List < PATH > path , int size ) { int minimum_Hd = int . MaxValue ; PATH p ; for ( int it = 0 ; it < size ; it ++ ) { p = path [ it ] ; minimum_Hd = Math . Min ( minimum_Hd , p . Hd ) ; } for ( int it = 0 ; it < size ; it ++ ) { p = path [ it ] ; int noOfUnderScores = Math . Abs ( p . Hd - minimum_Hd ) ; for ( int i = 0 ; i < noOfUnderScores ; i ++ ) Console . Write ( " _ " ) ; Console . WriteLine ( p . key ) ; } Console . WriteLine ( " = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = " ) ; } static void printAllPathsUtil ( Node root , List < PATH > AllPath , int HD , int order ) { if ( root == null ) return ; if ( root . left == null && root . right == null ) { AllPath [ order ] = new PATH ( HD , root . data ) ; printPath ( AllPath , order + 1 ) ; return ; } AllPath [ order ] = new PATH ( HD , root . data ) ; printAllPathsUtil ( root . left , AllPath , HD - 1 , order + 1 ) ; printAllPathsUtil ( root . right , AllPath , HD + 1 , order + 1 ) ; } static void printAllPaths ( Node root ) { if ( root == null ) return ; List < PATH > Allpaths = new List < PATH > ( ) ; for ( int i = 0 ; i < MAX_PATH_SIZE ; i ++ ) { Allpaths . Add ( new PATH ( ) ) ; } printAllPathsUtil ( root , Allpaths , 0 , 0 ) ; } public static void Main ( String [ ] args ) { Node root = newNode ( ' A ' ) ; root . left = newNode ( ' B ' ) ; root . right = newNode ( ' C ' ) ; root . left . left = newNode ( ' D ' ) ; root . left . right = newNode ( ' E ' ) ; root . right . left = newNode ( ' F ' ) ; root . right . right = newNode ( ' G ' ) ;...


"Print all safe primes below N | C # implementation of the approach ; Function to print first n safe primes ; Initialize all entries of integer array as 1. A value in prime [ i ] will finally be 0 if i is Not a prime , else 1 ; 0 and 1 are not primes ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; If i is prime ; 2 p + 1 ; If 2 p + 1 is also a prime then set prime [ 2 p + 1 ] = 2 ; i is a safe prime ; Driver code"
Solution in C#:

using System ; class GFG { static void printSafePrimes ( int n ) { int [ ] prime = new int [ n + 1 ] ; for ( int i = 2 ; i <= n ; i ++ ) prime [ i ] = 1 ; prime [ 0 ] = prime [ 1 ] = 0 ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == 1 ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = 0 ; } } for ( int i = 2 ; i <= n ; i ++ ) { if ( prime [ i ] != 0 ) { int temp = ( 2 * i ) + 1 ; if ( temp <= n && prime [ temp ] != 0 ) prime [ temp ] = 2 ; } } for ( int i = 5 ; i <= n ; i ++ ) if ( prime [ i ] == 2 ) Console . Write ( i + " ▁ " ) ; } public static void Main ( ) { int n = 20 ; printSafePrimes ( n ) ; } }


"Print all sequences of given length | C # program of above approach ; A utility function that prints a given arr [ ] of length size ; The core function that recursively generates and prints all sequences of length k ; A function that uses printSequencesRecur ( ) to prints all sequences from 1 , 1 , . .1 to n , n , . . n ; Driver Code"
Solution in C#:

using System ; class GFG { static void printArray ( int [ ] arr , int size ) { for ( int i = 0 ; i < size ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; return ; } static void printSequencesRecur ( int [ ] arr , int n , int k , int index ) { int i ; if ( k == 0 ) { printArray ( arr , index ) ; } if ( k > 0 ) { for ( i = 1 ; i <= n ; ++ i ) { arr [ index ] = i ; printSequencesRecur ( arr , n , k - 1 , index + 1 ) ; } } } static void printSequences ( int n , int k ) { int [ ] arr = new int [ k ] ; printSequencesRecur ( arr , n , k , 0 ) ; return ; } public static void Main ( ) { int n = 3 ; int k = 2 ; printSequences ( n , k ) ; } }


"Print all strings of maximum length from an array of strings | C # program to implement the above approach ; Function to find the length of the longest String from the given array of Strings ; Traverse the array ; Stores the length of current String ; Update maximum length ; Return the maximum length ; Function to print the longest Strings from the array ; Find the Strings having length equals to len ; Print the resultant vector of Strings ; Function to print all the longest Strings from the array ; Find the length of longest String ; Find and print all the Strings having length equals to max ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int maxLength ( String [ ] arr ) { int len = int . MinValue ; int N = arr . Length ; for ( int i = 0 ; i < N ; i ++ ) { int l = arr [ i ] . Length ; if ( len < l ) { len = l ; } } return len ; } static void maxStrings ( String [ ] arr , int len ) { int N = arr . Length ; List < String > ans = new List < String > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( len == arr [ i ] . Length ) { ans . Add ( arr [ i ] ) ; } } for ( int i = 0 ; i < ans . Count ; i ++ ) { Console . Write ( ans [ i ] + " ▁ " ) ; } } static void printStrings ( String [ ] arr ) { int max = maxLength ( arr ) ; maxStrings ( arr , max ) ; } public static void Main ( String [ ] args ) { String [ ] arr = { " aba " , " aa " , " ad " , " vcd " , " aba " } ; printStrings ( arr ) ; } }


"Print all submasks of a given mask | C # program for the above approach ; Function to print the submasks of N ; Driver Code"
Solution in C#:

using System ; class GFG { static void SubMasks ( int N ) { for ( int S = N ; S > 0 ; S = ( S - 1 ) & N ) { Console . Write ( S + " ▁ " ) ; } } static public void Main ( ) { int N = 25 ; SubMasks ( N ) ; } }


"Print all substring of a number without any conversion | C # implementation of above approach ; Function to print the substrings of a number ; Calculate the total number of digits ; 0.5 has been added because of it will return double value like 99.556 ; Print all the numbers from starting position ; Update the no . ; Update the no . of digits ; Driver code"
Solution in C#:

using System ; class GFG { static void printSubstrings ( int n ) { int s = ( int ) Math . Log10 ( n ) ; int d = ( int ) ( Math . Pow ( 10 , s ) + 0.5 ) ; int k = d ; while ( n > 0 ) { while ( d > 0 ) { Console . WriteLine ( n / d ) ; d = d / 10 ; } n = n % k ; k = k / 10 ; d = k ; } } public static void Main ( ) { int n = 123 ; printSubstrings ( n ) ; } }


"Print all the combinations of N elements by changing sign such that their sum is divisible by M | Print all the combinations of N elements by changing sign such that their sum is divisible by M ; Function to print all the combinations ; Iterate for all combinations ; Initially 100 in binary if n is 3 as 1 << ( 3 - 1 ) = 100 in binary ; Iterate in the array and assign signs to the array elements ; If the j - th bit from left is set take ' + ' sign ; Right shift to check if jth bit is set or not ; re - initialize ; Iterate in the array elements ; If the jth from left is set ; right shift ; Driver code"
Solution in C#:

 using System ; class GFG { static void printCombinations ( int [ ] a , int n , int m ) { for ( int i = 0 ; i < ( 1 << n ) ; i ++ ) { int sum = 0 ; int num = 1 << ( n - 1 ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( i & num ) > 0 ) sum += a [ j ] ; else sum += ( - 1 * a [ j ] ) ; num = num >> 1 ; } if ( sum % m == 0 ) { num = 1 << ( n - 1 ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( i & num ) > 0 ) Console . Write ( " + " + a [ j ] + " ▁ " ) ; else Console . Write ( " - " + a [ j ] + " ▁ " ) ; num = num >> 1 ; } Console . Write ( "
 " ) ; } } } public static void Main ( ) { int [ ] a = { 3 , 5 , 6 , 8 } ; int n = a . Length ; int m = 5 ; printCombinations ( a , n , m ) ; } }


"Print all the combinations of a string in lexicographical order | C # program to find all combinations of a string in lexicographical order ; function to print string ; Method to found all combination of string it is based in tree ; return if level is equal size of string ; if occurrence of char is 0 then skip the iteration of loop ; decrease the char occurrence by 1 ; store the char in result ; print the string till level ; call the function from level + 1 ; backtracking ; declare the map for store each char with occurrence ; initialize the input array with all unique char ; initialize the count array with occurrence the unique char ; temporary char array for store the result ; store the element of input array ; store the element of count array ; size of map ( no of unique char ) ; size of original string ; call function for print string combination ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void printResult ( char [ ] result , int len ) { for ( int i = 0 ; i <= len ; i ++ ) Console . Write ( result [ i ] ) ; Console . WriteLine ( ) ; } static void stringCombination ( char [ ] result , char [ ] str , int [ ] count , int level , int size , int length ) { if ( level == size ) return ; for ( int i = 0 ; i < length ; i ++ ) { if ( count [ i ] == 0 ) continue ; count [ i ] -- ; result [ level ] = str [ i ] ; printResult ( result , level ) ; stringCombination ( result , str , count , level + 1 , size , length ) ; count [ i ] ++ ; } } static void combination ( String str ) { int i ; Dictionary < char , int > mp = new Dictionary < char , int > ( ) ; for ( i = 0 ; i < str . Length ; i ++ ) if ( mp . ContainsKey ( str [ i ] ) ) mp [ str [ i ] ] = mp [ str [ i ] ] + 1 ; else mp . Add ( str [ i ] , 1 ) ; char [ ] input = new char [ mp . Count ] ; int [ ] count = new int [ mp . Count ] ; char [ ] result = new char [ str . Length ] ; i = 0 ; foreach ( KeyValuePair < char , int > entry in mp ) { input [ i ] = entry . Key ; count [ i ] = entry . Value ; i ++ ; } int length = mp . Count ; int size = str . Length ; stringCombination ( result , input , count , 0 , size , length ) ; } public static void Main ( String [ ] args ) { String str = " ABC " ; combination ( str ) ; } }


"Print all the leaf nodes of Binary Heap | C # implementation to print the leaf nodes of a Binary Heap ; Function to calculate height of the Binary heap with given the count of the nodes ; Function to find the leaf nodes of binary heap ; Calculate the height of the complete binary tree ; if the height if h - 1 , then there should not be any child nodes ; Function to print the leaf nodes ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int height ( int N ) { return ( int ) Math . Ceiling ( Math . Log ( N + 1 ) / Math . Log ( 2 ) ) - 1 ; } static void findLeafNodes ( int [ ] arr , int n ) { int h = height ( n ) ; List < int > arrlist = new List < int > ( ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( height ( i + 1 ) == h ) { arrlist . Add ( arr [ i ] ) ; } else if ( height ( i + 1 ) == h - 1 && n <= ( ( 2 * i ) + 1 ) ) { arrlist . Add ( arr [ i ] ) ; } else { break ; } } printLeafNodes ( arrlist ) ; } static void printLeafNodes ( List < int > arrlist ) { for ( int i = arrlist . Count - 1 ; i >= 0 ; i -- ) { Console . Write ( arrlist [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; findLeafNodes ( arr , arr . Length ) ; } }


"Print all the non | C # program to print all the non - repeating words from the two given sentences ; Function to print all the non - repeating words from the two given sentences ; Concatenate the two Strings into one ; Iterating over the whole concatenated String ; Searching for the word in A . If while searching , we reach the end of the String , then the word is not present in the String ; Initialise word for the next iteration ; Driver code"
Solution in C#:

using System ; class GFG { static void removeRepeating ( string s1 , string s2 ) { string s3 = s1 + " ▁ " + s2 + " ▁ " ; string words = " " ; int i = 0 ; foreach ( char x in s3 . ToCharArray ( ) ) { if ( x == ' ▁ ' ) { if ( ! s1 . Contains ( words ) || ! s2 . Contains ( words ) ) Console . Write ( words ) ; words = " ▁ " ; } else { words = words + x ; } } } public static void Main ( string [ ] args ) { string s1 = " I ▁ have ▁ go ▁ a ▁ pen " ; string s2 = " I ▁ want ▁ to ▁ go ▁ park " ; removeRepeating ( s1 , s2 ) ; } }


"Print all the pairs that contains the positive and negative values of an element | C # program to find pairs of positive and negative values present in an array . ; Function ; Sort the array ; Traverse the array ; For every arr [ i ] < 0 element , do a binary search for arr [ i ] > 0. ; If found , print the pair . ; Driver code"
Solution in C#:

using System ; public class GFG { static void printPairs ( int [ ] arr , int n ) { bool pair_exists = false ; Array . Sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 0 ) { if ( Array . BinarySearch ( arr , - arr [ i ] ) != - 1 ) { Console . WriteLine ( arr [ i ] + " , ▁ " + - arr [ i ] ) ; pair_exists = true ; } } else break ; } if ( pair_exists == false ) Console . WriteLine ( " No ▁ such ▁ pair ▁ exists " ) ; } public static void Main ( ) { int [ ] arr = { 4 , 8 , 9 , - 4 , 1 , - 1 , - 8 , - 9 } ; int n = arr . Length ; printPairs ( arr , n ) ; } }


"Print all the pairs that contains the positive and negative values of an element | C # program to find pairs of positive and negative values present in an array ; Function to print pairs of positive and negative values present in the array ; Store all the positive elements in the unordered_set ; Start traversing the array ; Check if the positive value of current element exists in the set or not ; Print that pair ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void printPairs ( int [ ] arr , int n ) { HashSet < int > pairs = new HashSet < int > ( ) ; bool pair_exists = false ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] > 0 ) pairs . Add ( arr [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 0 ) if ( pairs . Contains ( - arr [ i ] ) ) { Console . WriteLine ( arr [ i ] + " , ▁ " + - arr [ i ] ) ; pair_exists = true ; } } if ( pair_exists == false ) Console . WriteLine ( " No ▁ such ▁ pair ▁ exists " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 8 , 9 , - 4 , 1 , - 1 , - 8 , - 9 } ; int n = arr . Length ; printPairs ( arr , n ) ; } }


"Print all the permutation of length L using the elements of an array | Iterative | C # implementation for above approach ; Convert the number to Lth base and print the sequence ; Sequence is of length L ; Print the ith element of sequence ; Print all the permuataions ; There can be ( len ) ^ l permutations ; Convert i to len th base ; Driver code ; function call"
Solution in C#:

using System ; class GFG { static void convert_To_Len_th_base ( int n , int [ ] arr , int len , int L ) { for ( int i = 0 ; i < L ; i ++ ) { Console . Write ( arr [ n % len ] ) ; n /= len ; } Console . WriteLine ( ) ; } static void print ( int [ ] arr , int len , int L ) { for ( int i = 0 ; i < ( int ) Math . Pow ( len , L ) ; i ++ ) { convert_To_Len_th_base ( i , arr , len , L ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 } ; int len = arr . Length ; int L = 2 ; print ( arr , len , L ) ; } }


"Print all the sum pairs which occur maximum number of times | C # implementation of above approach ; Function to find the sum pairs that occur the most ; Hash - table ; Keep a count of sum pairs ; Variables to store maximum occurrence ; Iterate in the hash table ; Print all sum pair which occur maximum number of times ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void findSumPairs ( int [ ] a , int n ) { Dictionary < int , int > mpp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( mpp . ContainsKey ( a [ i ] + a [ j ] ) ) { var val = mpp [ a [ i ] + a [ j ] ] ; mpp . Remove ( a [ i ] + a [ j ] ) ; mpp . Add ( a [ i ] + a [ j ] , val + 1 ) ; } else { mpp . Add ( a [ i ] + a [ j ] , 1 ) ; } } } int occur = 0 ; foreach ( KeyValuePair < int , int > entry in mpp ) { if ( entry . Value > occur ) { occur = entry . Value ; } } foreach ( KeyValuePair < int , int > entry in mpp ) { if ( entry . Value == occur ) Console . WriteLine ( entry . Key ) ; } } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 8 , 3 , 11 , 4 , 9 , 2 , 7 } ; int n = a . Length ; findSumPairs ( a , n ) ; } }


"Print all triplets in sorted array that form AP | C # implementation to print all the triplets in given array that form Arithmetic Progression ; Function to print all triplets in given sorted array that forms AP ; Search other two elements of AP with arr [ i ] as middle . ; if a triplet is found ; Since elements are distinct , arr [ k ] and arr [ j ] cannot form any more triplets with arr [ i ] ; If middle element is more move to higher side , else move lower side . ; Driver code"
Solution in C#:

using System ; class GFG { static void findAllTriplets ( int [ ] arr , int n ) { for ( int i = 1 ; i < n - 1 ; i ++ ) { for ( int j = i - 1 , k = i + 1 ; j >= 0 && k < n ; ) { if ( arr [ j ] + arr [ k ] == 2 * arr [ i ] ) { Console . WriteLine ( arr [ j ] + " ▁ " + arr [ i ] + " ▁ " + arr [ k ] ) ; k ++ ; j -- ; } else if ( arr [ j ] + arr [ k ] < 2 * arr [ i ] ) k ++ ; else j -- ; } } } public static void Main ( ) { int [ ] arr = { 2 , 6 , 9 , 12 , 17 , 22 , 31 , 32 , 35 , 42 } ; int n = arr . Length ; findAllTriplets ( arr , n ) ; } }


"Print all triplets in sorted array that form AP | C # program to print all triplets in given array that form Arithmetic Progression ; Function to print all triplets in given sorted array that forms AP ; Use hash to find if there is a previous element with difference equal to arr [ j ] - arr [ i ] ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void printAllAPTriplets ( int [ ] arr , int n ) { List < int > s = new List < int > ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int diff = arr [ j ] - arr [ i ] ; bool exists = s . Exists ( element => element == ( arr [ i ] - diff ) ) ; if ( exists ) Console . WriteLine ( arr [ i ] - diff + " ▁ " + arr [ i ] + " ▁ " + arr [ j ] ) ; } s . Add ( arr [ i ] ) ; } } static void Main ( ) { int [ ] arr = new int [ ] { 2 , 6 , 9 , 12 , 17 , 22 , 31 , 32 , 35 , 42 } ; int n = arr . Length ; printAllAPTriplets ( arr , n ) ; } }


"Print all triplets with given sum | A simple C # program to find three elements whose sum is equal to given sum ; Prints all triplets in arr [ ] with given sum ; Driver code"
Solution in C#:

using System ; class GFG { static void findTriplets ( int [ ] arr , int n , int sum ) { for ( int i = 0 ; i < n - 2 ; i ++ ) { for ( int j = i + 1 ; j < n - 1 ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) { if ( arr [ i ] + arr [ j ] + arr [ k ] == sum ) { Console . WriteLine ( arr [ i ] + " ▁ " + arr [ j ] + " ▁ " + arr [ k ] ) ; } } } } } static public void Main ( ) { int [ ] arr = { 0 , - 1 , 2 , - 3 , 1 } ; int n = arr . Length ; findTriplets ( arr , n , - 2 ) ; } }


"Print all triplets with given sum | C # program to find triplets in a given array whose sum is equal to given sum . ; function to print triplets with given sum ; Find all pairs with sum equals to " sum - arr [ i ] " ; Driver code"
Solution in C#:

 using System ; using System . Collections . Generic ; public class GFG { static void findTriplets ( int [ ] arr , int n , int sum ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { HashSet < int > s = new HashSet < int > ( ) ; for ( int j = i + 1 ; j < n ; j ++ ) { int x = sum - ( arr [ i ] + arr [ j ] ) ; if ( s . Contains ( x ) ) Console . Write ( " { 0 } ▁ { 1 } ▁ { 2 }
 " , x , arr [ i ] , arr [ j ] ) ; else s . Add ( arr [ j ] ) ; } } } public static void Main ( String [ ] args ) { int [ ] arr = { 0 , - 1 , 2 , - 3 , 1 } ; int sum = - 2 ; int n = arr . Length ; findTriplets ( arr , n , sum ) ; } }


"Print all ways to reach the Nth stair with the jump of 1 or 2 units at a time | C # program for the above approach ; Function to find all the ways to reach Nth stair using one or two jumps ; Base Cases ; Recur for jump1 and jump2 ; Stores the total possible jumps ; Add "1" with every element present in jump1 ; Add "2" with every element present in jump2 ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static List < string > TotalPossibleJumps ( int N ) { if ( ( N - 1 ) == 0 ) { List < string > newvec = new List < string > ( ) ; newvec . Add ( " " ) ; return newvec ; } else { if ( N < 0 ) { List < string > newvec = new List < string > ( ) ; return newvec ; } } List < string > jump1 = TotalPossibleJumps ( N - 1 ) ; List < string > jump2 = TotalPossibleJumps ( N - 2 ) ; List < string > totaljumps = new List < string > ( ) ; foreach ( string s in jump1 ) { totaljumps . Add ( "1" + s ) ; } foreach ( string s in jump2 ) { totaljumps . Add ( "2" + s ) ; } return totaljumps ; } public static void Main ( ) { int N = 3 ; List < string > Ans = TotalPossibleJumps ( N ) ; foreach ( string it in Ans ) Console . WriteLine ( it ) ; } }


"Print all words matching a pattern in CamelCase Notation Dictionary | C # program to print all words in the CamelCase dictionary that matches with a given pattern ; Alphabet size ( # of upper - Case characters ) ; A Trie node ; isLeaf is true if the node represents end of a word ; vector to store list of complete words in leaf node ; Function to insert word into the Trie ; consider only uppercase characters ; get current character position ; mark last node as leaf ; push word into vector associated with leaf node ; Function to print all children of Trie node root ; if current node is leaf ; recurse for all children of root node ; search for pattern in Trie and print all words matching that pattern ; Invalid pattern ; print all words matching that pattern ; Main function to print all words in the CamelCase dictionary that matches with a given pattern ; construct Trie root node ; Construct Trie from given dict ; search for pattern in Trie ; Driver Code ; dictionary of words where each word follows CamelCase notation ; pattern consisting of uppercase characters only"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int ALPHABET_SIZE = 26 ; public class TrieNode { public TrieNode [ ] children = new TrieNode [ ALPHABET_SIZE ] ; public bool isLeaf ; public List < String > word ; public TrieNode ( ) { isLeaf = false ; for ( int i = 0 ; i < ALPHABET_SIZE ; i ++ ) children [ i ] = null ; word = new List < String > ( ) ; } } static TrieNode root ; static void insert ( String word ) { int index ; TrieNode pCrawl = root ; for ( int level = 0 ; level < word . Length ; level ++ ) { if ( char . IsLower ( word [ level ] ) ) continue ; index = word [ level ] - ' A ' ; if ( pCrawl . children [ index ] == null ) pCrawl . children [ index ] = new TrieNode ( ) ; pCrawl = pCrawl . children [ index ] ; } pCrawl . isLeaf = true ; ( pCrawl . word ) . Add ( word ) ; } static void printAllWords ( TrieNode root ) { if ( root . isLeaf ) { foreach ( String str in root . word ) Console . WriteLine ( str ) ; } for ( int i = 0 ; i < ALPHABET_SIZE ; i ++ ) { TrieNode child = root . children [ i ] ; if ( child != null ) printAllWords ( child ) ; } } static bool search ( String pattern ) { int index ; TrieNode pCrawl = root ; for ( int level = 0 ; level < pattern . Length ; level ++ ) { index = pattern [ level ] - ' A ' ; if ( pCrawl . children [ index ] == null ) return false ; pCrawl = pCrawl . children [ index ] ; } printAllWords ( pCrawl ) ; return true ; } static void findAllWords ( List < String > dict , String pattern ) { root = new TrieNode ( ) ; foreach ( String word in dict ) insert ( word ) ; if ( ! search ( pattern ) ) Console . WriteLine ( " No ▁ match ▁ found " ) ; } public static void Main ( String [ ] args ) { List < String > dict = new List < String > { " Hi " , " Hello " , " HelloWorld " , " HiTech " , " HiGeek " , " HiTechWorld " , " HiTechCity " , " HiTechLab " } ; String pattern = " HT " ; findAllWords ( dict , pattern ) ; } }


"Print alternate elements of an array | C # program to implement the above approach ; Function to print Alternate elements of the given array ; Print elements at odd positions ; If currIndex stores even index or odd position ; Driver Code"
Solution in C#:

using System ; class GFG { static void printAlter ( int [ ] arr , int N ) { for ( int currIndex = 0 ; currIndex < N ; currIndex ++ ) { if ( currIndex % 2 == 0 ) { Console . Write ( arr [ currIndex ] + " ▁ " ) ; } } } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int N = arr . Length ; printAlter ( arr , N ) ; } }


"Print alternate elements of an array | C # program to implement the above approach ; Function to print Alternate elements of the given array ; Print elements at odd positions ; Print elements of array ; Driver Code"
Solution in C#:

using System ; class GFG { static void printAlter ( int [ ] arr , int N ) { for ( int currIndex = 0 ; currIndex < N ; currIndex += 2 ) { Console . Write ( arr [ currIndex ] + " ▁ " ) ; } } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int N = arr . Length ; printAlter ( arr , N ) ; } }


"Print alternate nodes from all levels of a Binary Tree | C # program to implement the above approach ; Structure of a Node ; Print alternate nodes of a binary tree ; Store nodes of each level ; Store count of nodes of current level ; Print alternate nodes of the current level ; If left child exists ; Store left child ; If right child exists ; Store right child ; Driver Code ; Create a tree"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { class Node { public int data ; public Node left ; public Node right ; public Node ( int val ) { data = val ; left = right = null ; } } ; static void PrintAlternate ( Node root ) { Queue < Node > Q = new Queue < Node > ( ) ; Q . Enqueue ( root ) ; while ( Q . Count != 0 ) { int N = Q . Count ; for ( int i = 0 ; i < N ; i ++ ) { Node temp = Q . Peek ( ) ; Q . Dequeue ( ) ; if ( i % 2 == 0 ) { Console . Write ( temp . data + " ▁ " ) ; } if ( temp . left != null ) { Q . Enqueue ( temp . left ) ; } if ( temp . right != null ) { Q . Enqueue ( temp . right ) ; } } Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { Node root ; root = new Node ( 71 ) ; root . left = new Node ( 88 ) ; root . right = new Node ( 99 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right . left = new Node ( 6 ) ; root . right . right = new Node ( 7 ) ; root . left . left . left = new Node ( 8 ) ; root . left . left . right = new Node ( 9 ) ; root . left . right . left = new Node ( 10 ) ; root . left . right . right = new Node ( 11 ) ; root . right . left . right = new Node ( 13 ) ; root . right . right . left = new Node ( 14 ) ; PrintAlternate ( root ) ; } }


"Print an N x M matrix such that each row and column has all the vowels in it | C # implementation of the approach ; Function to print the required matrix ; Impossible to generate the required matrix ; Store all the vowels ; Print the matrix ; Print vowels for every index ; Shift the vowels by one ; Driver code"
Solution in C#:

using System ; class GFG { static void printMatrix ( int n , int m ) { if ( n < 5 m < 5 ) { Console . Write ( - 1 ) ; return ; } char [ ] s = " aeiou " . ToCharArray ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { Console . Write ( s [ j % 5 ] + " ▁ " ) ; } Console . WriteLine ( ) ; char c = s [ 0 ] ; for ( int k = 0 ; k < 4 ; k ++ ) { s [ k ] = s [ k + 1 ] ; } s [ 4 ] = c ; } } public static void Main ( String [ ] args ) { int n = 5 , m = 5 ; printMatrix ( n , m ) ; } }


"Print any pair of integers with sum of GCD and LCM equals to N | C # implementation to print any pair of integers whose summation of GCD and LCM is equal to integer N ; Function to print the required pair ; Print the pair ; Driver code"
Solution in C#:

using System ; public class GFG { static void printPair ( int n ) { Console . Write ( 1 + " ▁ " + ( n - 1 ) ) ; } public static void Main ( String [ ] args ) { int n = 14 ; printPair ( n ) ; } }


"Print array elements in alternatively increasing and decreasing order | C # program to print array elements in alternative increasing and decreasing order ; Function to print array elements in alternative increasing and decreasing order ; First sort the array in increasing order ; start with 2 elements in increasing order ; till all the elements are not printed ; printing the elements in increasing order ; printing the elements in decreasing order ; increasing the number of elements to printed in next iteration ; Driver Code"
Solution in C#:

using System ; class GFG { static void printArray ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int l = 0 , r = n - 1 , flag = 0 , i ; int k = 2 ; while ( l <= r ) { if ( flag == 0 ) { for ( i = l ; i < l + k && i <= r ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; flag = 1 ; l = i ; } else { for ( i = r ; i > r - k && i >= l ; i -- ) Console . Write ( arr [ i ] + " ▁ " ) ; flag = 0 ; r = i ; } k ++ ; } } static public void Main ( ) { int n = 6 ; int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 } ; printArray ( arr , n ) ; } }


"Print array of strings in sorted order without copying one string into another | C # implementation to print an array of strings in sorted order without copying one string into another ; function to print strings in sorted order ; Initially the index of the strings are assigned to the ' index [ ] ' ; selection sort technique is applied ; with the help of ' index [ ] ' strings are being compared ; index of the smallest string is placed at the ith index of ' index [ ] ' ; printing strings in sorted order ; Driver program to test above"
Solution in C#:

using System ; public class GFG { static void printInSortedOrder ( String [ ] arr , int n ) { int [ ] index = new int [ n ] ; int i , j , min ; for ( i = 0 ; i < n ; i ++ ) { index [ i ] = i ; } for ( i = 0 ; i < n - 1 ; i ++ ) { min = i ; for ( j = i + 1 ; j < n ; j ++ ) { if ( arr [ index [ min ] ] . CompareTo ( arr [ index [ j ] ] ) > 0 ) { min = j ; } } if ( min != i ) { int temp = index [ min ] ; index [ min ] = index [ i ] ; index [ i ] = temp ; } } for ( i = 0 ; i < n ; i ++ ) { Console . Write ( arr [ index [ i ] ] + " ▁ " ) ; } } static public void Main ( ) { String [ ] arr = { " geeks " , " quiz " , " geeks " , " for " } ; int n = 4 ; printInSortedOrder ( arr , n ) ; } }


"Print characters and their frequencies in order of occurrence using Binary Tree | C # implementation of the above approach ; Node in the tree where data holds the character of the String and cnt holds the frequency ; Function to add a new node to the Binary Tree ; Create a new node and populate its data part , set cnt as 1 and left and right children as null ; Function to add a node to the Binary Tree in level order ; Use the queue data structure for level order insertion and push the root of tree to Queue ; If the character to be inserted is present , update the cnt ; If the left child is empty add a new node as the left child ; If the character is present as a left child , update the cnt and exit the loop ; Add the left child to the queue for further processing ; If the right child is empty , add a new node to the right ; If the character is present as a right child , update the cnt and exit the loop ; Add the right child to the queue for further processing ; Function to print the level order traversal of the Binary Tree ; Add the root to the queue ; If the cnt of the character is more then one , display cnt ; If the cnt of character is one , display character only ; Add the left child to the queue for further processing ; Add the right child to the queue for further processing ; Driver code ; Add individual characters to the String one by one in level order ; Print the level order of the constructed binary tree"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public class node { public char data ; public int cnt ; public node left , right ; } ; static node add ( char data ) { node newnode = new node ( ) ; newnode . data = data ; newnode . cnt = 1 ; newnode . left = newnode . right = null ; return newnode ; } static node addinlvlorder ( node root , char data ) { if ( root == null ) { return add ( data ) ; } List < node > Q = new List < node > ( ) ; Q . Add ( root ) ; while ( Q . Count != 0 ) { node temp = Q [ 0 ] ; Q . RemoveAt ( 0 ) ; if ( temp . data == data ) { temp . cnt ++ ; break ; } if ( temp . left == null ) { temp . left = add ( data ) ; break ; } else { if ( temp . left . data == data ) { temp . left . cnt ++ ; break ; } Q . Add ( temp . left ) ; } if ( temp . right == null ) { temp . right = add ( data ) ; break ; } else { if ( temp . right . data == data ) { temp . right . cnt ++ ; break ; } Q . Add ( temp . right ) ; } } return root ; } static void printlvlorder ( node root ) { List < node > Q = new List < node > ( ) ; Q . Add ( root ) ; while ( Q . Count != 0 ) { node temp = Q [ 0 ] ; if ( temp . cnt > 1 ) { Console . Write ( ( temp . data + " " + temp . cnt ) ) ; } else { Console . Write ( ( char ) temp . data ) ; } Q . RemoveAt ( 0 ) ; if ( temp . left != null ) { Q . Add ( temp . left ) ; } if ( temp . right != null ) { Q . Add ( temp . right ) ; } } } public static void Main ( String [ ] args ) { String s = " geeksforgeeks " ; node root = null ; for ( int i = 0 ; i < s . Length ; i ++ ) { root = addinlvlorder ( root , s [ i ] ) ; } printlvlorder ( root ) ; } }


"Print characters in decreasing order of frequency | C # implementation of the approach ; Function to print the characters of the given String in decreasing order of their frequencies ; To store the ; Map 's size ; While there are elements in the map ; Finding the maximum value from the map ; Print the character alongwith its frequency ; Delete the maximum value ; Driver code"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static void printChar ( char [ ] arr , int len ) { Dictionary < char , int > occ = new Dictionary < char , int > ( ) ; for ( int i = 0 ; i < len ; i ++ ) if ( occ . ContainsKey ( arr [ i ] ) ) { occ [ arr [ i ] ] = occ [ arr [ i ] ] + 1 ; } else { occ . Add ( arr [ i ] , 1 ) ; } int size = occ . Count ; while ( size -- > 0 ) { int currentMax = 0 ; char arg_max = ( char ) 0 ; foreach ( KeyValuePair < char , int > it in occ ) { if ( it . Value > currentMax || ( it . Value == currentMax && it . Key > arg_max ) ) { arg_max = it . Key ; currentMax = it . Value ; } } Console . Write ( arg_max + " ▁ - ▁ " + currentMax + "
 " ) ; occ . Remove ( arg_max ) ; } } public static void Main ( String [ ] args ) { String str = " geeksforgeeks " ; int len = str . Length ; printChar ( str . ToCharArray ( ) , len ) ; } }


"Print combinations of distinct numbers which add up to give sum N | C # implementation of the approach ; arr [ ] to store all the distinct elements index - next location in array num - given number reducedNum - reduced number ; Set to store all the distinct elements ; Base condition ; Iterate over all the elements and store it into the set ; Calculate the sum of all the elements of the set ; Compare whether the sum is equal to n or not , if it is equal to n print the numbers ; Find previous number stored in the array ; Store all the numbers recursively into the arr [ ] ; Function to find all the distinct combinations of n ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void findCombinationsUtil ( int [ ] arr , int index , int n , int red_num ) { HashSet < int > s = new HashSet < int > ( ) ; int sum = 0 ; if ( red_num < 0 ) { return ; } if ( red_num == 0 ) { for ( int i = 0 ; i < index ; i ++ ) { s . Add ( arr [ i ] ) ; } foreach ( int itr in s ) { sum = sum + itr ; } if ( sum == n ) { foreach ( int i in s ) { Console . Write ( i + " ▁ " ) ; } Console . WriteLine ( ) ; return ; } } int prev = ( index == 0 ) ? 1 : arr [ index - 1 ] ; for ( int k = prev ; k <= n ; k ++ ) { if ( index < n ) { arr [ index ] = k ; findCombinationsUtil ( arr , index + 1 , n , red_num - k ) ; } } } static void findCombinations ( int n ) { int [ ] a = new int [ n ] ; findCombinationsUtil ( a , 0 , n , n ) ; } public static void Main ( String [ ] arr ) { int n = 7 ; findCombinations ( n ) ; } }


"Print common nodes on path from root ( or common ancestors ) | C # Program to find common nodes for given two nodes ; Class to represent Tree node ; Class to count full nodes of Tree ; Utility function to find the LCA of two given values n1 and n2 . ; Base case ; If either n1 or n2 matches with root 's key,   report the presence by returning root (Note   that if a key is ancestor of other, then the   ancestor key becomes LCA  ; Look for keys in left and right subtrees ; If both of the above calls return Non - NULL , then one key is present in once subtree and other is present in other , So this node is the LCA ; Otherwise check if left subtree or right subtree is LCA ; Utility Function to print all ancestors of LCA ; base cases ; If target is present in either left or right subtree of this node , then print this node ; Else return false ; Function to find nodes common to given two nodes ; Driver program to test above functions ; Let us create Binary Tree shown in above example"
Solution in C#:

using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = null ; right = null ; } } public class BinaryTree { public static Node root ; public static Node findLCA ( Node root , int n1 , int n2 ) { if ( root == null ) { return null ; } if ( root . data == n1 root . data == n2 ) { return root ; } Node left_lca = findLCA ( root . left , n1 , n2 ) ; Node right_lca = findLCA ( root . right , n1 , n2 ) ; if ( left_lca != null && right_lca != null ) { return root ; } return ( left_lca != null ) ? left_lca : right_lca ; } public static bool printAncestors ( Node root , int target ) { if ( root == null ) { return false ; } if ( root . data == target ) { Console . Write ( root . data + " ▁ " ) ; return true ; } if ( printAncestors ( root . left , target ) || printAncestors ( root . right , target ) ) { Console . Write ( root . data + " ▁ " ) ; return true ; } return false ; } public static bool findCommonNodes ( Node root , int first , int second ) { Node LCA = findLCA ( root , first , second ) ; if ( LCA == null ) { return false ; } printAncestors ( root , LCA . data ) ; return true ; } public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; 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 ) ; BinaryTree . root . right . left = new Node ( 6 ) ; BinaryTree . root . right . right = new Node ( 7 ) ; BinaryTree . root . left . left . left = new Node ( 8 ) ; BinaryTree . root . right . left . left = new Node ( 9 ) ; BinaryTree . root . right . left . right = new Node ( 10 ) ; if ( findCommonNodes ( root , 9 , 7 ) == false ) { Console . WriteLine ( " No ▁ Common ▁ nodes " ) ; } } }


"Print consecutive characters together in a line | C # program to print consecutive characters together in a line . ; Driver code"
Solution in C#:

 using System ; class GFG { static void print ( String str1 ) { char [ ] str = str1 . ToCharArray ( ) ; Console . Write ( str [ 0 ] ) ; for ( int i = 1 ; i < str . Length ; i ++ ) { if ( ( str [ i ] == str [ i - 1 ] + 1 ) || ( str [ i ] == str [ i - 1 ] - 1 ) ) { Console . Write ( str [ i ] ) ; } else { Console . Write ( "
 " + str [ i ] ) ; } } } public static void Main ( ) { String str = " ABCXYZACCD " ; print ( str ) ; } }