"Find the missing digit in given product of large positive integers | C # program for the above approach ; Function to find the replaced digit in the product of a * b ; Keeps track of the sign of the current digit ; Stores the value of a % 11 ; Find the value of a mod 11 for large value of a as per the derived formula ; Stores the value of b % 11 ; Find the value of b mod 11 for large value of a as per the derived formula ; Stores the value of c % 11 ; Keeps track of the sign of x ; If the current digit is the missing digit , then keep the track of its sign ; Find the value of x using the derived equation ; Check if x has a negative sign ; Return positive equivaluent of x mod 11 ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int findMissingDigit ( string a , string b , string c ) { int w = 1 ; int a_mod_11 = 0 ; for ( int i = a . Length - 1 ; i >= 0 ; i -- ) { a_mod_11 = ( a_mod_11 + w * ( ( int ) a [ i ] - 48 ) ) % 11 ; w = w * - 1 ; } int b_mod_11 = 0 ; w = 1 ; for ( int i = b . Length - 1 ; i >= 0 ; i -- ) { b_mod_11 = ( b_mod_11 + w * ( ( int ) b [ i ] - 48 ) ) % 11 ; w = w * - 1 ; } int c_mod_11 = 0 ; bool xSignIsPositive = true ; w = 1 ; for ( int i = c . Length - 1 ; i >= 0 ; i -- ) { if ( c [ i ] == ' x ' ) { xSignIsPositive = ( w == 1 ) ; } else { c_mod_11 = ( c_mod_11 + w * ( ( int ) c [ i ] - '0' ) ) % 11 ; } w = w * - 1 ; } int x = ( ( a_mod_11 * b_mod_11 ) - c_mod_11 ) % 11 ; if ( xSignIsPositive == false ) { x = - x ; } return ( x % 11 + 11 ) % 11 ; } public static void Main ( ) { string A = "123456789" ; string B = "987654321" ; string C = "12193263111x635269" ; Console . Write ( findMissingDigit ( A , B , C ) ) ; } }


"Find the missing elements from 1 to M in given N ranges | C # program to find missing elements from given Ranges ; Function to find missing elements from given Ranges ; First of all sort all the given ranges ; Store ans in a different vector ; prev is use to store end of last range ; j is used as a counter for ranges ; For last segment ; Finally print all answer ; Driver code ; Store ranges in vector of pair"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class GFG { class sortHelper : IComparer { int IComparer . Compare ( object a , object b ) { Pair first = ( Pair ) a ; Pair second = ( Pair ) b ; if ( first . first == second . first ) { return first . second - second . second ; } return first . first - second . first ; } } public class Pair { public int first , second ; public Pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void findMissingNumber ( ArrayList ranges , int m ) { IComparer myComparer = new sortHelper ( ) ; ranges . Sort ( myComparer ) ; ArrayList ans = new ArrayList ( ) ; int prev = 0 ; for ( int j = 0 ; j < ranges . Count ; j ++ ) { int start = ( ( Pair ) ranges [ j ] ) . first ; int end = ( ( Pair ) ranges [ j ] ) . second ; for ( int i = prev + 1 ; i < start ; i ++ ) ans . Add ( i ) ; prev = end ; } for ( int i = prev + 1 ; i <= m ; i ++ ) ans . Add ( i ) ; for ( int i = 0 ; i < ans . Count ; i ++ ) { if ( ( int ) ans [ i ] <= m ) Console . Write ( ans [ i ] + " ▁ " ) ; } } public static void Main ( string [ ] args ) { int M = 6 ; ArrayList ranges = new ArrayList ( ) ; ranges . Add ( new Pair ( 1 , 2 ) ) ; ranges . Add ( new Pair ( 4 , 5 ) ) ; findMissingNumber ( ranges , M ) ; } }


"Find the missing number in a string of numbers with no separator | C # program to find a missing number in a string of consecutive numbers without any separator . ; gets the integer at position i with length m , returns it or - 1 , if none ; Find value at index i and length m . ; Returns value of missing number ; Try all lengths for first number ; Get value of first number with current length / ; To store missing number of current length ; To indicate whether the sequence failed anywhere for current length . ; Find subsequent numbers with previous number as n ; If we haven 't yet found the missing number  for current length. Next number is n+2. Note  that we use Log10 as (n+2) may have more  length than n. ; } If next value is ( n + 1 ) ; return - 1 ; not found or no missing number ; Driver code"
Solution in C#:

using System ; public class GFG { static readonly int MAX_DIGITS = 6 ; static int getValue ( String str , int i , int m ) { if ( i + m > str . Length ) { return - 1 ; } int value = 0 ; for ( int j = 0 ; j < m ; j ++ ) { int c = str [ i + j ] - '0' ; if ( c < 0 c > 9 ) { return - 1 ; } value = value * 10 + c ; } return value ; } static int findMissingNumber ( String str ) { for ( int m = 1 ; m <= MAX_DIGITS ; ++ m ) { int n = getValue ( str , 0 , m ) ; if ( n == - 1 ) { break ; } int missingNo = - 1 ; bool fail = false ; for ( int i = m ; i != str . Length ; i += 1 + ( int ) Math . Log10 ( n ) ) { if ( ( missingNo == - 1 ) && ( getValue ( str , i , ( int ) ( 1 + Math . Log10 ( n + 2 ) ) ) == n + 2 ) ) { missingNo = n + 1 ; n += 2 ; else if ( getValue ( str , i , ( int ) ( 1 + Math . Log10 ( n + 1 ) ) ) == n + 1 ) { n ++ ; } else { fail = true ; break ; } } if ( ! fail ) { return missingNo ; } } } public static void Main ( ) { Console . WriteLine ( findMissingNumber ( "99101102" ) ) ; } }


"Find the modified array after performing k operations of given type | C # implementation of the approach ; Utility function to print the contents of an array ; Function to remove the minimum value of the array from every element of the array ; Get the minimum value from the array ; Remove the minimum value from every element of the array ; Function to remove every element of the array from the maximum value of the array ; Get the maximum value from the array ; Remove every element of the array from the maximum value of the array ; Function to print the modified array after k operations ; If k is odd then remove the minimum element of the array from every element of the array ; Else remove every element of the array from the maximum value from the array ; Print the modified array ; Driver code"
Solution in C#:

using System ; class GFG { static void printArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } static void removeMin ( int [ ] arr , int n ) { int i , minVal = arr [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) minVal = Math . Min ( minVal , arr [ i ] ) ; for ( i = 0 ; i < n ; i ++ ) arr [ i ] = arr [ i ] - minVal ; } static void removeFromMax ( int [ ] arr , int n ) { int i , maxVal = arr [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) maxVal = Math . Max ( maxVal , arr [ i ] ) ; for ( i = 0 ; i < n ; i ++ ) arr [ i ] = maxVal - arr [ i ] ; } static void modifyArray ( int [ ] arr , int n , int k ) { if ( k % 2 == 0 ) removeMin ( arr , n ) ; else removeFromMax ( arr , n ) ; printArray ( arr , n ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 8 , 12 , 16 } ; int n = arr . Length ; int k = 2 ; modifyArray ( arr , n , k ) ; } }


"Find the most frequent digit without using array / string | Finds maximum occurring digit without using any array / string ; Simple function to count occurrences of digit d in x ; Initialize count of digit d ; Increment count if current digit is same as d ; Returns the max occurring digit in x ; Handle negative number ; Initialize result which is a digit ; Initialize count of result ; Traverse through all digits ; Count occurrences of current digit ; Update max_count and result if needed ; Driver Code"
Solution in C#:

class GFG { static int countOccurrences ( int x , int d ) { int count = 0 ; while ( x > 0 ) { if ( x % 10 == d ) count ++ ; x = x / 10 ; } return count ; } static int maxOccurring ( int x ) { if ( x < 0 ) x = - x ; int result = 0 ; int max_count = 1 ; for ( int d = 0 ; d <= 9 ; d ++ ) { int count = countOccurrences ( x , d ) ; if ( count >= max_count ) { max_count = count ; result = d ; } } return result ; } static void Main ( ) { int x = 1223355 ; System . Console . WriteLine ( " Max ▁ occurring ▁ digit ▁ is ▁ " + maxOccurring ( x ) ) ; } }


"Find the n | C # Implementation of the above approach ; Function to return the nth string in the required sequence ; Length of the resultant string ; Relative Index ; Initial string of length len consists of all a 's since the list is sorted ; Convert relative index to Binary form and set 0 = a and 1 = b ; Reverse and return the string ; Utility reverse function ; Driver function"
Solution in C#:

using System ; using System . Text ; class GFG { static String obtain_str ( int n ) { int len = ( int ) Math . Floor ( ( Math . Log ( n + 1 ) / Math . Log ( 2 ) ) ) ; int rel_ind = n + 1 - ( int ) Math . Pow ( 2 , len ) ; int i = 0 ; StringBuilder str = new StringBuilder ( ) ; for ( i = 0 ; i < len ; i ++ ) { str . Append ( ' a ' ) ; } i = 0 ; while ( rel_ind > 0 ) { if ( rel_ind % 2 == 1 ) str [ i ] = ' b ' ; rel_ind /= 2 ; i ++ ; } return reverse ( str . ToString ( ) ) ; } static String reverse ( String input ) { char [ ] a = input . ToCharArray ( ) ; int l , r = a . Length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . Join ( " " , a ) ; } public static void Main ( String [ ] args ) { int n = 11 ; Console . Write ( obtain_str ( n ) ) ; } }


"Find the n | C # program to find n - th number whose binary representation is palindrome . ; Finds if the kth bit is set in the binary representation ; Returns the position of leftmost set bit in the binary representation ; Finds whether the integer in binary representation is palindrome or not ; One by one compare bits ; Compare left and right bits and converge ; Start from 1 , traverse through all the integers ; If we reach n , break the loop ; Driver code ; Function Call"
Solution in C#:

using System ; class GFG { static int INT_MAX = 2147483647 ; static int isKthBitSet ( int x , int k ) { return ( ( x & ( 1 << ( k - 1 ) ) ) > 0 ) ? 1 : 0 ; } static int leftmostSetBit ( int x ) { int count = 0 ; while ( x > 0 ) { count ++ ; x = x >> 1 ; } return count ; } static int isBinPalindrome ( int x ) { int l = leftmostSetBit ( x ) ; int r = 1 ; while ( l > r ) { if ( isKthBitSet ( x , l ) != isKthBitSet ( x , r ) ) return 0 ; l -- ; r ++ ; } return 1 ; } static int findNthPalindrome ( int n ) { int pal_count = 0 ; int i = 0 ; for ( i = 1 ; i <= INT_MAX ; i ++ ) { if ( isBinPalindrome ( i ) > 0 ) { pal_count ++ ; } if ( pal_count == n ) break ; } return i ; } static public void Main ( ) { int n = 9 ; Console . WriteLine ( findNthPalindrome ( n ) ) ; } }


"Find the n | C # program to find n - th palindrome ; Utility function which is used to convert binary string into integer ; Convert binary string into integer ; Function to find nth binary palindrome number ; Stores the binary palindrome string ; Base case ; Add 2 nd binary palindrome string ; Runs till the nth binary palindrome number ; Remove curr binary palindrome string from queue ; If n == 0 then we find the n 'th binary  palindrome so we return our answer ; Calculate length of curr binary palindrome string ; If length is even . so it is our first case we have two choices ; If length is odd . so it is our second case we have only one choice ; Driver code ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static int convertStringToInt ( String s ) { int ans = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == '1' ) ans += 1 << i ; } return ans ; } public static int getNthNumber ( int n ) { Queue < String > q = new Queue < String > ( ) ; if ( n == 1 ) return 1 ; n = n - 1 ; q . Enqueue ( "11" ) ; while ( n -- > 0 ) { String curr = q . Dequeue ( ) ; if ( n == 0 ) return convertStringToInt ( curr ) ; int len = curr . Length ; if ( len % 2 == 0 ) { q . Enqueue ( curr . Substring ( 0 , len / 2 ) + "0" + curr . Substring ( len / 2 ) ) ; q . Enqueue ( curr . Substring ( 0 , len / 2 ) + "1" + curr . Substring ( len / 2 ) ) ; } else { char midChar = curr [ len / 2 ] ; q . Enqueue ( curr . Substring ( 0 , len / 2 ) + midChar + curr . Substring ( len / 2 ) ) ; } } return - 1 ; } public static void Main ( String [ ] args ) { int n = 9 ; Console . WriteLine ( getNthNumber ( n ) ) ; } }


"Find the n | Efficient C # program to find n - th number made of even digits only ; function to find nth number made of even digits only ; If n = 1 return 0 ; vector to store the digits when converted into base 5 ; Reduce n to n - 1 to exclude 0 ; Reduce n to base 5 number and store digits ; pushing the digits into vector ; variable to represent the number after converting it to base 5. Since the digits are be in reverse order , we traverse vector from back ; return 2 * result ( to convert digits 0 , 1 , 2 , 3 , 4 to 0 , 2 , 4 , 6 , 8. ; Driver Code"
Solution in C#:

using System ; using System . Collections ; class GFG { static int findNthEvenDigitNumber ( int n ) { if ( n == 1 ) { return 0 ; } ArrayList v = new ArrayList ( ) ; n = n - 1 ; while ( n > 0 ) { v . Add ( n % 5 ) ; n = n / 5 ; } int result = 0 ; for ( int i = v . Count - 1 ; i >= 0 ; i -- ) { result = result * 10 ; result = result + ( int ) v [ i ] ; } return 2 * result ; } public static void Main ( ) { Console . WriteLine ( findNthEvenDigitNumber ( 2 ) ) ; Console . WriteLine ( findNthEvenDigitNumber ( 10 ) ) ; } }


"Find the n | Efficient C # program to find n - th palindrome ; Construct the nth binary palindrome with the given group number , aux_number and operation type ; No need to insert any bit in the middle ; Length of the final binary representation ; Fill first and last bit as 1 ; Start filling the a [ ] from middle , with the aux_num binary representation ; Get the auxiliary number 's ith bit and  fill around middle ; Insert bit 0 in the middle ; Length of the final binary representation ; Fill first and last bit as 1 ; Start filling the a [ ] from middle , with the aux_num binary representation ; Get the auxiliary number 's ith bit and  fill around middle ; else Insert bit 1 in the middle ; Length of the final binary representation ; Fill first and last bit as 1 ; Start filling the a [ ] from middle , with the aux_num binary representation ; Get the auxiliary number 's ith bit and  fill around middle ; Convert the number to decimal from binary ; Will return the nth binary palindrome number ; Add number of elements in all the groups , until the group of the nth number is found ; Total number of elements until this group ; Element 's offset position in the group ; Finding which bit to be placed in the middle and finding the number , which we will fill from the middle in both directions ; We need to fill this auxiliary number in binary form the middle in both directions ; op = 0 ; Need to Insert 0 at middle ; op = 1 ; Need to Insert 1 at middle ; Driver code ; Function Call"
Solution in C#:

using System ; class GFG { static int INT_SIZE = 32 ; static int constructNthNumber ( int group_no , int aux_num , int op ) { int [ ] a = new int [ INT_SIZE ] ; int num = 0 , len_f ; int i = 0 ; if ( op == 2 ) { len_f = 2 * group_no ; a [ len_f - 1 ] = a [ 0 ] = 1 ; while ( aux_num > 0 ) { a [ group_no + i ] = a [ group_no - 1 - i ] = aux_num & 1 ; aux_num = aux_num >> 1 ; i ++ ; } } else if ( op == 0 ) { len_f = 2 * group_no + 1 ; a [ len_f - 1 ] = a [ 0 ] = 1 ; a [ group_no ] = 0 ; while ( aux_num > 0 ) { a [ group_no + 1 + i ] = a [ group_no - 1 - i ] = aux_num & 1 ; aux_num = aux_num >> 1 ; i ++ ; } } { len_f = 2 * group_no + 1 ; a [ len_f - 1 ] = a [ 0 ] = 1 ; a [ group_no ] = 1 ; while ( aux_num > 0 ) { a [ group_no + 1 + i ] = a [ group_no - 1 - i ] = aux_num & 1 ; aux_num = aux_num >> 1 ; i ++ ; } } for ( i = 0 ; i < len_f ; i ++ ) num += ( 1 << i ) * a [ i ] ; return num ; } static int getNthNumber ( int n ) { int group_no = 0 , group_offset ; int count_upto_group = 0 , count_temp = 1 ; int op , aux_num ; while ( count_temp < n ) { group_no ++ ; count_upto_group = count_temp ; count_temp += 3 * ( 1 << ( group_no - 1 ) ) ; } group_offset = n - count_upto_group - 1 ; if ( ( group_offset + 1 ) <= ( 1 << ( group_no - 1 ) ) ) { aux_num = group_offset ; } else { if ( ( ( group_offset + 1 ) - ( 1 << ( group_no - 1 ) ) ) % 2 == 1 ) else aux_num = ( ( group_offset ) - ( 1 << ( group_no - 1 ) ) ) / 2 ; } return constructNthNumber ( group_no , aux_num , op ) ; } public static void Main ( String [ ] args ) { int n = 9 ; Console . Write ( " { 0 } " , getNthNumber ( n ) ) ; } }


"Find the n | Simple C # program to find the n - th number made of even digits only ; function to calculate nth number made of even digits only ; variable to note how many such numbers have been found till now ; bool variable to check if 1 , 3 , 5 , 7 , 9 is there or not ; checking each digit of the number ; If 1 , 3 , 5 , 7 , 9 is found temp is changed to false ; temp is true it means that it does not have 1 , 3 , 5 , 7 , 9 ; If nth such number is found return it ; Driver code"
Solution in C#:

using System ; class GFG { static int findNthEvenDigitNumber ( int n ) { int count = 0 ; for ( int i = 0 ; ; i ++ ) { int curr = i ; bool isCurrEvenDigit = true ; while ( curr != 0 ) { if ( curr % 10 == 1 curr % 10 == 3 curr % 10 == 5 curr % 10 == 7 curr % 10 == 9 ) isCurrEvenDigit = false ; curr = curr / 10 ; } if ( isCurrEvenDigit == true ) count ++ ; if ( count == n ) return i ; } } public static void Main ( ) { Console . WriteLine ( findNthEvenDigitNumber ( 2 ) ) ; Console . WriteLine ( findNthEvenDigitNumber ( 10 ) ) ; } }


"Find the nearest odd and even perfect squares of odd and even array elements respectively | C # program for the above approach ; Function to find the nearest even and odd perfect squares for even and odd array elements ; Traverse the array ; Calculate square root of current array element ; If both are of same parity ; Otherwise ; Driver Code"
Solution in C#:

using System ; class GFG { static void nearestPerfectSquare ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int sr = ( int ) Math . Sqrt ( arr [ i ] ) ; if ( ( sr & 1 ) == ( arr [ i ] & 1 ) ) Console . Write ( ( sr * sr ) + " ▁ " ) ; else { sr ++ ; Console . Write ( ( sr * sr ) + " ▁ " ) ; } } } static public void Main ( ) { int [ ] arr = { 6 , 3 , 2 , 15 } ; int N = arr . Length ; nearestPerfectSquare ( arr , N ) ; } }


