"Count of different numbers divisible by 3 that can be obtained by changing at most one digit | C # program fo the above approach ; Function to count the number of possible numbers divisible by 3 ; Calculate the sum ; Store the answer ; Iterate over the range ; Decreasing the sum ; Iterate over the range ; Checking if the new sum is divisible by 3 or not ; If yes increment the value of the count ; Driver Code ; Given number"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void findCount ( string number ) { int sum = 0 ; for ( int i = 0 ; i < number . Length ; ++ i ) { sum += ( int ) number [ i ] - 48 ; } int count = 0 ; for ( int i = 0 ; i < number . Length ; ++ i ) { int remaining_sum = sum - ( ( int ) number [ i ] - 48 ) ; for ( int j = 0 ; j <= 9 ; ++ j ) { if ( ( remaining_sum + j ) % 3 == 0 && j != number [ i ] - 48 ) { ++ count ; } } } Console . Write ( count ) ; } public static void Main ( ) { string number = "235" ; findCount ( number ) ; } }


"Count of different straight lines with total n points with m collinear | C # program to count number of straight lines with n total points , out of which m are collinear . ; Returns value of binomial coefficient Code taken from https : goo . gl / vhy4jp ; nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Function to calculate number of straight lines can be formed ; Driver Code"
Solution in C#:

using System ; public class GfG { public static int nCk ( int n , int k ) { int [ ] C = new int [ k + 1 ] ; C [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = Math . Min ( i , k ) ; j > 0 ; j -- ) C [ j ] = C [ j ] + C [ j - 1 ] ; } return C [ k ] ; } public static int count_Straightlines ( int n , int m ) { return ( nCk ( n , 2 ) - nCk ( m , 2 ) + 1 ) ; } public static void Main ( String [ ] args ) { int n = 4 , m = 3 ; Console . WriteLine ( count_Straightlines ( n , m ) ) ; } }


"Count of digits after concatenation of first N positive integers | C # program to find the number of digits after concatenating the first N positive integers ; Function to find the number of digits after concatenating the first N positive integers ; Driver code"
Solution in C#:

using System ; class GFG { static void numberOfDigits ( int N ) { int nod = ( int ) Math . Floor ( Math . Log10 ( N ) + 1 ) ; int toDecrease = ( int ) ( Math . Pow ( 10 , nod ) - 1 ) / 9 ; Console . Write ( ( N + 1 ) * nod - toDecrease ) ; } public static void Main ( ) { int N = 13 ; numberOfDigits ( N ) ; } }


"Count of distinct GCDs among all the non | C # program for the above approach ;  ; Function to calculate the number of distinct GCDs among all non - empty subsequences of an array ; Variables to store the largest element in array and the required count ; Map to store whether a number is present in A ; Calculate largest number in A and mapping A to Mp ; Iterate over all possible values of GCD ; Variable to check current GCD ; Iterate over all multiples of i ; If j is present in A ; Calculate gcd of all encountered multiples of i ; Current GCD is possible ; Return answer ; Driver code ; Input ; Function calling"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int gcd ( int a , int b ) { return b == 0 ? a : gcd ( b , a % b ) ; } static int distinctGCDs ( int [ ] arr , int N ) { int M = - 1 , ans = 0 ; Dictionary < int , int > Mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { M = Math . Max ( M , arr [ i ] ) ; if ( Mp . ContainsKey ( arr [ i ] ) ) Mp [ arr [ i ] ] = 1 ; else Mp . Add ( arr [ i ] , 1 ) ; } for ( int i = 1 ; i <= M ; i ++ ) { int currGcd = 0 ; for ( int j = i ; j <= M ; j += i ) { if ( Mp . ContainsKey ( j ) ) { currGcd = gcd ( currGcd , j ) ; if ( currGcd == i ) { ans ++ ; break ; } } } } return ans ; } public static void Main ( ) { int [ ] arr = { 3 , 11 , 14 , 6 , 12 } ; int N = arr . Length ; Console . Write ( distinctGCDs ( arr , N ) ) ; } }


"Count of distinct GCDs among all the non | C # program for the above approach ; Function to calculate gcd ; Function to calculate the number of distinct GCDs among all non - empty subsequences of an array ; Variables to store the largest element in array and the required count ; Map to store whether a number is present in A ; Calculate largest number in A and mapping A to Mp ; Iterate over all possible values of GCD ; Variable to check current GCD ; Iterate over all multiples of i ; If j is present in A ; Calculate gcd of all encountered multiples of i ; Current GCD is possible ; Return answer ; Driver code ; Input ; Function calling"
Solution in C#:


"Count of distinct N | C # program for the approach ; Function to find the count of distinct arrays of size n having elements in range [ 1 , k ] and all adjacent elements ( P , Q ) follows ( P <= Q ) or ( P % Q > 0 ) ; Stores the divisors of all integers in the range [ 1 , k ] ; Calculate the divisors of all integers using the Sieve ; Stores the dp states such that dp [ i ] [ j ] with i elements having j as the last element of array ; Initialize the dp array ; Calculate the dp states using the derived relation ; Calculate the sum for len - 1 ; Subtract dp [ len - 1 ] [ j ] for each factor of j from [ 1 , K ] ; Calculate the final result ; Return the resultant sum ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int countArrays ( int n , int k ) { List < int > [ ] divisors = new List < int > [ k + 1 ] ; for ( int i = 0 ; i < divisors . Length ; i ++ ) divisors [ i ] = new List < int > ( ) ; for ( int i = 1 ; i <= k ; i ++ ) { for ( int j = 2 * i ; j <= k ; j += i ) { divisors [ j ] . Add ( i ) ; } } int [ , ] dp = new int [ n + 1 , k + 1 ] ; for ( int j = 1 ; j <= k ; j ++ ) { dp [ 1 , j ] = 1 ; } int sum ; for ( int x = 2 ; x <= n ; x ++ ) { sum = 0 ; for ( int j = 1 ; j <= k ; j ++ ) { sum += dp [ x - 1 , j ] ; } for ( int y = 1 ; y <= k ; y ++ ) { dp [ x , y ] = sum ; foreach ( int d in divisors [ y ] ) { dp [ x , y ] = ( dp [ x , y ] - dp [ x - 1 , d ] ) ; } } } sum = 0 ; for ( int j = 1 ; j <= k ; j ++ ) { sum += dp [ n , j ] ; } return sum ; } public static void Main ( ) { int N = 2 , K = 3 ; Console . Write ( countArrays ( N , K ) ) ; } }


"Count of distinct N | C # program of the above approach ; Function to find the count of distinct odd integers with N digits using the given digits in the array arr [ ] ; Stores the factorial of a number ; Calculate the factorial of all numbers from 1 to N ; Stores the frequency of each digit ; Stores the final answer ; Loop to iterate over all values of Nth digit i and 1 st digit j ; If digit i does not exist in the given array move to next i ; Fixing i as Nth digit ; Stores the answer of a specific value of i and j ; If digit j does not exist move to the next j ; Fixing j as 1 st digit ; Calculate number of ways to arrange remaining N - 2 digits ; Including j back into the set of digits ; Including i back into the set of the digits ; Return Answer ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static int countOddIntegers ( int [ ] arr , int N ) { int [ ] Fact = new int [ N ] ; Fact [ 0 ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) { Fact [ i ] = i * Fact [ i - 1 ] ; } int [ ] freq = new int [ 10 ] ; for ( int i = 0 ; i < N ; i ++ ) { freq [ arr [ i ] ] ++ ; } int ans = 0 ; for ( int i = 1 ; i <= 9 ; i += 2 ) { if ( freq [ i ] == 0 ) continue ; freq [ i ] -- ; for ( int j = 1 ; j <= 9 ; j ++ ) { int cur_ans = 0 ; if ( freq [ j ] == 0 ) { continue ; } freq [ j ] -- ; cur_ans = Fact [ N - 2 ] ; for ( int k = 0 ; k <= 9 ; k ++ ) { cur_ans = cur_ans / Fact [ freq [ k ] ] ; } ans += cur_ans ; freq [ j ] ++ ; } freq [ i ] ++ ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] A = { 2 , 3 , 4 , 1 , 2 , 3 } ; int N = A . Length ; Console . Write ( countOddIntegers ( A , N ) ) ; } }


"Count of distinct Primonacci Numbers in a given range [ L , R ] | C # Program to implement the above approach ; Stores list of all primes ; Function to find all primes ; To mark the prime ones ; Initially all indices as prime ; If i is prime ; Set all multiples of i as non - prime ; Adding all primes to a list ; Function to return the count of Primonacci Numbers in the range [ l , r ] ; dp [ i ] contains ith Primonacci Number ; Stores the Primonacci Numbers ; Iterate over all smaller primes ; If Primonacci number lies within the range [ L , R ] ; Count of Primonacci Numbers ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static List < int > primes ; static int M = 100005 ; static void sieve ( ) { primes = new List < int > ( ) ; bool [ ] mark = new bool [ M ] ; for ( int i = 2 ; i < M ; i ++ ) mark [ i ] = true ; for ( int i = 2 ; i * i < M ; i ++ ) { if ( mark [ i ] ) { for ( int j = i * i ; j < M ; j += i ) mark [ j ] = false ; } } for ( int i = 2 ; i < M ; i ++ ) if ( mark [ i ] ) primes . Add ( i ) ; } static void countPrimonacci ( int l , int r ) { List < int > dp = new List < int > ( ) ; dp . Add ( 1 ) ; dp . Add ( 1 ) ; int i = 2 ; HashSet < int > s = new HashSet < int > ( ) ; while ( true ) { int x = 0 ; for ( int j = 0 ; j < primes . Count ; j ++ ) { int p = primes [ j ] ; if ( p >= i ) break ; x += dp [ i - p ] ; } if ( x >= l && x <= r ) s . Add ( x ) ; if ( x > r ) break ; dp . Add ( x ) ; i ++ ; } Console . WriteLine ( s . Count ) ; } public static void Main ( String [ ] args ) { sieve ( ) ; int L = 1 , R = 10 ; countPrimonacci ( L , R ) ; } }


"Count of distinct Strings possible by swapping prefixes of pairs of Strings from the Array | C # Program to implement the above approach ; Function to count the distinct strings possible after swapping the prefixes between two possible strings of the array ; Stores the count of unique characters for each index ; Store current string ; Stores the total number of distinct strings possible ; Return the answer ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int mod = 1000000007 ; public static long countS ( String [ ] str , int n , int m ) { Dictionary < int , HashSet < char > > counts = new Dictionary < int , HashSet < char > > ( ) ; for ( int i = 0 ; i < m ; i ++ ) { counts . Add ( i , new HashSet < char > ( ) ) ; } for ( int i = 0 ; i < n ; i ++ ) { String s = str [ i ] ; for ( int j = 0 ; j < m ; j ++ ) { counts [ j ] . Add ( s [ j ] ) ; } } long result = 1 ; foreach ( int index in counts . Keys ) result = ( result * counts [ index ] . Count ) % mod ; return result ; } public static void Main ( String [ ] args ) { String [ ] str = { "112" , "211" } ; int N = 2 , M = 3 ; Console . WriteLine ( countS ( str , N , M ) ) ; } }


"Count of distinct groups of strings formed after performing equivalent operation | C # program for the above approach ; Function to perform the find operation to find the parent of a disjoint set ; Function to perform union operation of disjoint set union ; Find the parent of node a and b ; Update the rank ; Function to find the number of distinct strings after performing the given operations ; Stores the parent elements of the sets ; Stores the rank of the sets ; Update parent [ i ] to i ; Stores the total characters traversed through the strings ; Stores the current characters traversed through a string ; Update current [ i ] to false ; Update current [ ch - ' a ' ] to true ; Check if current [ j ] is true ; Update total [ j ] to true ; Add arr [ i ] [ 0 ] - ' a ' and j elements to same set ; Stores the count of distinct strings ; Check total [ i ] is true and parent of i is i only ; Increment the value of distCount by 1 ; Print the value of distCount ; Driver Code"
Solution in C#:

