"Print cousins of a given node in Binary Tree | C # program to print cousins of a node ; A Binary Tree Node ; A utility function to create a new Binary Tree Node ; It returns level of the node if it is present in tree , otherwise returns 0. ; base cases ; If node is present in left subtree ; If node is not present in left subtree ; Print nodes at a given level such that sibling of node is not printed if it exists ; Base cases ; If current node is parent of a node with given level ; Recur for left and right subtrees ; This function prints cousins of a given node ; Get level of given node ; Print nodes of given level . ; Driver code"
Solution in C#:

using System ; public class GfG { class Node { public int data ; public Node left , right ; } static Node newNode ( int item ) { Node temp = new Node ( ) ; temp . data = item ; temp . left = null ; temp . right = null ; return temp ; } static int getLevel ( Node root , Node node , int level ) { if ( root == null ) return 0 ; if ( root == node ) return level ; int downlevel = getLevel ( root . left , node , level + 1 ) ; if ( downlevel != 0 ) return downlevel ; return getLevel ( root . right , node , level + 1 ) ; } static void printGivenLevel ( Node root , Node node , int level ) { if ( root == null level < 2 ) return ; if ( level == 2 ) { if ( root . left == node root . right == node ) return ; if ( root . left != null ) Console . Write ( root . left . data + " ▁ " ) ; if ( root . right != null ) Console . Write ( root . right . data + " ▁ " ) ; } else if ( level > 2 ) { printGivenLevel ( root . left , node , level - 1 ) ; printGivenLevel ( root . right , node , level - 1 ) ; } } static void printCousins ( Node root , Node node ) { int level = getLevel ( root , node , 1 ) ; printGivenLevel ( root , node , level ) ; } public static void Main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; root . left . right . right = newNode ( 15 ) ; root . right . left = newNode ( 6 ) ; root . right . right = newNode ( 7 ) ; root . right . left . right = newNode ( 8 ) ; printCousins ( root , root . left . right ) ; } }


"Print digit 's position to be removed to make a number divisible by 6 | C # program to print digit 's position to be removed to make number divisible by 6 ; function to print the number divisible by 6 after exactly removing a digit ; stores the sum of all elements ; traverses the string and converts string to number array and sums up ; if ( a [ n - 1 ] % 2 != 0 ) ODD CHECK ; if second last is odd or sum of n - 1 elements are not divisible by 3. ; second last is even and print n - 1 elements removing last digit ; last digit removed ; counter to check if any element after removing , its sum % 3 == 0 ; traverse till second last element ; to check if any element after removing , its sum % 3 == 0 ; the leftmost element ; break at the leftmost element ; stores the right most element ; if no element has been found as a [ i + 1 ] > a [ i ] ; if second last is even , then remove last if ( sum - last ) % 3 == 0 ; if no element which on removing gives sum % 3 == 0 ; Driver Code"
Solution in C#:

using System ; class GFG { static void greatest ( string s ) { int n = s . Length ; int [ ] a = new int [ n ] ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = s [ i ] - '0' ; sum += a [ i ] ; } { if ( a [ n - 2 ] % 2 != 0 || ( sum - a [ n - 1 ] ) % 3 != 0 ) { Console . Write ( " - 1" ) ; } else { Console . Write ( n ) ; } } else { int re = sum % 3 ; int del = - 1 ; int flag = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( ( a [ i ] ) % 3 == re ) { if ( a [ i + 1 ] > a [ i ] ) { del = i ; flag = 1 ; break ; } else { del = i ; } } } if ( flag == 0 ) { if ( a [ n - 2 ] % 2 == 0 && re == a [ n - 1 ] % 3 ) del = n - 1 ; } if ( del == - 1 ) Console . Write ( - 1 ) ; else { Console . Write ( del + 1 ) ; } } } public static void Main ( ) { string s = "7510222" ; greatest ( s ) ; } }


"Print digits for each array element that does not divide any digit of that element | C # program for the above approach ; Function to find digits for each array element that doesn 't divide any digit  of the that element ; Traverse the array arr [ ] ; Iterate over the range [ 2 , 9 ] ; Stores if there exists any digit in arr [ i ] which is divisible by j ; If any digit of the number is divisible by j ; If the digit j doesn 't  divide any digit of arr[i] ; Driver Code"
Solution in C#:

using System ; class GFG { static void indivisibleDigits ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( arr [ i ] + " : ▁ " ) ; for ( int j = 2 ; j < 10 ; j ++ ) { int temp = arr [ i ] ; bool flag = true ; while ( temp > 0 ) { if ( ( temp % 10 ) != 0 && ( temp % 10 ) % j == 0 ) { flag = false ; break ; } temp /= 10 ; } if ( flag ) { Console . Write ( j + " ▁ " ) ; } } Console . WriteLine ( ) ; } } public static void Main ( ) { int [ ] arr = { 4162 , 1152 , 99842 } ; int N = arr . Length ; indivisibleDigits ( arr , N ) ; } }


"Print distinct sorted permutations with duplicates allowed in input | C # program to print all permutations of a string in sorted order . ; Calculating factorial of a number ; Method to print the array ; Method to find total number of permutations ; Building Dictionary to store frequencies of all characters . ; Traversing hashmap and finding duplicate elements . ; Start traversing from the end and find position ' i - 1' of the first character which is greater than its successor . ; Finding smallest character after ' i - 1' and greater than temp [ i - 1 ] ; Swapping the above found characters . ; Sort all digits from position next to ' i - 1' to end of the string . ; Print the String ; Sorting String ; Print first permutation ; Finding the total permutations ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int factorial ( int n ) { int f = 1 ; for ( int i = 1 ; i <= n ; i ++ ) f = f * i ; return f ; } static void print ( char [ ] temp ) { for ( int i = 0 ; i < temp . Length ; i ++ ) Console . Write ( temp [ i ] ) ; Console . WriteLine ( ) ; } static int calculateTotal ( char [ ] temp , int n ) { int f = factorial ( n ) ; Dictionary < char , int > hm = new Dictionary < char , int > ( ) ; for ( int i = 0 ; i < temp . Length ; i ++ ) { if ( hm . ContainsKey ( temp [ i ] ) ) hm [ temp [ i ] ] = hm [ temp [ i ] ] + 1 ; else hm . Add ( temp [ i ] , 1 ) ; } foreach ( KeyValuePair < char , int > e in hm ) { int x = e . Value ; if ( x > 1 ) { int temp5 = factorial ( x ) ; f = f / temp5 ; } } return f ; } static void nextPermutation ( char [ ] temp ) { int i ; for ( i = temp . Length - 1 ; i > 0 ; i -- ) if ( temp [ i ] > temp [ i - 1 ] ) break ; int min = i ; int j , x = temp [ i - 1 ] ; for ( j = i + 1 ; j < temp . Length ; j ++ ) if ( ( temp [ j ] < temp [ min ] ) && ( temp [ j ] > x ) ) min = j ; char temp_to_swap ; temp_to_swap = temp [ i - 1 ] ; temp [ i - 1 ] = temp [ min ] ; temp [ min ] = temp_to_swap ; Array . Sort ( temp , i , temp . Length - i ) ; print ( temp ) ; } static void printAllPermutations ( String s ) { char [ ] temp = s . ToCharArray ( ) ; Array . Sort ( temp ) ; print ( temp ) ; int total = calculateTotal ( temp , temp . Length ) ; for ( int i = 1 ; i < total ; i ++ ) nextPermutation ( temp ) ; } public static void Main ( String [ ] args ) { String s = " AAB " ; printAllPermutations ( s ) ; } }