"Find the nearest perfect square for each element of the array | C # program for the above approach ; Function to find the nearest perfect square for every element in the given array ; Traverse the array ; Calculate square root of current element ; Calculate perfect square ; Find the nearest ; Driver Code"
Solution in C#:

using System ; class GFG { static void nearestPerfectSquare ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int sr = ( int ) Math . Sqrt ( arr [ i ] ) ; int a = sr * sr ; int b = ( sr + 1 ) * ( sr + 1 ) ; if ( ( arr [ i ] - a ) < ( b - arr [ i ] ) ) Console . Write ( a + " ▁ " ) ; else Console . Write ( b + " ▁ " ) ; } } public static void Main ( ) { int [ ] arr = { 5 , 2 , 7 , 13 } ; int N = arr . Length ; nearestPerfectSquare ( arr , N ) ; } }


"Find the nearest power of 2 for every array element | C # program to implement the above approach ; Function to find nearest power of two for every array element ; Traverse the array ; Calculate log of the current array element ; Find the nearest ; Driver Code"
Solution in C#:

using System ; class GFG { static void nearestPowerOfTwo ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int lg = ( int ) ( Math . Log ( arr [ i ] ) / Math . Log ( 2 ) ) ; int a = ( int ) ( Math . Pow ( 2 , lg ) ) ; int b = ( int ) ( Math . Pow ( 2 , lg + 1 ) ) ; if ( ( arr [ i ] - a ) < ( b - arr [ i ] ) ) Console . Write ( a + " ▁ " ) ; else Console . Write ( b + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 2 , 7 , 12 } ; int N = arr . Length ; nearestPowerOfTwo ( arr , N ) ; } }


"Find the next Factorial greater than N | C # implementation of the above approach ; Array that stores the factorial till 20 ; Function to pre - compute the factorial till 20 ; Precomputing factorials ; Function to return the next factorial number greater than N ; Traverse the factorial array ; Find the next just greater factorial than N ; Driver Code ; Function to precalculate the factorial till 20 ; Function call"
Solution in C#:

using System ; class GFG { static int [ ] fact = new int [ 21 ] ; static void preCompute ( ) { fact [ 0 ] = 1 ; for ( int i = 1 ; i < 18 ; i ++ ) fact [ i ] = ( fact [ i - 1 ] * i ) ; } static void nextFactorial ( int N ) { for ( int i = 0 ; i < 21 ; i ++ ) { if ( N < fact [ i ] ) { Console . WriteLine ( fact [ i ] ) ; break ; } } } public static void Main ( string [ ] args ) { preCompute ( ) ; int N = 120 ; nextFactorial ( N ) ; } }