using System ; class GFG { static int Find ( int [ ] parent , int a ) { return parent [ a ] = ( parent [ a ] == a ? a : Find ( parent , parent [ a ] ) ) ; } static void Union ( int [ ] parent , int [ ] rank , int a , int b ) { a = Find ( parent , a ) ; b = Find ( parent , b ) ; if ( rank [ a ] == rank [ b ] ) rank [ a ] ++ ; if ( rank [ a ] > rank [ b ] ) parent [ b ] = a ; else parent [ a ] = b ; } static void numOfDistinctStrings ( string [ ] arr , int N ) { int [ ] parent = new int [ ( 27 ) ] ; int [ ] rank = new int [ ( 27 ) ] ; for ( int j = 0 ; j < 27 ; j ++ ) { parent [ j ] = j ; } bool [ ] total = new bool [ 26 ] ; bool [ ] current = new bool [ 26 ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < 26 ; j ++ ) { current [ j ] = false ; } foreach ( char ch in arr [ i ] ) { current [ ch - ' a ' ] = true ; } for ( int j = 0 ; j < 26 ; j ++ ) { if ( current [ j ] ) { total [ j ] = true ; Union ( parent , rank , arr [ i ] [ 0 ] - ' a ' , j ) ; } } } int distCount = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( total [ i ] && Find ( parent , i ) == i ) { distCount ++ ; } } Console . WriteLine ( distCount ) ; } public static void Main ( ) { string [ ] arr = { " a " , " ab " , " b " , " d " } ; int N = arr . Length ; numOfDistinctStrings ( arr , N ) ; } }


"Count of distinct index pair ( i , j ) such that element sum of First Array is greater | C # program for the above problem ; function to find the number of pairs satisfying the given cond . ; variables used for traversal ; count variable to store the count of possible pairs ; Nested loop to find out the possible pairs ; Check if the given condition is satisfied or not . If yes then increment the count . ; Return the count value ; Driver Code ; Size of the arrays ; Initialise the arrays ; function call that returns the count of possible pairs"
Solution in C#:

using System ; class GFG { static int count_pairs ( int [ ] a , int [ ] b , int N ) { int i , j ; int count = 0 ; for ( i = 0 ; i < ( N - 1 ) ; i ++ ) { for ( j = ( i + 1 ) ; j < N ; j ++ ) { if ( ( a [ i ] + a [ j ] ) > ( b [ i ] + b [ j ] ) ) { count ++ ; } } } return count ; } public static void Main ( ) { int N = 5 ; int [ ] a = new int [ ] { 1 , 2 , 3 , 4 , 5 } ; int [ ] b = new int [ ] { 2 , 5 , 6 , 1 , 9 } ; Console . Write ( count_pairs ( a , b , N ) ) ; } }


"Count of distinct index pair ( i , j ) such that element sum of First Array is greater | C # program of the above approach ; Function to find the number of pairs . ; Array c [ ] where c [ i ] = a [ i ] - b [ i ] ; Sort the array c ; Initialise answer as 0 ; Iterate from index 0 to n - 1 ; If c [ i ] <= 0 then in the sorted array c [ i ] + c [ pos ] can never greater than 0 where pos < i ; Which is equivalent to c [ j ] >= - c [ i ] + 1 ; Add ( i - pos ) to answer ; Return the answer ; Driver Code ; Number of elements in a and b ; Array a ; Array b"
Solution in C#:

using System ; class GFG { static int numberOfPairs ( int [ ] a , int [ ] b , int n ) { int [ ] c = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { c [ i ] = a [ i ] - b [ i ] ; } Array . Sort ( c ) ; int answer = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( c [ i ] <= 0 ) continue ; int pos = - 1 ; for ( int j = 0 ; j < n ; j ++ ) { if ( c [ i ] + c [ j ] > 0 ) { pos = j ; break ; } } answer += ( i - pos ) ; } return answer ; } static void Main ( ) { int n = 5 ; int [ ] a = { 1 , 2 , 3 , 4 , 5 } ; int [ ] b = { 2 , 5 , 6 , 1 , 9 } ; Console . WriteLine ( numberOfPairs ( a , b , n ) ) ; } }


"Count of distinct integers belonging to first N terms of at least one of given GPs | C # program for the above approach ; Function to find the count of distinct integers that belong to the first N terms of at least one of them is GP ; Stores the integers that occur in GPs in a set data - structure ; Stores the current integer of the first GP ; Iterate first N terms of first GP ; Insert the ith term of GP in S ; Stores the current integer of the second GP ; Iterate first N terms of second GP ; Return Answer ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { static int UniqueGeometricTerms ( int N , int a1 , int r1 , int a2 , int r2 ) { HashSet < int > S = new HashSet < int > ( ) ; int p1 = a1 ; for ( int i = 0 ; i < N ; i ++ ) { S . Add ( p1 ) ; p1 = ( p1 * r1 ) ; } int p2 = a2 ; for ( int i = 0 ; i < N ; i ++ ) { S . Add ( p2 ) ; p2 = ( p2 * r2 ) ; } return S . Count ; } public static void Main ( string [ ] args ) { int N = 5 ; int a1 = 3 , r1 = 2 , a2 = 2 , r2 = 3 ; Console . Write ( UniqueGeometricTerms ( N , a1 , r1 , a2 , r2 ) ) ; } }


"Count of distinct integers in range [ 1 , N ] that do not have any subset sum as K | C # program for the above approach ; Function to find maximum number of distinct integers in [ 1 , N ] having no subset with sum equal to K ; Declare a vector to store the required numbers ; Store all the numbers in [ 1 , N ] except K ; Store the maximum number of distinct numbers ; Reverse the array ; Print the required numbers ; Driver Code ; Given Input ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void findSet ( int N , int K ) { List < int > a = new List < int > ( ) ; for ( int i = 1 ; i <= N ; i ++ ) { if ( i != K ) a . Add ( i ) ; } int MaxDistinct = ( N - K ) + ( K / 2 ) ; a . Reverse ( ) ; for ( int i = 0 ; i < MaxDistinct ; i ++ ) Console . Write ( a [ i ] + " ▁ " ) ; } public static void Main ( ) { int N = 5 , K = 3 ; findSet ( N , K ) ; } }


"Count of distinct numbers formed by shuffling the digits of a large number N | C # program for the above approach ; Recursive function to return the value of ( x ^ n ) % m ; Base Case ; If N is even ; Else N is odd ; Function to find modular inverse of a number x under modulo m ; Using Fermat 's little theorem ; Function to count of numbers formed by shuffling the digits of a large number N ; Modulo value ; Array to store the factorials upto the maximum value of N ; Store factorial of i at index i ; To store count of occurrence of a digit ; Increment the count of digit occured ; Assign the factorial of length of input ; Multiplying result with the modulo multiplicative inverse of factorial of count of i ; Print the result ; Driver code ; Given number as string ; Function call"
Solution in C#:

using System . Collections . Generic ; using System ; class GFG { static long modexp ( long x , long n , long m ) { if ( n == 0 ) { return 1 ; } else if ( n % 2 == 0 ) { return modexp ( ( x * x ) % m , n / 2 , m ) ; } else { return ( x * modexp ( ( x * x ) % m , ( n - 1 ) / 2 , m ) % m ) ; } } static long modInverse ( long x , long m ) { return modexp ( x , m - 2 , m ) ; } static void countNumbers ( string N ) { long m = 1000000007 ; long [ ] factorial = new long [ 100001 ] ; factorial [ 0 ] = 1 ; for ( int i = 1 ; i < 100001 ; i ++ ) { factorial [ i ] = ( factorial [ i - 1 ] * i ) % m ; } long [ ] count = new long [ 10 ] ; for ( int i = 0 ; i < 10 ; i ++ ) { count [ i ] = 0 ; } long length = N . Length ; for ( int i = 0 ; i < length ; i ++ ) count [ N [ i ] - '0' ] ++ ; long result = factorial [ ( int ) length ] ; for ( int i = 0 ; i < 10 ; i ++ ) { result = ( result * modInverse ( factorial [ ( int ) count [ i ] ] , m ) ) % m ; } Console . WriteLine ( result ) ; } public static void Main ( ) { string N = "0223" ; countNumbers ( N ) ; } }


"Count of distinct permutations of every possible length of given string | C # implementation of the above approach ; Function to find the factorial of a number ; Loop to find the factorial of the given number ; Function to find the number of permutations possible for a given String ; Function to find the total number of combinations possible ; Driver Code"
Solution in C#:

using System ; class GFG { static int fact ( int a ) { int i , f = 1 ; for ( i = 2 ; i <= a ; i ++ ) f = f * i ; return f ; } static int permute ( int n , int r ) { int ans = 0 ; ans = ( fact ( n ) / fact ( n - r ) ) ; return ans ; } static int findPermutations ( int n ) { int sum = 0 , P ; for ( int r = 1 ; r <= n ; r ++ ) { P = permute ( n , r ) ; sum = sum + P ; } return sum ; } public static void Main ( String [ ] args ) { String str = " xz " ; int n ; n = str . Length ; Console . Write ( findPermutations ( n ) ) ; } }


"Count of distinct permutations of length N having no similar adjacent characters | C # program to implement the above approach ; Function to print the number of permutations possible ; Driver Code"
Solution in C#:

using System ; class GFG { static int countofPermutations ( int N ) { return ( int ) ( 3 * Math . Pow ( 2 , N - 1 ) ) ; } public static void Main ( String [ ] args ) { int N = 5 ; Console . Write ( countofPermutations ( N ) ) ; } }


"Count of distinct possible strings after performing given operations | C # implementation of the above approach ; Function that prints the number of different strings that can be formed ; Computing the length of the given string ; Base case ; Traverse the given string ; If two consecutive 1 ' s ▁ ▁ or ▁ 2' s are present ; Otherwise take the previous value ; Driver code"
Solution in C#:

using System ; class GFG { static void differentStrings ( string s ) { int n = s . Length ; int [ ] dp = new int [ n + 1 ] ; dp [ 0 ] = dp [ 1 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( s [ i ] == s [ i - 1 ] && ( s [ i ] == '1' s [ i ] == '2' ) ) dp [ i + 1 ] = dp [ i ] + dp [ i - 1 ] ; else dp [ i + 1 ] = dp [ i ] ; } Console . Write ( dp [ n ] ) ; } public static void Main ( ) { string S = "0111022110" ; differentStrings ( S ) ; } }


"Count of distinct power of prime factor of N | C # program for the above approach ; Function to count the number of distinct positive power of prime factor of integer N ; Iterate for all prime factor ; If it is a prime factor , count the total number of times it divides n . ; Find the Number of distinct possible positive numbers ; Return the final count ; Driver code ; Given Number N ; Function Call"
Solution in C#:

using System ; class GFG { static int countFac ( int n ) { int m = n ; int count = 0 ; for ( int i = 2 ; ( i * i ) <= m ; ++ i ) { int total = 0 ; while ( n % i == 0 ) { n /= i ; ++ total ; } int temp = 0 ; for ( int j = 1 ; ( temp + j ) <= total ; ++ j ) { temp += j ; ++ count ; } } if ( n != 1 ) ++ count ; return count ; } public static void Main ( ) { int N = 24 ; Console . Write ( countFac ( N ) ) ; } }


"Count of distinct rectangles inscribed in an equilateral triangle | C # implementation of the approach ; Function to return the count of rectangles when n is odd ; Calculating number of dots in vertical level ; Calculating number of ways to select two points in the horizontal level i ; Multiply both to obtain the number of rectangles formed at that level ; Calculating number of dots in vertical level ; Calculating number of ways to select two points in the horizontal level i ; Multiply both to obtain the number of rectangles formed at that level ; Function to return the count of rectangles when n is even ; Driver code ; If n is odd"
Solution in C#:

using System ; class GFG { static int countOdd ( int n ) { int coun = 0 , m , j , i ; for ( i = n - 2 ; i >= 1 ; i -- ) { if ( i >= 1 ) { m = ( n - i ) / 2 ; j = ( i * ( i + 1 ) ) / 2 ; coun += j * m ; } else { m = ( ( n - 1 ) - i ) / 2 ; j = ( i * ( i + 1 ) ) / 2 ; coun += j * m ; } } return coun ; } static int countEven ( int n ) { int coun = 0 , m , j , i ; for ( i = n - 2 ; i >= 1 ; i -- ) { if ( i >= 1 ) { m = ( ( n - 1 ) - i ) / 2 ; j = ( i * ( i + 1 ) ) / 2 ; coun += j * m ; } else { m = ( n - i ) / 2 ; j = ( i * ( i + 1 ) ) / 2 ; coun += j * m ; } } return coun ; } static public void Main ( ) { int n = 5 ; if ( n >= 1 ) Console . Write ( countOdd ( n ) ) ; else Console . Write ( countEven ( n ) ) ; } }


"Count of distinct remainders when N is divided by all the numbers from the range [ 1 , N ] | C # implementation of the above approach ; Function to return the count of distinct remainders that can be obtained when n is divided by every element from the range [ 1 , n ] ; If n is even ; If n is odd ; Driver code"
Solution in C#:

using System ; class GFG { static int distinctRemainders ( int n ) { if ( n % 2 == 0 ) return ( n / 2 ) ; return ( 1 + ( n / 2 ) ) ; } public static void Main ( ) { int n = 5 ; Console . WriteLine ( distinctRemainders ( n ) ) ; } }