"Print equal sum sets of array ( Partition Problem ) | Set 2 | C # program to print equal sum sets of array . ; Function to print equal sum sets of array . ; Finding sum of array elements ; Check sum is even or odd . If odd then array cannot be partitioned . Print - 1 and return . ; Divide sum by 2 to find sum of two possible subsets . ; Boolean DP table to store result of states . dp [ i , j ] = true if there is a subset of elements in first i elements of array that has sum equal to j . ; If number of elements are zero , then no sum can be obtained . ; Sum 0 can be obtained by not selecting any element . ; Fill the DP table in bottom up manner . ; Excluding current element . ; Including current element ; Required sets set1 and set2 . ; If partition is not possible print - 1 and return . ; Start from last element in dp table . ; If current element does not contribute to k , then it belongs to set 2. ; If current element contribute to k then it belongs to set 1. ; Print elements of both the sets . ; Driver Code ."
Solution in C#:

 using System ; using System . Linq ; using System . Collections . Generic ; class GFG { static void printEqualSumSets ( int [ ] arr , int n ) { int i , currSum , sum = 0 ; for ( i = 0 ; i < arr . Length ; i ++ ) sum += arr [ i ] ; if ( ( sum & 1 ) == 1 ) { Console . Write ( " - 1" ) ; return ; } int k = sum >> 1 ; bool [ , ] dp = new bool [ n + 1 , k + 1 ] ; for ( i = 1 ; i <= k ; i ++ ) dp [ 0 , i ] = false ; for ( i = 0 ; i <= n ; i ++ ) dp [ i , 0 ] = true ; for ( i = 1 ; i <= n ; i ++ ) { for ( currSum = 1 ; currSum <= k ; currSum ++ ) { dp [ i , currSum ] = dp [ i - 1 , currSum ] ; if ( arr [ i - 1 ] <= currSum ) dp [ i , currSum ] = dp [ i , currSum ] | dp [ i - 1 , currSum - arr [ i - 1 ] ] ; } } List < int > set1 = new List < int > ( ) ; List < int > set2 = new List < int > ( ) ; if ( ! dp [ n , k ] ) { Console . Write ( " - 1
 " ) ; return ; } i = n ; currSum = k ; while ( i > 0 && currSum >= 0 ) { if ( dp [ i - 1 , currSum ] ) { i -- ; set2 . Add ( arr [ i ] ) ; } else if ( dp [ i - 1 , currSum - arr [ i - 1 ] ] ) { i -- ; currSum -= arr [ i ] ; set1 . Add ( arr [ i ] ) ; } } Console . Write ( " Set ▁ 1 ▁ elements : ▁ " ) ; for ( i = 0 ; i < set1 . Count ; i ++ ) Console . Write ( set1 [ i ] + " ▁ " ) ; Console . Write ( "
 Set ▁ 2 ▁ elements : ▁ " ) ; for ( i = 0 ; i < set2 . Count ; i ++ ) Console . Write ( set2 [ i ] + " ▁ " ) ; } static void Main ( ) { int [ ] arr = { 5 , 5 , 1 , 11 } ; int n = arr . Length ; printEqualSumSets ( arr , n ) ; } }


"Print equal sum sets of array ( Partition problem ) | Set 1 | C # program to print equal sum two subsets of an array if it can be partitioned into subsets . ; / Function to print the equal sum sets of the array . ; / Print set 1. ; / Print set 2. ; Utility function to find the sets of the array which have equal sum . ; If entire array is traversed , compare both the sums . ; If sums are equal print both sets and return true to show sets are found . ; If sums are not equal then return sets are not found . ; Add current element to set 1. ; Recursive call after adding current element to set 1. ; If this inclusion results in equal sum sets partition then return true to show desired sets are found . ; If not then backtrack by removing current element from set1 and include it in set 2. ; Recursive call after including current element to set 2. ; Return true if array arr can be partitioned into two equal sum sets or not . ; Calculate sum of elements in array . ; If sum is odd then array cannot be partitioned . ; Declare Lists to store both the sets . ; / Find both the sets . ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; using System . Linq ; using System . Collections ; class GFG { static void printSets ( List < int > set1 , List < int > set2 ) { int i ; for ( i = 0 ; i < set1 . Count ; i ++ ) { Console . Write ( set1 [ i ] + " ▁ " ) ; } Console . WriteLine ( ) ; for ( i = 0 ; i < set2 . Count ; i ++ ) { Console . Write ( set2 [ i ] + " ▁ " ) ; } } static bool findSets ( int [ ] arr , int n , ref List < int > set1 , ref List < int > set2 , int sum1 , int sum2 , int pos ) { if ( pos == n ) { if ( sum1 == sum2 ) { printSets ( set1 , set2 ) ; return true ; } else return false ; } set1 . Add ( arr [ pos ] ) ; bool res = findSets ( arr , n , ref set1 , ref set2 , sum1 + arr [ pos ] , sum2 , pos + 1 ) ; if ( res == true ) return res ; set1 . RemoveAt ( set1 . Count - 1 ) ; set2 . Add ( arr [ pos ] ) ; res = findSets ( arr , n , ref set1 , ref set2 , sum1 , sum2 + arr [ pos ] , pos + 1 ) ; if ( res == false ) if ( set2 . Count > 0 ) set2 . RemoveAt ( set2 . Count - 1 ) ; return res ; } static bool isPartitionPoss ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; if ( sum % 2 != 0 ) return false ; List < int > set1 = new List < int > ( ) ; List < int > set2 = new List < int > ( ) ; return findSets ( arr , n , ref set1 , ref set2 , 0 , 0 , 0 ) ; } public static void Main ( ) { int [ ] arr = { 5 , 5 , 1 , 11 } ; int n = arr . Length ; if ( isPartitionPoss ( arr , n ) == false ) { Console . Write ( " - 1" ) ; } } }


"Print first K distinct Moran numbers from a given array |  ; Function to calculate the sum of digits of a number ; Stores the sum of digits ; Add the digit to sum ; Remove digit ; Returns the sum of digits ; Function to check if a number is prime or not ; If r has any divisor ; Set r as non - prime ; Function to check if a number is moran number ; Calculate sum of digits ; Check if n is divisible by the sum of digits ; Calculate the quotient ; If the quotient is prime ; Function to print the first K Moran numbers from the array ; Sort the given array ; Initialise a set ; Traverse the array from the end ; If the current array element is a Moran number ; Insert into the set ; Driver Code"
Solution in C#:

 using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int digiSum ( int a ) { int sum = 0 ; while ( a != 0 ) { sum += a % 10 ; a = a / 10 ; } return sum ; } static bool isPrime ( int r ) { bool s = true ; for ( int i = 2 ; i * i <= r ; i ++ ) { if ( r % i == 0 ) { s = false ; break ; } } return s ; } static bool isMorannumber ( int n ) { int dup = n ; int sum = digiSum ( dup ) ; if ( n % sum == 0 ) { int c = n / sum ; if ( isPrime ( c ) ) { return true ; } } return false ; } static void FirstKMorannumber ( int [ ] a , int n , int k ) { int X = k ; Array . Sort ( a ) ; SortedSet < int > s = new SortedSet < int > ( ) ; for ( int i = n - 1 ; i >= 0 && k > 0 ; i -- ) { if ( isMorannumber ( a [ i ] ) ) { s . Add ( a [ i ] ) ; k -- ; } } if ( k > 0 ) { Console . WriteLine ( X + " ▁ Moran ▁ numbers ▁ are " + " ▁ not ▁ present ▁ in ▁ the ▁ array " ) ; return ; } foreach ( var val in s ) { Console . Write ( val + " , ▁ " ) ; } Console . Write ( "
 " ) ; } public static void Main ( ) { int [ ] A = { 34 , 198 , 21 , 42 , 63 , 45 , 22 , 44 , 43 } ; int K = 4 ; int N = A . Length ; FirstKMorannumber ( A , N , K ) ; } }


"Print first N Mosaic numbers | C # implementation of the approach ; Function to return the nth mosaic number ; Iterate from 2 to the number ; If i is the factor of n ; Find the count where i ^ count is a factor of n ; Divide the number by i ; Increase the count ; Multiply the answer with count and i ; Return the answer ; Function to print first N Mosaic numbers ; Driver code"
Solution in C#:

using System ; class GFG { static int mosaic ( int n ) { int i , ans = 1 ; for ( i = 2 ; i <= n ; i ++ ) { if ( n % i == 0 && n > 0 ) { int count = 0 ; while ( n % i == 0 ) { n /= i ; count ++ ; } ans *= count * i ; } } return ans ; } static void nMosaicNumbers ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) Console . Write ( mosaic ( i ) + " ▁ " ) ; } public static void Main ( ) { int n = 10 ; nMosaicNumbers ( n ) ; } }


"Print first N terms of Lower Wythoff sequence | C # implementation of the approach ; Function to print the first n terms of the lower Wythoff sequence ; Calculate value of phi ; Find the numbers ; a ( n ) = floor ( n * phi ) ; Print the nth numbers ; Driver code"
Solution in C#:

using System ; class GFG { static void lowerWythoff ( int n ) { double phi = ( 1 + Math . Sqrt ( 5 ) ) / 2.0 ; for ( int i = 1 ; i <= n ; i ++ ) { double ans = Math . Floor ( i * phi ) ; Console . Write ( ( int ) ans ) ; if ( i != n ) Console . Write ( " ▁ , ▁ " ) ; } } static public void Main ( ) { int n = 5 ; lowerWythoff ( n ) ; } }


"Print first k digits of 1 / n where n is a positive integer | C # code to Print first k digits of 1 / n where n is a positive integer ; Function to print first k digits after dot in value of 1 / n . n is assumed to be a positive integer . ; Initialize remainder ; Run a loop k times to print k digits ; The next digit can always be obtained as doing ( 10 * rem ) / 10 ; Update remainder ; Driver program"
Solution in C#:

using System ; class GFG { static void print ( int n , int k ) { int rem = 1 ; for ( int i = 0 ; i < k ; i ++ ) { Console . Write ( ( 10 * rem ) / n ) ; rem = ( 10 * rem ) % n ; } } public static void Main ( ) { int n = 7 , k = 3 ; print ( n , k ) ; Console . WriteLine ( ) ; n = 21 ; k = 4 ; print ( n , k ) ; } }


"Print first n Fibonacci Numbers using direct formula | C # code to print fibonacci numbers till n using direct formula . \ ; Function to calculate fibonacci using recurrence relation formula ; Using direct formula ; Driver code"
Solution in C#:

using System ; public class GFG { static void fibonacci ( double n ) { double fib ; for ( double i = 0 ; i < n ; i ++ ) { fib = ( Math . Pow ( ( 1 + Math . Sqrt ( 5 ) ) , i ) - Math . Pow ( ( 1 - Math . Sqrt ( 5 ) ) , i ) ) / ( Math . Pow ( 2 , i ) * Math . Sqrt ( 5 ) ) ; Console . Write ( ( int ) fib + " ▁ " ) ; } } static public void Main ( ) { double n = 8 ; fibonacci ( n ) ; } }


"Print first n numbers with exactly two set bits | C # program to print first n numbers with exactly two set bits ; Function to print first n numbers with two set bits ; Initialize higher of two sets bits ; Keep reducing n for every number with two set bits ; Consider all lower set bits for current higher set bit ; Print current number ; If we have found n numbers ; Consider next lower bit for current higher bit . ; Increment higher set bit ; Driver program"
Solution in C#:

using System ; class GFG { static void printTwoSetBitNums ( int n ) { int x = 1 ; while ( n > 0 ) { int y = 0 ; while ( y < x ) { Console . Write ( ( ( 1 << x ) + ( 1 << y ) ) + " ▁ " ) ; n -- ; if ( n == 0 ) return ; y ++ ; } x ++ ; } } public static void Main ( ) { int n = 4 ; printTwoSetBitNums ( n ) ; } }


"Print given sentence into its equivalent ASCII form | C # implementation for converting a string into it 's ASCII equivalent sentence ; Function to compute the ASCII value of each character one by one ; Driver function"
Solution in C#:

using System ; class GeeksforGeeks { static void ASCIISentence ( string str ) { int l = str . Length ; int convert ; for ( int i = 0 ; i < l ; i ++ ) { convert = str [ i ] ; Console . Write ( convert ) ; } } public static void Main ( ) { string str = " GeeksforGeeks " ; Console . WriteLine ( " ASCII ▁ Sentence : " ) ; ASCIISentence ( str ) ; } }


"Print indices in non | C # program to implement the above approach ; Function to print the order of array elements generating non - decreasing quotient after division by X ; Stores the quotient and the order ; Traverse the array ; Sort the vector ; Print the order ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void printOrder ( int [ ] order , int N , int X ) { List < Tuple < int , int > > vect = new List < Tuple < int , int > > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( order [ i ] % X == 0 ) { vect . Add ( new Tuple < int , int > ( ( order [ i ] / X ) , i + 1 ) ) ; } else { vect . Add ( new Tuple < int , int > ( ( order [ i ] / X + 1 ) , i + 1 ) ) ; } } vect . Sort ( ) ; for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( vect [ i ] . Item2 + " ▁ " ) ; } Console . WriteLine ( ) ; } public static void Main ( ) { int N = 3 , X = 3 ; int [ ] order = { 2 , 7 , 4 } ; printOrder ( order , N , X ) ; } }


"Print indices of pair of array elements required to be removed to split array into 3 equal sum subarrays | C # program for the above approach ; Function to check if array can be split into three equal sum subarrays by removing a pair ; Stores prefix sum array ; Copy array elements ; Traverse the array ; Stores sums of all three subarrays ; Sum of left subarray ; Sum of middle subarray ; Sum of right subarray ; Check if sum of subarrays are equal ; Print the possible pair ; If no such pair exists , print - 1 ; Driver Code ; Given array ; Size of the array"
Solution in C#:

 using System ; public class GFG { static void findSplit ( int [ ] arr , int N ) { int [ ] sum = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { sum [ i ] = arr [ i ] ; } for ( int i = 1 ; i < N ; i ++ ) { sum [ i ] += sum [ i - 1 ] ; } for ( int l = 1 ; l <= N - 4 ; l ++ ) { for ( int r = l + 2 ; r <= N - 2 ; r ++ ) { int lsum = 0 , rsum = 0 , msum = 0 ; lsum = sum [ l - 1 ] ; msum = sum [ r - 1 ] - sum [ l ] ; rsum = sum [ N - 1 ] - sum [ r ] ; if ( lsum == rsum && rsum == msum ) { Console . Write ( l + " ▁ " + r + "
 " ) ; return ; } } } Console . Write ( - 1 + "
 " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 5 , 12 , 7 , 19 , 4 , 3 } ; int N = arr . Length ; findSplit ( arr , N ) ; } }


"Print indices of pair of array elements required to be removed to split array into 3 equal sum subarrays | C # program for the above approach ; Function to check if array can be split into three equal sum subarrays by removing a pair ; Two pointers l and r ; Stores prefix sum array ; Traverse the array ; Two pointer approach ; Sum of left subarray ; Sum of middle subarray ; Sum of right subarray ; Print split indices if sum is equal ; Move left pointer if lsum < rsum ; Move right pointer if rsum > lsum ; Move both pointers if lsum = rsum but they are not equal to msum ; If no possible pair exists , print - 1 ; Driver code ; Given array ; Size of the array"
Solution in C#:

using System ; class GFG { static void findSplit ( int [ ] arr , int N ) { int l = 1 , r = N - 2 ; int lsum , msum , rsum ; int [ ] sum = new int [ N ] ; sum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { sum [ i ] = sum [ i - 1 ] + arr [ i ] ; } while ( l < r ) { lsum = sum [ l - 1 ] ; msum = sum [ r - 1 ] - sum [ l ] ; rsum = sum [ N - 1 ] - sum [ r ] ; if ( lsum == msum && msum == rsum ) { Console . Write ( l + " ▁ " + r ) ; return ; } if ( lsum < rsum ) l ++ ; else if ( lsum > rsum ) r -- ; else { l ++ ; r -- ; } } Console . Write ( - 1 ) ; } static void Main ( ) { int [ ] arr = { 2 , 5 , 12 , 7 , 19 , 4 , 3 } ; int N = arr . Length ; findSplit ( arr , N ) ; } }


"Print indices of pair of array elements required to be removed to split array into 3 equal sum subarrays | C # program for the above approach ; Function to check if array can be split into three equal sum subarrays by removing two elements ; Stores sum of all three subarrays ; Sum of left subarray ; Sum of middle subarray ; Sum of right subarray ; Check if sum of subarrays are equal ; Print the possible pair ; If no pair exists , print - 1 ; Driver Code ; Given array ; Size of the array"
Solution in C#:

using System ; class GFG { static void findSplit ( int [ ] arr , int N ) { for ( int l = 1 ; l <= N - 4 ; l ++ ) { for ( int r = l + 2 ; r <= N - 2 ; r ++ ) { int lsum = 0 , rsum = 0 , msum = 0 ; for ( int i = 0 ; i <= l - 1 ; i ++ ) { lsum += arr [ i ] ; } for ( int i = l + 1 ; i <= r - 1 ; i ++ ) { msum += arr [ i ] ; } for ( int i = r + 1 ; i < N ; i ++ ) { rsum += arr [ i ] ; } if ( lsum == rsum && rsum == msum ) { Console . WriteLine ( l + " ▁ " + r ) ; return ; } } } Console . Write ( - 1 ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 2 , 5 , 12 , 7 , 19 , 4 , 3 } ; int N = arr . Length ; findSplit ( arr , N ) ; } }


"Print k different sorted permutations of a given array | C # implementation of the approach ; Utility function to print the original indices of the elements of the array ; Function to print the required permutations ; To keep track of original indices ; Sort the array ; Count the number of swaps that can be made ; Cannot generate 3 permutations ; Print the first permutation ; Find an index to swap and create second permutation ; Print the last permutation ; Driver code ; Function call"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int next_pos = 1 ; public class pair { public int first , second ; public pair ( ) { first = 0 ; second = 0 ; } } class sortHelper : IComparer { int IComparer . Compare ( object a , object b ) { pair first = ( pair ) a ; pair second = ( pair ) b ; return first . first < second . first ? - 1 : 1 ; } } static void printIndices ( int n , pair [ ] a ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( a [ i ] . second + " ▁ " ) ; Console . WriteLine ( ) ; } static void printPermutations ( int n , int [ ] a , int k ) { pair [ ] arr = new pair [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = new pair ( ) ; arr [ i ] . first = a [ i ] ; arr [ i ] . second = i ; } Array . Sort ( arr , new sortHelper ( ) ) ; int count = 1 ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i ] . first == arr [ i - 1 ] . first ) count ++ ; if ( count < k ) { Console . Write ( " - 1" ) ; return ; } for ( int i = 0 ; i < k - 1 ; i ++ ) { printIndices ( n , arr ) ; for ( int j = next_pos ; j < n ; j ++ ) { if ( arr [ j ] . first == arr [ j - 1 ] . first ) { pair t = arr [ j ] ; arr [ j ] = arr [ j - 1 ] ; arr [ j - 1 ] = t ; next_pos = j + 1 ; break ; } } } printIndices ( n , arr ) ; } public static void Main ( string [ ] args ) { int [ ] a = { 1 , 3 , 3 , 1 } ; int n = a . Length ; int k = 3 ; printPermutations ( n , a , k ) ; } }


"Print k numbers where all pairs are divisible by m | C # program to find a list of k elements from an array such that difference between all of them is divisible by m . ; function to generate k numbers whose difference is divisible by m ; Using an adjacency list like representation to store numbers that lead to same remainder . ; stores the modulus when divided by m ; If we found k elements which have same remainder . ; If we could not find k elements ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void print_result ( int [ ] a , int n , int k , int m ) { List < List < int > > v = new List < List < int > > ( m ) ; for ( int i = 0 ; i < m ; i ++ ) v . Add ( new List < int > ( ) ) ; for ( int i = 0 ; i < n ; i ++ ) { int rem = a [ i ] % m ; v [ rem ] . Add ( a [ i ] ) ; if ( v [ rem ] . Count == k ) { for ( int j = 0 ; j < k ; j ++ ) Console . Write ( v [ rem ] [ j ] + " ▁ " ) ; return ; } } Console . Write ( " - 1" ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 8 , 4 } ; int n = a . Length ; print_result ( a , n , 2 , 3 ) ; } }


"Print last character of each word in a string | C # implementation of the approach ; Function to print the last character of each word in the given string ; Now , last word is also followed by a space ; If current character is a space ; Then previous character must be the last character of some word ; Driver code"
Solution in C#:

using System ; class GFG { static void printLastChar ( string str ) { str = str + " ▁ " ; for ( int i = 1 ; i < str . Length ; i ++ ) { if ( str [ i ] == ' ▁ ' ) Console . Write ( str [ i - 1 ] + " ▁ " ) ; } } public static void Main ( ) { string str = " Geeks ▁ for ▁ Geeks " ; printLastChar ( str ) ; } }


"Print last k digits of a ^ b ( a raised to power b ) | C # code to find last k digits of a ^ b ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; x = x % p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Method to print last k digits of a ^ b ; Generating 10 ^ k ; Calling modular exponentiation ; Printing leftmost zeros . Since ( a ^ b ) % k can have digits less then k . In that case we need to print zeros ; If temp is not zero then print temp If temp is zero then already printed ; Driver Code"
Solution in C#:

using System ; class GFG { static int power ( long x , long y , long p ) { long res = 1 ; while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return ( int ) res ; } static void printLastKDigits ( int a , int b , int k ) { Console . Write ( " Last ▁ " + k + " ▁ digits ▁ of ▁ " + a + " ^ " + b + " ▁ = ▁ " ) ; int temp = 1 ; for ( int i = 1 ; i <= k ; i ++ ) temp *= 10 ; temp = power ( a , b , temp ) ; for ( int i = 0 ; i < k - temp . ToString ( ) . Length ; i ++ ) Console . WriteLine ( 0 ) ; if ( temp != 0 ) Console . Write ( temp ) ; } public static void Main ( ) { int a = 11 ; int b = 3 ; int k = 2 ; printLastKDigits ( a , b , k ) ; } }


"Print left rotation of array in O ( n ) time and O ( 1 ) space | C # implementation of left rotation of an array K number of times ; Function to leftRotate array multiple times ; To get the starting point of rotated array ; Prints the rotated array from start position ; Driver Code ; Function Call ; Function Call ; Function Call"
Solution in C#:

using System ; class GFG { static void leftRotate ( int [ ] arr , int n , int k ) { int mod = k % n ; for ( int i = 0 ; i < n ; ++ i ) Console . Write ( arr [ ( i + mod ) % n ] + " ▁ " ) ; Console . WriteLine ( ) ; } static public void Main ( ) { int [ ] arr = { 1 , 3 , 5 , 7 , 9 } ; int n = arr . Length ; int k = 2 ; leftRotate ( arr , n , k ) ; k = 3 ; leftRotate ( arr , n , k ) ; k = 4 ; leftRotate ( arr , n , k ) ; } }


"Print leftmost and rightmost nodes of a Binary Tree | C # program to print corner node at each level in a binary tree ; A binary tree node has key , pointer to left child and a pointer to right child ; Function to print corner node at each level ; star node is for keeping track of levels ; pushing root node and star node ; Do level order traversal of Binary Tree ; n is the no of nodes in current Level ; If it is leftmost corner value or rightmost corner value then print it ; push the left and right children of the temp node ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; public class Node { public int key ; public Node left , right ; public Node ( int key ) { this . key = key ; left = right = null ; } } public class BinaryTree { Node root ; void printCorner ( 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 == 0 i == n - 1 ) Console . Write ( temp . key + " ▁ " ) ; if ( temp . left != null ) q . Enqueue ( temp . left ) ; if ( temp . right != null ) q . Enqueue ( temp . right ) ; } } } public static void Main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 15 ) ; tree . root . left = new Node ( 10 ) ; tree . root . right = new Node ( 20 ) ; tree . root . left . left = new Node ( 8 ) ; tree . root . left . right = new Node ( 12 ) ; tree . root . right . left = new Node ( 16 ) ; tree . root . right . right = new Node ( 25 ) ; tree . printCorner ( tree . root ) ; } }


"Print list items containing all characters of a given word | C # program to print all strings that contain all characters of a word ; Prints list items having all characters of word ; Since calloc is used , map [ ] is initialized as 0 ; Set the values in map ; Get the length ( ) of given word ; Check each item of list if has all characters of word ; unset the bit so that strings like sss not printed ; Set the values in map for next item ; Driver code"
Solution in C#:

using System ; class GFG { static int NO_OF_CHARS = 256 ; static void print ( string [ ] list , string word , int list_size ) { int [ ] map = new int [ NO_OF_CHARS ] ; int i , j , count , word_size ; for ( i = 0 ; i < word . Length ; i ++ ) map [ word [ i ] ] = 1 ; word_size = word . Length ; for ( i = 0 ; i < list_size ; i ++ ) { for ( j = 0 , count = 0 ; j < list [ i ] . Length ; j ++ ) { if ( map [ list [ i ] [ j ] ] > 0 ) { count ++ ; map [ list [ i ] [ j ] ] = 0 ; } } if ( count == word_size ) Console . WriteLine ( list [ i ] ) ; for ( j = 0 ; j < word . Length ; j ++ ) map [ word [ j ] ] = 1 ; } } public static void Main ( string [ ] args ) { string str = " sun " ; string [ ] list = { " geeksforgeeks " , " unsorted " , " sunday " , " just " , " sss " } ; print ( list , str , 5 ) ; } }


"Print longest palindrome word in a sentence | C # program to print longest palindrome word in a sentence and its length ; Function to check if a word is palindrome ; making the check case case insensitive ; loop to check palindrome ; Function to find longest palindrome word ; to check last word for palindrome ; to store each word ; extracting each word ; Driver code"
Solution in C#:

using System ; class GFG { static bool checkPalin ( string word ) { int n = word . Length ; word = word . ToLower ( ) ; for ( int i = 0 ; i < n ; i ++ , n -- ) if ( word [ i ] != word [ n - 1 ] ) return false ; return true ; } static string longestPalin ( string str ) { str = str + " ▁ " ; string longestword = " " , word = " " ; int length , length1 = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { char ch = str [ i ] ; if ( ch != ' ▁ ' ) word = word + ch ; else { length = word . Length ; if ( checkPalin ( word ) && length > length1 ) { length1 = length ; longestword = word ; } word = " " ; } } return longestword ; } public static void Main ( ) { string s = " My ▁ name ▁ is ▁ ava ▁ and ▁ i " + " ▁ love ▁ Geeksforgeeks " ; if ( longestPalin ( s ) == " " ) Console . Write ( " No ▁ Palindrome ▁ Word " ) ; else Console . Write ( longestPalin ( s ) ) ; } }


"Print matrix after applying increment operations in M ranges | C # implementation of the above approach ; Function to update and print the matrix after performing queries ; Add 1 to the first element of the sub - matrix ; If there is an element after the last element of the sub - matrix then decrement it by 1 ; Calculate the running sum ; Print the updated element ; Next line ; Driver code ; Size of the matrix ; Queries"
Solution in C#:

using System ; public class GFG { static void updateMatrix ( int n , int [ , ] q , int [ , ] mat ) { int i , j ; for ( i = 0 ; i < q . GetLength ( 0 ) ; i ++ ) { int X1 = q [ i , 0 ] ; int Y1 = q [ i , 1 ] ; int X2 = q [ i , 2 ] ; int Y2 = q [ i , 3 ] ; mat [ X1 , Y1 ] ++ ; if ( Y2 + 1 < n ) mat [ X2 , Y2 + 1 ] -- ; else if ( X2 + 1 < n ) mat [ X2 + 1 , 0 ] -- ; } int sum = 0 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) { sum += mat [ i , j ] ; Console . Write ( sum + " ▁ " ) ; } Console . WriteLine ( ) ; } } public static void Main ( ) { int n = 5 ; int [ , ] mat = new int [ n , n ] ; int [ , ] q = { { 0 , 0 , 1 , 2 } , { 1 , 2 , 3 , 4 } , { 1 , 4 , 3 , 4 } } ; updateMatrix ( n , q , mat ) ; } }


"Print matrix elements from top | C # program for the above approach ; Function to traverse the matrix diagonally upwards ; Store the number of rows ; Initialize queue ; Push the index of first element i . e . , ( 0 , 0 ) ; Get the front element ; Pop the element at the front ; Insert the element below if the current element is in first column ; Insert the right neighbour if it exists ; Driver Code ; Given vector of vectors arr ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void printDiagonalTraversal ( int [ , ] nums ) { int m = nums . GetLength ( 0 ) ; Queue < pair > q = new Queue < pair > ( ) ; q . Enqueue ( new pair ( 0 , 0 ) ) ; while ( q . Count != 0 ) { pair p = q . Peek ( ) ; q . Dequeue ( ) ; Console . Write ( nums [ p . first , p . second ] + " ▁ " ) ; if ( p . second == 0 && p . first + 1 < m ) { q . Enqueue ( new pair ( p . first + 1 , p . second ) ) ; } if ( p . second + 1 < nums . GetLength ( 1 ) ) q . Enqueue ( new pair ( p . first , p . second + 1 ) ) ; } } public static void Main ( String [ ] args ) { int [ , ] arr = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; printDiagonalTraversal ( arr ) ; } }


"Print matrix elements from top | C # program for the above approach ; Function to traverse the matrix diagonally upwards ; Stores the maximum size of vector from all row of matrix nums [ ] [ ] ; Store elements in desired order ; Store every element on the basis of sum of index ( i + j ) ; Print the stored result ; print in reverse order ; Driver Code ; Given vector of vectors arr ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void printDiagonalTraversal ( int [ , ] nums ) { int max_size = nums . GetLength ( 0 ) ; List < List < int > > v = new List < List < int > > ( ) ; for ( int i = 0 ; i < 2 * max_size - 1 ; i ++ ) { v . Add ( new List < int > ( ) ) ; } for ( int i = 0 ; i < nums . GetLength ( 0 ) ; i ++ ) { for ( int j = 0 ; j < nums . GetLength ( 0 ) ; j ++ ) { v [ i + j ] . Add ( nums [ i , j ] ) ; } } for ( int i = 0 ; i < v . Count ; i ++ ) { for ( int j = v [ i ] . Count - 1 ; j >= 0 ; j -- ) { Console . Write ( v [ i ] [ j ] + " ▁ " ) ; } } } static void Main ( ) { int [ , ] arr = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; printDiagonalTraversal ( arr ) ; } }


"Print matrix elements using DFS traversal | C # program for the above approach ; Direction vectors ; Function to check if curruent position is valid or not ; Check if the cell is out of bounds ; Check if the cell is visited ; Function to print the matrix elements ; Stores if a position in the matrix been visited or not ; Initialize stack to implement DFS ; Push the first position of grid [ ] [ ] in the stack ; Mark the cell ( 0 , 0 ) visited ; Stores top element of stack ; Delete the top ( ) element of stack ; Print element at the cell ; Traverse in all four adjacent sides of current positions ; Check if x and y is valid position and then push the position of current cell in the stack ; Push the current cell ; Mark current cell visited ; Driver Code ; Given matrix ; Row of the matrix ; Column of the matrix"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int [ ] dRow = { - 1 , 0 , 1 , 0 } ; static int [ ] dCol = { 0 , 1 , 0 , - 1 } ; static List < List < bool > > vis ; static bool isValid ( int row , int col , int COL , int ROW ) { if ( row < 0 col < 0 col > COL - 1 row > ROW - 1 ) return false ; if ( vis [ row ] [ col ] == true ) return false ; return true ; } static void DFS_iterative ( int [ , ] grid , int M , int N ) { vis = new List < List < bool > > ( ) ; for ( int i = 0 ; i < M + 5 ; i ++ ) { vis . Add ( new List < bool > ( ) ) ; for ( int j = 0 ; j < N + 5 ; j ++ ) { vis [ i ] . Add ( false ) ; } } List < Tuple < int , int > > st = new List < Tuple < int , int > > ( ) ; st . Add ( new Tuple < int , int > ( 0 , 0 ) ) ; vis [ 0 ] [ 0 ] = true ; while ( st . Count > 0 ) { Tuple < int , int > p = st [ st . Count - 1 ] ; st . RemoveAt ( st . Count - 1 ) ; int row = p . Item1 ; int col = p . Item2 ; Console . Write ( grid [ row , col ] + " ▁ " ) ; for ( int i = 0 ; i < 4 ; i ++ ) { int x = row + dRow [ i ] ; int y = col + dCol [ i ] ; if ( isValid ( x , y , M , N ) ) { st . Add ( new Tuple < int , int > ( x , y ) ) ; vis [ x ] [ y ] = true ; } } } } static void Main ( ) { int [ , ] grid = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; int M = 4 ; int N = 4 ; DFS_iterative ( grid , M , N ) ; } }


"Print matrix elements using DFS traversal | C # program to implement the above approach ; Direction vectors ; Function to check if current position is valid or not ; Check if the cell is out of bounds ; Check if the cell is visited or not ; Utility function to print matrix elements using DFS Traversal ; Mark the current cell visited ; Print the element at the cell ; Traverse all four adjacent cells of the current element ; Check if x and y is valid index or not ; Function to print the matrix elements ; Initialize a visiting matrix ; Function call to print matrix elements by DFS traversal ; Driver Code ; Given matrix ; Row of the matrix ; Column of the matrix"
Solution in C#:

using System ; public class GFG { static int [ ] dRow = { - 1 , 0 , 1 , 0 } ; static int [ ] dCol = { 0 , 1 , 0 , - 1 } ; static bool isValid ( bool [ , ] vis , int row , int col , int COL , int ROW ) { if ( row < 0 col < 0 col > COL - 1 row > ROW - 1 ) return false ; if ( vis [ row , col ] == true ) return false ; return true ; } static void DFSUtil ( int row , int col , int [ , ] grid , bool [ , ] vis , int M , int N ) { vis [ row , col ] = true ; Console . Write ( grid [ row , col ] + " ▁ " ) ; for ( int i = 0 ; i < 4 ; i ++ ) { int x = row + dRow [ i ] ; int y = col + dCol [ i ] ; if ( isValid ( vis , x , y , M , N ) ) DFSUtil ( x , y , grid , vis , M , N ) ; } } static void DFS ( int row , int col , int [ , ] grid , int M , int N ) { bool [ , ] vis = new bool [ M + 1 , N + 1 ] ; for ( int i = 0 ; i < M + 1 ; i ++ ) { for ( int j = 0 ; j < N + 1 ; j ++ ) { vis [ i , j ] = false ; } } DFSUtil ( 0 , 0 , grid , vis , M , N ) ; } public static void Main ( String [ ] args ) { int [ , ] grid = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; int M = grid . GetLength ( 0 ) ; int N = grid . GetLength ( 1 ) ; DFS ( 0 , 0 , grid , M , N ) ; } }


"Print matrix in antispiral form | C # Code for Print matrix in antispiral form ; k - starting row index m - ending row index l - starting column index n - ending column index i - iterator ; Print the first row from the remaining rows ; Print the last column from the remaining columns ; Print the last row from the remaining rows ; Print the first column from the remaining columns ; Driver program to test above function"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { public static void antiSpiralTraversal ( int m , int n , int [ ] [ ] a ) { int i , k = 0 , l = 0 ; Stack < int > stk = new Stack < int > ( ) ; while ( k <= m && l <= n ) { for ( i = l ; i <= n ; ++ i ) { stk . Push ( a [ k ] [ i ] ) ; } k ++ ; for ( i = k ; i <= m ; ++ i ) { stk . Push ( a [ i ] [ n ] ) ; } n -- ; if ( k <= m ) { for ( i = n ; i >= l ; -- i ) { stk . Push ( a [ m ] [ i ] ) ; } m -- ; } if ( l <= n ) { for ( i = m ; i >= k ; -- i ) { stk . Push ( a [ i ] [ l ] ) ; } l ++ ; } } while ( stk . Count > 0 ) { Console . Write ( stk . Peek ( ) + " ▁ " ) ; stk . Pop ( ) ; } } public static void Main ( string [ ] args ) { int [ ] [ ] mat = new int [ ] [ ] { new int [ ] { 1 , 2 , 3 , 4 , 5 } , new int [ ] { 6 , 7 , 8 , 9 , 10 } , new int [ ] { 11 , 12 , 13 , 14 , 15 } , new int [ ] { 16 , 17 , 18 , 19 , 20 } } ; antiSpiralTraversal ( mat . Length - 1 , mat [ 0 ] . Length - 1 , mat ) ; } }


"Print matrix in diagonal pattern | C # program to print matrix in diagonal order ; Driver code ; Initialize matrix ; n - size mode - switch to derive up / down traversal it - iterator count - increases until it reaches n and then decreases ; 2 n will be the number of iterations"
Solution in C#:

using System ; public class MatrixDiag { public static void Main ( String [ ] args ) { int [ , ] mat = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; int n = 4 , mode = 0 , it = 0 , lower = 0 ; for ( int t = 0 ; t < ( 2 * n - 1 ) ; t ++ ) { int t1 = t ; if ( t1 >= n ) { mode ++ ; t1 = n - 1 ; it -- ; lower ++ ; } else { lower = 0 ; it ++ ; } for ( int i = t1 ; i >= lower ; i -- ) { if ( ( t1 + mode ) % 2 == 0 ) { Console . WriteLine ( mat [ i , t1 + lower - i ] ) ; } else { Console . WriteLine ( mat [ t1 + lower - i , i ] ) ; } } } } }


"Print matrix in diagonal pattern | C # program to print matrix in diagonal order ; Initialize indexes of element to be printed next ; Direction is initially from down to up ; Traverse the matrix till all elements get traversed ; If isUp = true then traverse from downward to upward ; Set i and j according to direction ; If isUp = 0 then traverse up to down ; Set i and j according to direction ; Revert the isUp to change the direction ; Driver code"
Solution in C#:

using System ; class GFG { static int MAX = 100 ; static void printMatrixDiagonal ( int [ , ] mat , int n ) { int i = 0 , j = 0 ; bool isUp = true ; for ( int k = 0 ; k < n * n ; ) { if ( isUp ) { for ( ; i >= 0 && j < n ; j ++ , i -- ) { Console . Write ( mat [ i , j ] + " ▁ " ) ; k ++ ; } if ( i < 0 && j <= n - 1 ) i = 0 ; if ( j == n ) { i = i + 2 ; j -- ; } } else { for ( ; j >= 0 && i < n ; i ++ , j -- ) { Console . Write ( mat [ i , j ] + " ▁ " ) ; k ++ ; } if ( j < 0 && i <= n - 1 ) j = 0 ; if ( i == n ) { j = j + 2 ; i -- ; } } isUp = ! isUp ; } } public static void Main ( ) { int [ , ] mat = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; int n = 3 ; printMatrixDiagonal ( mat , n ) ; } }


"Print modified array after executing the commands of addition and subtraction | Program to find modified array after executing m commands / queries ; Update function for every command ; If q == 0 , add ' k ' and ' - k ' to ' l - 1' and ' r ' index ; If q == 1 , add ' - k ' and ' k ' to ' l - 1' and ' r ' index ; Function to generate final array after executing all commands ; Generate final array with the help of DP concept ; Driver code ; Generate final array ; Printing the final modified array"
Solution in C#:

using System ; class GFG { static void updateQuery ( int [ ] arr , int n , int q , int l , int r , int k ) { if ( q == 0 ) { arr [ l - 1 ] += k ; arr [ r ] += - k ; } else { arr [ l - 1 ] += - k ; arr [ r ] += k ; } return ; } static void generateArray ( int [ ] arr , int n ) { for ( int i = 1 ; i < n ; ++ i ) arr [ i ] += arr [ i - 1 ] ; } public static void Main ( ) { int n = 5 ; int [ ] arr = new int [ n + 1 ] ; for ( int i = 0 ; i < arr . Length ; i ++ ) arr [ i ] = 0 ; int q = 0 , l = 1 , r = 3 , k = 2 ; updateQuery ( arr , n , q , l , r , k ) ; q = 1 ; l = 3 ; r = 5 ; k = 3 ; updateQuery ( arr , n , q , l , r , k ) ; q = 0 ; l = 2 ; r = 5 ; k = 1 ; updateQuery ( arr , n , q , l , r , k ) ; generateArray ( arr , n ) ; for ( int i = 0 ; i < n ; ++ i ) Console . Write ( arr [ i ] + " ▁ " ) ; } }


"Print modified array after multiple array range increment operations | C # implementation to increment values in the given range by a value d for multiple queries ; structure to store the ( start , end ) index pair for each query ; function to increment values in the given range by a value d for multiple queries ; for each ( start , end ) index pair , perform the following operations on ' sum [ ] ' ; increment the value at index ' start ' by the given value ' d ' in ' sum [ ] ' ; if the index ' ( end + 1 ) ' exists then decrement the value at index ' ( end + 1 ) ' by the given value ' d ' in ' sum [ ] ' ; Now , perform the following operations : accumulate values in the ' sum [ ] ' array and then add them to the corresponding indexes in ' arr [ ] ' ; function to print the elements of the given array ; Driver code ; modifying the array for multiple queries"
Solution in C#:

 using System ; class GFG { public class query { public int start , end ; public query ( int start , int end ) { this . start = start ; this . end = end ; } } public static void incrementByD ( int [ ] arr , query [ ] q_arr , int n , int m , int d ) { int [ ] sum = new int [ n ] ; for ( int i = 0 ; i < m ; i ++ ) { sum [ q_arr [ i ] . start ] += d ; if ( ( q_arr [ i ] . end + 1 ) < n ) sum [ q_arr [ i ] . end + 1 ] -= d ; } arr [ 0 ] += sum [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { sum [ i ] += sum [ i - 1 ] ; arr [ i ] += sum [ i ] ; } } public static void printArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 5 , 4 , 8 , 6 , 1 } ; query [ ] q_arr = new query [ 5 ] ; q_arr [ 0 ] = new query ( 0 , 3 ) ; q_arr [ 1 ] = new query ( 4 , 5 ) ; q_arr [ 2 ] = new query ( 1 , 4 ) ; q_arr [ 3 ] = new query ( 0 , 1 ) ; q_arr [ 4 ] = new query ( 2 , 5 ) ; int n = arr . Length ; int m = q_arr . Length ; int d = 2 ; Console . WriteLine ( " Original ▁ Array : " ) ; printArray ( arr , n ) ; incrementByD ( arr , q_arr , n , m , d ) ; Console . WriteLine ( "
 Modified ▁ Array : " ) ; printArray ( arr , n ) ; } }


"Print n 0 s and m 1 s such that no two 0 s and no three 1 s are together | C # implementation of the above approach ; Function to print the required pattern ; When condition fails ; When m = n - 1 ; Driver code"
Solution in C#:

using System ; class GFG { static void printPattern ( int n , int m ) { if ( m > 2 * ( n + 1 ) m < n - 1 ) { Console . Write ( " - 1" ) ; } else if ( Math . Abs ( n - m ) <= 1 ) { while ( n > 0 && m > 0 ) { Console . Write ( "01" ) ; n -- ; m -- ; } if ( n != 0 ) { Console . Write ( "0" ) ; } if ( m != 0 ) { Console . Write ( "1" ) ; } } else { while ( m - n > 1 && n > 0 ) { Console . Write ( "110" ) ; m = m - 2 ; n = n - 1 ; } while ( n > 0 ) { Console . Write ( "10" ) ; n -- ; m -- ; } while ( m > 0 ) { Console . Write ( "1" ) ; m -- ; } } } public static void Main ( ) { int n = 4 , m = 8 ; printPattern ( n , m ) ; } }


"Print n numbers such that their sum is a perfect square | C # implementation of the approach ; Function to print n numbers such that their sum is a perfect square ; Print ith odd number ; Driver code"
Solution in C#:

using System ; public class GFG { public static void findNumbers ( int n ) { int i = 1 ; while ( i <= n ) { Console . Write ( ( ( 2 * i ) - 1 ) + " ▁ " ) ; i ++ ; } } public static void Main ( string [ ] args ) { int n = 3 ; findNumbers ( n ) ; } }


"Print n terms of Newman | C # Program to print n terms of Newman - Conway Sequence ; Function to find the n - th element ; Declare array to store sequence ; Driver code"
Solution in C#:

using System ; class GFG { public static void sequence ( int n ) { int [ ] f = new int [ n + 1 ] ; f [ 0 ] = 0 ; f [ 1 ] = 1 ; f [ 2 ] = 1 ; Console . Write ( f [ 1 ] + " ▁ " + f [ 2 ] + " ▁ " ) ; for ( int i = 3 ; i <= n ; i ++ ) { f [ i ] = f [ f [ i - 1 ] ] + f [ i - f [ i - 1 ] ] ; Console . Write ( f [ i ] + " ▁ " ) ; } } public static void Main ( ) { int n = 13 ; sequence ( n ) ; } }


"Print n x n spiral matrix using O ( 1 ) extra space | C # program to print a n x n spiral matrix in clockwise direction using O ( 1 ) space ; Prints spiral matrix of size n x n containing numbers from 1 to n x n ; x stores the layer in which ( i , j ) th element lies ; Finds minimum of four inputs ; For upper right half ; for lower left half ; Driver code ; print a n x n spiral matrix in O ( 1 ) space"
Solution in C#:

using System ; class GFG { static void printSpiral ( int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { int x ; x = Math . Min ( Math . Min ( i , j ) , Math . Min ( n - 1 - i , n - 1 - j ) ) ; if ( i <= j ) Console . Write ( ( n - 2 * x ) * ( n - 2 * x ) - ( i - x ) - ( j - x ) + " 	 " ) ; else Console . Write ( ( n - 2 * x - 2 ) * ( n - 2 * x - 2 ) + ( i - x ) + ( j - x ) + " 	 " ) ; } Console . WriteLine ( ) ; } } public static void Main ( ) { int n = 5 ; printSpiral ( n ) ; } }


"Print negative weight cycle in a Directed Graph | C # program for the above approach ; Structure to represent a weighted edge in graph ; Structure to represent a directed and weighted graph ; V . Number of vertices , E . Number of edges ; graph is represented as an array of edges . ; Creates a new graph with V vertices and E edges ; Function runs Bellman - Ford algorithm and prints negative cycle ( if present ) ; Initialize distances from src to all other vertices as INFINITE and all parent as - 1 ; Relax all edges | V | - 1 times . ; Check for negative - weight cycles ; Store one of the vertex of the negative weight cycle ; To store the cycle vertex ; Reverse cycle [ ] ; Printing the negative cycle ; Driver Code ; Number of vertices in graph ; Number of edges in graph ; Given Graph ; Function Call"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class GFG { class Edge { public int src , dest , weight ; } class Graph { public int V , E ; public Edge [ ] edge ; } static Graph createGraph ( int V , int E ) { Graph graph = new Graph ( ) ; graph . V = V ; graph . E = E ; graph . edge = new Edge [ graph . E ] ; for ( int i = 0 ; i < graph . E ; i ++ ) { graph . edge [ i ] = new Edge ( ) ; } return graph ; } static void NegCycleBellmanFord ( Graph graph , int src ) { int V = graph . V ; int E = graph . E ; int [ ] dist = new int [ V ] ; int [ ] parent = new int [ V ] ; for ( int i = 0 ; i < V ; i ++ ) { dist [ i ] = 1000000 ; parent [ i ] = - 1 ; } dist [ src ] = 0 ; for ( int i = 1 ; i <= V - 1 ; i ++ ) { for ( int j = 0 ; j < E ; j ++ ) { int u = graph . edge [ j ] . src ; int v = graph . edge [ j ] . dest ; int weight = graph . edge [ j ] . weight ; if ( dist [ u ] != 1000000 && dist [ u ] + weight < dist [ v ] ) { dist [ v ] = dist [ u ] + weight ; parent [ v ] = u ; } } } int C = - 1 ; for ( int i = 0 ; i < E ; i ++ ) { int u = graph . edge [ i ] . src ; int v = graph . edge [ i ] . dest ; int weight = graph . edge [ i ] . weight ; if ( dist [ u ] != 1000000 && dist [ u ] + weight < dist [ v ] ) { C = v ; break ; } } if ( C != - 1 ) { for ( int i = 0 ; i < V ; i ++ ) C = parent [ C ] ; ArrayList cycle = new ArrayList ( ) ; for ( int v = C ; ; v = parent [ v ] ) { cycle . Add ( v ) ; if ( v == C && cycle . Count > 1 ) break ; } cycle . Reverse ( ) ; foreach ( int v in cycle ) Console . Write ( v + " ▁ " ) ; Console . WriteLine ( ) ; } else Console . WriteLine ( - 1 ) ; } public static void Main ( string [ ] args ) { int V = 5 ; int E = 5 ; Graph graph = createGraph ( V , E ) ; graph . edge [ 0 ] . src = 0 ; graph . edge [ 0 ] . dest = 1 ; graph . edge [ 0 ] . weight = 1 ; graph . edge [ 1 ] . src = 1 ; graph . edge [ 1 ] . dest = 2 ; graph . edge [ 1 ] . weight = 2 ; graph . edge [ 2 ] . src = 2 ; graph . edge [ 2 ] . dest = 3 ; graph . edge [ 2 ] . weight = 3 ; graph ....


"Print nodes at k distance from root | Iterative | C # program to print all nodes of level k iterative approach ; Node of binary tree ; Function to add a new node ; Function to print nodes of given level ; extra null is added to keep track of all the nodes to be added before level is incremented by 1 ; print when level is equal to k ; break the loop if level exceeds the given level number ; Driver code ; create a binary tree"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { public class Node { public int data ; public Node left , right ; } static Node newNode ( int data ) { Node newnode = new Node ( ) ; newnode . data = data ; newnode . left = newnode . right = null ; return newnode ; } static Boolean printKDistant ( Node root , int klevel ) { Queue < Node > q = new Queue < Node > ( ) ; int level = 1 ; Boolean flag = false ; q . Enqueue ( root ) ; q . Enqueue ( null ) ; while ( q . Count > 0 ) { Node temp = q . Peek ( ) ; if ( level == klevel && temp != null ) { flag = true ; Console . Write ( temp . data + " ▁ " ) ; } q . Dequeue ( ) ; if ( temp == null ) { if ( q . Count > 0 && q . Peek ( ) != null ) q . Enqueue ( null ) ; level += 1 ; if ( level > klevel ) break ; } else { if ( temp . left != null ) q . Enqueue ( temp . left ) ; if ( temp . right != null ) q . Enqueue ( temp . right ) ; } } Console . Write ( "
 " ) ; return flag ; } public static void Main ( String [ ] srga ) { Node root = newNode ( 20 ) ; root . left = newNode ( 10 ) ; root . right = newNode ( 30 ) ; root . left . left = newNode ( 5 ) ; root . left . right = newNode ( 15 ) ; root . left . right . left = newNode ( 12 ) ; root . right . left = newNode ( 25 ) ; root . right . right = newNode ( 40 ) ; Console . Write ( " data ▁ at ▁ level ▁ 1 ▁ : ▁ " ) ; Boolean ret = printKDistant ( root , 1 ) ; if ( ret == false ) Console . Write ( " Number ▁ exceeds ▁ total ▁ " + " number ▁ of ▁ levels
 " ) ; Console . Write ( " data ▁ at ▁ level ▁ 2 ▁ : ▁ " ) ; ret = printKDistant ( root , 2 ) ; if ( ret == false ) Console . Write ( " Number ▁ exceeds ▁ total ▁ " + " number ▁ of ▁ levels
 " ) ; Console . Write ( " data ▁ at ▁ level ▁ 3 ▁ : ▁ " ) ; ret = printKDistant ( root , 3 ) ; if ( ret == false ) Console . Write ( " Number ▁ exceeds ▁ total ▁ " + " number ▁ of ▁ levels
 " ) ; Console . Write ( " data ▁ at ▁ level ▁ 6 ▁ : ▁ " ) ; ret = printKDistant ( root , 6 ) ; if ( ret == false ) Console . Write ( " Number ▁ exceeds ▁ total " + " number ▁ of ▁...


"Print nodes at k distance from root | c # program to print nodes at k distance from root ; A binary tree node has data , pointer to left child and a pointer to right child ; Driver program to test above functions ; Constructed binary tree is 1 / \ 2 3 / \ / 4 5 8"
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 printKDistant ( Node node , int k ) { if ( node == null k < 0 ) { return ; } if ( k == 0 ) { Console . Write ( node . data + " ▁ " ) ; return ; } printKDistant ( node . left , k - 1 ) ; printKDistant ( node . right , k - 1 ) ; } 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 ( 8 ) ; tree . printKDistant ( tree . root , 2 ) ; } }


"Print nodes between two given level numbers of a binary tree | c # program to print Nodes level by level between given two levels ; A binary tree Node has key , pointer to left and right children ; Given a binary tree , print nodes from level number ' low ' to level number ' high ' ; Marker node to indicate end of level ; Initialize level number ; Enqueue the only first level node and marker node for end of level ; Simple level order traversal loop ; Remove the front item from queue ; Check if end of level is reached ; print a new line and increment level number ; Check if marker node was last node in queue or level number is beyond the given upper limit ; Enqueue the marker for end of next level ; If this is marker , then we don 't need print it  and enqueue its children ; If level is equal to or greater than given lower level , print it ; Enqueue children of non - marker node ; Driver program to test for above functions"
Solution in C#:

using System ; using System . Collections . Generic ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class BinaryTree { public Node root ; public virtual void printLevels ( Node node , int low , int high ) { LinkedList < Node > Q = new LinkedList < Node > ( ) ; Node marker = new Node ( 4 ) ; int level = 1 ; Q . AddLast ( node ) ; Q . AddLast ( marker ) ; while ( Q . Count > 0 ) { Node n = Q . First . Value ; Q . RemoveFirst ( ) ; if ( n == marker ) { Console . WriteLine ( " " ) ; level ++ ; if ( Q . Count == 0 level > high ) { break ; } Q . AddLast ( marker ) ; continue ; } if ( level >= low ) { Console . Write ( n . data + " ▁ " ) ; } if ( n . left != null ) { Q . AddLast ( n . left ) ; } if ( n . right != null ) { Q . AddLast ( n . right ) ; } } } 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 ) ; Console . Write ( " Level ▁ Order ▁ traversal ▁ between ▁ given ▁ two ▁ levels ▁ is ▁ " ) ; tree . printLevels ( tree . root , 2 , 3 ) ; } }


"Print number in ascending order which contains 1 , 2 and 3 in their digits . | C # program to print all number containing 1 , 2 and 3 in any order . ; prints all the number containing 1 , 2 , 3 in any order ; check if the number contains 1 , 2 & 3 in any order ; sort all the numbers ; convert the number to string and find if it contains 1 , 2 & 3. ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; using System . Text ; class GFG { private static string printNumbers ( int [ ] numbers ) { List < int > array = new List < int > ( ) ; foreach ( int number in numbers ) { if ( findContainsOneTwoThree ( number ) ) { array . Add ( number ) ; } } array . Sort ( ) ; StringBuilder strbuf = new StringBuilder ( ) ; System . Collections . IEnumerator it = array . GetEnumerator ( ) ; while ( it . MoveNext ( ) ) { int value = ( int ) it . Current ; if ( strbuf . Length > 0 ) { strbuf . Append ( " , ▁ " ) ; } strbuf . Append ( Convert . ToString ( value ) ) ; } return ( strbuf . Length > 0 ) ? strbuf . ToString ( ) : " - 1" ; } private static bool findContainsOneTwoThree ( int number ) { string str = Convert . ToString ( number ) ; return ( str . Contains ( "1" ) && str . Contains ( "2" ) && str . Contains ( "3" ) ) ; } public static void Main ( string [ ] args ) { int [ ] numbers = new int [ ] { 123 , 1232 , 456 , 234 , 32145 } ; Console . WriteLine ( printNumbers ( numbers ) ) ; } }


"Print numbers in descending order along with their frequencies | C # program to print the elements in descending along with their frequencies ; Function to print the elements in descending along with their frequencies ; Sorts the element in decreasing order ; traverse the array elements ; Prints the number and count ; Prints the last step ; Driver Code"
Solution in C#:

 using System ; class GFG { static void printElements ( int [ ] a , int n ) { Array . Sort ( a ) ; a = reverse ( a ) ; int cnt = 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( a [ i ] != a [ i + 1 ] ) { Console . Write ( a [ i ] + " ▁ occurs ▁ " + cnt + " ▁ times
 " ) ; cnt = 1 ; } else cnt += 1 ; } Console . Write ( a [ n - 1 ] + " ▁ occurs ▁ " + cnt + " ▁ times
 " ) ; } static int [ ] reverse ( int [ ] a ) { int i , n = a . Length , t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } return a ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 1 , 1 , 2 , 3 , 4 , 9 , 9 , 10 } ; int n = a . Length ; printElements ( a , n ) ; } }


"Print numbers in the range 1 to n having bits in alternate pattern | C # implementation to print numbers in the range 1 to n having bits in alternate pattern ; function to print numbers in the range 1 to n having bits in alternate pattern ; first number having bits in alternate pattern ; display ; loop until n < curr_num ; generate next number having alternate bit pattern ; if true then break ; display ; generate next number having alternate bit pattern ; if true then break ; display ; Driver code"
Solution in C#:

using System ; class GFG { public static void printNumHavingAltBitPatrn ( int n ) { int curr_num = 1 , i = 1 ; Console . Write ( curr_num + " ▁ " ) ; while ( i != 0 ) { curr_num <<= 1 ; if ( n < curr_num ) break ; Console . Write ( curr_num + " ▁ " ) ; curr_num = ( ( curr_num ) << 1 ) ^ 1 ; if ( n < curr_num ) break ; Console . Write ( curr_num + " ▁ " ) ; } } public static void Main ( ) { int n = 50 ; printNumHavingAltBitPatrn ( n ) ; } }


"Print numbers such that no two consecutive numbers are co | C # implementation of the approach ; Function for Sieve of Eratosthenes ; Function to print the required sequence ; Store only the required primes ; Base condition ; First integer in the list ; Second integer in the list ; Third integer in the list ; Generate ( N - 1 ) th term ; Generate Nth term ; Modify first term ; Print the sequence ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int MAX = 620000 ; static int [ ] prime = new int [ MAX ] ; static void Sieve ( ) { for ( int i = 2 ; i < MAX ; i ++ ) { if ( prime [ i ] == 0 ) { for ( int j = 2 * i ; j < MAX ; j += i ) { prime [ j ] = 1 ; } } } } static void printSequence ( int n ) { Sieve ( ) ; List < int > v = new List < int > ( ) ; List < int > u = new List < int > ( ) ; for ( int i = 13 ; i < MAX ; i ++ ) { if ( prime [ i ] == 0 ) { v . Add ( i ) ; } } if ( n == 3 ) { Console . Write ( 6 + " ▁ " + 10 + " ▁ " + 15 ) ; return ; } int k ; for ( k = 0 ; k < n - 2 ; k ++ ) { if ( k % 3 == 0 ) { u . Add ( v [ k ] * 6 ) ; } else if ( k % 3 == 1 ) { u . Add ( v [ k ] * 15 ) ; } else { u . Add ( v [ k ] * 10 ) ; } } k -- ; u . Add ( v [ k ] * 7 ) ; u . Add ( 7 * 11 ) ; u [ 0 ] = u [ 0 ] * 11 ; for ( int i = 0 ; i < u . Count ; i ++ ) { Console . Write ( u [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int n = 4 ; printSequence ( n ) ; } }


"Print numbers such that no two consecutive numbers are co | C # implementation of the approach ; Function to generate Sieve of Eratosthenes ; If prime [ p ] is not changed , then it is a prime ; Add the prime numbers to the array b ; Function to return the gcd of a and b ; Function to print the required sequence of integers ; Including the primes in a series of primes which will be later multiplied ; This is done to mark a product as existing ; Maximum number of primes that we consider ; For different interval ; For different starting index of jump ; For storing the numbers ; Checking for occurrence of a product . Also checking for the same prime occurring consecutively ; Including the primes in a series of primes which will be later multiplied ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int limit = 1000000000 ; static int MAX_PRIME = 2000000 ; static int MAX = 1000000 ; static int I_MAX = 50000 ; static Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; static int [ ] b = new int [ MAX ] ; static int [ ] p = new int [ MAX ] ; static int j = 0 ; static bool [ ] prime = new bool [ MAX_PRIME + 1 ] ; static void SieveOfEratosthenes ( int n ) { for ( int i = 0 ; i < MAX_PRIME + 1 ; i ++ ) prime [ i ] = true ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) prime [ i ] = false ; } } for ( int p = 2 ; p <= n ; p ++ ) { if ( prime [ p ] ) { b [ j ++ ] = p ; } } } static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static void printSeries ( int n ) { SieveOfEratosthenes ( MAX_PRIME ) ; int i , g , k , l , m , d ; int [ ] ar = new int [ I_MAX + 2 ] ; for ( i = 0 ; i < j ; i ++ ) { if ( ( b [ i ] * b [ i + 1 ] ) > limit ) break ; p [ i ] = b [ i ] ; mp . Add ( b [ i ] * b [ i + 1 ] , 1 ) ; } d = 550 ; bool flag = false ; for ( k = 2 ; ( k < d - 1 ) && ! flag ; k ++ ) { for ( m = 2 ; ( m < d ) && ! flag ; m ++ ) { for ( l = m + k ; l < d ; l += k ) { if ( ( ( b [ l ] * b [ l + k ] ) < limit ) && mp . ContainsKey ( b [ l ] * b [ l + k ] ) && mp . ContainsKey ( p [ i - 1 ] * b [ l ] ) && ( l + k ) < d && p [ i - 1 ] != b [ l + k ] && p [ i - 1 ] != b [ l ] && mp [ b [ l ] * b [ l + k ] ] != 1 ) { if ( mp [ p [ i - 1 ] * b [ l ] ] != 1 ) { p [ i ] = b [ l ] ; mp . Add ( p [ i - 1 ] * b [ l ] , 1 ) ; i ++ ; } } if ( i >= I_MAX ) { flag = true ; break ; } } } } for ( i = 0 ; i < n ; i ++ ) ar [ i ] = p [ i ] * p [ i + 1 ] ; for ( i = 0 ; i < n - 1 ; i ++ ) Console . Write ( ar [ i ] + " ▁ " ) ; g = gcd ( ar [ n - 1 ] , ar [ n - 2 ] ) ; Console . Write ( g * 2 ) ; } public static void Main ( String [ ] args ) { int n = 4 ; printSeries ( n ) ; } }


"Print pair with maximum AND value in an array | C # Program to find pair with maximum AND value ; Utility function to check number of elements having set msb as of pattern ; Function for finding maximum and value pair ; iterate over total of 30 bits from msb to lsb ; find the count of element having set msb ; if count >= 2 set particular bit in result ; Find the elements ; print the pair of elements ; inc count value after printing element ; return the result value ; Driver code"
Solution in C#:

using System ; class GFG { static int checkBit ( int pattern , int [ ] arr , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( ( pattern & arr [ i ] ) == pattern ) count ++ ; return count ; } static int maxAND ( int [ ] arr , int n ) { int res = 0 , count ; for ( int bit = 31 ; bit >= 0 ; bit -- ) { count = checkBit ( res | ( 1 << bit ) , arr , n ) ; if ( count >= 2 ) res |= ( 1 << bit ) ; } if ( res == 0 ) Console . Write ( " Not ▁ Possible " ) ; else { Console . Write ( " Pair ▁ = ▁ " ) ; count = 0 ; for ( int i = 0 ; i < n && count < 2 ; i ++ ) { if ( ( arr [ i ] & res ) == res ) { count ++ ; Console . Write ( arr [ i ] + " ▁ " ) ; } } Console . WriteLine ( ) ; } return res ; } public static void Main ( ) { int [ ] arr = { 4 , 8 , 6 , 2 } ; int n = arr . Length ; Console . WriteLine ( " Maximum ▁ AND ▁ Value ▁ = ▁ " + maxAND ( arr , n ) ) ; } }


"Print path from a node to root of given Complete Binary Tree | C # program for the above approach ; Function to print the path from node to root ; Iterate until root is reached ; Print the value of the current node ; Move to parent of the current node ; Driver Code"
Solution in C#:

using System ; class GFG { static void path_to_root ( int node ) { while ( node >= 1 ) { Console . Write ( node + " ▁ " ) ; node /= 2 ; } } public static void Main ( String [ ] args ) { int N = 7 ; path_to_root ( N ) ; } }


"Print path from given Source to Destination in 2 | C # program for printing a path from given source to destination ; Function to print the path ; Base condition ; Pop stores elements ; Recursive call for printing stack In reverse order ; Function to store the path into The stack , if path exist ; Base condition ; Push current elements ; Condition to check whether reach to the Destination or not ; Increment ' x ' ordinate of source by ( 2 * x + y ) Keeping ' y ' constant ; Increment ' y ' ordinate of source by ( 2 * y + x ) Keeping ' x ' constant ; Pop current elements form stack ; If no path exists ; Utility function to check whether path exist or not ; To store x co - ordinate ; To store y co - ordinate ; Function to find the path ; Print - 1 , if path doesn 't exist ; Driver code ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void printExistPath ( Stack < int > sx , Stack < int > sy , int last ) { if ( sx . Count == 0 sy . Count == 0 ) { return ; } int x = sx . Peek ( ) ; int y = sy . Peek ( ) ; sx . Pop ( ) ; sy . Pop ( ) ; printExistPath ( sx , sy , last ) ; if ( sx . Count == last - 1 ) { Console . Write ( " ( " + x + " , ▁ " + y + " ) " ) ; } else { Console . Write ( " ( " + x + " , ▁ " + y + " ) - > " ) ; } } static bool storePath ( int srcX , int srcY , int destX , int destY , Stack < int > sx , Stack < int > sy ) { if ( srcX > destX srcY > destY ) { return false ; } sx . Push ( srcX ) ; sy . Push ( srcY ) ; if ( srcX == destX && srcY == destY ) { printExistPath ( sx , sy , sx . Count ) ; return true ; } if ( storePath ( ( 2 * srcX ) + srcY , srcY , destX , destY , sx , sy ) ) { return true ; } if ( storePath ( srcX , ( 2 * srcY ) + srcX , destX , destY , sx , sy ) ) { return true ; } sx . Pop ( ) ; sy . Pop ( ) ; return false ; } static bool isPathExist ( int srcX , int srcY , int destX , int destY ) { Stack < int > sx = new Stack < int > ( ) ; Stack < int > sy = new Stack < int > ( ) ; return storePath ( srcX , srcY , destX , destY , sx , sy ) ; } static void printPath ( int srcX , int srcY , int destX , int destY ) { if ( ! isPathExist ( srcX , srcY , destX , destY ) ) { Console . Write ( " - 1" ) ; } } public static void Main ( String [ ] args ) { int srcX = 5 , srcY = 8 ; int destX = 83 , destY = 21 ; printPath ( srcX , srcY , destX , destY ) ; } }


"Print path from root to a given node in a binary tree | C # implementation to print the path from root to a given node in a binary tree ; A node of binary tree ; Returns true if there is a path from root to the given node . It also populates ' arr ' with the given path ; if root is NULL there is no path ; push the node ' s ▁ value ▁ in ▁ ' arr '  ; if it is the required node return true ; else check whether the required node lies in the left subtree or right subtree of the current node ; required node does not lie either in the left or right subtree of the current node Thus , remove current node ' s ▁ value ▁ from ▁ ▁ ▁ ' arr 'and then return false  ; function to print the path from root to the given node if the node lies in the binary tree ; List to store the path ; if required node ' x ' is present then print the path ; ' x ' is not present in the binary tree ; Driver code ; binary tree formation"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class PrintPath { public class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } public static Boolean hasPath ( Node root , List < int > arr , int x ) { if ( root == null ) return false ; arr . Add ( root . data ) ; if ( root . data == x ) return true ; if ( hasPath ( root . left , arr , x ) || hasPath ( root . right , arr , x ) ) return true ; arr . RemoveAt ( arr . Count - 1 ) ; return false ; } public static void printPath ( Node root , int x ) { List < int > arr = new List < int > ( ) ; if ( hasPath ( root , arr , x ) ) { for ( int i = 0 ; i < arr . Count - 1 ; i ++ ) Console . Write ( arr [ i ] + " - > " ) ; Console . Write ( arr [ arr . Count - 1 ] ) ; } else Console . Write ( " No ▁ Path " ) ; } public static void Main ( String [ ] args ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right . left = new Node ( 6 ) ; root . right . right = new Node ( 7 ) ; int x = 5 ; printPath ( root , x ) ; } }


"Print prime factors of a given integer in decreasing order using Stack | C # program for the above approach ; Function to print prime factors of N in decreasing order ; Stores prime factors of N in decreasing order ; Insert i into stack ; Update N ; Update i ; Print value of stack st ; Driver Code ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void PrimeFactors ( int N ) { Stack < int > st = new Stack < int > ( ) ; int i = 2 ; while ( N != 1 ) { if ( N % i == 0 ) { st . Push ( i ) ; while ( N % i == 0 ) { N = N / i ; } } i ++ ; } while ( st . Count != 0 ) { Console . Write ( st . Peek ( ) ) ; st . Pop ( ) ; } } public static void Main ( ) { int N = 8 ; PrimeFactors ( N ) ; ; } }


"Print prime numbers from 1 to N in reverse order | C # program to print all primes between 1 to N in reverse order using Sieve of Eratosthenes ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . 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 ; Print all prime numbers ; Driver code ; static input ; To display ; calling the function"
Solution in C#:

using System ; class GFG { static void reverseorder ( int n ) { bool [ ] prime = new bool [ n + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) prime [ i ] = true ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } for ( int i = n ; i >= 2 ; i -- ) if ( prime [ i ] == true ) Console . Write ( i + " ▁ " ) ; } public static void Main ( ) { int N = 25 ; Console . WriteLine ( " Prime ▁ number ▁ in " + " ▁ reverse ▁ order " ) ; if ( N == 1 ) Console . WriteLine ( " No ▁ prime ▁ no " + " ▁ exist ▁ in ▁ this ▁ range " ) ; else reverseorder ( N ) ; } }


"Print prime numbers with prime sum of digits in an array | C # implementation of the above approach ; Function to store the primes ; Function to return the sum of digits ; Function to print additive primes ; If the number is prime ; Check if it 's digit sum is prime ; Driver code"
Solution in C#:

using System . Linq ; using System ; class GFG { static void sieve ( int maxEle , int [ ] prime ) { prime [ 0 ] = prime [ 1 ] = 1 ; for ( int i = 2 ; i * i <= maxEle ; i ++ ) { if ( prime [ i ] == 0 ) { for ( int j = 2 * i ; j <= maxEle ; j += i ) prime [ j ] = 1 ; } } } static int digitSum ( int n ) { int sum = 0 ; while ( n > 0 ) { sum += n % 10 ; n = n / 10 ; } return sum ; } static void printAdditivePrime ( int [ ] arr , int n ) { int maxEle = arr . Max ( ) ; int [ ] prime = new int [ maxEle + 1 ] ; sieve ( maxEle , prime ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] == 0 ) { int sum = digitSum ( arr [ i ] ) ; if ( prime [ sum ] == 0 ) Console . Write ( arr [ i ] + " ▁ " ) ; } } } static void Main ( ) { int [ ] a = { 2 , 4 , 6 , 11 , 12 , 18 , 7 } ; int n = a . Length ; printAdditivePrime ( a , n ) ; } }


"Print rectangular pattern with given center | C # program for the above approach ; Function to print the matrix filled with rectangle pattern having center coordinates are c1 , c2 ; Iterate in the range [ 0 , n - 1 ] ; Iterate in the range [ 0 , n - 1 ] ; Driver Code ; Given Input ; Function Call"
Solution in C#:

using System ; class GFG { static void printRectPattern ( int c1 , int c2 , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { Console . Write ( ( Math . Max ( Math . Abs ( c1 - i ) , Math . Abs ( c2 - j ) ) ) + " ▁ " ) ; } Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { int c1 = 2 ; int c2 = 2 ; int n = 5 ; printRectPattern ( c1 , c2 , n ) ; } }


"Print root to leaf paths without using recursion | C # program to Print root to leaf path WITHOUT using recursion ; A binary tree node ; Function to print root to leaf path for a leaf using parent nodes stored in map ; start from leaf node and keep on pushing nodes into stack till root node is reached ; Start popping nodes from stack and print them ; An iterative function to do preorder traversal of binary tree and print root to leaf path without using recursion ; Corner Case ; Create an empty stack and push root to it ; Create a map to store parent pointers of binary tree nodes ; parent of root is NULL ; Pop all items one by one . Do following for every popped item a ) push its right child and set its parent pointer b ) push its left child and set its parent pointer Note that right child is pushed first so that left is processed first ; Pop the top item from stack ; If leaf node encountered , print Top To Bottom path ; Push right & left children of the popped node to stack . Also set their parent pointer in the map ; Driver code ; Constructed binary tree is 10 / \ 8 2 / \ / 3 5 2"
Solution in C#:

using System ; using System . Collections . Generic ; public class PrintPath { public class Node { public int data ; public Node left , right ; public Node ( int data ) { left = right = null ; this . data = data ; } } ; public static void printTopToBottomPath ( Node curr , Dictionary < Node , Node > parent ) { Stack < Node > stk = new Stack < Node > ( ) ; while ( curr != null ) { stk . Push ( curr ) ; curr = parent [ curr ] ; } while ( stk . Count != 0 ) { curr = stk . Pop ( ) ; Console . Write ( curr . data + " ▁ " ) ; } Console . WriteLine ( ) ; } public static void printRootToLeaf ( Node root ) { if ( root == null ) return ; Stack < Node > nodeStack = new Stack < Node > ( ) ; nodeStack . Push ( root ) ; Dictionary < Node , Node > parent = new Dictionary < Node , Node > ( ) ; parent . Add ( root , null ) ; while ( nodeStack . Count != 0 ) { Node current = nodeStack . Pop ( ) ; if ( current . left == null && current . right == null ) printTopToBottomPath ( current , parent ) ; if ( current . right != null ) { parent . Add ( current . right , current ) ; nodeStack . Push ( current . right ) ; } if ( current . left != null ) { parent . Add ( current . left , current ) ; nodeStack . Push ( current . left ) ; } } } public static void Main ( String [ ] args ) { Node root = new Node ( 10 ) ; root . left = new Node ( 8 ) ; root . right = new Node ( 2 ) ; root . left . left = new Node ( 3 ) ; root . left . right = new Node ( 5 ) ; root . right . left = new Node ( 2 ) ; printRootToLeaf ( root ) ; } }


"Print siblings of a given Node in N | C # program for the above approach ; Structure of a node of N - ary tree ; Function to create a new node ; Function to find the siblings of the node / value ; Stores nodes level wise ; Push the root ; Continue until all levels are traversed ; Stores current node ; Enqueue all children of the current node ; If node value is found ; Print all children of current node except value as the answer ; Push the child nodes of temp into the queue ; Driver Code ; Stores root of the constructed tree ; Print siblings of Node X"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public class Node { public int key ; public List < Node > child = new List < Node > ( ) ; } ; static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; return temp ; } static void Siblings ( Node root , int value ) { int flag = 0 ; if ( root == null ) return ; Queue < Node > q = new Queue < Node > ( ) ; q . Enqueue ( root ) ; while ( q . Count != 0 ) { Node temp = q . Peek ( ) ; q . Dequeue ( ) ; for ( int i = 0 ; i < temp . child . Count ; i ++ ) { if ( temp . child [ i ] . key == value ) { flag = 1 ; for ( int j = 0 ; j < temp . child . Count ; j ++ ) { if ( value != temp . child [ j ] . key ) Console . Write ( temp . child [ j ] . key + " ▁ " ) ; } break ; } q . Enqueue ( temp . child [ i ] ) ; } } if ( flag == 0 ) Console . Write ( " No ▁ siblings ! ! " ) ; } static Node constructTree ( ) { Node root = newNode ( 10 ) ; ( root . child ) . Add ( newNode ( 20 ) ) ; ( root . child ) . Add ( newNode ( 30 ) ) ; ( root . child ) . Add ( newNode ( 40 ) ) ; ( root . child [ 0 ] . child ) . Add ( newNode ( 50 ) ) ; ( root . child [ 0 ] . child ) . Add ( newNode ( 60 ) ) ; ( root . child [ 1 ] . child ) . Add ( newNode ( 70 ) ) ; ( root . child [ 1 ] . child ) . Add ( newNode ( 80 ) ) ; ( root . child [ 2 ] . child ) . Add ( newNode ( 90 ) ) ; ( root . child [ 2 ] . child ) . Add ( newNode ( 100 ) ) ; ( root . child [ 2 ] . child ) . Add ( newNode ( 110 ) ) ; return root ; } public static void Main ( String [ ] args ) { Node root = constructTree ( ) ; int X = 30 ; Siblings ( root , X ) ; } }


"Print steps to make a number in form of 2 ^ X | C # program to implement the above approach ; Function to find the leftmost unset bit in a number . ; Function that perform the step ; Find the leftmost unset bit ; If the number has no bit unset , it means it is in form 2 ^ x - 1 ; Count the steps ; Iterate till number is of form 2 ^ x - 1 ; At even step increase by 1 ; Odd step xor with any 2 ^ m - 1 ; Find the leftmost unset bit ; 2 ^ m - 1 ; Perform the step ; Increase the steps ; Driver code"
Solution in C#:

using System ; class GFG { static int find_leftmost_unsetbit ( int n ) { int ind = - 1 ; int i = 1 ; while ( n > 0 ) { if ( ( n % 2 ) != 1 ) { ind = i ; } i ++ ; n >>= 1 ; } return ind ; } static void perform_steps ( int n ) { int left = find_leftmost_unsetbit ( n ) ; if ( left == - 1 ) { Console . Write ( " No ▁ steps ▁ required " ) ; return ; } int step = 1 ; while ( find_leftmost_unsetbit ( n ) != - 1 ) { if ( step % 2 == 0 ) { n += 1 ; Console . WriteLine ( " Step " + step + " : ▁ Increase ▁ by ▁ 1" ) ; } else { int m = find_leftmost_unsetbit ( n ) ; int num = ( int ) ( Math . Pow ( 2 , m ) - 1 ) ; n = n ^ num ; Console . WriteLine ( " Step " + step + " : ▁ Xor ▁ with ▁ " + num ) ; } step += 1 ; } } static public void Main ( ) { int n = 39 ; perform_steps ( n ) ; } }


"Print string after removing all ( “ 10 ” or “ 01 ” ) from the binary string | C # program to print the readonly String after removing all the occurrences of "10" and "01" from the given binary String ; Function to print the readonly String after removing all the occurrences of "10" and "01" from the given binary String ; Variables to store the count of 1 ' s ▁ and ▁ 0' s ; Variable left will store whether 0 ' s ▁ or ▁ 1' s is left in the readonly String ; Length of the String ; For loop to count the occurrences of 1 ' s ▁ and ▁ 0' s in the String ; To check if the count of 1 ' s ▁ is ▁ ▁ greater ▁ than ▁ the ▁ count ▁ of ▁ 0' s or not . If x is greater , then those many 1 's  are printed. ; Length of the readonly remaining String after removing all the occurrences ; Printing the readonly String ; Driver Code"
Solution in C#:

using System ; class GFG { static void finalString ( String str ) { int x = 0 , y = 0 ; int left ; int n = str . Length ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == '1' ) x ++ ; else y ++ ; } if ( x > y ) left = 1 ; else left = 0 ; int length = n - 2 * Math . Min ( x , y ) ; for ( int i = 0 ; i < length ; i ++ ) { Console . Write ( left ) ; } } public static void Main ( String [ ] args ) { String str = "010110100100000" ; finalString ( str ) ; } }


"Print string of odd length in ' X ' format | C # program to print cross pattern ; Function to print given string in cross pattern ; i and j are the indexes of characters to be displayed in the ith iteration i = 0 initially and go upto length of string j = length of string initially in each iteration of i , we increment i and decrement j , we print character only of k == i or k == j ; Driver code"
Solution in C#:

using System ; class GFG { static void pattern ( String str , int len ) { for ( int i = 0 ; i < len ; i ++ ) { int j = len - 1 - i ; for ( int k = 0 ; k < len ; k ++ ) { if ( k == i k == j ) Console . Write ( str [ k ] ) ; else Console . Write ( " ▁ " ) ; } Console . WriteLine ( " " ) ; } } public static void Main ( String [ ] args ) { String str = " geeksforgeeks " ; int len = str . Length ; pattern ( str , len ) ; } }


"Print string of odd length in ' X ' format | C # program to print the given pattern ; Function to print the given string in respective pattern ; Print characters at corresponding places satisfying the two conditions ; Print blank space at rest of places ; Driver code"
Solution in C#:

using System ; public class GFG { static void printPattern ( string str , int len ) { for ( int i = 0 ; i < len ; i ++ ) { for ( int j = 0 ; j < len ; j ++ ) { if ( ( i == j ) || ( i + j == len - 1 ) ) Console . Write ( str [ j ] ) ; else Console . Write ( " ▁ " ) ; } Console . WriteLine ( ) ; } } static public void Main ( ) { String str = " geeksforgeeks " ; int len = str . Length ; printPattern ( str , len ) ; } }


"Print the DFS traversal step | C # program for the above approach ; Function to print the complete DFS - traversal ; Check if all th node is visited or not and count unvisited nodes ; If all the node is visited return ; ; Mark not visited node as visited ; Track the current edge ; Print the node ; Check for not visited node and proceed with it . ; call the DFs function if not visited ; Backtrack through the last visited nodes ; Function to call the DFS function which prints the DFS - traversal stepwise ; Create a array of visited node ; Vector to track last visited road ; Initialize all the node with false ; call the function ; Function to insert edges in Graph ; Driver Code ; number of nodes and edges in the graph ; Function call to create the graph ; Call the function to print"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { static int N = 1000 ; static List < List < int > > adj = new List < List < int > > ( ) ; static void dfsUtil ( int u , int node , bool [ ] visited , List < List < int > > road_used , int parent , int it ) { int c = 0 ; for ( int i = 0 ; i < node ; i ++ ) if ( visited [ i ] ) c ++ ; if ( c == node ) return ; visited [ u ] = true ; road_used . Add ( new List < int > ( ) { parent , u } ) ; Console . Write ( u + " ▁ " ) ; foreach ( int x in adj [ u ] ) { if ( ! visited [ x ] ) { dfsUtil ( x , node , visited , road_used , u , it + 1 ) ; } } for ( int y = 0 ; y < road_used . Count ; y ++ ) { if ( road_used [ y ] [ 1 ] == u ) { dfsUtil ( road_used [ y ] [ 0 ] , node , visited , road_used , u , it + 1 ) ; } } } static void dfs ( int node ) { bool [ ] visited = new bool [ node ] ; List < List < int > > road_used = new List < List < int > > ( ) ; for ( int i = 0 ; i < node ; i ++ ) { visited [ i ] = false ; } dfsUtil ( 0 , node , visited , road_used , - 1 , 0 ) ; } static void insertEdge ( int u , int v ) { adj [ u ] . Add ( v ) ; adj [ v ] . Add ( u ) ; } static public void Main ( ) { int node = 11 ; for ( int i = 0 ; i < N ; i ++ ) { adj . Add ( new List < int > ( ) ) ; } insertEdge ( 0 , 1 ) ; insertEdge ( 0 , 2 ) ; insertEdge ( 1 , 5 ) ; insertEdge ( 1 , 6 ) ; insertEdge ( 2 , 4 ) ; insertEdge ( 2 , 9 ) ; insertEdge ( 6 , 7 ) ; insertEdge ( 6 , 8 ) ; insertEdge ( 7 , 8 ) ; insertEdge ( 2 , 3 ) ; insertEdge ( 3 , 9 ) ; insertEdge ( 3 , 10 ) ; insertEdge ( 9 , 10 ) ; dfs ( node ) ; } }


"Print the Forests of a Binary Tree after removal of given Nodes | C # program to implement the above approach ; Stores the nodes to be deleted ; Structure of a Tree node ; Function to create a new node ; Function to check whether the node needs to be deleted or not ; Function to perform tree pruning by performing post order traversal ; If the node needs to be deleted ; Store the its subtree ; Perform Inorder Traversal ; Function to print the forests ; Stores the remaining nodes ; Print the inorder traversal of Forests ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static Dictionary < int , Boolean > mp = new Dictionary < int , Boolean > ( ) ; class Node { public int key ; public Node left , right ; } ; static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . left = temp . right = null ; return ( temp ) ; } static bool deleteNode ( int nodeVal ) { return mp . ContainsKey ( nodeVal ) ; } static Node treePruning ( Node root , List < Node > result ) { if ( root == null ) return null ; root . left = treePruning ( root . left , result ) ; root . right = treePruning ( root . right , result ) ; if ( deleteNode ( root . key ) ) { if ( root . left != null ) { result . Add ( root . left ) ; } if ( root . right != null ) { result . Add ( root . right ) ; } return null ; } return root ; } static void printInorderTree ( Node root ) { if ( root == null ) return ; printInorderTree ( root . left ) ; Console . Write ( root . key + " ▁ " ) ; printInorderTree ( root . right ) ; } static void printForests ( Node root , int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { mp . Add ( arr [ i ] , true ) ; } List < Node > result = new List < Node > ( ) ; if ( treePruning ( root , result ) != null ) result . Add ( root ) ; for ( int i = 0 ; i < result . Count ; i ++ ) { printInorderTree ( result [ i ] ) ; Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 12 ) ; root . right = newNode ( 13 ) ; root . right . left = newNode ( 14 ) ; root . right . right = newNode ( 15 ) ; root . right . left . left = newNode ( 21 ) ; root . right . left . right = newNode ( 22 ) ; root . right . right . left = newNode ( 23 ) ; root . right . right . right = newNode ( 24 ) ; int [ ] arr = { 14 , 23 , 1 } ; int n = arr . Length ; printForests ( root , arr , n ) ; } }


"Print the Maximum Subarray Sum | C # program for the above approach ; Function to print the elements of Subarray with maximum sum ; Initialize currMax and globalMax with first value of nums ; Initialize endIndex startIndex , globalStartIndex ; Iterate for all the elemensts of the array ; Update currMax and startIndex ; startIndex = i ; update the new startIndex ; Update currMax ; Update globalMax anf globalMaxStartIndex ; Printing the elements of subarray with max sum ; Driver Code ; Given array arr [ ] ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { static void SubarrayWithMaxSum ( List < int > nums ) { int currMax = nums [ 0 ] , globalMax = nums [ 0 ] ; int endIndex = 0 ; int startIndex = 0 , globalMaxStartIndex = 0 ; for ( int i = 1 ; i < nums . Count ; ++ i ) { if ( nums [ i ] > nums [ i ] + currMax ) { currMax = nums [ i ] ; } else if ( nums [ i ] < nums [ i ] + currMax ) { currMax = nums [ i ] + currMax ; } if ( currMax > globalMax ) { globalMax = currMax ; endIndex = i ; globalMaxStartIndex = startIndex ; } } for ( int i = globalMaxStartIndex ; i <= endIndex ; ++ i ) { Console . Write ( nums [ i ] + " ▁ " ) ; } } static public void Main ( ) { List < int > arr = new List < int > ( ) ; arr . Add ( - 2 ) ; arr . Add ( - 5 ) ; arr . Add ( 6 ) ; arr . Add ( - 2 ) ; arr . Add ( - 3 ) ; arr . Add ( 1 ) ; arr . Add ( 5 ) ; arr . Add ( - 6 ) ; SubarrayWithMaxSum ( arr ) ; } }


"Print the Maximum Subarray Sum | C # program for the above approach ; Function to print the elements of Subarray with maximum sum ; Initialize currMax and globalMax with first value of nums ; Iterate for all the elemensts of the array ; Update currMax ; Check if currMax is greater than globalMax ; Traverse in left direction to find start Index of subarray ; Decrement the start index ; Printing the elements of subarray with max sum ; Driver Code ; Given array [ ] arr ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void SubarrayWithMaxSum ( List < int > nums ) { int endIndex = 0 , currMax = nums [ 0 ] ; int globalMax = nums [ 0 ] ; for ( int i = 1 ; i < nums . Count ; ++ i ) { currMax = Math . Max ( nums [ i ] , nums [ i ] + currMax ) ; if ( currMax > globalMax ) { globalMax = currMax ; endIndex = i ; } } int startIndex = endIndex ; while ( startIndex >= 0 ) { globalMax -= nums [ startIndex ] ; if ( globalMax == 0 ) break ; startIndex -- ; } for ( int i = startIndex ; i <= endIndex ; ++ i ) { Console . Write ( nums [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { List < int > arr = new List < int > ( ) ; arr . Add ( - 2 ) ; arr . Add ( - 5 ) ; arr . Add ( 6 ) ; arr . Add ( - 2 ) ; arr . Add ( - 3 ) ; arr . Add ( 1 ) ; arr . Add ( 5 ) ; arr . Add ( - 6 ) ; SubarrayWithMaxSum ( arr ) ; } }


"Print the arranged positions of characters to make palindrome | C # program to print original positions of characters in a String after rearranging and forming a palindrome ; Maximum number of characters ; Insert all positions of every character in the given String . ; find the number of odd elements . Takes O ( n ) ; A palindrome cannot contain more than 1 odd characters ; Print positions in first half of palindrome ; Consider one instance odd character ; Print positions in second half of palindrome ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int MAX = 256 ; static void printPalindromePos ( String str ) { List < int > [ ] pos = new List < int > [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) pos [ i ] = new List < int > ( ) ; int n = str . Length ; for ( int i = 0 ; i < n ; i ++ ) pos [ str [ i ] ] . Add ( i + 1 ) ; int oddCount = 0 ; char oddChar = ( char ) 0 ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( pos [ i ] . Count % 2 != 0 ) { oddCount ++ ; oddChar = ( char ) i ; } } if ( oddCount > 1 ) Console . Write ( " NO ▁ PALINDROME " ) ; for ( int i = 0 ; i < MAX ; i ++ ) { int mid = pos [ i ] . Count / 2 ; for ( int j = 0 ; j < mid ; j ++ ) Console . Write ( pos [ i ] [ j ] + " ▁ " ) ; } if ( oddCount > 0 ) { int last = pos [ oddChar ] . Count - 1 ; Console . Write ( pos [ oddChar ] [ last ] + " ▁ " ) ; pos [ oddChar ] . RemoveAt ( pos [ oddChar ] . Count - 1 ) ; } for ( int i = MAX - 1 ; i >= 0 ; i -- ) { int count = pos [ i ] . Count ; for ( int j = count / 2 ; j < count ; j ++ ) Console . Write ( pos [ i ] [ j ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { String s = " geeksgk " ; printPalindromePos ( s ) ; } }


"Print the balanced bracket expression using given brackets | C # implementation of the approach ; Function to print balanced bracket expression if it is possible ; If the condition is met ; Print brackets of type - 1 ; Print brackets of type - 3 ; Print brackets of type - 4 ; Print brackets of type - 2 ; If the condition is not met ; Driver code"
Solution in C#:

using System ; class GFG { static void printBalancedExpression ( int a , int b , int c , int d ) { if ( ( ( a == d ) && ( a != 0 ) ) || ( ( a == 0 ) && ( c == 0 ) && ( d == 0 ) ) ) { for ( int i = 1 ; i <= a ; i ++ ) Console . Write ( " ( ( " ) ; for ( int i = 1 ; i <= c ; i ++ ) Console . Write ( " ) ( " ) ; for ( int i = 1 ; i <= d ; i ++ ) Console . Write ( " ) ) " ) ; for ( int i = 1 ; i <= b ; i ++ ) Console . Write ( " ( ) " ) ; } else Console . Write ( - 1 ) ; } public static void Main ( ) { int a = 3 , b = 1 , c = 4 , d = 3 ; printBalancedExpression ( a , b , c , d ) ; } }


"Print the first N terms of the series 6 , 28 , 66 , 120 , 190 , 276 , ... | C # program for the above approach ; Function to print the series ; Initialise the value of k with 2 ; Iterate from 1 to n ; Print each number ; Increment the value of K by 2 for next number ; Driver code ; Given number N ; Function call"
Solution in C#:

using System ; class GFG { static void printSeries ( int n ) { int k = 2 ; for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( k * ( 2 * k - 1 ) + " ▁ " ) ; k += 2 ; } Console . WriteLine ( ) ; } public static void Main ( ) { int N = 12 ; printSeries ( N ) ; } }


"Print the first and last character of each word in a String | C # program to print the first and last character of each word in a String ; Function to print the first and last character of each word . ; If it is the first word of the string then print it . ; If it is the last word of the string then also print it . ; If there is a space print the successor and predecessor to space . ; Driver code"
Solution in C#:

using System ; class GFG { static void FirstAndLast ( string str ) { int i ; for ( i = 0 ; i < str . Length ; i ++ ) { if ( i == 0 ) Console . Write ( str [ i ] ) ; if ( i == str . Length - 1 ) Console . Write ( str [ i ] ) ; if ( str [ i ] == ' ▁ ' ) { Console . Write ( str [ i - 1 ] + " ▁ " + str [ i + 1 ] ) ; } } } public static void Main ( ) { string str = " Geeks ▁ for ▁ Geeks " ; FirstAndLast ( str ) ; } }


"Print the frequency of each character in Alphabetical order | C # implementation of the approach ; Function to print the frequency of each of the characters of s in alphabetical order ; To store the frequency of the characters ; Update the frequency array ; Print the frequency in alphatecial order ; If the current alphabet doesn 't  appear in the string ; Driver code"
Solution in C#:

using System ; class GFG { static int MAX = 26 ; static void compressString ( string s , int n ) { int [ ] freq = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { freq [ s [ i ] - ' a ' ] ++ ; } for ( int i = 0 ; i < MAX ; i ++ ) { if ( freq [ i ] == 0 ) continue ; Console . Write ( ( char ) ( i + ' a ' ) + " " + freq [ i ] ) ; } } public static void Main ( ) { string s = " geeksforgeeks " ; int n = s . Length ; compressString ( s , n ) ; } }