"Find the next Non | C # implementation of the approach ; Function to check if a number is perfect square ; Function to check if a number is Fibinacci Number ; N is Fibinacci if either ( 5 * N * N + 4 ) , ( 5 * N * N - 4 ) or both is a perferct square ; Function to find the next Non - Fibinacci Number ; Case 1 If N <= 3 , then 4 will be next Non - Fibinacci Number ; Case 2 If N + 1 is Fibinacci , then N + 2 will be next Non - Fibinacci Number ; If N + 1 is Non - Fibinacci , then N + 2 will be next Non - Fibinacci Number ; Driver code"
Solution in C#:

 using System ; class GFG { static bool isPerfectSquare ( int x ) { int s = ( int ) Math . Sqrt ( x ) ; return ( s * s == x ) ; } static bool isFibonacci ( int N ) { return isPerfectSquare ( 5 * N * N + 4 ) || isPerfectSquare ( 5 * N * N - 4 ) ; } static int nextNonFibonacci ( int N ) { if ( N <= 3 ) return 4 ; if ( isFibonacci ( N + 1 ) ) return N + 2 ; else return N + 1 ; } public static void Main ( String [ ] args ) { int N = 3 ; Console . Write ( nextNonFibonacci ( N ) + "
 " ) ; N = 5 ; Console . Write ( nextNonFibonacci ( N ) + "
 " ) ; N = 7 ; Console . Write ( nextNonFibonacci ( N ) + "
 " ) ; } }


"Find the next fibonacci number | C # implementation of the approach ; Function to return the next fibonacci number ; Driver code"
Solution in C#:

using System ; class GFG { static long nextFibonacci ( int n ) { double a = n * ( 1 + Math . Sqrt ( 5 ) ) / 2.0 ; return ( long ) Math . Round ( a ) ; } public static void Main ( String [ ] args ) { int n = 5 ; Console . WriteLine ( nextFibonacci ( n ) ) ; } }


"Find the next greater element in a Circular Array | Set 2 | C # program for the above approach ; Function to find the NGE for the given circular array [ ] arr ; Initialize stack and nge [ ] array ; Initialize nge [ ] array to - 1 ; Traverse the array ; If stack is not empty and current element is greater than top element of the stack ; Assign next greater element for the top element of the stack ; Pop the top element of the stack ; Print the nge [ ] array ; Driver Code ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void printNGE ( int [ ] arr , int n ) { Stack < int > s = new Stack < int > ( ) ; int [ ] nge = new int [ n ] ; int i = 0 ; for ( i = 0 ; i < n ; i ++ ) { nge [ i ] = - 1 ; } i = 0 ; while ( i < 2 * n ) { while ( s . Count != 0 && arr [ i % n ] > arr [ s . Peek ( ) ] ) { nge [ s . Peek ( ) ] = arr [ i % n ] ; s . Pop ( ) ; } s . Push ( i % n ) ; i ++ ; } for ( i = 0 ; i < n ; i ++ ) { Console . Write ( nge [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , - 2 , 5 , 8 } ; int N = arr . Length ; printNGE ( arr , N ) ; } }


"Find the next number by adding natural numbers in order on alternating indices from last | C # program for the above approach ; Function to generate the resultant number using the given criteria ; Storing end result ; Find the length of numeric string ; Traverse the string ; Storing digit at ith position ; Checking that the number would be added or not ; Logic for summing the digits if the digit is greater than 10 ; Storing the result ; Returning the result ; Driver Code"
Solution in C#:

using System ; public class GFG { public static String generateNumber ( string number ) { int temp = 0 , adding_number = 0 ; string result = " " ; int len = number . Length ; for ( int i = len - 1 ; i >= 0 ; i -- ) { int digit = ( int ) number [ i ] - ( int ) '0' ; if ( temp % 2 == 0 ) { adding_number += 1 ; digit += adding_number ; if ( digit >= 10 ) { digit %= 9 ; if ( digit == 0 ) digit = 9 ; } } result = digit + result ; temp += 1 ; } return result ; } public static void Main ( string [ ] args ) { string S = "1345" ; Console . WriteLine ( generateNumber ( S ) ) ; } }


"Find the node at the center of an N | C # implementation of the above approach ; To create tree ; Function to store the path from given vertex to the target vertex in a vector path ; If the target node is found , push it into path vector ; To prevent visiting a node already visited ; Recursive call to the neighbours of current node inorder to get the path ; Function to obtain and return the farthest node from a given vertex ; If the current height is maximum so far , then save the current node ; Iterate over all the neighbours of current node ; This is to prevent visiting a already visited node ; Next call will be at 1 height higher than our current height ; Function to add edges ; Reset to - 1 ; Reset to - 1 ; Stores one end of the diameter ; Reset the maxHeight ; Stores the second end of the diameter ; Store the diameter into the vector path ; Diameter is equal to the path between the two farthest nodes leaf1 and leaf2 ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static Dictionary < int , List < int > > tree ; static List < int > path ; static int maxHeight , maxHeightNode ; static bool getDiameterPath ( int vertex , int targetVertex , int parent , List < int > path ) { if ( vertex == targetVertex ) { path . Add ( vertex ) ; return true ; } foreach ( int i in tree [ vertex ] ) { if ( i == parent ) continue ; if ( getDiameterPath ( i , targetVertex , vertex , path ) ) { path . Add ( vertex ) ; return true ; } } return false ; } static void farthestNode ( int vertex , int parent , int height ) { if ( height > maxHeight ) { maxHeight = height ; maxHeightNode = vertex ; } if ( tree . ContainsKey ( vertex ) && tree [ vertex ] . Count > 0 ) { foreach ( int i in tree [ vertex ] ) { if ( i == parent ) continue ; farthestNode ( i , vertex , height + 1 ) ; } } } static void addedge ( int a , int b ) { if ( ! tree . ContainsKey ( a ) ) tree [ a ] = new List < int > ( ) ; tree [ a ] . Add ( b ) ; if ( ! tree . ContainsKey ( b ) ) tree [ b ] = new List < int > ( ) ; tree [ b ] . Add ( a ) ; } static void FindCenter ( int n ) { maxHeight = - 1 ; maxHeightNode = - 1 ; farthestNode ( 0 , - 1 , 0 ) ; int leaf1 = maxHeightNode ; maxHeight = - 1 ; farthestNode ( maxHeightNode , - 1 , 0 ) ; int leaf2 = maxHeightNode ; path = new List < int > ( ) ; getDiameterPath ( leaf1 , leaf2 , - 1 , path ) ; int pathSize = path . Count ; if ( pathSize % 2 == 1 ) { Console . WriteLine ( path [ pathSize / 2 ] ) ; } else { Console . WriteLine ( path [ pathSize / 2 ] + " , ▁ " + path [ ( pathSize - 1 ) / 2 ] ) ; } } static void Main ( ) { int N = 4 ; tree = new Dictionary < int , List < int > > ( ) ; addedge ( 1 , 0 ) ; addedge ( 1 , 2 ) ; addedge ( 1 , 3 ) ; FindCenter ( N ) ; } }


"Find the node whose sum with X has maximum set bits | C # implementation of the approach ; number of set bits ; Function to perform dfs to find the maximum value ; If current set bits value is greater than the current maximum ; If count is equal to the maximum then choose the node with minimum value ; Driver code ; Weights of the node ; Edges of the tree"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int maximum = int . MinValue , x , ans = int . MaxValue ; static List < List < int > > graph = new List < List < int > > ( ) ; static List < int > weight = new List < int > ( ) ; static int __builtin_popcount ( int x ) { int c = 0 ; for ( int i = 0 ; i < 60 ; i ++ ) if ( ( ( x >> i ) & 1 ) != 0 ) c ++ ; return c ; } static void dfs ( int node , int parent ) { int a = __builtin_popcount ( weight [ node ] + x ) ; if ( maximum < a ) { maximum = a ; ans = node ; } else if ( maximum == a ) ans = Math . Min ( ans , node ) ; for ( int i = 0 ; i < graph [ node ] . Count ; i ++ ) { if ( graph [ node ] [ i ] == parent ) continue ; dfs ( graph [ node ] [ i ] , node ) ; } } public static void Main ( ) { x = 15 ; weight . Add ( 0 ) ; weight . Add ( 5 ) ; weight . Add ( 10 ) ; weight . Add ( 11 ) ; ; weight . Add ( 8 ) ; weight . Add ( 6 ) ; for ( int i = 0 ; i < 100 ; i ++ ) graph . Add ( new List < int > ( ) ) ; graph [ 1 ] . Add ( 2 ) ; graph [ 2 ] . Add ( 3 ) ; graph [ 2 ] . Add ( 4 ) ; graph [ 1 ] . Add ( 5 ) ; dfs ( 1 , 1 ) ; Console . Write ( ans ) ; } }


"Find the node with minimum value in a Binary Search Tree | C # program to find minimum value node in Binary Search Tree ; A binary tree node ; Given a binary search tree and a number , inserts a new node with the given number in the correct place in the tree . Returns the new root pointer which the caller should then use ( the standard trick to avoid using reference parameters ) . ; 1. If the tree is empty , return a new , single node ; 2. Otherwise , recur down the tree ; return the ( unchanged ) node pointer ; Given a non - empty binary search tree , return the minimum data value found in that tree . Note that the entire tree does not need to be searched . ; loop down to find the leftmost leaf ; Driver program to test above functions"
Solution in C#:

using System ; public class Node { public int data ; public Node left , right ; public Node ( int d ) { data = d ; left = right = null ; } } public class BinaryTree { public static Node head ; public virtual Node insert ( Node node , int data ) { if ( node == null ) { return ( new Node ( data ) ) ; } else { if ( data <= node . data ) { node . left = insert ( node . left , data ) ; } else { node . right = insert ( node . right , data ) ; } return node ; } } public virtual int minvalue ( Node node ) { Node current = node ; while ( current . left != null ) { current = current . left ; } return ( current . data ) ; } public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; Node root = null ; root = tree . insert ( root , 4 ) ; tree . insert ( root , 2 ) ; tree . insert ( root , 1 ) ; tree . insert ( root , 3 ) ; tree . insert ( root , 6 ) ; tree . insert ( root , 5 ) ; Console . WriteLine ( " Minimum ▁ value ▁ of ▁ BST ▁ is ▁ " + tree . minvalue ( root ) ) ; } }


"Find the non decreasing order array from given array | C # implementation of the approach ; Utility function to print the contents of the array ; Function to build array B [ ] ; Lower and upper limits ; To store the required array ; Apply greedy approach ; Print the built array b [ ] ; Driver code"
Solution in C#:

using System ; public class GFG { static void printArr ( int [ ] b , int n ) { for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( " ▁ " + b [ i ] + " ▁ " ) ; } } static void ModifiedArray ( int [ ] a , int n ) { int l = 0 , r = int . MaxValue ; int [ ] b = new int [ n ] ; for ( int i = 0 ; i < n / 2 ; i ++ ) { b [ i ] = Math . Max ( l , a [ i ] - r ) ; b [ n - i - 1 ] = a [ i ] - b [ i ] ; l = b [ i ] ; r = b [ n - i - 1 ] ; } printArr ( b , n ) ; } static public void Main ( ) { int [ ] a = { 5 , 6 } ; int n = a . Length ; ModifiedArray ( a , 2 * n ) ; } }


"Find the nth term of the given series | C # implementation of the approach ; Function to return the nth term of the given series ; Driver code"
Solution in C#:

using System ; class GFG { static int oddTriangularNumber ( int N ) { return ( N * ( ( 2 * N ) - 1 ) ) ; } public static void Main ( String [ ] args ) { int N = 3 ; Console . WriteLine ( oddTriangularNumber ( N ) ) ; } }


"Find the nth term of the series 0 , 8 , 64 , 216 , 512 , . . . | C # implementation of the approach ; Function to return the nth term of the given series ; Common difference and first term ; First term ; nth term ; nth term of the given series ; Driver code"
Solution in C#:

using System ; public class GFG { static int nthTerm ( int n ) { int d = 2 ; int a1 = 0 ; int An = a1 + ( n - 1 ) * d ; return ( int ) Math . Pow ( An , 3 ) ; } public static void Main ( ) { int n = 5 ; Console . WriteLine ( nthTerm ( n ) ) ; } }


"Find the number after successive division | C # implementation of the above approach ; Function to find the number ; Driver Code"
Solution in C#:

using System ; class GFG { static int findNum ( int [ ] div , int [ ] rem , int N ) { int num = rem [ N - 1 ] ; for ( int i = N - 2 ; i >= 0 ; i -- ) { num = num * div [ i ] + rem [ i ] ; } return num ; } static public void Main ( ) { int [ ] div = { 8 , 3 } ; int [ ] rem = { 2 , 2 } ; int N = div . Length ; Console . WriteLine ( findNum ( div , rem , N ) ) ; } }


"Find the number closest to n and divisible by m | C # implementation to find the number closest to n and divisible by m ; function to find the number closest to n and divisible by m ; find the quotient ; 1 st possible closest number ; 2 nd possible closest number ; if true , then n1 is the required closest number ; else n2 is the required closest number ; Driver program to test above"
Solution in C#:

using System ; class GFG { static int closestNumber ( int n , int m ) { int q = n / m ; int n1 = m * q ; int n2 = ( n * m ) > 0 ? ( m * ( q + 1 ) ) : ( m * ( q - 1 ) ) ; if ( Math . Abs ( n - n1 ) < Math . Abs ( n - n2 ) ) return n1 ; return n2 ; } public static void Main ( ) { int n = 13 , m = 4 ; Console . WriteLine ( closestNumber ( n , m ) ) ; n = - 15 ; m = 6 ; Console . WriteLine ( closestNumber ( n , m ) ) ; n = 0 ; m = 8 ; Console . WriteLine ( closestNumber ( n , m ) ) ; n = 18 ; m = - 7 ; Console . WriteLine ( closestNumber ( n , m ) ) ; } }


"Find the number in a range having maximum product of the digits | C # Program to find the number in a range having maximum product of the digits ; Returns the product of digits of number x ; This function returns the number having maximum product of the digits ; Converting both integers to strings string a = l . ToString ( ) ; ; Let the current answer be r ; Stores the current number having current digit one less than current digit in b ; Replace all following digits with 9 to maximise the product ; Convert string to number ; Check if it lies in range and its product is greater than max product ; Driver Code"
Solution in C#:

using System ; class GFG { static int product ( int x ) { int prod = 1 ; while ( x > 0 ) { prod *= ( x % 10 ) ; x /= 10 ; } return prod ; } static int findNumber ( int l , int r ) { string b = r . ToString ( ) ; int ans = r ; for ( int i = 0 ; i < b . Length ; i ++ ) { if ( b [ i ] == '0' ) continue ; char [ ] curr = b . ToCharArray ( ) ; curr [ i ] = ( char ) ( ( ( int ) ( curr [ i ] - ( int ) '0' ) - 1 ) + ( int ) ( '0' ) ) ; for ( int j = i + 1 ; j < curr . Length ; j ++ ) curr [ j ] = '9' ; int num = 0 ; for ( int j = 0 ; j < curr . Length ; j ++ ) num = num * 10 + ( curr [ j ] - '0' ) ; if ( num >= l && product ( ans ) < product ( num ) ) ans = num ; } return ans ; } static void Main ( ) { int l = 1 , r = 10 ; Console . WriteLine ( findNumber ( l , r ) ) ; l = 51 ; r = 62 ; Console . WriteLine ( findNumber ( l , r ) ) ; } }


"Find the number obtained after concatenation of binary representation of M and N | C # implementation of the approach ; Function to convert decimal number n to its binary representation stored as an array [ ] arr ; Funtion to convert the number represented as a binary array [ ] arr into its decimal equivalent ; Function to concatenate the binary numbers and return the decimal result ; Number of bits in both the numbers ; Convert the bits in both the integers to the arrays [ ] a and [ ] b ; c [ ] will be the binary array for the result ; Update the c [ ] array ; Return the decimal equivalent of the result ; Driver code"
Solution in C#:

using System ; class GFG { static void decBinary ( int [ ] arr , int n ) { int k = ( int ) ( Math . Log ( n ) / Math . Log ( 2 ) ) ; while ( n > 0 ) { arr [ k -- ] = n % 2 ; n /= 2 ; } } static int binaryDec ( int [ ] arr , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) ans += arr [ i ] << ( n - i - 1 ) ; return ans ; } static int concat ( int m , int n ) { int k = ( int ) ( Math . Log ( m ) / Math . Log ( 2 ) ) + 1 ; int l = ( int ) ( Math . Log ( n ) / Math . Log ( 2 ) ) + 1 ; int [ ] a = new int [ k ] ; int [ ] b = new int [ l ] ; int [ ] c = new int [ k + l ] ; decBinary ( a , m ) ; decBinary ( b , n ) ; int iN = 0 ; for ( int i = 0 ; i < k ; i ++ ) c [ iN ++ ] = a [ i ] ; for ( int i = 0 ; i < l ; i ++ ) c [ iN ++ ] = b [ i ] ; return ( binaryDec ( c , k + l ) ) ; } public static void Main ( String [ ] args ) { int m = 4 , n = 5 ; Console . WriteLine ( concat ( m , n ) ) ; } }


"Find the number obtained after concatenation of binary representation of M and N | C # implementation of the approach ; Utility function to calculate binary length of a number . ; Function to concatenate the binary numbers and return the decimal result ; Find binary length of n ; left binary shift m and then add n ; Driver code"
Solution in C#:

using System ; class GFG { static int getBinaryLength ( int n ) { int length = 0 ; while ( n > 0 ) { length += 1 ; n /= 2 ; } return length ; } static int concat ( int m , int n ) { int length = getBinaryLength ( n ) ; return ( m << length ) + n ; } static void Main ( ) { int m = 4 , n = 5 ; Console . WriteLine ( concat ( m , n ) ) ; } }


"Find the number of Islands | Set 2 ( Using Disjoint Set ) | C # program to find number of islands using Disjoint Set data structure . ; Class to represent Disjoint Set Data structure ; Initially , all elements are in their own set . ; Finds the representative of the set that x is an element of ; if x is not the parent of itself , then x is not the representative of its set . so we recursively call Find on its parent and move i 's node directly under the  representative of this set ; Unites the set that includes x and the set that includes y ; Find the representatives ( or the root nodes ) for x an y ; Elements are in the same set , no need to unite anything . ; If x ' s ▁ rank ▁ is ▁ less ▁ than ▁ y ' s rank Then move x under y so that depth of tree remains less ; Else if y ' s ▁ rank ▁ is ▁ less ▁ than ▁ x ' s rank Then move y under x so that depth of tree remains less ; Else if their ranks are the same ; Then move y under x ( doesn 't matter which one goes where) ; And increment the result tree 's  rank by 1 ; Returns number of islands in [ , ] a ; The following loop checks for its neighbours and unites the indexes if both are 1. ; If cell is 0 , nothing to do ; Check all 8 neighbours and do a union with neighbour 's set if neighbour is  also 1 ; Array to note down frequency of each set ; If frequency of set is 0 , increment numberOfIslands ; Driver Code"
Solution in C#:

using System ; class GFG { class DisjointUnionSets { int [ ] rank , parent ; int n ; public DisjointUnionSets ( int n ) { rank = new int [ n ] ; parent = new int [ n ] ; this . n = n ; makeSet ( ) ; } public void makeSet ( ) { for ( int i = 0 ; i < n ; i ++ ) parent [ i ] = i ; } public int find ( int x ) { if ( parent [ x ] != x ) { return find ( parent [ x ] ) ; } return x ; } public void union ( int x , int y ) { int xRoot = find ( x ) ; int yRoot = find ( y ) ; if ( xRoot == yRoot ) return ; if ( rank [ xRoot ] < rank [ yRoot ] ) parent [ xRoot ] = yRoot ; else if ( rank [ yRoot ] < rank [ xRoot ] ) parent [ yRoot ] = xRoot ; else { parent [ yRoot ] = xRoot ; rank [ xRoot ] = rank [ xRoot ] + 1 ; } } } static int countIslands ( int [ , ] a ) { int n = a . GetLength ( 0 ) ; int m = a . GetLength ( 1 ) ; DisjointUnionSets dus = new DisjointUnionSets ( n * m ) ; for ( int j = 0 ; j < n ; j ++ ) { for ( int k = 0 ; k < m ; k ++ ) { if ( a [ j , k ] == 0 ) continue ; if ( j + 1 < n && a [ j + 1 , k ] == 1 ) dus . union ( j * ( m ) + k , ( j + 1 ) * ( m ) + k ) ; if ( j - 1 >= 0 && a [ j - 1 , k ] == 1 ) dus . union ( j * ( m ) + k , ( j - 1 ) * ( m ) + k ) ; if ( k + 1 < m && a [ j , k + 1 ] == 1 ) dus . union ( j * ( m ) + k , ( j ) * ( m ) + k + 1 ) ; if ( k - 1 >= 0 && a [ j , k - 1 ] == 1 ) dus . union ( j * ( m ) + k , ( j ) * ( m ) + k - 1 ) ; if ( j + 1 < n && k + 1 < m && a [ j + 1 , k + 1 ] == 1 ) dus . union ( j * ( m ) + k , ( j + 1 ) * ( m ) + k + 1 ) ; if ( j + 1 < n && k - 1 >= 0 && a [ j + 1 , k - 1 ] == 1 ) dus . union ( j * m + k , ( j + 1 ) * ( m ) + k - 1 ) ; if ( j - 1 >= 0 && k + 1 < m && a [ j - 1 , k + 1 ] == 1 ) dus . union ( j * m + k , ( j - 1 ) * m + k + 1 ) ; if ( j - 1 >= 0 && k - 1 >= 0 && a [ j - 1 , k - 1 ] == 1 ) dus . union ( j * m + k , ( j - 1 ) * m + k - 1 ) ; } } int [ ] c = new int [ n * m ] ; int numberOfIslands = 0 ; for ( int j = 0 ; j < n ; j ++ ) { for ( int k = 0 ; k < m ; k ++ ) { if ( a [ j , k ] == 1 ) { int x = dus . find ( j * m + k ) ; if ( c [ x ] == 0 ) {...


"Find the number of binary strings of length N with at least 3 consecutive 1 s | C # implementation of the above approach ; Function to return the count of required strings ; '0' at ith position ; '1' at ith position ; Driver code ; Base condition : 2 ^ ( i + 1 ) because of 0 indexing"
Solution in C#:

using System ; class GFG { static int n ; static int solve ( int i , int x , int [ , ] dp ) { if ( i < 0 ) { return x == 3 ? 1 : 0 ; } if ( dp [ i , x ] != - 1 ) { return dp [ i , x ] ; } dp [ i , x ] = solve ( i - 1 , 0 , dp ) ; dp [ i , x ] += solve ( i - 1 , x + 1 , dp ) ; return dp [ i , x ] ; } public static void Main ( String [ ] args ) { n = 4 ; int [ , ] dp = new int [ n , 4 ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < 4 ; j ++ ) { dp [ i , j ] = - 1 ; } } for ( int i = 0 ; i < n ; i ++ ) { dp [ i , 3 ] = ( 1 << ( i + 1 ) ) ; } Console . Write ( solve ( n - 1 , 0 , dp ) ) ; } }


"Find the number of binary strings of length N with at least 3 consecutive 1 s | C # implementation of the approach value x ; Function that returns true if s contains three consecutive 1 's ; Function to return the count of required strings ; Driver code"
Solution in C#:

using System ; class GFG { static bool check ( String s ) { int n = s . Length ; for ( int i = 2 ; i < n ; i ++ ) { if ( s [ i ] == '1' && s [ i - 1 ] == '1' && s [ i - 2 ] == '1' ) return true ; } return false ; } static int countStr ( int i , String s ) { if ( i < 0 ) { if ( check ( s ) ) return 1 ; return 0 ; } char [ ] myNameChars = s . ToCharArray ( ) ; myNameChars [ i ] = '0' ; s = String . Join ( " " , myNameChars ) ; int ans = countStr ( i - 1 , s ) ; char [ ] myChar = s . ToCharArray ( ) ; myChar [ i ] = '1' ; s = String . Join ( " " , myChar ) ; ans += countStr ( i - 1 , s ) ; char [ ] myChar1 = s . ToCharArray ( ) ; myChar1 [ i ] = '0' ; s = String . Join ( " " , myChar1 ) ; return ans ; } public static void Main ( String [ ] args ) { int N = 4 ; String s = "0000" ; Console . WriteLine ( countStr ( N - 1 , s ) ) ; } }


"Find the number of consecutive zero at the end after multiplying n numbers | C # program to find the number of consecutive zero at the end after multiplying n numbers ; Function to count two 's factor ; Count number of 2 s present in n ; Function to count five 's  factor ; Function to count number of zeros ; Count the two 's factor  of n number ; Count the five 's factor  of n number ; Return the minimum ; driver function"
Solution in C#:

using System ; public class GfG { static int two_factor ( int n ) { int twocount = 0 ; while ( n % 2 == 0 ) { twocount ++ ; n = n / 2 ; } return twocount ; } static int five_factor ( int n ) { int fivecount = 0 ; while ( n % 5 == 0 ) { fivecount ++ ; n = n / 5 ; } return fivecount ; } static int find_con_zero ( int [ ] arr , int n ) { int twocount = 0 ; int fivecount = 0 ; for ( int i = 0 ; i < n ; i ++ ) { twocount += two_factor ( arr [ i ] ) ; fivecount += five_factor ( arr [ i ] ) ; } if ( twocount < fivecount ) return twocount ; else return fivecount ; } public static void Main ( ) { int [ ] arr = { 100 , 10 , 5 , 25 , 35 , 14 } ; int n = 6 ; Console . WriteLine ( find_con_zero ( arr , n ) ) ; } }


"Find the number of different numbers in the array after applying the given operation q times | C # implementation for above approach ; To store the tree in lazy propagation ; To store the different numbers ; Function to update in the range [ x , y ) with given value ; check out of bound ; check for complete overlap ; find the mid number ; check for pending updates ; make lazy [ id ] = 0 , so that it has no pending updates ; call for two child nodes ; Function to find non - zero integers in the range [ l , r ) ; if id contains positive number ; There is no need to see the children , because all the interval have same number ; check for out of bound ; find the middle number ; call for two child nodes ; Driver Code ; size of the array and number of queries ; Update operation for l , r , x , id , 0 , n ; Query operation to get answer in the range [ 0 , n - 1 ] ; Print the count of non - zero elements"
Solution in C#:

using System ; using System . Collections . Generic ; public class geeks { static int N = 100005 ; static int [ ] lazy = new int [ 4 * N ] ; static HashSet < int > se = new HashSet < int > ( ) ; public static void update ( int x , int y , int value , int id , int l , int r ) { if ( x >= r l >= y ) return ; if ( x <= l && r <= y ) { lazy [ id ] = value ; return ; } int mid = ( l + r ) / 2 ; if ( lazy [ id ] != 0 ) lazy [ 2 * id ] = lazy [ 2 * id + 1 ] = lazy [ id ] ; lazy [ id ] = 0 ; update ( x , y , value , 2 * id , l , mid ) ; update ( x , y , value , 2 * id + 1 , mid , r ) ; } public static void query ( int id , int l , int r ) { if ( lazy [ id ] != 0 ) { se . Add ( lazy [ id ] ) ; return ; } if ( r - l < 2 ) return ; int mid = ( l + r ) / 2 ; query ( 2 * id , l , mid ) ; query ( 2 * id + 1 , mid , r ) ; } public static void Main ( String [ ] args ) { int n = 5 , q = 3 ; update ( 1 , 4 , 1 , 1 , 0 , n ) ; update ( 0 , 2 , 2 , 1 , 0 , n ) ; update ( 3 , 4 , 3 , 1 , 0 , n ) ; query ( 1 , 0 , n ) ; Console . WriteLine ( se . Count ) ; } }


"Find the number of distinct pairs of vertices which have a distance of exactly k in a tree | C # implementation of the approach ; To store vertices and value of k ; To store number vertices at a level i ; To store the readonly answer ; Function to add an edge between two nodes ; Function to find the number of distinct pairs of the vertices which have a distance of exactly k in a tree ; At level zero vertex itself is counted ; Count the pair of vertices at distance k ; For all levels count vertices ; Driver code ; Add edges ; Function call ; Required answer"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static readonly int N = 5005 ; static int n , k ; static List < int > [ ] gr = new List < int > [ N ] ; static int [ , ] d = new int [ N , 505 ] ; static int ans = 0 ; static void Add_edge ( int x , int y ) { gr [ x ] . Add ( y ) ; gr [ y ] . Add ( x ) ; } static void dfs ( int v , int par ) { d [ v , 0 ] = 1 ; foreach ( int i in gr [ v ] ) { if ( i != par ) { dfs ( i , v ) ; for ( int j = 1 ; j <= k ; j ++ ) ans += d [ i , j - 1 ] * d [ v , k - j ] ; for ( int j = 1 ; j <= k ; j ++ ) d [ v , j ] += d [ i , j - 1 ] ; } } } public static void Main ( String [ ] args ) { n = 5 ; k = 2 ; for ( int i = 0 ; i < N ; i ++ ) gr [ i ] = new List < int > ( ) ; Add_edge ( 1 , 2 ) ; Add_edge ( 2 , 3 ) ; Add_edge ( 3 , 4 ) ; Add_edge ( 2 , 5 ) ; dfs ( 1 , 0 ) ; Console . Write ( ans ) ; } }


"Find the number of divisors of all numbers in the range [ 1 , n ] | C # implementation of the approach ; Function to find the number of divisors of all numbers in the range [ 1 , n ] ; Array to store the count of divisors ; For every number from 1 to n ; Increase divisors count for every number divisible by i ; Print the divisors ; Driver code"
Solution in C#:

using System ; class GFG { static void findDivisors ( int n ) { int [ ] div = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j * i <= n ; j ++ ) div [ i * j ] ++ ; } for ( int i = 1 ; i <= n ; i ++ ) Console . Write ( div [ i ] + " ▁ " ) ; } static void Main ( ) { int n = 10 ; findDivisors ( n ) ; } }


"Find the number of elements X such that X + K also exists in the array | C # implementation of find number of elements x in this array such x + k also present in this array . ; Function to return the count of element x such that x + k also lies in this array ; Key in map will store elements and value will store the frequency of the elements ; Find if i . first + K is present in this map or not ; If we find i . first or key + K in this map then we have to increase in answer the frequency of this element ; Driver code ; array initialisation ; size of array ; initialise k"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { static int count_element ( int N , int K , int [ ] arr ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; ++ i ) if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1 ; } else { mp . Add ( arr [ i ] , 1 ) ; } int answer = 0 ; foreach ( KeyValuePair < int , int > i in mp ) { if ( mp . ContainsKey ( i . Key + K ) ) answer += i . Value ; } return answer ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 6 , 2 , 8 , 7 , 6 , 5 , 9 } ; int N = arr . Length ; int K = 2 ; Console . Write ( count_element ( N , K , arr ) ) ; } }