"Count of distinct substrings of a string using Suffix Trie | C # program to find the count of distinct substring of a string using trie data structure ; A Suffix Trie ( A Trie of all suffixes ) Node ; public SuffixTrieNode ( ) Constructor ; Initialize all child pointers as NULL ; A recursive function to insert a suffix of the s in subtree rooted with this node ; If string has more characters ; Find the first character and convert it into 0 - 25 range . ; If there is no edge for this character , add a new edge ; Recur for next suffix ; A Trie of all suffixes ; Constructor ( Builds a trie of suffies of the given text ) ; Consider all suffixes of given string and insert them into the Suffix Trie using recursive function insertSuffix ( ) in SuffixTrieNode class ; A recursive function to count nodes in trie ; If all characters of pattern have been processed , ; if children is not NULL then find count of all nodes in this subtrie ; return count of nodes of subtrie and plus 1 because of node 's own count ; method to count total nodes in suffix trie ; Returns count of distinct substrings of str ; Construct a Trie of all suffixes ; Return count of nodes in Trie of Suffixes ; Driver program to test above function"
Solution in C#:

using System ; public class Suffix { public class SuffixTrieNode { static readonly int MAX_CHAR = 26 ; public SuffixTrieNode [ ] children = new SuffixTrieNode [ MAX_CHAR ] ; { for ( int i = 0 ; < ; ++ ) children [ i ] = null ; } public void insertSuffix ( String s ) { if ( s . Length > 0 ) { char cIndex = ( char ) ( s [ 0 ] - ' a ' ) ; if ( children [ cIndex ] == null ) children [ cIndex ] = new SuffixTrieNode ( ) ; children [ cIndex ] . insertSuffix ( s . Substring ( 1 ) ) ; } } } public class Suffix_trie { static readonly int MAX_CHAR = 26 ; public SuffixTrieNode root ; public Suffix_trie ( String s ) { root = new SuffixTrieNode ( ) ; for ( int i = 0 ; i < s . Length ; i ++ ) root . insertSuffix ( s . Substring ( i ) ) ; } public int _countNodesInTrie ( SuffixTrieNode node ) { if ( node == null ) return 0 ; int count = 0 ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { if ( node . children [ i ] != null ) count += _countNodesInTrie ( node . children [ i ] ) ; } return ( 1 + count ) ; } public int countNodesInTrie ( ) { return _countNodesInTrie ( root ) ; } } static int ( ) { = new Suffix_trie ( str ) ; return sTrie . countNodesInTrie ( ) ; } public static void Main ( String [ ] args ) { String str = " ababa " ; Console . WriteLine ( " Count ▁ of ▁ distinct ▁ substrings ▁ is ▁ " + countDistinctSubstring ( str ) ) ; } }


"Count of distinct sums formed by N numbers taken form range [ L , R ] | C # program for the above approach ; Function to find total number of different sums of N numbers in the range [ L , R ] ; To store minimum possible sum with N numbers with all as L ; To store maximum possible sum with N numbers with all as R ; All other numbers in between maxSum and minSum can also be formed so numbers in this range is the final answer ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int countDistinctSums ( int N , int L , int R ) { int minSum = L * N ; int maxSum = R * N ; return maxSum - minSum + 1 ; } public static void Main ( ) { int N = 2 , L = 1 , R = 3 ; Console . Write ( countDistinctSums ( N , L , R ) ) ; } }


"Count of distinct values till C formed by adding or subtracting A , B , or 0 any number of times | C # program for the above approach ; Function to calculate gcd ; Function to find number of possible final values ; Find the gcd of two numbers ; Calculate number of distinct values ; Return values ; Driver code"
Solution in C#:

using System ; class GFG { static int gcd ( int A , int B ) { if ( B == 0 ) return A ; else return gcd ( B , A % B ) ; } static int getDistinctValues ( int A , int B , int C ) { int g = gcd ( A , B ) ; int num_values = C / g ; return num_values ; } static void Main ( ) { int A = 2 ; int B = 3 ; int C = 10 ; Console . Write ( getDistinctValues ( A , B , C ) ) ; } }


"Count of divisors having more set bits than quotient on dividing N | C # Program to find number of Divisors which on integer division produce quotient having less set bit than divisor ; Return the count of set bit . ; check if q and d have same number of set bit . ; Binary Search to find the point at which number of set in q is less than or equal to d . ; while left index is less than right index ; finding the middle . ; check if q and d have same number of set it or not . ; Driver Code"
Solution in C#:

using System ; class GFG { static int bit ( int x ) { int ans = 0 ; while ( x > 0 ) { x /= 2 ; ans ++ ; } return ans ; } static bool check ( int d , int x ) { if ( bit ( x / d ) <= bit ( d ) ) return true ; return false ; } static int bs ( int n ) { int l = 1 , r = ( int ) Math . Sqrt ( n ) ; while ( l < r ) { int m = ( l + r ) / 2 ; if ( check ( m , n ) ) r = m ; else l = m + 1 ; } if ( ! check ( l , n ) ) return l + 1 ; else return l ; } static int countDivisor ( int n ) { return n - bs ( n ) + 1 ; } static public void Main ( ) { int n = 5 ; Console . WriteLine ( countDivisor ( n ) ) ; } }