"Find the number of elements greater than k in a sorted array | C # implementation of the approach ; Function to return the count of elements from the array which are greater than k ; Stores the index of the left most element from the array which is greater than k ; Finds number of elements greater than k ; If mid element is greater than k update leftGreater and r ; If mid element is less than or equal to k update l ; Return the count of elements greater than k ; Driver code"
Solution in C#:

using System ; class GFG { static int countGreater ( int [ ] arr , int n , int k ) { int l = 0 ; int r = n - 1 ; int leftGreater = n ; while ( l <= r ) { int m = l + ( r - l ) / 2 ; if ( arr [ m ] > k ) { leftGreater = m ; r = m - 1 ; } else l = m + 1 ; } return ( n - leftGreater ) ; } public static void Main ( ) { int [ ] arr = { 3 , 3 , 4 , 7 , 7 , 7 , 11 , 13 , 13 } ; int n = arr . Length ; int k = 7 ; Console . WriteLine ( countGreater ( arr , n , k ) ) ; } }


"Find the number of good permutations | C # implementation of the above approach . ; Function to return the count of good permutations ; For m = 0 , ans is 1 ; If k is greater than 1 ; If k is greater than 2 ; If k is greater than 3 ; Driver code"
Solution in C#:

using System ; class GFG { static int Permutations ( int n , int k ) { int ans = 1 ; if ( k >= 2 ) ans += ( n ) * ( n - 1 ) / 2 ; if ( k >= 3 ) ans += ( n ) * ( n - 1 ) * ( n - 2 ) * 2 / 6 ; if ( k >= 4 ) ans += ( n ) * ( n - 1 ) * ( n - 2 ) * ( n - 3 ) * 9 / 24 ; return ans ; } public static void Main ( ) { int n = 5 , k = 2 ; Console . WriteLine ( Permutations ( n , k ) ) ; } }


"Find the number of integers from 1 to n which contains digits 0 ' s ▁ and ▁ 1' s only | C # program to find the number of integers from 1 to n which contains digits 0 ' s ▁ and ▁ 1' s only ; Function to find the number of integers from 1 to n which contains 0 ' s ▁ and ▁ 1' s only ; If number is greater than n ; otherwise add count this number and call two functions ; Driver code"
Solution in C#:

using System ; class GFG { static int countNumbers ( int x , int n ) { if ( x > n ) return 0 ; return 1 + countNumbers ( x * 10 , n ) + countNumbers ( x * 10 + 1 , n ) ; } public static void Main ( ) { int n = 120 ; Console . WriteLine ( countNumbers ( 1 , n ) ) ; } }


"Find the number of integers x in range ( 1 , N ) for which x and x + 1 have same number of divisors | C # implementation of the approach ; To store number of divisors and Prefix sum of such numbers ; Function to find the number of integers 1 < x < N for which x and x + 1 have the same number of positive divisors ; Count the number of divisors ; Run a loop upto sqrt ( i ) ; If j is divisor of i ; If it is perfect square ; x and x + 1 have same number of positive divisors ; Driver code ; Function call ; Required answer"
Solution in C#:

using System ; class GFG { static int N = 100005 ; static int [ ] d = new int [ N ] ; static int [ ] pre = new int [ N ] ; static void Positive_Divisors ( ) { for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 1 ; j * j <= i ; j ++ ) { if ( i % j == 0 ) { if ( j * j == i ) d [ i ] ++ ; else d [ i ] += 2 ; } } } int ans = 0 ; for ( int i = 2 ; i < N ; i ++ ) { if ( d [ i ] == d [ i - 1 ] ) ans ++ ; pre [ i ] = ans ; } } public static void Main ( String [ ] args ) { Positive_Divisors ( ) ; int n = 15 ; Console . WriteLine ( pre [ n ] ) ; } }


"Find the number of jumps to reach X in the number line from zero | C # program to find the number of jumps to reach X in the number line from zero ; Utility function to calculate sum of numbers from 1 to x ; Function to find the number of jumps to reach X in the number line from zero ; First make number positive Answer will be same either it is Positive or negative ; To store required answer ; Continue till number is lesser or not in same parity ; Return the required answer ; Driver code"
Solution in C#:

using System ; class GFG { static int getsum ( int x ) { return ( x * ( x + 1 ) ) / 2 ; } static int countJumps ( int n ) { n = Math . Abs ( n ) ; int ans = 0 ; while ( getsum ( ans ) < n || ( ( getsum ( ans ) - n ) & 1 ) > 0 ) ans ++ ; return ans ; } static void Main ( ) { int n = 9 ; Console . WriteLine ( countJumps ( n ) ) ; } }


"Find the number of occurrences of a character upto preceding position | C # program to find the number of occurrences of a character at position P upto p - 1 ; Function to find the number of occurrences of a character at position P upto p - 1 ; Iterate over the string ; Store the Occurrence of same character ; Increase its frequency ; Return the required count ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { static int countOccurrence ( String s , int position ) { int [ ] alpha = new int [ 26 ] ; int [ ] b = new int [ s . Length ] ; for ( int i = 0 ; i < s . Length ; i ++ ) { b [ i ] = alpha [ ( int ) s [ i ] - 97 ] ; alpha [ ( int ) s [ i ] - 97 ] ++ ; } return b [ position - 1 ] ; } public static void Main ( String [ ] args ) { String s = " ababababab " ; int p = 9 ; Console . WriteLine ( countOccurrence ( s , p ) ) ; } }


"Find the number of occurrences of a character upto preceding position | C # program to find the number of occurrences of a character at position P upto p - 1 ; Function to find the number of occurrences of a character at position P upto p - 1 ; Return the required count ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { static int Occurrence ( String s , int position ) { int count = 0 ; for ( int i = 0 ; i < position - 1 ; i ++ ) if ( s [ i ] == s [ position - 1 ] ) count ++ ; return count ; } public static void Main ( String [ ] args ) { String s = " ababababab " ; int p = 9 ; Console . WriteLine ( Occurrence ( s , p ) ) ; } }


"Find the number of ordered pairs such that a * p + b * q = N , where p and q are primes | C # program to find the number of ordered pairs such that a * p + b * q = N where p and q are primes ; Sieve of erastothenes to store the prime numbers and their frequency in form a * p + b * q ; Performing Sieve of Eratosthenes to find the prime numbers unto 10001 ; Loop to find the number of ordered pairs for every combination of the prime numbers ; Driver code ; Printing the number of ordered pairs for every query"
Solution in C#:

using System ; class GFG { static int size = 10001 ; static int [ ] prime = new int [ size ] ; static int [ ] freq = new int [ size ] ; static void sieve ( int a , int b ) { prime [ 1 ] = 1 ; for ( int i = 2 ; i * i < size ; i ++ ) { if ( prime [ i ] == 0 ) { for ( int j = i * 2 ; j < size ; j += i ) prime [ j ] = 1 ; } } for ( int p = 1 ; p < size ; p ++ ) { for ( int q = 1 ; q < size ; q ++ ) { if ( prime [ p ] == 0 && prime [ q ] == 0 && a * p + b * q < size ) { freq [ a * p + b * q ] ++ ; } } } } public static void Main ( string [ ] args ) { int queries = 2 , a = 1 , b = 2 ; sieve ( a , b ) ; int [ ] arr = { 15 , 25 } ; for ( int i = 0 ; i < queries ; i ++ ) { Console . Write ( freq [ arr [ i ] ] + " ▁ " ) ; } } }


"Find the number of players who roll the dice when the dice output sequence is given | C # implementation of the approach ; Function to return the number of players ; Initialize cnt as 0 ; Iterate in the string ; Check for numbers other than x ; Driver code"
Solution in C#:

using System ; class GFG { static int findM ( String s , int x ) { int cnt = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] - '0' != x ) cnt ++ ; } return cnt ; } public static void Main ( ) { String s = "3662123" ; int x = 6 ; Console . Write ( findM ( s , x ) ) ; } }


"Find the number of positive integers less than or equal to N that have an odd number of digits | C # implementation of the approach ; Function to return the number of positive integers less than or equal to N that have odd number of digits ; Driver code"
Solution in C#:

using System ; class GFG { static int odd_digits ( int n ) { if ( n < 10 ) return n ; else if ( n / 10 < 10 ) return 9 ; else if ( n / 100 < 10 ) return 9 + n - 99 ; else if ( n / 1000 < 10 ) return 9 + 900 ; else if ( n / 10000 < 10 ) return 909 + n - 9999 ; else return 90909 ; } public static void Main ( String [ ] args ) { int n = 893 ; Console . WriteLine ( odd_digits ( n ) ) ; } }


"Find the number of rectangles of size 2 * 1 which can be placed inside a rectangle of size n * m | C # program to Find the number of rectangles of size 2 * 1 can be placed inside a rectangle of size n * m ; function to Find the number of rectangles of size 2 * 1 can be placed inside a rectangle of size n * m ; if n is even ; if m is even ; if both are odd ; Driver Code ; function call"
Solution in C#:

using System ; class GFG { static int NumberOfRectangles ( int n , int m ) { if ( n % 2 == 0 ) return ( n / 2 ) * m ; else if ( m % 2 == 0 ) return ( m / 2 ) * n ; return ( n * m - 1 ) / 2 ; } public static void Main ( ) { int n = 3 , m = 3 ; Console . WriteLine ( NumberOfRectangles ( n , m ) ) ; } }


"Find the number of solutions to the given equation | C # implementation of the approach ; Function to return the count of valid values of X ; Iterate through all possible sum of digits of X ; Get current value of X for sum of digits i ; Find sum of digits of cr ; If cr is a valid choice for X ; Return the count ; Driver code"
Solution in C#:

using System ; class GFG { static int getCount ( int a , int b , int c ) { int count = 0 ; for ( int i = 1 ; i <= 81 ; i ++ ) { int cr = b * ( int ) Math . Pow ( i , a ) + c ; int tmp = cr ; int sm = 0 ; while ( tmp != 0 ) { sm += tmp % 10 ; tmp /= 10 ; } if ( sm == i && cr < 1e9 ) count ++ ; } return count ; } public static void Main ( ) { int a = 3 , b = 2 , c = 8 ; Console . Write ( getCount ( a , b , c ) ) ; } }


"Find the number of spectators standing in the stadium at time t | C # program to find number of spectators standing at a time ; If the time is less than k then we can print directly t time . ; If the time is n then k spectators are standing . ; Otherwise we calculate the spectators standing . ; Driver code ; Stores the value of n , k and t t is time n & k is the number of specators"
Solution in C#:

using System ; class GFG { static void result ( long n , long k , long t ) { if ( t <= k ) Console . WriteLine ( t ) ; else if ( t <= n ) Console . WriteLine ( k ) ; else { long temp = t - n ; temp = k - temp ; Console . WriteLine ( temp ) ; } } public static void Main ( ) { long n , k , t ; n = 10 ; k = 5 ; t = 12 ; result ( n , k , t ) ; } }


"Find the number of squares inside the given square grid | C # implementation of the approach ; Function to return the number of squares inside an n * n grid ; Driver code"
Solution in C#:

using System ; class GFG { static int cntSquares ( int n ) { return n * ( n + 1 ) * ( 2 * n + 1 ) / 6 ; } public static void Main ( String [ ] args ) { Console . Write ( cntSquares ( 4 ) ) ; } }


"Find the number of stair steps | C # program to find the number of steps ; Modified Binary search function to solve the equation ; if mid is solution to equation ; if our solution to equation lies between mid and mid - 1 ; if solution to equation is greater than mid ; if solution to equation is less than mid ; Driver function ; call binary search method to solve for limits 1 to T ; Because our pattern starts from 2 , 3 , 4 , 5. . . so , we subtract 1 from ans"
Solution in C#:

using System ; public class GfG { public static int solve ( int low , int high , int T ) { while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( ( mid * ( mid + 1 ) ) == T ) return mid ; if ( mid > 0 && ( mid * ( mid + 1 ) ) > T && ( mid * ( mid - 1 ) ) <= T ) return mid - 1 ; if ( ( mid * ( mid + 1 ) ) > T ) high = mid - 1 ; else low = mid + 1 ; } return - 1 ; } public static void Main ( ) { int T = 15 ; int ans = solve ( 1 , T , 2 * T ) ; if ( ans != - 1 ) ans -- ; Console . WriteLine ( " Number ▁ of ▁ stair ▁ steps ▁ = ▁ " + ans ) ; } }


"Find the number of strings formed using distinct characters of a given string | C # implementation of the approach ; Function to return the factorial of n ; Function to return the count of all possible strings that can be formed with the characters of the given string without repeating characters ; To store the distinct characters of the string str ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int fact ( int n ) { int fact = 1 ; for ( int i = 1 ; i <= n ; i ++ ) fact *= i ; return fact ; } static int countStrings ( String str , int n ) { HashSet < char > distinct_char = new HashSet < char > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { distinct_char . Add ( str [ i ] ) ; } return fact ( distinct_char . Count ) ; } public static void Main ( String [ ] args ) { String str = " geeksforgeeks " ; int n = str . Length ; Console . WriteLine ( countStrings ( str , n ) ) ; } }


"Find the number of sub arrays in the permutation of first N natural numbers such that their median is M | C # implementation of the approach ; Function to return the count of sub - arrays in the given permutation of first n natural numbers such that their median is m ; If element is less than m ; If element greater than m ; If m is found ; Count the answer ; Increment sum ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static int segments ( int n , int [ ] p , int m ) { Dictionary < int , int > c = new Dictionary < int , int > ( ) ; c . Add ( 0 , 1 ) ; bool has = false ; int sum = 0 ; int ans = 0 ; for ( int r = 0 ; r < n ; r ++ ) { if ( p [ r ] < m ) sum -- ; else if ( p [ r ] > m ) sum ++ ; if ( p [ r ] == m ) has = true ; if ( has ) ans += ( ! c . ContainsKey ( sum ) ? 0 : c [ sum ] ) + ( ! c . ContainsKey ( sum - 1 ) ? 0 : c [ sum - 1 ] ) ; else c . Add ( sum , ! c . ContainsKey ( sum ) ? 1 : c [ sum ] + 1 ) ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] a = { 2 , 4 , 5 , 3 , 1 } ; int n = a . Length ; int m = 4 ; Console . WriteLine ( segments ( n , a , m ) ) ; } }


"Find the number of times every day occurs in a month | C # program to count occurrence of days in a month ; function to find occurrences ; stores days in a week ; Initialize all counts as 4. ; find index of the first day ; number of days whose occurrence will be 5 ; mark the occurrence to be 5 of n - 28 days ; print the days ; Driver function"
Solution in C#:

using System ; public class GfG { public static void occurrenceDays ( int n , string firstday ) { String [ ] days = new String [ ] { " Monday " , " Tuesday " , " Wednesday " , " Thursday " , " Friday " , " Saturday " , " Sunday " } ; int [ ] count = new int [ 7 ] ; for ( int i = 0 ; i < 7 ; i ++ ) count [ i ] = 4 ; int pos = 0 ; for ( int i = 0 ; i < 7 ; i ++ ) { if ( firstday == days [ i ] ) { pos = i ; break ; } } int inc = n - 28 ; for ( int i = pos ; i < pos + inc ; i ++ ) { if ( i > 6 ) count [ i % 7 ] = 5 ; else count [ i ] = 5 ; } for ( int i = 0 ; i < 7 ; i ++ ) { Console . WriteLine ( days [ i ] + " ▁ " + count [ i ] ) ; } } public static void Main ( ) { int n = 31 ; string firstday = " Tuesday " ; occurrenceDays ( n , firstday ) ; } }


"Find the number of valid parentheses expressions of given length | C # program to find valid paranthesisations of length n The majority of code is taken from method 3 of https : www . geeksforgeeks . org / program - nth - catalan - number / ; Returns value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; A Binomial coefficient based function to find nth catalan number in O ( n ) time ; Calculate value of 2 nCn ; return 2 nCn / ( n + 1 ) ; Function to find possible ways to put balanced parenthesis in an expression of length n ; If n is odd , not possible to create any valid parentheses ; Otherwise return n / 2 'th  Catalan Number ; Driver program to test above functions"
Solution in C#:

using System ; class GFG { static long binomialCoeff ( int n , int k ) { long res = 1 ; if ( k > n - k ) k = n - k ; for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } static long catalan ( int n ) { long c = binomialCoeff ( 2 * n , n ) ; return c / ( n + 1 ) ; } static long findWays ( int n ) { if ( ( n & 1 ) != 0 ) return 0 ; return catalan ( n / 2 ) ; } public static void Main ( ) { int n = 6 ; Console . Write ( " Total ▁ possible ▁ expressions " + " of ▁ length ▁ " + n + " ▁ is ▁ " + findWays ( 6 ) ) ; } }


"Find the number of ways to divide number into four parts such that a = c and b = d | C # implementation for above approach ; Function to find the number of ways to divide N into four parts such that a = c and b = d ; Driver code"
Solution in C#:

class GFG { static int possibleways ( int n ) { if ( n % 2 == 1 ) return 0 ; else if ( n % 4 == 0 ) return n / 4 - 1 ; else return n / 4 ; } static void Main ( ) { int n = 20 ; System . Console . WriteLine ( possibleways ( n ) ) ; } }


"Find the number of ways to reach Kth step in stair case | C # implementation of the approach ; Function to return the number of ways to reach the kth step ; Create the dp array ; Broken steps ; Calculate the number of ways for the rest of the positions ; If it is a blocked position ; Number of ways to get to the ith step ; Return the required answer ; Driver code"
Solution in C#:

using System ; class GFG { static readonly int MOD = 1000000007 ; static int number_of_ways ( int [ ] arr , int n , int k ) { if ( k == 1 ) return 1 ; int [ ] dp = new int [ k + 1 ] ; int i ; for ( i = 0 ; i < k + 1 ; i ++ ) dp [ i ] = - 1 ; for ( i = 0 ; i < n ; i ++ ) dp [ arr [ i ] ] = 0 ; dp [ 0 ] = 1 ; dp [ 1 ] = ( dp [ 1 ] == - 1 ) ? 1 : dp [ 1 ] ; for ( i = 2 ; i <= k ; ++ i ) { if ( dp [ i ] == 0 ) continue ; dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] ; dp [ i ] %= MOD ; } return dp [ k ] ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 } ; int n = arr . Length ; int k = 6 ; Console . WriteLine ( number_of_ways ( arr , n , k ) ) ; } }


"Find the number of words of X vowels and Y consonants that can be formed from M vowels and N consonants | C # program to find the number of words of X vowels and Y consonants can be formed from M vowels and N consonants ; Function to returns factorial of n ; Function to find nCr ; Function to find the number of words of X vowels and Y consonants can be formed from M vowels and N consonants ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { static int fact ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } static int nCr ( int n , int r ) { return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } static int NumberOfWays ( int X , int Y , int M , int N ) { return fact ( X + Y ) * nCr ( M , X ) * nCr ( N , Y ) ; } public static void Main ( String [ ] args ) { int X = 2 , Y = 2 , M = 3 , N = 3 ; Console . WriteLine ( NumberOfWays ( X , Y , M , N ) ) ; } }


"Find the numbers from 1 to N that contains exactly k non | C # implementation of the above approach ; Function to find number less than N having k non - zero digits ; Store the memorised values ; Initialise ; Base ; Calculate all states For every state , from numbers 1 to N , the count of numbers which contain exactly j non zero digits is being computed and updated in the dp array . ; Return the required answer ; Driver code ; Function call"
Solution in C#:

using System ; using System . Collections ; class GFG { static int k_nonzero_numbers ( string s , int n , int k ) { int [ , , ] dp = new int [ n + 1 , 2 , k + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j < 2 ; j ++ ) for ( int x = 0 ; x <= k ; x ++ ) dp [ i , j , x ] = 0 ; dp [ 0 , 0 , 0 ] = 1 ; for ( int i = 0 ; i < n ; ++ i ) { int sm = 0 ; while ( sm < 2 ) { for ( int j = 0 ; j < k + 1 ; ++ j ) { int x = 0 ; while ( x <= ( sm != 0 ? 9 : s [ i ] - '0' ) ) { if ( j + ( x > 0 ? 1 : 0 ) < k + 1 ) { dp [ i + 1 , ( ( sm != 0 || x < ( s [ i ] - '0' ) ) ? 1 : 0 ) , j + ( x > 0 ? 1 : 0 ) ] += dp [ i , sm , j ] ; } ++ x ; } } ++ sm ; } } return dp [ n , 0 , k ] + dp [ n , 1 , k ] ; } public static void Main ( string [ ] args ) { string s = "25" ; int k = 2 ; int n = s . Length ; Console . Write ( k_nonzero_numbers ( s , n , k ) ) ; } }


"Find the numbers of strings that can be formed after processing Q queries | C # program to implement above approach ; To store the size of string and number of queries ; To store parent and rank of ith place ; To store maximum interval ; Function for initialization ; Function to find parent ; Function to make union ; Power function to calculate a raised to m1 under modulo 10000007 ; Function to take maxmium interval ; Function to find different possible strings ; Make union of all chracters which are meant to be same ; Find number of different sets formed ; Return the required answer ; Driver Code ; Queries"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { const int N = 2005 ; const int mod = 1000000007 ; static int n ; static int [ ] par = new int [ N ] ; static int [ ] Rank = new int [ N ] ; static Dictionary < int , int > m = new Dictionary < int , int > ( ) ; static void initialize ( ) { for ( int i = 0 ; i <= n ; i ++ ) { par [ i ] = i ; Rank [ i ] = 0 ; } } static int find ( int x ) { if ( par [ x ] != x ) par [ x ] = find ( par [ x ] ) ; return par [ x ] ; } static void Union ( int x , int y ) { int xpar = find ( x ) ; int ypar = find ( y ) ; if ( Rank [ xpar ] < Rank [ ypar ] ) par [ xpar ] = ypar ; else if ( Rank [ xpar ] > Rank [ ypar ] ) par [ ypar ] = xpar ; else { par [ ypar ] = xpar ; Rank [ xpar ] ++ ; } } static long power ( long a , long m1 ) { if ( m1 == 0 ) return 1 ; else if ( m1 == 1 ) return a ; else if ( m1 == 2 ) return ( 1L * a * a ) % mod ; else if ( m1 % 2 == 1 ) return ( 1L * a * power ( power ( a , m1 / 2 ) , 2 ) ) % mod ; else return power ( power ( a , m1 / 2 ) , 2 ) % mod ; } static void query ( int l , int r ) { if ( m . ContainsKey ( l + r ) ) m [ l + r ] = Math . Max ( m [ l + r ] , r ) ; else m [ l + r ] = r ; } static long possiblestrings ( ) { initialize ( ) ; foreach ( KeyValuePair < int , int > i in m ) { int x = i . Key - m [ i . Key ] ; int y = m [ i . Key ] ; while ( x < y ) { Union ( x , y ) ; x ++ ; y -- ; } } int ans = 0 ; for ( int i = 1 ; i <= n ; i ++ ) if ( par [ i ] == i ) ans ++ ; return power ( 26 , ans ) % mod ; } public static void Main ( string [ ] args ) { n = 4 ; query ( 1 , 3 ) ; query ( 2 , 4 ) ; Console . Write ( possiblestrings ( ) ) ; } }


"Find the numbers present at Kth level of a Fibonacci Binary Tree | C # program to print the Fibonacci numbers present at K - th level of a Binary Tree ; Initializing the max value ; Array to store all the fibonacci numbers ; Function to generate fibonacci numbers using Dynamic Programming ; 0 th and 1 st number of the series are 0 and 1 ; Add the previous two numbers in the series and store it ; Function to print the Fibonacci numbers present at Kth level of a Binary Tree ; Finding the left and right index ; Iterating and printing the numbers ; Driver code ; Precomputing Fibonacci numbers"
Solution in C#:

using System ; class GFG { static int MAX_SIZE = 100005 ; static int [ ] fib = new int [ MAX_SIZE + 1 ] ; static void fibonacci ( ) { int i ; fib [ 0 ] = 0 ; fib [ 1 ] = 1 ; for ( i = 2 ; i <= MAX_SIZE ; i ++ ) { fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] ; } } static void printLevel ( int level ) { int left_index = ( int ) Math . Pow ( 2 , level - 1 ) ; int right_index = ( int ) ( Math . Pow ( 2 , level ) - 1 ) ; for ( int i = left_index ; i <= right_index ; i ++ ) { Console . Write ( fib [ i - 1 ] + " ▁ " ) ; } Console . WriteLine ( ) ; } public static void Main ( string [ ] args ) { fibonacci ( ) ; int K = 4 ; printLevel ( K ) ; } }


"Find the occurrence of the given binary pattern in the binary representation of the array elements | C # code implementation for above approach ; Function to return the binary representation of n ; Array to store binary representation ; Counter for binary array ; Storing remainder in binary array ; To store the binary representation as a string ; Function to return the frequency of pat in the given string txt ; A loop to slide pat [ ] one by one ; For current index i , check for pattern match ; If pat [ 0. . . M - 1 ] = txt [ i , i + 1 , ... i + M - 1 ] ; Function to find the occurrence of the given pattern in the binary representation of elements of arr [ ] ; For every element of the array ; Find its binary representation ; Print the occurrence of the given pattern in its binary representation ; Driver code"
Solution in C#:

using System ; class GFG { static String decToBinary ( int n ) { int [ ] binaryNum = new int [ 32 ] ; int i = 0 ; while ( n > 0 ) { binaryNum [ i ] = n % 2 ; n = n / 2 ; i ++ ; } String binary = " " ; for ( int j = i - 1 ; j >= 0 ; j -- ) { binary += String . Join ( " " , binaryNum [ j ] ) ; } return binary ; } static int countFreq ( String pat , String txt ) { int M = pat . Length ; int N = txt . Length ; int res = 0 ; for ( int i = 0 ; i <= N - M ; i ++ ) { int j ; for ( j = 0 ; j < M ; j ++ ) { if ( txt [ i + j ] != pat [ j ] ) { break ; } } if ( j == M ) { res ++ ; j = 0 ; } } return res ; } static void findOccurrence ( int [ ] arr , int n , String pattern ) { for ( int i = 0 ; i < n ; i ++ ) { String binary = decToBinary ( arr [ i ] ) ; Console . Write ( countFreq ( pattern , binary ) + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 106 , 7 , 8 } ; String pattern = "10" ; int n = arr . Length ; findOccurrence ( arr , n , pattern ) ; } }


"Find the occurrences of digit d in the range [ 0. . n ] | C # program to count appearances of a digit ' d ' in range from [ 0. . n ] ; Initialize result ; Count appearances in numbers starting from d . ; When the last digit is equal to d ; When the first digit is equal to d then ; increment result as well as number ; In case of reverse of number such as 12 and 21 ; Driver code"
Solution in C#:

using System ; public class GFG { static int getOccurence ( int n , int d ) { int result = 0 ; int itr = d ; while ( itr <= n ) { if ( itr % 10 == d ) result ++ ; if ( itr != 0 && itr / 10 == d ) { result ++ ; itr ++ ; } else if ( itr / 10 == d - 1 ) itr = itr + ( 10 - d ) ; else itr = itr + 10 ; } return result ; } public static void Main ( ) { int n = 11 , d = 1 ; Console . Write ( getOccurence ( n , d ) ) ; } }


"Find the one missing number in range | C # program to find missing number in a range . ; Find the missing number in a range ; here we xor of all the number ; xor last number ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static int missingNum ( int [ ] arr , int n ) { List < int > list = new List < int > ( arr . Length ) ; foreach ( int i in arr ) { list . Add ( i ) ; } int minvalue = list . Min ( ) ; int xornum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { xornum ^= ( minvalue ) ^ arr [ i ] ; minvalue ++ ; } return xornum ^ minvalue ; } public static void Main ( String [ ] args ) { int [ ] arr = { 13 , 12 , 11 , 15 } ; int n = arr . Length ; Console . WriteLine ( missingNum ( arr , n ) ) ; } }


"Find the only missing number in a sorted array | C # program to find the only missing element . ; If this is the first element which is not index + 1 , then missing element is mid + 1 ; if this is not the first missing element search in left side ; if it follows index + 1 property then search in right side ; if no element is missing ; Driver code"
Solution in C#:

using System ; class GFG { static int findmissing ( int [ ] ar , int N ) { int l = 0 , r = N - 1 ; while ( l <= r ) { int mid = ( l + r ) / 2 ; if ( ar [ mid ] != mid + 1 && ar [ mid - 1 ] == mid ) return ( mid + 1 ) ; if ( ar [ mid ] != mid + 1 ) r = mid - 1 ; else l = mid + 1 ; } return - 1 ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 7 , 8 } ; int N = arr . Length ; Console . WriteLine ( findmissing ( arr , N ) ) ; } }


"Find the only repeating element in a sorted array of size n | C # program to find the only repeating element in an array of size n and elements from range 1 to n - 1. ; Returns index of second appearance of a repeating element . The function assumes that array elements are in range from 1 to n - 1. ; low = 0 , high = n - 1 ; ; Check if the mid element is the repeating one ; If mid element is not at its position that means the repeated element is in left ; If mid is at proper position then repeated one is in right . ; Driver method"
Solution in C#:

using System ; class Test { static int findRepeatingElement ( int [ ] arr , int low , int high ) { if ( low > high ) return - 1 ; int mid = ( low + high ) / 2 ; if ( arr [ mid ] != mid + 1 ) { if ( mid > 0 && arr [ mid ] == arr [ mid - 1 ] ) return mid ; return findRepeatingElement ( arr , low , mid - 1 ) ; } return findRepeatingElement ( arr , mid + 1 , high ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 3 , 4 , 5 } ; int index = findRepeatingElement ( arr , 0 , arr . Length - 1 ) ; if ( index != - 1 ) Console . Write ( arr [ index ] ) ; } }


"Find the order of execution of given N processes in Round Robin Scheduling | C # program for the above approach ; Function to sort the array order [ ] on the basis of the array freq [ ] ; If order [ i ] th is less than order [ temp ] th process ; Otherwise ; Add the left half to tempOrder [ ] ; Add right half to tempOrder [ ] ; Copy the tempOrder [ ] array to order [ ] array ; Utility function to sort the array order [ ] on the basis of freq [ ] ; Base Case ; Divide array into 2 parts ; Sort the left array ; Sort the right array ; Merge the sorted arrays ; Function to find the order of processes in which execution occurs ; Store the frequency ; Find elements in array freq [ ] ; Store the order of completion of processes ; Initialize order [ i ] as i ; Function Call to find the order of execution ; Print order of completion of processes ; Driver Code ; Burst Time of the processes ; Quantum Time ; Function Call"
Solution in C#:

using System ; class GFG { static void merge ( int [ ] order , int [ ] freq , int i , int mid , int j ) { int [ ] tempOrder = new int [ j - i + 1 ] ; int temp = mid + 1 , index = - 1 ; while ( i <= mid && temp <= j ) { if ( freq [ order [ i ] ] <= freq [ order [ temp ] ] ) { tempOrder [ ++ index ] = order [ i ++ ] ; } else { tempOrder [ ++ index ] = order [ temp ++ ] ; } } while ( i <= mid ) { tempOrder [ ++ index ] = order [ i ++ ] ; } while ( temp <= j ) { tempOrder [ ++ index ] = order [ temp ++ ] ; } int ind = index ; for ( index = ind ; index >= 0 ; index -- ) { order [ j -- ] = tempOrder [ index ] ; } } static void divide ( int [ ] order , int [ ] freq , int i , int j ) { if ( i >= j ) return ; int mid = i / 2 + j / 2 ; divide ( order , freq , i , mid ) ; divide ( order , freq , mid + 1 , j ) ; merge ( order , freq , i , mid , j ) ; } static void orderProcesses ( int [ ] A , int N , int q ) { int i = 0 ; int [ ] freq = new int [ N ] ; for ( i = 0 ; i < N ; i ++ ) { freq [ i ] = ( A [ i ] / q ) ; if ( A [ i ] % q > 0 ) freq [ i ] += 1 ; } int [ ] order = new int [ 4 ] ; for ( i = 0 ; i < N ; i ++ ) { order [ i ] = i ; } divide ( order , freq , 0 , N - 1 ) ; for ( i = 0 ; i < N ; i ++ ) { Console . Write ( order [ i ] + " ▁ " ) ; } } public static void Main ( ) { int [ ] arr = { 3 , 7 , 4 } ; int Q = 3 ; int N = arr . Length ; orderProcesses ( arr , N , Q ) ; } }


"Find the original Array using XOR values of all adjacent elements | C # implementation to find the Array from the XOR array of the adjacent elements of array ; XOR of all elements from 1 to N ; Function to find the Array from the XOR Array ; Take a vector to store the permutation ; XOR of N natural numbers ; Loop to find the XOR of adjacent elements of the XOR Array ; Loop to find the other elements of the permutation ; Finding the next and next elements ; Driver Code ; Required Permutation"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int xor_all_elements ( int n ) { switch ( n & 3 ) { case 0 : return n ; case 1 : return 1 ; case 2 : return n + 1 ; } return 0 ; } static List < int > findArray ( int [ ] xorr , int n ) { List < int > arr = new List < int > ( ) ; int xor_all = xor_all_elements ( n ) ; int xor_adjacent = 0 ; for ( int i = 0 ; i < n - 1 ; i += 2 ) { xor_adjacent = xor_adjacent ^ xorr [ i ] ; } int last_element = xor_all ^ xor_adjacent ; arr . Add ( last_element ) ; for ( int i = n - 2 ; i >= 0 ; i -- ) { last_element = xorr [ i ] ^ last_element ; arr . Add ( last_element ) ; } return arr ; } public static void Main ( String [ ] args ) { List < int > arr = new List < int > ( ) ; int [ ] xorr = { 7 , 5 , 3 , 7 } ; int n = 5 ; arr = findArray ( xorr , n ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } }


"Find the original coordinates whose Manhattan distances are given | C # implementation of the approach ; Function to find the original coordinated ; Maximum of the given distances ; Sum of the given distances ; Conditions when the solution doesn 't exist ; First coordinate ; Second coordinate ; Third coordinate ; Driver code"
Solution in C#:

using System ; class GFG { static void solve ( int d1 , int d2 , int d3 ) { int maxx = Math . Max ( d1 , Math . Max ( d2 , d3 ) ) ; int sum = ( d1 + d2 + d3 ) ; if ( 2 * maxx > sum sum % 2 == 1 ) { Console . WriteLine ( " - 1" ) ; return ; } int x1 = 0 , y1 = 0 ; int x2 = d1 , y2 = 0 ; int x3 = ( d1 + d2 - d3 ) / 2 ; int y3 = ( d2 + d3 - d1 ) / 2 ; Console . WriteLine ( " ( " + x1 + " , ▁ " + y1 + " ) , ▁ ( " + x2 + " , ▁ " + y2 + " ) ▁ and ▁ ( " + x3 + " , ▁ " + y3 + " ) " ) ; } static void Main ( ) { int d1 = 3 , d2 = 4 , d3 = 5 ; solve ( d1 , d2 , d3 ) ; } }


"Find the original matrix when largest element in a row and a column are given | C # implementation of the approach ; Function that prints the original matrix ; Iterate in the row ; Iterate in the column ; If previously existed an element ; Driver code"
Solution in C#:

using System ; class GFG { static int N = 3 ; static int M = 7 ; static void printOriginalMatrix ( int [ ] a , int [ ] b , int [ , ] mat ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( mat [ i , j ] == 1 ) Console . Write ( Math . Min ( a [ i ] , b [ j ] ) + " ▁ " ) ; else Console . Write ( "0" + " ▁ " ) ; } Console . WriteLine ( ) ; } } public static void Main ( ) { int [ ] a = { 2 , 1 , 3 } ; int [ ] b = { 2 , 3 , 0 , 0 , 2 , 0 , 1 } ; int [ , ] mat = { { 1 , 0 , 0 , 0 , 1 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 1 } , { 1 , 1 , 0 , 0 , 0 , 0 , 0 } } ; printOriginalMatrix ( a , b , mat ) ; } }


"Find the other end point of a line with given one end and mid | C # program to find the end point of a line ; function to find the end pointof a line ; find end point for x coordinates ; find end point for y coordinates ; Driver Program"
Solution in C#:

using System ; class GFG { static void otherEndPoint ( int x1 , int y1 , int m1 , int m2 ) { float x2 = ( float ) ( 2 * m1 - x1 ) ; float y2 = ( float ) ( 2 * m2 - y1 ) ; Console . WriteLine ( " x2 ▁ = ▁ " + x2 + " , ▁ " + " y2 ▁ = ▁ " + y2 ) ; } public static void Main ( String [ ] args ) { int x1 = - 4 , y1 = - 1 , m1 = 3 , m2 = 5 ; otherEndPoint ( x1 , y1 , m1 , m2 ) ; } }


"Find the other number when LCM and HCF given | C # program to find other number from given HCF and LCM ; Function that will calculates the zeroes at the end ; Driver code ; Calling function ."
Solution in C#:

using System ; class GFG { static int otherNumber ( int A , int Lcm , int Hcf ) { return ( Lcm * Hcf ) / A ; } static public void Main ( String [ ] args ) { int A = 8 , Lcm = 8 , Hcf = 1 ; int result = otherNumber ( A , Lcm , Hcf ) ; Console . WriteLine ( " B ▁ = ▁ " + result ) ; } }


"Find the other | C # program to find the other - end point of diameter ; function to find the other - end point of diameter ; find end point for x coordinates ; find end point for y coordinates ; Driver Code"
Solution in C#:

using System ; class GFG { static void endPointOfDiameterofCircle ( int x1 , int y1 , int c1 , int c2 ) { Console . Write ( " x2 ▁ = ▁ " + ( 2 * c1 - x1 ) + " ▁ " ) ; Console . Write ( " y2 ▁ = ▁ " + ( 2 * c2 - y1 ) ) ; } public static void Main ( ) { int x1 = - 4 , y1 = - 1 ; int c1 = 3 , c2 = 5 ; endPointOfDiameterofCircle ( x1 , y1 , c1 , c2 ) ; } }