"Count of elements having Euler 's Totient value one less than itself | C # program for the above approach ; Seiev of Erotosthenes method to compute all primes ; If current number is marked prime then mark its multiple as non - prime ; Function to count the number of element satisfying the condition ; Compute the number of primes in count prime array ; Print the number of elements satisfying the condition ; Driver Code ; Given array ; Size of the array ; Function Call"
Solution in C#:

 using System ; class GFG { static int [ ] prime = new int [ 1000001 ] ; static void seiveOfEratosthenes ( ) { for ( int i = 2 ; i < 1000001 ; i ++ ) { prime [ i ] = 1 ; } for ( int i = 2 ; i * i < 1000001 ; i ++ ) { if ( prime [ i ] == 1 ) { for ( int j = i * i ; j < 1000001 ; j += i ) { prime [ j ] = 0 ; } } } } static void CountElements ( int [ ] arr , int n , int L , int R ) { seiveOfEratosthenes ( ) ; int [ ] countPrime = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { countPrime [ i ] = countPrime [ i - 1 ] + prime [ arr [ i - 1 ] ] ; } Console . Write ( countPrime [ R ] - countPrime [ L - 1 ] + "
 " ) ; return ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 5 , 8 } ; int N = arr . Length ; int L = 1 , R = 3 ; CountElements ( arr , N , L , R ) ; } }


"Count of elements having odd number of divisors in index range [ L , R ] for Q queries | C # program for the above approach ; Function count the number of elements having odd number of divisors ; Initialise [ ] dp array ; Precomputation ; Find the Prefix Sum ; Iterate for each query ; Find the answer for each query ; Driver Code ; Given array [ ] arr ; Given Query ; Function Call"
Solution in C#:

 using System ; class GFG { class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void OddDivisorsCount ( int n , int q , int [ ] a , pair [ ] Query ) { int [ ] DP = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int x = ( int ) Math . Sqrt ( a [ i ] ) ; if ( x * x == a [ i ] ) DP [ i ] = 1 ; } for ( int i = 1 ; i < n ; i ++ ) { DP [ i ] = DP [ i - 1 ] + DP [ i ] ; } int l , r ; for ( int i = 0 ; i < q ; i ++ ) { l = Query [ i ] . first ; r = Query [ i ] . second ; if ( l == 0 ) { Console . Write ( DP [ r ] + "
 " ) ; } else { Console . Write ( DP [ r ] - DP [ l - 1 ] + "
 " ) ; } } } public static void Main ( String [ ] args ) { int N = 5 ; int Q = 3 ; int [ ] arr = { 2 , 4 , 5 , 6 , 9 } ; pair [ ] Query = { new pair ( 0 , 2 ) , new pair ( 1 , 3 ) , new pair ( 1 , 4 ) } ; OddDivisorsCount ( N , Q , arr , Query ) ; } }


"Count of elements in an Array whose set bits are in a multiple of K | C # implementation of above approach ; Function to find the count of numbers ; Get the set - bits count of each element ; Check if the setbits count is divisible by K ; Increment the count of required numbers by 1 ;  ; Driver code"
Solution in C#:

using System ; class GFG { static int find_count ( int [ ] arr , int k ) { int ans = 0 ; foreach ( int i in arr ) { int x = countSetBits ( i ) ; if ( x % k == 0 ) ans += 1 ; } return ans ; } static int countSetBits ( long x ) { int setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } public static void Main ( String [ ] args ) { int [ ] arr = { 12 , 345 , 2 , 68 , 7896 } ; int K = 2 ; Console . Write ( find_count ( arr , K ) ) ; } }


"Count of elements not divisible by any other elements of Array | C # program for the above approach ; Function to count the number of elements of array which are not divisible by any other element in the array [ ] arr ; Iterate over the array ; Check if the element is itself or not ; Check for divisibility ; Return the readonly result ; Driver Code ; Given array ; Function call"
Solution in C#:

using System ; class GFG { static int count ( int [ ] a , int n ) { int countElements = 0 ; for ( int i = 0 ; i < n ; i ++ ) { bool flag = true ; for ( int j = 0 ; j < n ; j ++ ) { if ( i == j ) continue ; if ( a [ i ] % a [ j ] == 0 ) { flag = false ; break ; } } if ( flag == true ) ++ countElements ; } return countElements ; } public static void Main ( String [ ] args ) { int [ ] arr = { 86 , 45 , 18 , 4 , 8 , 28 , 19 , 33 , 2 } ; int n = arr . Length ; Console . Write ( count ( arr , n ) ) ; } }


"Count of elements not divisible by any other elements of Array | C # program for the above approach ; Function to count the number of elements of array which are not divisible by any other element of same array ; Length for bool array ; Hash map for storing the element and it 's frequency ; Update the maximum element ; Boolean array of size of the max element + 1 ; Marking the multiples as false ; To store the readonly count ; Traverse bool array ; Check if i is not divisible by any other array elements and appears in the array only once ; Return the readonly Count ; Driver Code ; Given array ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int countEle ( int [ ] a , int n ) { int len = 0 ; Dictionary < int , int > hmap = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { len = Math . Max ( len , a [ i ] ) ; if ( hmap . ContainsKey ( a [ i ] ) ) { hmap [ a [ i ] ] ++ ; } else { hmap . Add ( a [ i ] , 1 ) ; } } bool [ ] v = new bool [ len + 1 ] ; for ( int i = 0 ; i <= len ; i ++ ) { v [ i ] = true ; } for ( int i = 0 ; i < n ; i ++ ) { if ( v [ a [ i ] ] == false ) continue ; for ( int j = 2 * a [ i ] ; j <= len ; j += a [ i ] ) { v [ j ] = false ; } } int count = 0 ; for ( int i = 1 ; i <= len ; i ++ ) { if ( v [ i ] == true && hmap . ContainsKey ( i ) && hmap [ i ] == 1 && hmap [ i ] == 1 ) { count += 1 ; } } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 86 , 45 , 18 , 4 , 8 , 28 , 19 , 33 , 2 } ; int n = arr . Length ; Console . Write ( countEle ( arr , n ) ) ; } }


"Count of elements on the left which are divisible by current element | C # implementation of above approach ; Utility function to print the elements of the array ; Function to generate and print the required array ; For every element of the array ; To store the count of elements on the left that the current element divides ; Print the generated array ; Driver code"
Solution in C#:

using System ; class GFG { static void printArr ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } static void generateArr ( int [ ] A , int n ) { int [ ] B = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int cnt = 0 ; for ( int j = 0 ; j < i ; j ++ ) { if ( A [ j ] % A [ i ] == 0 ) cnt ++ ; } B [ i ] = cnt ; } printArr ( B , n ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 3 , 5 , 1 } ; int n = A . Length ; generateArr ( A , n ) ; } }


"Count of elements on the left which are divisible by current element | Set 2 | C # implementation of the approach ; Utility function to print the elements of the array ; Function to increment the count for each factor of given val ; Function to generate and print the required array ; Find max element of array ; Create count array of maxi size ; For every element of the array ; Count [ A [ i ] ] denotes how many previous elements are there whose factor is the current element . ; Increment in count array for factors of A [ i ] ; Print the generated array ; Driver code"
Solution in C#:

using System ; using System . Linq ; class GFG { static void printArr ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } static void IncrementFactors ( int [ ] count , int val ) { for ( int i = 1 ; i * i <= val ; i ++ ) { if ( val % i == 0 ) { if ( i == val / i ) { count [ i ] ++ ; } else { count [ i ] ++ ; count [ val / i ] ++ ; } } } } static void generateArr ( int [ ] A , int n ) { int [ ] B = new int [ n ] ; int maxi = A . Max ( ) ; int [ ] count = new int [ maxi + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { B [ i ] = count [ A [ i ] ] ; IncrementFactors ( count , A [ i ] ) ; } printArr ( B , n ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 8 , 1 , 28 , 4 , 2 , 6 , 7 } ; int n = arr . Length ; generateArr ( arr , n ) ; } }


"Count of elements that can be deleted without disturbing the mean of the initial array | C # implementation of the approach ; Function to find the elements which do not change the mean on removal ; To store the sum of the array elements ; To store the initial mean ; to store the count of required elements ; Iterate over the array ; Finding the new mean ; If the new mean equals to the initial mean ; Driver code"
Solution in C#:

using System ; class GFG { static int countElements ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; float mean = ( float ) sum / n ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { float newMean = ( float ) ( sum - arr [ i ] ) / ( n - 1 ) ; if ( newMean == mean ) cnt ++ ; } return cnt ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . Length ; Console . WriteLine ( countElements ( arr , n ) ) ; } }


"Count of elements to be multiplied with integers to make each pair of Array a perfect square | C # program for the above approach ; Function to find the smallest prime factor of the elements ; Initializing the first element of the array ; Loop to add the remaining elements to the array ; Marking the smallest prime factor for every number to be itself ; Separately marking spf for every even number as 2 ; Checking if i is prime ; Marking SPF for all the numbers divisible by i ; Marking spf [ j ] if it is not previously marked ; Function to find the minimum number of steps to modify the array such that the product of any two numbers in the array is a perfect square ; Map created to store the unique prime numbers ; Variable to store the minimum number of operations ; Loop to store every unique prime number ; Erasing 1 as a key because it is not a prime number ; Iterating through the hash ; Two variables used for counting the frequency of prime is even or odd ; First prime number ; Iterating the number D ; Check if prime is a factor of the element in the array ; Loop for calculating the frequency of the element ; Check for frequency odd or even ; If it is not a factor of the element , then it is automatically even ; Storing the minimum of two variable even or odd ; Driver Code ; Input array ; Creating shortest prime factorisation array"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void spf_array ( int [ ] spf ) { spf [ 1 ] = 1 ; for ( int i = 2 ; i < 1000 ; i ++ ) spf [ i ] = i ; for ( int i = 4 ; i < 1000 ; i += 2 ) spf [ i ] = 2 ; for ( int i = 3 ; i * i < 1000 ; i ++ ) { if ( spf [ i ] == i ) { for ( int j = i * i ; j < 1000 ; j += i ) if ( spf [ j ] == j ) spf [ j ] = i ; } } } static int minimum_operation ( int [ ] b , int d , int [ ] spf ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; int i = 0 ; int c = 0 ; for ( i = 0 ; i < d ; i ++ ) { int x = b [ i ] ; while ( x != 1 ) { x = x / spf [ x ] ; if ( ! m . ContainsKey ( spf [ x ] ) ) { m . Add ( spf [ x ] , 1 ) ; } } } m . Remove ( 1 ) ; foreach ( KeyValuePair < int , int > x in m ) { int e = 0 , o = 0 ; int j = x . Key ; for ( i = 0 ; i < d ; i ++ ) { if ( b [ i ] % j == 0 ) { int h = 0 ; int g = b [ i ] ; while ( g != 0 ) { if ( g % j != 0 ) { break ; } g = g / j ; h = h + 1 ; } if ( h % 2 == 0 ) { e = e + 1 ; } else { o = o + 1 ; } } else { e = e + 1 ; } } c = c + Math . Min ( o , e ) ; } return c ; } public static void Main ( String [ ] args ) { int [ ] spf = new int [ 1001 ] ; int [ ] b = { 1 , 4 , 6 } ; int d = b . Length ; spf_array ( spf ) ; Console . Write ( minimum_operation ( b , d , spf ) ) ; } }


"Count of elements which are second smallest among three consecutive elements | C # implementation of the approach ; Function to return the count of elements P [ i ] such that P [ i ] is the second smallest among P [ i - 1 ] , P [ i ] and P [ i + 1 ] ; To store the required answer ; Traverse from the second element to the second last element ; Return the required answer ; Driver code"
Solution in C#:

using System ; class GFG { static int countElements ( int [ ] p , int n ) { int ans = 0 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( p [ i - 1 ] > p [ i ] && p [ i ] > p [ i + 1 ] ) ans ++ ; else if ( p [ i - 1 ] < p [ i ] && p [ i ] < p [ i + 1 ] ) ans ++ ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] p = { 2 , 5 , 1 , 3 , 4 } ; int n = p . Length ; Console . WriteLine ( countElements ( p , n ) ) ; } }


"Count of elements which cannot form any pair whose sum is power of 2 | C # program to count of array elements which do not form a pair with sum equal to a power of 2 with any other array element ; Function to calculate and return the count of elements ; Stores the frequencies of every array element ; Stores the count of removals ; For every element , check if it can form a sum equal to any power of 2 with any other element ; Store Math . Pow ( 2 , j ) - a [ i ] ; Check if s is present in the array ; If frequency of s exceeds 1 ; If s has frequency 1 but is different from a [ i ] ; Pair possible ; If no pair possible for the current element ; Return the answer ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int powerOfTwo ( int [ ] a , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( a [ i ] ) ) { mp [ a [ i ] ] = mp [ a [ i ] ] + 1 ; } else { mp . Add ( a [ i ] , 1 ) ; } } int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { bool f = false ; for ( int j = 0 ; j < 31 ; j ++ ) { int s = ( 1 << j ) - a [ i ] ; if ( mp . ContainsKey ( s ) && ( mp [ s ] > 1 || mp [ s ] == 1 && s != a [ i ] ) ) f = true ; } if ( f == false ) count ++ ; } return count ; } public static void Main ( String [ ] args ) { int [ ] a = { 6 , 2 , 11 } ; int n = a . Length ; Console . Write ( powerOfTwo ( a , n ) ) ; } }


"Count of elements which form a loop in an Array according to given constraints | C # program to number of elements which form a cycle in an array ; Function to count number of elements forming a cycle ; Array to store parent node of traversal . ; Array to determine whether current node is already counted in the cycle . ; Initialize the arrays . ; Check if current node is already traversed or not . If node is not traversed yet then parent value will be - 1. ; Traverse the graph until an already visited node is not found . ; Check parent value to ensure a cycle is present . ; Count number of nodes in the cycle . ; Driver code"
Solution in C#:

using System ; class GFG { static int solve ( int [ ] A , int n ) { int i , cnt = 0 , j ; int [ ] parent = new int [ n ] ; int [ ] vis = new int [ n ] ; Array . Fill ( parent , - 1 ) ; Array . Fill ( vis , 0 ) ; for ( i = 0 ; i < n ; i ++ ) { j = i ; if ( parent [ j ] == - 1 ) { while ( parent [ j ] == - 1 ) { parent [ j ] = i ; j = __gcd ( j , A [ j ] ) % n ; } if ( parent [ j ] == i ) { while ( vis [ j ] == 0 ) { vis [ j ] = 1 ; cnt ++ ; j = __gcd ( j , A [ j ] ) % n ; } } } } return cnt ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } static void Main ( ) { int [ ] A = { 1 , 1 , 6 , 2 } ; int n = A . Length ; Console . WriteLine ( solve ( A , n ) ) ; } }


"Count of elements which is product of a pair or an element square | C # Program to implement the above approach ; Stores all factors a number ; Function to calculate and store in a vector ; Function to return the count of array elements which are a product of two array elements ; Copy elements into a a duplicate array ; Sort the duplicate array ; Store the count of elements ; If the factors are not calculated already ; Traverse its factors ; If a pair of factors is found ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static List < int > [ ] v = new List < int > [ 100000 ] ; static void div ( int n ) { for ( int i = 2 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { v [ n ] . Add ( i ) ; } } } static int prodof2elements ( int [ ] arr , int n ) { int [ ] arr2 = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { arr2 [ i ] = arr [ i ] ; } Array . Sort ( arr2 ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( v [ arr [ i ] ] . Count == 0 ) div ( arr [ i ] ) ; foreach ( int j in v [ arr [ i ] ] ) { if ( Array . BinarySearch ( arr2 , j ) >= 0 && Array . BinarySearch ( arr2 , ( int ) arr [ i ] / j ) >= 0 ) { ans ++ ; break ; } } } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 1 , 8 , 4 , 32 , 18 } ; int N = arr . Length ; for ( int i = 0 ; i < v . Length ; i ++ ) v [ i ] = new List < int > ( ) ; Console . Write ( prodof2elements ( arr , N ) ) ; } }


"Count of elements which is the sum of a subarray of the given Array | C # implementation to count the elements such that their exist a subarray whose sum is equal to this element ; Function to count the elements such that their exist a subarray whose sum is equal to this element ; Loop to count the frequency ; Loop to iterate over every possible subarray of the array ; Driver Code"
Solution in C#:

using System ; class GFG { static int countElement ( int [ ] arr , int n ) { int [ ] freq = new int [ n + 1 ] ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { freq [ arr [ i ] ] ++ ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { int tmpsum = arr [ i ] ; for ( int j = i + 1 ; j < n ; j ++ ) { tmpsum += arr [ j ] ; if ( tmpsum <= n ) { ans += freq [ tmpsum ] ; freq [ tmpsum ] = 0 ; } } } return ans ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 } ; int n = arr . Length ; Console . WriteLine ( countElement ( arr , n ) ) ; } }


"Count of elements whose absolute difference with the sum of all the other elements is greater than k | C # implementation of the approach ; Function to return the number of anomalies ; To store the count of anomalies ; To store the sum of the array elements ; Find the sum of the array elements ; Count the anomalies ; Driver code"
Solution in C#:

using System ; class GFG { static int countAnomalies ( int [ ] arr , int n , int k ) { int cnt = 0 ; int i , sum = 0 ; for ( i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; for ( i = 0 ; i < n ; i ++ ) if ( Math . Abs ( arr [ i ] - ( sum - arr [ i ] ) ) > k ) cnt ++ ; return cnt ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 5 } ; int n = arr . Length ; int k = 1 ; Console . WriteLine ( countAnomalies ( arr , n , k ) ) ; } }


"Count of even and odd set bit Array elements after XOR with K for Q queries | C # program to count number of even and odd set bits elements after XOR with a given element ; Store the count of set bits ; Brian Kernighan 's algorithm ; Function to solve Q queries ; Store set bits in X ; Count set bits of X ; Driver code"
Solution in C#:

 using System ; class GFG { static int even , odd ; static void keep_count ( int [ ] arr , int N ) { int count ; for ( int i = 0 ; i < N ; i ++ ) { count = 0 ; while ( arr [ i ] != 0 ) { arr [ i ] = ( arr [ i ] - 1 ) & arr [ i ] ; count ++ ; } if ( count % 2 == 0 ) even ++ ; else odd ++ ; } return ; } static void solveQueries ( int [ ] arr , int n , int [ ] q , int m ) { even = 0 ; odd = 0 ; keep_count ( arr , n ) ; for ( int i = 0 ; i < m ; i ++ ) { int X = q [ i ] ; int count = 0 ; while ( X != 0 ) { X = ( X - 1 ) & X ; count ++ ; } if ( count % 2 == 0 ) { Console . Write ( even + " ▁ " + odd + "
 " ) ; } else { Console . Write ( odd + " ▁ " + even + "
 " ) ; } } } public static void Main ( ) { int [ ] arr = { 2 , 7 , 4 , 5 , 3 } ; int n = arr . Length ; int [ ] q = { 3 , 4 , 12 , 6 } ; int m = q . Length ; solveQueries ( arr , n , q , m ) ; } }


"Count of even set bits between XOR of two arrays | C # program for the above approach ; Function that count the XOR of [ ] B with all the element in [ ] A having even set bit ; Count the set bits in A [ i ] ; check for even or Odd ; To store the count of element for [ ] B such that XOR with all the element in [ ] A having even set bit ; Count set bit for B [ i ] ; check for Even or Odd ; Driver Code"
Solution in C#:

using System ; class GFG { static void countEvenBit ( int [ ] A , int [ ] B , int n , int m ) { int i , cntOdd = 0 , cntEven = 0 ; for ( i = 0 ; i < n ; i ++ ) { int x = bitCount ( A [ i ] ) ; if ( x % 2 == 1 ) { cntEven ++ ; } else { cntOdd ++ ; } } int [ ] CountB = new int [ m ] ; for ( i = 0 ; i < m ; i ++ ) { int x = bitCount ( B [ i ] ) ; if ( x % 2 == 1 ) { CountB [ i ] = cntEven ; } else { CountB [ i ] = cntOdd ; } } for ( i = 0 ; i < m ; i ++ ) { Console . Write ( CountB [ i ] + " ▁ " ) ; } } static int bitCount ( int x ) { int setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } public static void Main ( String [ ] args ) { int [ ] A = { 4 , 2 , 15 , 9 , 8 , 8 } ; int [ ] B = { 3 , 4 , 22 } ; countEvenBit ( A , B , 6 , 3 ) ; } }


"Count of even sum triplets in the array for Q range queries | C # program for above approach ; Function to count number of triplets with even sum in range l , r for each query ; Initialization of array ; Initialization of variables ; Traversing array ; If element is odd ; If element is even ; Storing count of even and odd till each i ; Traversing each query ; Count of odd numbers in l to r ; Count of even numbers in l to r ; Finding the ans ; Printing the ans ; Driver Code ; Given Input ; Function Call"
Solution in C#:

using System ; class GFG { static void countTriplets ( int size , int queries , int [ ] arr , int [ , ] Q ) { int [ ] arr_even = new int [ size + 1 ] ; int [ ] arr_odd = new int [ size + 1 ] ; int even = 0 ; int odd = 0 ; arr_even [ 0 ] = 0 ; arr_odd [ 0 ] = 0 ; for ( int i = 0 ; i < size ; i ++ ) { if ( arr [ i ] % 2 == 1 ) { odd ++ ; } else { even ++ ; } arr_even [ i + 1 ] = even ; arr_odd [ i + 1 ] = odd ; } for ( int i = 0 ; i < queries ; i ++ ) { int l = Q [ i , 0 ] , r = Q [ i , 1 ] ; odd = arr_odd [ r ] - arr_odd [ l - 1 ] ; even = arr_even [ r ] - arr_even [ l - 1 ] ; int ans = ( even * ( even - 1 ) * ( even - 2 ) ) / 6 + ( odd * ( odd - 1 ) / 2 ) * even ; Console . Write ( ans + " ▁ " ) ; } } public static void Main ( ) { int N = 6 , Q = 2 ; int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 } ; int [ , ] queries = { { 1 , 3 } , { 2 , 5 } } ; countTriplets ( N , Q , arr , queries ) ; } }


"Count of exponential paths in a Binary Tree | C # program to find the count exponential paths in Binary Tree ; Structure of a Tree node ; Function to create a new node ; Function to find x ; Take log10 of n ; Log ( n ) with base i ; Raising i to the power p ; Function to check whether the given node equals to x ^ y for some y > 0 ; Take logx ( n ) with base x ; Utility function to count the exponent path in a given Binary tree ; Base Condition , when node pointer becomes null or node value is not a number of pow ( x , y ) ; Increment count when encounter leaf node ; Left recursive call save the value of count ; Right recursive call and return value of count ; Function to count exponential paths ; Driver code ; Create Tree ; Retrieve the value of x ; Function call"
Solution in C#:

using System ; class GFG { public class Node { public int key ; public Node left , right ; } static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . left = temp . right = null ; return ( temp ) ; } static int find_x ( int n ) { if ( n == 1 ) return 1 ; double num , den , p ; num = Math . Log10 ( n ) ; int x = 0 , no = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { den = Math . Log10 ( i ) ; p = num / den ; no = ( int ) ( Math . Pow ( i , ( int ) p ) ) ; if ( Math . Abs ( no - n ) < 0.000001 ) { x = i ; break ; } } return x ; } static bool is_key ( int n , int x ) { double p ; p = Math . Log10 ( n ) / Math . Log10 ( x ) ; int no = ( int ) ( Math . Pow ( x , ( int ) p ) ) ; if ( n == no ) return true ; return false ; } static int evenPaths ( Node node , int count , int x ) { if ( node == null || ! is_key ( node . key , x ) ) { return count ; } if ( node . left == null && node . right == null ) { count ++ ; } count = evenPaths ( node . left , count , x ) ; return evenPaths ( node . right , count , x ) ; } static int countExpPaths ( Node node , int x ) { return evenPaths ( node , 0 , x ) ; } public static void Main ( string [ ] args ) { Node root = newNode ( 27 ) ; root . left = newNode ( 9 ) ; root . right = newNode ( 81 ) ; root . left . left = newNode ( 3 ) ; root . left . right = newNode ( 10 ) ; root . right . left = newNode ( 70 ) ; root . right . right = newNode ( 243 ) ; root . right . right . left = newNode ( 81 ) ; root . right . right . right = newNode ( 909 ) ; int x = find_x ( root . key ) ; Console . Write ( countExpPaths ( root , x ) ) ; } }


"Count of groups among N people having only one leader in each group | C # program for the above approach ; Function to find 2 ^ x using modular exponentiation ; Base cases ; If B is even ; If B is odd ; Function to count the number of ways to form the group having one leader ; Find 2 ^ ( N - 1 ) using modular exponentiation ; Count total ways ; Print the total ways ; Driver Code ; Given N number of peoples ; Function Call"
Solution in C#:

using System ; class GFG { static long mod = 1000000007 ; static int exponentMod ( int A , int B ) { if ( A == 0 ) return 0 ; if ( B == 0 ) return 1 ; long y ; if ( B % 2 == 0 ) { y = exponentMod ( A , B / 2 ) ; y = ( y * y ) % mod ; } else { y = A % mod ; y = ( y * exponentMod ( A , B - 1 ) % mod ) % mod ; } return ( int ) ( ( y + mod ) % mod ) ; } static void countWays ( int N ) { long select = exponentMod ( 2 , N - 1 ) ; long ways = ( ( N % mod ) * ( select % mod ) ) ; ways %= mod ; Console . Write ( ways ) ; } public static void Main ( String [ ] args ) { int N = 5 ; countWays ( N ) ; } }


"Count of groups having largest size while grouping according to sum of its digits | C # implementation to Count the number of groups having the largest size where groups are according to the sum of its digits ; Function to return sum of digits of i ; Create the dictionary of unique sum ; dictionary that contain unique sum count ; Calculate the sum of its digits ; Function to find the largest size of group ; Count of largest size group ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int sumDigits ( int n ) { int sum = 0 ; while ( n != 0 ) { sum += n % 10 ; n /= 10 ; } return sum ; } static Dictionary < int , int > constDict ( int n ) { Dictionary < int , int > d = new Dictionary < int , int > ( ) ; for ( int i = 1 ; i < n + 1 ; ++ i ) { int sum1 = sumDigits ( i ) ; if ( ! d . ContainsKey ( sum1 ) ) d . Add ( sum1 , 1 ) ; else d [ sum1 ] += 1 ; } return d ; } static int countLargest ( int n ) { Dictionary < int , int > d = constDict ( n ) ; int size = 0 ; int count = 0 ; foreach ( KeyValuePair < int , int > it in d ) { int k = it . Key ; int val = it . Value ; if ( val > size ) { size = val ; count = 1 ; } else if ( val == size ) count += 1 ; } return count ; } static void Main ( ) { int n = 13 ; int group = countLargest ( n ) ; Console . WriteLine ( group ) ; } }


"Count of index pairs in array whose range product is a positive integer | C # program to find the count of index pairs in the array positive range product ; Condition if number of negative elements is even then increase even_count ; Otherwise increase odd_count ; Condition if current element is negative ; Condition if number of negative elements is even then add even_count in answer ; Otherwise add odd_count in answer ; Driver Code"
Solution in C#:

using System ; class GFG { public static void positiveProduct ( int [ ] arr , int n ) { int even_count = 0 ; int odd_count = 0 ; int total_count = 0 ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( total_count % 2 == 0 ) { even_count ++ ; } else { odd_count ++ ; } if ( arr [ i ] < 0 ) { total_count ++ ; } if ( total_count % 2 == 0 ) ans += even_count ; else ans += odd_count ; } Console . WriteLine ( ans ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 5 , - 3 , 3 , - 1 , 1 } ; int size = A . Length ; positiveProduct ( A , size ) ; } }


"Count of index pairs with equal elements in an array | C # program to count of index pairs with equal elements in an array . ; Return the number of pairs with equal values . ; Finding frequency of each number . ; Calculating count of pairs with equal values ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static int countPairs ( int [ ] arr , int n ) { Dictionary < int , int > hm = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( hm . ContainsKey ( arr [ i ] ) ) { int a = hm [ arr [ i ] ] ; hm . Remove ( arr [ i ] ) ; hm . Add ( arr [ i ] , a + 1 ) ; } else hm . Add ( arr [ i ] , 1 ) ; } int ans = 0 ; foreach ( var it in hm ) { int count = it . Value ; ans += ( count * ( count - 1 ) ) / 2 ; } return ans ; } public static void Main ( ) { int [ ] arr = new int [ ] { 1 , 2 , 3 , 1 } ; Console . WriteLine ( countPairs ( arr , arr . Length ) ) ; } }


"Count of index pairs with equal elements in an array | C # program to count of pairs with equal elements in an array . ; Return the number of pairs with equal values . ; for each index i and j ; finding the index with same value but different index . ; Driver code"
Solution in C#:

using System ; class GFG { static int countPairs ( int [ ] arr , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] == arr [ j ] ) ans ++ ; return ans ; } public static void Main ( ) { int [ ] arr = { 1 , 1 , 2 } ; int n = arr . Length ; Console . WriteLine ( countPairs ( arr , n ) ) ; } }


"Count of index pairs with equal elements in an array | Set 2 | C # program for the above approach ; Function that count the pairs having same elements in the array [ ] arr ; Hash map to keep track of occurences of elements ; Traverse the array [ ] arr ; Check if occurence of arr [ i ] > 0 add count [ arr [ i ] ] to answer ; Return the result ; Driver Code ; Given array [ ] arr ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int countPairs ( int [ ] arr , int n ) { int ans = 0 ; Dictionary < int , int > count = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( count . ContainsKey ( arr [ i ] ) ) { ans += count [ arr [ i ] ] ; count [ arr [ i ] ] = count [ arr [ i ] ] + 1 ; } else { count . Add ( arr [ i ] , 1 ) ; } } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 1 , 1 } ; int N = arr . Length ; Console . Write ( countPairs ( arr , N ) ) ; } }


"Count of index pairs with equal elements in an array | Set 2 | C # program for the above approach ; Function that counts the pair in the array [ ] arr ; Sort the array ; Initialize two pointers ; Add all valid pairs to answer ; Return the answer ; Driver Code ; Given array [ ] arr ; Function call"
Solution in C#:

using System ; class GFG { static int countPairs ( int [ ] arr , int n ) { int ans = 0 ; Array . Sort ( arr ) ; int left = 0 , right = 1 ; while ( right < n ) { if ( arr [ left ] == arr [ right ] ) ans += right - left ; else left = right ; right ++ ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 2 , 3 , 2 , 3 } ; int N = arr . Length ; Console . Write ( countPairs ( arr , N ) ) ; } }


"Count of indices pairs such that product of elements at these indices is equal to absolute difference of indices | C # program for the above approach ; Function to count the number of pairs ( i , j ) such that arr [ i ] * arr [ j ] is equal to abs ( i - j ) ; Stores the resultant number of pairs ; Generate all possible pairs from the array arr [ ] ; If the given condition satisfy then increment the value of count ; Return the resultant count ; Driver Code"
Solution in C#:

using System ; class GFG { static int getPairsCount ( int [ ] a , int n ) { int count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( ( a [ i ] * a [ j ] ) == Math . Abs ( i - j ) ) count ++ ; } } return count ; } public static void Main ( ) { int [ ] arr = { 1 , 1 , 2 , 4 } ; int N = arr . Length ; Console . Write ( getPairsCount ( arr , N ) ) ; } }