"Find the percentage change in the area of a Rectangle | C # implementation to find the percentage ; Function to calculate percentage change in area of rectangle ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static int calculate_change ( int length , int breadth ) { int change = 0 ; change = length + breadth + ( ( length * breadth ) / 100 ) ; return change ; } public static void Main ( String [ ] args ) { int cL = 20 ; int cB = - 10 ; int cA = calculate_change ( cL , cB ) ; Console . Write ( cA ) ; } }


"Find the perimeter of a cylinder | C # program to find perimeter of cylinder ; Function to calculate perimeter ; Driver program to test above function"
Solution in C#:

 using System ; class GFG { static int perimeter ( int diameter , int height ) { return 2 * ( diameter + height ) ; } public static void Main ( String [ ] args ) { int diameter = 5 ; int height = 10 ; Console . Write ( " Perimeter ▁ = ▁ " + perimeter ( diameter , height ) + " ▁ units
 " ) ; } }


"Find the permutation of first N natural numbers such that sum of i % Pi is maximum possible | C # implementation of the approach ; Function to find the permutation of the first N natural numbers such that the sum of ( i % Pi ) is maximum possible and return the maximum sum ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { static int Max_Sum ( int n ) { return ( n * ( n - 1 ) ) / 2 ; } public static void Main ( String [ ] args ) { int n = 8 ; Console . WriteLine ( Max_Sum ( n ) ) ; } }


"Find the player to last modify a string such that even number of consonants and no vowels are left in the string | C # program for the above approach ; Function to find a winner of the game if both the player plays optimally ; Stores the count of vowels and consonants ; Traverse the String ; Check if character is vowel ; Increment vowels count ; Otherwise increment the consonants count ; Check if Player B wins ; Check if Player A wins ; Check if Player A wins ; If game ends in a Draw ; Driver Code ; Given String s ; Function Call"
Solution in C#:

using System ; class GFG { static void findWinner ( char [ ] s ) { int vowels_count = 0 , consonants_count = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == ' a ' s [ i ] == ' e ' s [ i ] == ' i ' s [ i ] == ' o ' s [ i ] == ' u ' ) { vowels_count ++ ; } else { consonants_count ++ ; } } if ( vowels_count == 0 ) { if ( consonants_count % 2 == 0 ) { Console . Write ( " Player ▁ B " ) ; } else { Console . Write ( " Player ▁ A " ) ; } } else if ( vowels_count == 1 && consonants_count % 2 != 0 ) { Console . Write ( " Player ▁ A " ) ; } else { Console . Write ( " D " ) ; } } public static void Main ( String [ ] args ) { String s = " abcd " ; findWinner ( s . ToCharArray ( ) ) ; } }


"Find the player to reach at least N by multiplying with any value from given range | C # program for the above approach ; Function to find the winner ; Backtrack from N to 1 ; Driver Code"
Solution in C#:

using System ; class GFG { static char Winner ( int N ) { bool player = true ; while ( N > 1 ) { int den = ( player ) ? 9 : 2 ; int X = N / den ; int Y = N % den ; N = ( Y > 0 ) ? X + 1 : X ; player = ! player ; } if ( player ) return ' B ' ; else return ' A ' ; } static public void Main ( ) { int N = 10 ; Console . WriteLine ( Winner ( N ) ) ; } }


"Find the player who rearranges the characters to get a palindrome string first | C # program to print the winner of the game ; Function that returns the winner of the game ; Initialize the freq array to 0 ; Iterate and count the frequencies of each character in the string ; Count the odd occurring character ; If odd occurrence ; Check condition for Player - 1 winning the game ; Driver code ; Function call that returns the winner"
Solution in C#:

using System ; class GfG { static int returnWinner ( String s , int l ) { int [ ] freq = new int [ 26 ] ; for ( int i = 0 ; i < l ; i ++ ) { freq [ s [ i ] - ' a ' ] ++ ; } int cnt = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] % 2 != 0 ) cnt ++ ; } if ( ( cnt == 0 ) || ( cnt & 1 ) == 1 ) return 1 ; else return 2 ; } public static void Main ( String [ ] args ) { String s = " abaaab " ; int l = s . Length ; int winner = returnWinner ( s , l ) ; Console . WriteLine ( " Player - " + winner ) ; } }


"Find the player who will win by choosing a number in range [ 1 , K ] with sum total N | C # program for the above approach ; Function to predict the winner ; Driver Code ; Given Input ; Function call"
Solution in C#:

using System ; public class GFG { static void predictTheWinner ( int K , int N ) { if ( N % ( K + 1 ) == 0 ) Console . WriteLine ( " Bob " ) ; else Console . WriteLine ( " Alice " ) ; } public static void Main ( string [ ] args ) { int K = 7 , N = 50 ; predictTheWinner ( K , N ) ; } }


"Find the player who will win the Coin game | C # program to find the player who wins the game ; Function to check the wining player ; As discussed in the above approach ; Driver Code"
Solution in C#:

using System ; class GFG { static void findWinner ( int n ) { if ( ( n - 1 ) % 6 == 0 ) { Console . WriteLine ( " Second ▁ Player ▁ wins ▁ the ▁ game " ) ; } else { Console . WriteLine ( " First ▁ Player ▁ wins ▁ the ▁ game " ) ; } } public static void Main ( ) { int n = 7 ; findWinner ( n ) ; } }


"Find the player who wins the game by removing the last of given N cards | C # program to implement the above approach ; Function to check which player can win the game ; Driver code"
Solution in C#:

using System ; class GFG { static void checkWinner ( int N , int K ) { if ( N % ( K + 1 ) > 0 ) { Console . Write ( " A " ) ; } else { Console . Write ( " B " ) ; } } public static void Main ( String [ ] args ) { int N = 50 ; int K = 10 ; checkWinner ( N , K ) ; } }


"Find the player who wins the game of placing alternate + and | C # program for the above approach ; Function to check which player wins the game ; Stores the difference between + ve and - ve array elements ; Traverse the array ; Update diff ; Checks if diff is even ; Driver Code ; Given Input ; Function call to check which player wins the game"
Solution in C#:

using System ; class GFG { static void checkWinner ( int [ ] arr , int N ) { int diff = 0 ; for ( int i = 0 ; i < N ; i ++ ) { diff -= arr [ i ] ; } if ( diff % 2 == 0 ) { Console . Write ( " A " ) ; } else { Console . Write ( " B " ) ; } } public static void Main ( ) { int [ ] arr = { 1 , 2 } ; int N = arr . Length ; checkWinner ( arr , N ) ; } }


"Find the player with least 0 s after emptying a Binary String by removing non | C # program to implement the above approach ; Function to find the player who wins the game ; Stores total count of 0 s in the string ; Stores count of consecutive 1 s ; Stores Nim - Sum on count of consecutive 1 s ; Stores length of the string ; Traverse the string ; If the current character is 1 ; Update cntConOne ; Update nimSum ; Update cntConOne ; Update cntZero ; Update nimSum ; If countZero is an even number ; nimSum is not 0 ; If nimSum is zero ; Driver Code"
Solution in C#:

using System ; class GFG { public static void FindwinnerOfGame ( string S ) { int cntZero = 0 ; int cntConOne = 0 ; int nimSum = 0 ; int N = S . Length ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] == '1' ) { cntConOne += 1 ; } else { nimSum ^= cntConOne ; cntConOne = 0 ; cntZero ++ ; } } nimSum ^= cntConOne ; if ( cntZero % 2 == 0 ) { Console . Write ( " Tie " ) ; } else if ( nimSum != 0 ) { Console . Write ( " player ▁ 1" ) ; } else { Console . Write ( " player ▁ 2" ) ; } } public static void Main ( string [ ] args ) { string S = "0110011" ; FindwinnerOfGame ( S ) ; } }


"Find the point on X | C # program to implement the above approach ; Function to find median of the array ; Sort the given array ; If number of elements are even ; Return the first median ; Otherwise ; Driver Code"
Solution in C#:

using System ; class GFG { static int findLeastDist ( int [ ] A , int N ) { Array . Sort ( A ) ; if ( N % 2 == 0 ) { return A [ ( N - 1 ) / 2 ] ; } else { return A [ N / 2 ] ; } } public static void Main ( string [ ] args ) { int [ ] A = { 4 , 1 , 5 , 10 , 2 } ; int N = A . Length ; Console . Write ( " ( " + findLeastDist ( A , N ) + " , ▁ " + 0 + " ) " ) ; } }


"Find the point where maximum intervals overlap | C # Program to find maximum guest at any time in a party ; Sort arrival and exit arrays ; guests_in indicates number of guests at a time ; Similar to merge in merge sort to process all events in sorted order ; If next event in sorted order is arrival , increment count of guests ; Update max_guests if needed ; increment index of arrival array ; If event is exit , decrement count of guests . ; of guests . ; Driver Code"
Solution in C#:

using System ; class GFG { static void findMaxGuests ( int [ ] arrl , int [ ] exit , int n ) { Array . Sort ( arrl ) ; Array . Sort ( exit ) ; int guests_in = 1 , max_guests = 1 , time = arrl [ 0 ] ; int i = 1 , j = 0 ; while ( i < n && j < n ) { if ( arrl [ i ] <= exit [ j ] ) { guests_in ++ ; if ( guests_in > max_guests ) { max_guests = guests_in ; time = arrl [ i ] ; } i ++ ; } else { guests_in -- ; j ++ ; } } Console . Write ( " Maximum ▁ Number ▁ of ▁ Guests ▁ = ▁ " + max_guests + " ▁ at ▁ time ▁ " + time ) ; } public static void Main ( ) { int [ ] arrl = { 1 , 2 , 10 , 5 , 5 } ; int [ ] exit = { 4 , 5 , 12 , 9 , 12 } ; int n = arrl . Length ; findMaxGuests ( arrl , exit , n ) ; } }


"Find the point where maximum intervals overlap | C # implementation of above approach ; Finding maximum starting time ; Finding maximum ending time ; reating an auxiliary array ; Lazy addition ; Lazily Calculating value at index i ; Driver code"
Solution in C#:

using System ; using System . Linq ; class GFG { public static void maxOverlap ( int [ ] start , int [ ] end , int n ) { int maxa = start . Max ( ) ; int maxb = end . Max ( ) ; int maxc = Math . Max ( maxa , maxb ) ; int [ ] x = new int [ maxc + 2 ] ; int cur = 0 , idx = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ++ x [ start [ i ] ] ; -- x [ end [ i ] + 1 ] ; } int maxy = int . MinValue ; for ( int i = 0 ; i <= maxc ; i ++ ) { cur += x [ i ] ; if ( maxy < cur ) { maxy = cur ; idx = i ; } } Console . WriteLine ( " Maximum ▁ value ▁ is ▁ " + maxy + " ▁ at ▁ position : ▁ " + idx + " " ) ; } public static void Main ( ) { int [ ] start = { 13 , 28 , 29 , 14 , 40 , 17 , 3 } ; int [ ] end = { 107 , 95 , 111 , 105 , 70 , 127 , 74 } ; int n = start . Length ; maxOverlap ( start , end , n ) ; } }


"Find the position of box which occupies the given ball | C # implementation of the approach ; Function to print the position of each boxes where a ball has to be placed ; Find the cumulative sum of array A [ ] ; Find the position of box for each ball ; Row number ; Column ( position of box in particular row ) ; Row + 1 denotes row if indexing of array start from 1 ; Driver code"
Solution in C#:

 using System ; class GFG { static void printPosition ( int [ ] A , int [ ] B , int sizeOfA , int sizeOfB ) { for ( int i = 1 ; i < sizeOfA ; i ++ ) { A [ i ] += A [ i - 1 ] ; } for ( int i = 0 ; i < sizeOfB ; i ++ ) { int row = lower_bound ( A , 0 , A . Length , B [ i ] ) ; int boxNumber = ( row >= 1 ) ? B [ i ] - A [ row - 1 ] : B [ i ] ; Console . WriteLine ( row + 1 + " , ▁ " + boxNumber + "
 " ) ; } } private static int lower_bound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( element > a [ middle ] ) { low = middle + 1 ; } else { high = middle ; } } return low ; } static public void Main ( ) { int [ ] A = { 2 , 2 , 2 , 2 } ; int [ ] B = { 1 , 2 , 3 , 4 } ; int sizeOfA = A . Length ; int sizeOfB = B . Length ; printPosition ( A , B , sizeOfA , sizeOfB ) ; } }


"Find the position of the given Prime Number | C # program to find the position of the given prime number ; Function to precompute the position of every prime number using Sieve ; 0 and 1 are not prime numbers ; Variable to store the position ; Incrementing the position for every prime number ; Driver code"
Solution in C#:

using System ; class GFG { static readonly int limit = 1000000 ; static int [ ] position = new int [ limit + 1 ] ; static void sieve ( ) { position [ 0 ] = - 1 ; position [ 1 ] = - 1 ; int pos = 0 ; for ( int i = 2 ; i <= limit ; i ++ ) { if ( position [ i ] == 0 ) { position [ i ] = ++ pos ; for ( int j = i * 2 ; j <= limit ; j += i ) position [ j ] = - 1 ; } } } public static void Main ( String [ ] args ) { sieve ( ) ; int n = 11 ; Console . Write ( position [ n ] ) ; } }