"Count of indices pairs such that product of elements at these indices is equal to absolute difference of indices | C # program for the above approach ; Function to count the number of pairs ( i , j ) such that arr [ i ] * arr [ j ] is equal to abs ( i - j ) ; Stores the resultant number of pairs ; Iterate over the range [ 0 , N ) ; Now , iterate from the value arr [ i ] - ( i % arr [ i ] ) till N with an increment of arr [ i ] ; If the given criteria satisfy then increment the value of count ; Return the resultant count ; Driver Code"
Solution in C#:

using System ; class GFG { static int getPairsCount ( int [ ] arr , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = arr [ i ] - ( i % arr [ i ] ) ; j < n ; j += arr [ i ] ) { if ( i < j && ( arr [ i ] * arr [ j ] ) == Math . Abs ( i - j ) ) { count ++ ; } } } return count ; } public static void Main ( ) { int [ ] arr = { 1 , 1 , 2 , 4 } ; int N = arr . Length ; Console . Write ( getPairsCount ( arr , N ) ) ; } }


"Count of integers K in range [ 0 , N ] such that ( K XOR K + 1 ) equals ( K + 2 XOR K + 3 ) | C # program for the above approach ; Function to count all the integers less than N satisfying the given condition ; Store the count of even numbers less than N + 1 ; Return the count ; Driver code ; Given Input ; Function Call"
Solution in C#:

using System ; class GFG { static int countXor ( int N ) { int cnt = ( int ) N / 2 + 1 ; return cnt ; } static void Main ( ) { int N = 4 ; Console . WriteLine ( countXor ( N ) ) ; } }


"Count of integers having difference with its reverse equal to D | C # program for the above approach ; Maximum digits in N ; Utility function to find count of N such that N + D = reverse ( N ) ; If d is a multiple of 9 , no such value N found ; Divide D by 9 check reverse of number and its sum ; B [ i ] : Stores power of ( 10 , i ) ; Calculate power ( 10 , i ) ; Update B [ i ] ; Stores count of N such that N + D = reverse ( N ) ; Iterate over the range [ 1 , MAXL ] ; Update ans ; Function to find count of possible values of N such that N + D = reverse ( N ) ; Base Case ; If D is not qual to 0 ; Stores count of possible values of N such that N + D = reverse ( N ) ; Update ans ; If l is even ; Update ans ; Stores count of possible values of N such that N + D = reverse ( N ) ; Iterae over the range [ - 9 , 9 ] ; Update ans ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { static int MAXL = 17 ; static int N ; static long [ ] v ; static long findN ( int D ) { if ( D % 9 != 0 ) return 0 ; D /= 9 ; long [ ] B = new long [ MAXL ] ; B [ 0 ] = 1 ; for ( int i = 1 ; i < MAXL ; i ++ ) { B [ i ] = B [ i - 1 ] * 10 ; } long ans = 0 ; for ( int i = 1 ; i <= MAXL ; i ++ ) { N = ( i + 1 ) / 2 ; v = new long [ N ] ; for ( int j = 0 ; j < N ; j ++ ) for ( int k = j ; k < i - j ; k ++ ) v [ j ] += B [ k ] ; ans += count ( D , i , 0 , new int [ N ] ) ; } return ans ; } static long count ( long D , int l , int t , int [ ] x ) { if ( t == N ) { if ( D != 0 ) return 0 ; long ans = 1 ; for ( int i = 0 ; i < N ; i ++ ) { ans *= ( i == 0 ? 9 : 10 ) - Math . Abs ( x [ i ] ) ; } if ( l % 2 == 0 ) { ans *= 10 ; } return ans ; } long anss = 0 ; for ( int m = - 9 ; m <= 9 ; m ++ ) { if ( - v [ t ] < D + v [ t ] * m && D + v [ t ] * m < v [ t ] ) { x [ t ] = m ; anss += count ( D + v [ t ] * m , l , t + 1 , x ) ; } } return anss ; } public static void Main ( String [ ] args ) { int D = 63 ; Console . WriteLine ( findN ( D ) ) ; } }


"Count of integers in given range having their last K digits are equal | C # Program of the above approach ; Function to return the count of integers from 1 to X having the last K digits as equal ; Stores the total count of integers ; Loop to iterate over all possible values of z ; Terminate the loop when z > X ; Add count of integers with last K digits equal to z ; Return count ; Function to return the count of integers from L to R having the last K digits as equal ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static int intCount ( int X , int K ) { int ans = 0 ; for ( int z = 0 ; z < Math . Pow ( 10 , K ) ; z += ( ( int ) Math . Pow ( 10 , K ) - 1 ) / 9 ) { if ( z > X ) break ; ans += ( ( X - z ) / ( int ) Math . Pow ( 10 , K ) + 1 ) ; } return ans ; } static int intCountInRange ( int L , int R , int K ) { return ( intCount ( R , K ) - intCount ( L - 1 , K ) ) ; } public static void Main ( String [ ] args ) { int L = 49 ; int R = 101 ; int K = 2 ; Console . Write ( intCountInRange ( L , R , K ) ) ; } }


"Count of integers in range [ L , R ] having even frequency of each digit | C # program for the above approach ; Stores the upper limit of the range ; Stores the overlapping states ; Recursive Function to calculate the count of valid integers in the range [ 1 , s ] using memoization ; Base Case ; If current integer has even count of digits and is not repeated ; If current state is already considered ; Stores the maximum valid digit at the current index ; Stores the count of valid integers ; If the current digit is not the most significant digit , i . e , the integer is already started ; Iterate through all valid digits ; Recursive call for ith digit at the current index ; Recursive call for integers having leading zeroes in the beginning ; Iterate through all valid digits as most significant digits ; Recursive call for ith digit at the current index ; Return answer ; Function to calculate valid number in the range [ 1 , X ] ; Initialize dp array with - 1 ; Store the range in form of string ; Return Count ; Function to find the count of integers in the range [ L , R ] such that the frequency of each digit is even ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static string s ; static int [ , , , ] dp = new int [ 1024 , 10 , 2 , 2 ] ; static int calcCnt ( int mask , int len , int smaller , int started ) { if ( len == s . Length ) { if ( mask == 0 && started != 0 ) return 1 ; else return 0 ; } if ( dp [ mask , len , smaller , started ] != - 1 ) return dp [ mask , len , smaller , started ] ; int mx = 9 ; if ( smaller == 0 ) { mx = ( int ) s [ len ] - 48 ; } int ans = 0 ; if ( started != 0 ) { for ( int i = 0 ; i <= mx ; i ++ ) { ans += calcCnt ( mask ^ ( 1 << i ) , len + 1 , Convert . ToBoolean ( smaller ) || ( i < ( int ) s [ len ] - 48 ) ? 1 : 0 , 1 ) ; } } else { ans = calcCnt ( mask , len + 1 , 1 , 0 ) ; for ( int i = 1 ; i <= mx ; i ++ ) { ans += calcCnt ( mask ^ ( 1 << i ) , len + 1 , Convert . ToBoolean ( smaller ) || ( i < ( int ) s [ len ] - 48 ) ? 1 : 0 , 1 ) ; } } dp [ mask , len , smaller , started ] = ans ; return ans ; } static int countInt ( int x ) { for ( int i = 0 ; i < 1024 ; i ++ ) { for ( int j = 0 ; j < 10 ; j ++ ) { for ( int k = 0 ; k < 2 ; k ++ ) { for ( int l = 0 ; l < 2 ; l ++ ) dp [ i , j , k , l ] = - 1 ; } } } s = x . ToString ( ) ; return calcCnt ( 0 , 0 , 0 , 0 ) ; } static int countIntInRange ( int L , int R ) { return countInt ( R ) - countInt ( L - 1 ) ; } public static void Main ( ) { int L = 32 , R = 1010 ; Console . Write ( countIntInRange ( L , R ) ) ; } }


"Count of integers obtained by replacing ? in the given string that give remainder 5 when divided by 13 | C # implementation of the approach ; Function to find the count of integers obtained by replacing ' ? ' in a given string such that formed integer gives remainder 5 when it is divided by 13 ; Initialise ; Place digit j at ? position ; Get the remainder ; Return the required answer ; Driver code"
Solution in C#:

using System ; class GFG { static int MOD = ( int ) ( 1e9 + 7 ) ; static int modulo_13 ( String s , int n ) { long [ , ] dp = new long [ n + 1 , 13 ] ; dp [ 0 , 0 ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < 10 ; j ++ ) { int nxt = s [ i ] - '0' ; if ( s [ i ] == ' ? ' ) nxt = j ; for ( int k = 0 ; k < 13 ; k ++ ) { int rem = ( 10 * k + nxt ) % 13 ; dp [ i + 1 , rem ] += dp [ i , k ] ; dp [ i + 1 , rem ] %= MOD ; } if ( s [ i ] != ' ? ' ) break ; } } return ( int ) dp [ n , 5 ] ; } public static void Main ( String [ ] args ) { String s = " ? 44" ; int n = s . Length ; Console . WriteLine ( modulo_13 ( s , n ) ) ; } }


"Count of integers up to N which are non divisors and non coprime with N | C # program to implement the above approach ; Function to return the count of integers less than N satisfying given conditions ; Stores Euler counts ; Store Divisor counts ; Based on Sieve of Eratosthenes ; Update phi values of all multiples of i ; Update count of divisors ; Return the final count ; Driver Code"
Solution in C#:

using System ; class GFG { public static int count ( int n ) { int [ ] phi = new int [ n + 1 ] ; int [ ] divs = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { phi [ i ] += i ; for ( int j = i * 2 ; j <= n ; j += i ) phi [ j ] -= phi [ i ] ; for ( int j = i ; j <= n ; j += i ) divs [ j ] ++ ; } return ( n - phi [ n ] - divs [ n ] + 1 ) ; } public static void Main ( String [ ] args ) { int N = 42 ; Console . WriteLine ( count ( N ) ) ; } }


"Count of integers up to N which represent a Binary number | C # program to count the number of integers upto N which are of the form of binary representations ; Function to return the count ; If the current last digit is 1 ; Add 2 ^ ( ctr - 1 ) possible integers to the answer ; If the current digit exceeds 1 ; Set answer as 2 ^ ctr - 1 as all possible binary integers with ctr number of digits can be obtained ; Driver Code"
Solution in C#:

using System ; class GFG { static int countBinaries ( int N ) { int ctr = 1 ; int ans = 0 ; while ( N > 0 ) { if ( N % 10 == 1 ) { ans += ( int ) Math . Pow ( 2 , ctr - 1 ) ; } else if ( N % 10 > 1 ) { ans = ( int ) ( Math . Pow ( 2 , ctr ) - 1 ) ; } ctr ++ ; N /= 10 ; } return ans ; } public static void Main ( String [ ] args ) { int N = 20 ; Console . Write ( countBinaries ( N ) ) ; } }


"Count of integers up to N which represent a Binary number | C # program to count the number of integers upto N which are of the form of binary representations ; Function to return the count ; PreCompute and store the powers of 2 ; If the current last digit is 1 ; Add 2 ^ ( ctr - 1 ) possible integers to the answer ; If the current digit exceeds 1 ; Set answer as 2 ^ ctr - 1 as all possible binary integers with ctr number of digits can be obtained ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int countBinaries ( int N ) { List < int > powersOfTwo = new List < int > ( ) ; powersOfTwo . Add ( 1 ) ; for ( int i = 1 ; i < 11 ; i ++ ) { powersOfTwo . Add ( powersOfTwo [ i - 1 ] * 2 ) ; } int ctr = 1 ; int ans = 0 ; while ( N > 0 ) { if ( N % 10 == 1 ) { ans += powersOfTwo [ ctr - 1 ] ; } else if ( N % 10 > 1 ) { ans = powersOfTwo [ ctr ] - 1 ; } ctr ++ ; N /= 10 ; } return ans ; } static public void Main ( ) { int N = 20 ; Console . Write ( countBinaries ( N ) ) ; } }


"Count of integral points that lie at a distance D from origin | C # program for the above approach ; Function to find the total valid integer coordinates at a distance D from origin ; Stores the count of valid points ; Iterate over possibel x coordinates ; Find the respective y coordinate with the pythagoras theorem ; Adding 4 to compensate the coordinates present on x and y axes . ; Return the answer ; Driver Code"
Solution in C#:

using System ; public class GFG { static int countPoints ( int D ) { int count = 0 ; for ( int x = 1 ; x * x < D * D ; x ++ ) { int y = ( int ) Math . Sqrt ( ( D * D - x * x ) ) ; if ( x * x + y * y == D * D ) { count += 4 ; } } count += 4 ; return count ; } public static void Main ( ) { int D = 5 ; Console . Write ( countPoints ( D ) ) ; } }


"Count of intersections of M line segments with N vertical lines in XY plane | C # implementation for the above approach . ; Function to create prefix sum array ; Initialize the prefix array to remove garbage values ; Marking the occurences of vertical lines ; x is the value after Index mapping ; Creating the prefix array ; Function returns the count of total intersection ; ans is the number of points of intersection of the line segments with the vertical lines ; Index mapping ; We don 't consider a vertical  line segment because even if  it falls on a verticale line  then it just touches it and  not intersects. ; We have assumed that x1 will be left and x2 right but if not then we just swap them ; Driver code ; N is the number of vertical lines M is the number of line segments ; Format : x1 , y1 , x2 , y1 ; First create the prefix array ; Print the total number of intersections"
Solution in C#:

 using System ; class GFG { static void createPrefixArray ( int n , int [ ] arr , int prefSize , int [ ] pref ) { for ( int i = 0 ; i < prefSize ; i ++ ) { pref [ i ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) { int x = arr [ i ] + 1000000 ; pref [ x ] ++ ; } for ( int i = 1 ; i < prefSize ; i ++ ) { pref [ i ] += pref [ i - 1 ] ; } } static int pointsOfIntersection ( int m , int [ , ] segments , int size , int [ ] pref ) { int ans = 0 ; for ( int i = 0 ; i < m ; i ++ ) { int x1 = segments [ i , 0 ] ; int x2 = segments [ i , 2 ] ; x1 = x1 + 1000000 ; x2 = x2 + 1000000 ; if ( x1 != x2 ) { if ( x1 > x2 ) { int temp = x1 ; x1 = x2 ; x2 = temp ; } int Occ_Till_Right = pref [ x2 - 1 ] ; int Occ_Till_Left = pref [ x1 ] ; ans = ans + ( Occ_Till_Right - Occ_Till_Left ) ; } } return ans ; } public static void Main ( String [ ] args ) { int N = 4 ; int M = 8 ; int size = 2000000 + 2 ; int [ ] pref = new int [ size ] ; int [ ] lines = { - 5 , - 3 , 2 , 3 } ; int [ , ] segments = { { - 2 , 5 , 5 , - 6 } , { - 5 , - 2 , - 3 , - 5 } , { - 2 , 3 , - 6 , 1 } , { - 1 , - 3 , 4 , 2 } , { 2 , 5 , 2 , 1 } , { 4 , 5 , 4 , - 5 } , { - 2 , - 4 , 5 , 3 } , { 1 , 2 , - 2 , 1 } } ; createPrefixArray ( N , lines , size , pref ) ; Console . Write ( pointsOfIntersection ( M , segments , size , pref ) + "
 " ) ; } }


"Count of largest sized groups while grouping according to product of digits | C # implementation to count the groups having largest size while grouping is according to the product of its digits ; Function to find out product of digit ; Calculate product ; Return the product of digits ; Function to find the count ; Hash map for counting frequency ; Counting freq of each element ; Find the maximum ; Count the number of groups having size of equal to largest group . ; Driver Code ; Initialise N"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; using System . Text ; class GFG { static int digit_prod ( int x ) { int prod = 1 ; while ( x != 0 ) { prod *= x % 10 ; x = x / 10 ; } return prod ; } static int find_count ( int n ) { Dictionary < int , int > mpp = new Dictionary < int , int > ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { int t = digit_prod ( i ) ; if ( mpp . ContainsKey ( t ) ) { mpp [ t ] ++ ; } else { mpp [ i ] = 1 ; } } int ans = 1 ; int maxm = 0 ; foreach ( KeyValuePair < int , int > x in mpp ) { if ( x . Value > maxm ) { maxm = x . Value ; ans = 1 ; } else if ( x . Value == maxm ) { ans ++ ; } } return ans ; } public static void Main ( string [ ] args ) { int N = 13 ; Console . Write ( find_count ( N ) ) ; } }


"Count of lexicographically smaller characters on right |  ; function to count the smaller characters at the right of index i ; store the length of String ; for each index initialize count as zero ; increment the count if characters are smaller than at ith index ; print the count of characters smaller than the index i ; Driver code ; input String"
Solution in C#:

using System ; class GFG { static void countSmaller ( String str ) { int n = str . Length ; for ( int i = 0 ; i < n ; i ++ ) { int cnt = 0 ; for ( int j = i + 1 ; j < n ; j ++ ) { if ( str [ j ] < str [ i ] ) { cnt += 1 ; } } Console . Write ( cnt + " ▁ " ) ; } } public static void Main ( String [ ] args ) { String str = " edcba " ; countSmaller ( str ) ; } }


"Count of lexicographically smaller characters on right | Function to count the smaller characters on the right of index i ; store the length of String ; initialize each elements of arr to zero ; array to store count of smaller characters on the right side of that index ; initialize the variable to store the count of characters smaller than that at index i ; adding the count of characters smaller than index i ; print the count of characters smaller than index i stored in ans array ; Driver Code"
Solution in C#:

using System ; class GFG { static void countSmaller ( String str ) { int n = str . Length ; int [ ] arr = new int [ 26 ] ; int [ ] ans = new int [ n ] ; for ( int i = n - 1 ; i >= 0 ; i -- ) { arr [ str [ i ] - ' a ' ] ++ ; int ct = 0 ; for ( int j = 0 ; j < str [ i ] - ' a ' ; j ++ ) { ct += arr [ j ] ; } ans [ i ] = ct ; } for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( ans [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { String str = " edcbaa " ; countSmaller ( str ) ; } }


"Count of lines required to write the given String | C # implementation of the approach ; Function to return the number of lines required ; If String is empty ; Initialize lines and width ; Iterate through S ; Return lines and width used ; Driver Code ; Function call to print required answer"
Solution in C#:

 using System ; class GFG { static int [ ] numberOfLines ( String S , int [ ] widths ) { if ( S . Length == 0 ) return new int [ ] { 0 , 0 } ; int lines = 1 , width = 0 ; foreach ( char character in S . ToCharArray ( ) ) { int w = widths [ character - ' a ' ] ; width += w ; if ( width >= 10 ) { lines ++ ; width = w ; } } return new int [ ] { lines , width } ; } public static void Main ( String [ ] args ) { String S = " bbbcccdddaa " ; int [ ] widths = { 4 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 } ; int [ ] ans = numberOfLines ( S , widths ) ; Console . Write ( ans [ 0 ] + " ▁ " + ans [ 1 ] + "
 " ) ; } }


"Count of longest possible subarrays with sum not divisible by K | C # program for the above problem ; Function to find the count of longest subarrays with sum not divisible by K ; Sum of all elements in an array ; If overall sum is not divisible then return 1 , as only one subarray of size n is possible ; Index of the first number not divisible by K ; Index of the last number not divisible by K ; Subarray doesn 't exist ; Sum of the window ; Calculate the sum of rest of the windows of size len ; Driver Code"
Solution in C#:

using System ; class GFG { static int CountLongestSubarrays ( int [ ] arr , int n , int k ) { int i , s = 0 ; for ( i = 0 ; i < n ; ++ i ) { s += arr [ i ] ; } if ( ( s % k ) != 0 ) { return 1 ; } else { int ini = 0 ; while ( ini < n && arr [ ini ] % k == 0 ) { ++ ini ; } int fin = n - 1 ; while ( fin >= 0 && arr [ fin ] % k == 0 ) { -- fin ; } int len , sum = 0 , count = 0 ; if ( ini == n ) { return - 1 ; } else { len = Math . Max ( n - 1 - ini , fin ) ; } for ( i = 0 ; i < len ; i ++ ) { sum += arr [ i ] ; } if ( sum % k != 0 ) { count ++ ; } for ( i = len ; i < n ; i ++ ) { sum = sum + arr [ i ] ; sum = sum - arr [ i - len ] ; if ( sum % k != 0 ) { count ++ ; } } return count ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 2 , 2 , 3 } ; int n = arr . Length ; int k = 3 ; Console . WriteLine ( CountLongestSubarrays ( arr , n , k ) ) ; } }


"Count of m digit integers that are divisible by an integer n | C # program to count m digit numbers having n as divisor . ; Returns count of m digit numbers having n as divisor ; generating largest number of m digit ; generating largest number of m - 1 digit ; returning number of dividend ; main function"
Solution in C#:

using System ; class GfG { static int findCount ( int m , int n ) { int num1 = 0 ; for ( int i = 0 ; i < m ; i ++ ) num1 = ( num1 * 10 ) + 9 ; int num2 = 0 ; for ( int i = 0 ; i < ( m - 1 ) ; i ++ ) num2 = ( num2 * 10 ) + 9 ; return ( ( num1 / n ) - ( num2 / n ) ) ; } public static void Main ( ) { int m = 2 , n = 6 ; Console . Write ( findCount ( m , n ) ) ; } }


"Count of matchsticks required to represent the given number | C # implementation of the approach ; stick [ i ] stores the count of sticks required to represent the digit i ; Function to return the count of matchsticks required to represent the given number ; For every digit of the given number ; Add the count of sticks required to represent the current digit ; Driver code"
Solution in C#:

using System ; class GFG { static int [ ] sticks = { 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 } ; static int countSticks ( String str , int n ) { int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { cnt += ( sticks [ str [ i ] - '0' ] ) ; } return cnt ; } public static void Main ( String [ ] args ) { String str = "56" ; int n = str . Length ; Console . WriteLine ( countSticks ( str , n ) ) ; } }


"Count of maximum occurring subsequence using only those characters whose indices are in GP | C # program for the above approach ; Function to count maximum occurring subsequence using only those characters whose indexes are in GP ; Iterate till the length of the given String ; Update ans for 1 - length subsequence ; Update ans for 2 - length subsequence ; Return the answer ; Driver Code ; Given String s ; Function call"
Solution in C#:

using System ; class GFG { static int findMaxTimes ( String S ) { int [ ] arr = new int [ 26 ] ; int [ , ] dp = new int [ 26 , 26 ] ; for ( int i = 0 ; i < S . Length ; i ++ ) { int now = S [ i ] - ' a ' ; for ( int j = 0 ; j < 26 ; j ++ ) { dp [ j , now ] += arr [ j ] ; } arr [ now ] ++ ; } int ans = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) ans = Math . Max ( ans , arr [ i ] ) ; for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = 0 ; j < 26 ; j ++ ) { ans = Math . Max ( ans , dp [ i , j ] ) ; } } return ans ; } public static void Main ( String [ ] args ) { String S = " ddee " ; Console . Write ( findMaxTimes ( S ) ) ; } }


"Count of minimum numbers having K as the last digit required to obtain sum N | C # program for the above approach ; Stores the smallest number that ends with digit i ( 0 , 9 ) ; Stores the minimum number of steps to create a number ending with digit i ; Initialize elements as infinity ; Minimum number ending with digit i ; Minimum steps to create a number ending with digit i ; If N < SmallestNumber then , return - 1 ; Otherwise , return answer ; Driver Code"
Solution in C#:

using System ; public class GFG { static int minCount ( int N , int K ) { int [ ] SmallestNumber = new int [ 10 ] ; int [ ] MinimumSteps = new int [ 10 ] ; for ( int i = 0 ; i <= 9 ; i ++ ) { SmallestNumber [ i ] = Int32 . MaxValue ; MinimumSteps [ i ] = Int32 . MaxValue ; } for ( int i = 1 ; i <= 10 ; i ++ ) { int num = K * i ; SmallestNumber [ num % 10 ] = Math . Min ( SmallestNumber [ num % 10 ] , num ) ; MinimumSteps [ num % 10 ] = Math . Min ( MinimumSteps [ num % 10 ] , i ) ; } if ( N < SmallestNumber [ N % 10 ] ) { return - 1 ; } else { return MinimumSteps [ N % 10 ] ; } } static public void Main ( ) { int N = 42 , K = 7 ; Console . Write ( minCount ( N , K ) ) ; } }


"Count of multiples in an Array before every element | C # program to count of multiples in an Array before every element ; Function to find all factors of N and keep their count in map ; Traverse from 1 to Math . Sqrt ( N ) if i divides N , increment i and N / i in map ; Function to count of multiples in an Array before every element ; To store factors all of all numbers ; Traverse for all possible i 's ; Printing value of a [ i ] in map ; Now updating the factors of a [ i ] in the map ; Driver code ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void add_factors ( int n , Dictionary < int , int > mp ) { for ( int i = 1 ; i <= ( Math . Sqrt ( n ) ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) { if ( mp . ContainsKey ( i ) ) mp [ i ] = mp [ i ] + 1 ; else mp . Add ( i , 1 ) ; } else { if ( mp . ContainsKey ( i ) ) mp [ i ] = mp [ i ] + 1 ; else mp . Add ( i , 1 ) ; if ( mp . ContainsKey ( n / i ) ) mp [ n / i ] = mp [ n / i ] + 1 ; else mp . Add ( n / i , 1 ) ; } } } } static void count_divisors ( int [ ] a , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( ! mp . ContainsKey ( a [ i ] ) ? 0 + " ▁ " : mp [ a [ i ] ] + " ▁ " ) ; add_factors ( a [ i ] , mp ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 8 , 1 , 28 , 4 , 2 , 6 , 7 } ; int n = arr . Length ; count_divisors ( arr , n ) ; } }


"Count of multiplicative partitions of N | C # implementation to find the multiplicative partitions of the given number N ; Function to return number of ways of factoring N with all factors greater than 1 ; Variable to store number of ways of factoring n with all factors greater than 1 ; Driver code ; 2 is the minimum factor of number other than 1. So calling recursive function to find number of ways of factoring N with all factors greater than 1"
Solution in C#:

using System ; class GFG { static int getDivisors ( int min , int n ) { int total = 0 ; for ( int i = min ; i < n ; ++ i ) if ( n % i == 0 && n / i >= i ) { ++ total ; if ( n / i > i ) total += getDivisors ( i , n / i ) ; } return total ; } public static void Main ( ) { int n = 30 ; Console . Write ( 1 + getDivisors ( 2 , n ) ) ; } }


"Count of n digit numbers whose sum of digits equals to given sum | A C # memoization based recursive program to count numbers with sum of n as given ' sum ' ; A lookup table used for memoization ; Memoization based implementation of recursive function ; Base case ; If this subproblem is already evaluated , return the evaluated value ; Initialize answer ; Traverse through every digit and recursively count numbers beginning with it ; This is mainly a wrapper over countRec . It explicitly handles leading digit and calls countRec ( ) for remaining n . ; Initialize all entries of lookup table ; Initialize final answer ; Traverse through every digit from 1 to 9 and count numbers beginning with it ; Driver program to test above function"
Solution in C#:

using System ; class sum_dig { static int [ , ] lookup = new int [ 101 , 501 ] ; static int countRec ( int n , int sum ) { if ( n == 0 ) return sum == 0 ? 1 : 0 ; if ( lookup [ n , sum ] != - 1 ) return lookup [ n , sum ] ; int ans = 0 ; for ( int i = 0 ; i < 10 ; i ++ ) if ( sum - i >= 0 ) ans += countRec ( n - 1 , sum - i ) ; return lookup [ n , sum ] = ans ; } static int finalCount ( int n , int sum ) { for ( int i = 0 ; i <= 100 ; ++ i ) { for ( int j = 0 ; j <= 500 ; ++ j ) { lookup [ i , j ] = - 1 ; } } int ans = 0 ; for ( int i = 1 ; i <= 9 ; i ++ ) if ( sum - i >= 0 ) ans += countRec ( n - 1 , sum - i ) ; return ans ; } public static void Main ( ) { int n = 3 , sum = 5 ; Console . Write ( finalCount ( n , sum ) ) ; } }


"Count of n digit numbers whose sum of digits equals to given sum | A C # program using recursive to count numbers with sum of digits as given ' sum ' ; Recursive function to count ' n ' digit numbers with sum of digits as ' sum ' . This function considers leading 0 's  also as digits, that is  why not directly called ; Base case ; Initialize answer ; Traverse through every digit and count numbers beginning with it using recursion ; This is mainly a wrapper over countRec . It explicitly handles leading digit and calls countRec ( ) for remaining digits . ; Initialize final answer ; Traverse through every digit from 1 to 9 and count numbers beginning with it ; Driver Code"
Solution in C#:

using System ; class GFG { static int countRec ( int n , int sum ) { if ( n == 0 ) return sum == 0 ? 1 : 0 ; if ( sum == 0 ) return 1 ; int ans = 0 ; for ( int i = 0 ; i <= 9 ; i ++ ) if ( sum - i >= 0 ) ans += countRec ( n - 1 , sum - i ) ; return ans ; } static int finalCount ( int n , int sum ) { int ans = 0 ; for ( int i = 1 ; i <= 9 ; i ++ ) if ( sum - i >= 0 ) ans += countRec ( n - 1 , sum - i ) ; return ans ; } public static void Main ( ) { int n = 2 , sum = 5 ; Console . Write ( finalCount ( n , sum ) ) ; } }


"Count of n digit numbers whose sum of digits equals to given sum | C # program to Count of n digit numbers whose sum of digits equals to given sum ; in case n = 2 start is 10 and end is ( 100 - 1 ) = 99 ; Driver Code"
Solution in C#:

using System ; class GFG { private static void findCount ( int n , int sum ) { int start = ( int ) Math . Pow ( 10 , n - 1 ) ; int end = ( int ) Math . Pow ( 10 , n ) - 1 ; int count = 0 ; int i = start ; while ( i < end ) { int cur = 0 ; int temp = i ; while ( temp != 0 ) { cur += temp % 10 ; temp = temp / 10 ; } if ( cur == sum ) { count ++ ; i += 9 ; } else i ++ ; } Console . WriteLine ( count ) ; } public static void Main ( ) { int n = 3 ; int sum = 5 ; findCount ( n , sum ) ; } }


"Count of natural numbers in range [ L , R ] which are relatively prime with N | C # code to count of natural numbers in range [ L , R ] which are relatively prime with N ; Container of all the primes up to sqrt ( n ) ; Function to calculate prime factors of n ; Run the sieve of Eratosthenes ; 0 ( false ) means prime , 1 ( true ) means not prime ; No even number is prime except for 2 ; All the multiples of each each prime numbers are non - prime ; Get all the primes in prime vector ; Count the number of numbers up to m which are divisible by given prime numbers ; Run from i = 000. . 0 to i = 111. . 1 or check all possible subsets of the array ; If the number of set bits is odd , then add to the number of multiples ; Function calculates all number not greater than ' m ' which are relatively prime with n . ; If square of the prime number is greater than ' n ' , it can ' t ▁ ▁ be ▁ a ▁ factor ▁ of ▁ ' n ' ; If prime is a factor of n then increment count ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int maxN = 100000000 ; static List < int > prime = new List < int > ( ) ; static void sieve ( int n ) { bool [ ] check = new bool [ 1000007 ] ; for ( int I = 0 ; I < 1000007 ; I ++ ) check [ I ] = false ; int i , j ; check [ 0 ] = false ; check [ 1 ] = true ; check [ 2 ] = false ; for ( i = 4 ; i <= n ; i += 2 ) check [ i ] = true ; for ( i = 3 ; i * i <= n ; i += 2 ) if ( ! check [ i ] ) { for ( j = i * i ; j <= n ; j += 2 * i ) check [ j ] = true ; } prime . Add ( 2 ) ; for ( i = 3 ; i <= n ; i += 2 ) if ( ! check [ i ] ) prime . Add ( i ) ; return ; } static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } static int count ( int [ ] a , int n , int m ) { int [ ] parity = new int [ 3 ] ; for ( int i = 0 ; i < 3 ; i ++ ) parity [ i ] = 0 ; for ( int i = 1 ; i < ( 1 << n ) ; i ++ ) { int mult = 1 ; for ( int j = 0 ; j < n ; j ++ ) if ( ( i & ( 1 << j ) ) != 0 ) mult *= a [ j ] ; parity [ countSetBits ( i ) & 1 ] += ( m / mult ) ; } return parity [ 1 ] - parity [ 0 ] ; } static int countRelPrime ( int n , int m ) { int [ ] a = new int [ 20 ] ; int i = 0 , j = 0 ; int pz = prime . Count ; while ( n != 1 && i < pz ) { if ( ( int ) prime [ i ] * ( int ) prime [ i ] > n ) break ; if ( n % prime [ i ] == 0 ) { a [ j ] = ( int ) prime [ i ] ; j ++ ; } while ( n % prime [ i ] == 0 ) n /= prime [ i ] ; i ++ ; } if ( n != 1 ) { a [ j ] = n ; j ++ ; } return m - count ( a , j , m ) ; } static void countRelPrimeInRange ( int n , int l , int r ) { sieve ( ( int ) Math . Sqrt ( maxN ) ) ; int result = countRelPrime ( n , r ) - countRelPrime ( n , l - 1 ) ; Console . WriteLine ( result ) ; } static void Main ( ) { int N = 7 , L = 3 , R = 9 ; countRelPrimeInRange ( N , L , R ) ; } }


"Count of nested polygons that can be drawn by joining vertices internally | C # program for the above approach ; Function that counts the nested polygons inside another polygons ; Stores the count ; Child polygons can only existss if parent polygon has sides > 5 ; Get next nested polygon ; Return the count ; Driver Code ; Given side of polygon ; Function call"
Solution in C#:

using System ; class GFG { static int countNestedPolygons ( int sides ) { int count = 0 ; while ( sides > 5 ) { sides /= 2 ; count += 1 ; } return count ; } public static void Main ( String [ ] args ) { int N = 12 ; Console . Write ( countNestedPolygons ( N ) ) ; } }


"Count of nodes having odd divisors in the given subtree for Q queries | C # implementation to count the number of nodes having odd number of divisors for each query ; Adjacency list for tree . ; Array for values and answer at ith node . ; Function to check whether N has odd divisors or not ; DFS function to pre - compute the answers ; Initialize the count ; Repeat for every child ; Increase the count if current node has odd number of divisors ; Driver Code ; Adjacency List ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static readonly int N = 100001 ; static List < int > [ ] adj = new List < int > [ N ] ; static int [ ] a = new int [ N ] ; static int [ ] ans = new int [ N ] ; static bool hasOddNumberOfDivisors ( int n ) { if ( ( double ) Math . Sqrt ( n ) == ( int ) Math . Sqrt ( n ) ) return true ; return false ; } static int dfs ( int node , int parent ) { int count = 0 ; foreach ( int i in adj [ node ] ) { if ( i != parent ) { count += dfs ( i , node ) ; } } if ( hasOddNumberOfDivisors ( a [ node ] ) ) ++ count ; ans [ node ] = count ; return count ; } public static void Main ( String [ ] args ) { int n = 5 ; int [ ] q = { 4 , 1 , 5 , 3 } ; for ( int i = 0 ; i < adj . Length ; i ++ ) adj [ i ] = new List < int > ( ) ; adj [ 1 ] . Add ( 2 ) ; adj [ 2 ] . Add ( 1 ) ; adj [ 2 ] . Add ( 3 ) ; adj [ 3 ] . Add ( 2 ) ; adj [ 3 ] . Add ( 4 ) ; adj [ 4 ] . Add ( 3 ) ; adj [ 1 ] . Add ( 5 ) ; adj [ 5 ] . Add ( 1 ) ; a [ 1 ] = 4 ; a [ 2 ] = 9 ; a [ 3 ] = 14 ; a [ 4 ] = 100 ; a [ 5 ] = 5 ; dfs ( 1 , - 1 ) ; for ( int i = 0 ; i < q . Length ; i ++ ) { Console . Write ( ans [ q [ i ] ] + " ▁ " ) ; } } }


"Count of nodes in a Binary Tree whose child is its prime factors | C # program for Counting nodes whose immediate children are its factors ; To store all prime numbers ; Create a bool array " prime [ 0 . . N ] " and initialize all its entries as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiples of p and are less than p ^ 2 are already marked . ; A Tree node ; Utility function to create a new node ; Function to check if immediate children of a node are its factors or not ; Function to get the count of full Nodes in a binary tree ; If tree is empty ; Initialize count of full nodes having children as their factors ; If only right child exist ; If only left child exist ; Both left and right child exist ; Check for left child ; Check for right child ; Driver Code ; Create Binary Tree as shown ; To save all prime numbers ; Print Count of all nodes having children as their factors"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { static int N = 1000000 ; static List < int > prime = new List < int > ( ) ; static void SieveOfEratosthenes ( ) { bool [ ] check = new bool [ N + 1 ] ; for ( int i = 0 ; i <= N ; i += 1 ) check [ i ] = true ; for ( int p = 2 ; p * p <= N ; p ++ ) { if ( check [ p ] == true ) { prime . Add ( p ) ; for ( int i = p * p ; i <= N ; i += p ) check [ i ] = false ; } } } class Node { public int key ; public Node left , right ; } ; static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . left = temp . right = null ; return ( temp ) ; } static bool IsChilrenPrimeFactor ( Node parent , Node a ) { if ( prime [ a . key ] > 0 && ( parent . key % a . key == 0 ) ) return true ; else return false ; } static int GetCount ( Node node ) { if ( node == null ) return 0 ; List < Node > q = new List < Node > ( ) ; int count = 0 ; q . Add ( node ) ; while ( q . Count != 0 ) { Node temp = q [ 0 ] ; q . RemoveAt ( 0 ) ; if ( temp . left == null && temp . right != null ) { if ( IsChilrenPrimeFactor ( temp , temp . right ) ) count ++ ; } else { if ( temp . right == null && temp . left != null ) { if ( IsChilrenPrimeFactor ( temp , temp . left ) ) count ++ ; } else { if ( temp . left != null && temp . right != null ) { if ( IsChilrenPrimeFactor ( temp , temp . right ) && IsChilrenPrimeFactor ( temp , temp . left ) ) count ++ ; } } } if ( temp . left != null ) q . Add ( temp . left ) ; if ( temp . right != null ) q . Add ( temp . right ) ; } return count ; } public static void Main ( String [ ] args ) { Node root = newNode ( 10 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 5 ) ; root . right . left = newNode ( 18 ) ; root . right . right = newNode ( 12 ) ; root . right . left . left = newNode ( 2 ) ; root . right . left . right = newNode ( 3 ) ; root . right . right . left = newNode ( 3 ) ; root . right . right . right = newNode ( 14 ) ; root . right . right . right . left = newNode ( 7 ) ; SieveOfEratosthenes ( ) ; Console ....