"Count quadruples from four sorted arrays whose sum is equal to a given value x | C # implementation to count quadruples from four sorted arrays whose sum is equal to a given value x ; find the ' value ' in the given array ' arr [ ] ' binary search technique is applied ; ' value ' found ; ' value ' not found ; function to count all quadruples from four sorted arrays whose sum is equal to a given value x ; generate all triplets from the 1 st three arrays ; calculate the sum of elements in the triplet so generated ; check if ' x - T ' is present in 4 th array or not ; increment count ; required count of quadruples ; Driver code ; four sorted arrays each of size ' n '"
Solution in C#:

using System ; class GFG { static bool isPresent ( int [ ] arr , int low , int high , int value ) { while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( arr [ mid ] == value ) return true ; else if ( arr [ mid ] > value ) high = mid - 1 ; else low = mid + 1 ; } return false ; } static int countQuadruples ( int [ ] arr1 , int [ ] arr2 , int [ ] arr3 , int [ ] arr4 , int n , int x ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) for ( int k = 0 ; k < n ; k ++ ) { int T = arr1 [ i ] + arr2 [ j ] + arr3 [ k ] ; if ( isPresent ( arr4 , 0 , n - 1 , x - T ) ) count ++ ; } return count ; } public static void Main ( String [ ] args ) { int [ ] arr1 = { 1 , 4 , 5 , 6 } ; int [ ] arr2 = { 2 , 3 , 7 , 8 } ; int [ ] arr3 = { 1 , 4 , 6 , 10 } ; int [ ] arr4 = { 2 , 4 , 7 , 8 } ; int n = 4 ; int x = 30 ; Console . WriteLine ( " Count ▁ = ▁ " + countQuadruples ( arr1 , arr2 , arr3 , arr4 , n , x ) ) ; } }


"Count quadruples from four sorted arrays whose sum is equal to a given value x | C # implementation to count quadruples from four sorted arrays whose sum is equal to a given value x ; function to count all quadruples from four sorted arrays whose sum is equal to a given value x ; unordered_map ' um ' implemented as hash table for < sum , frequency > tuples ; count frequency of each sum obtained from the pairs of arr1 [ ] and arr2 [ ] and store them in ' um ' ; generate pair from arr3 [ ] and arr4 [ ] ; calculate the sum of elements in the pair so generated ; if ' x - p _ sum ' is present in ' um ' then add frequency of ' x - p _ sum ' to ' count ' ; required count of quadruples ; Driver code ; four sorted arrays each of size ' n '"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int countQuadruples ( int [ ] arr1 , int [ ] arr2 , int [ ] arr3 , int [ ] arr4 , int n , int x ) { int count = 0 ; Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( m . ContainsKey ( arr1 [ i ] + arr2 [ j ] ) ) { var val = m [ arr1 [ i ] + arr2 [ j ] ] ; m . Remove ( arr1 [ i ] + arr2 [ j ] ) ; m . Add ( ( arr1 [ i ] + arr2 [ j ] ) , val + 1 ) ; } else m . Add ( ( arr1 [ i ] + arr2 [ j ] ) , 1 ) ; for ( int k = 0 ; k < n ; k ++ ) for ( int l = 0 ; l < n ; l ++ ) { int p_sum = arr3 [ k ] + arr4 [ l ] ; if ( m . ContainsKey ( x - p_sum ) ) count += m [ x - p_sum ] ; } return count ; } public static void Main ( String [ ] args ) { int [ ] arr1 = { 1 , 4 , 5 , 6 } ; int [ ] arr2 = { 2 , 3 , 7 , 8 } ; int [ ] arr3 = { 1 , 4 , 6 , 10 } ; int [ ] arr4 = { 2 , 4 , 7 , 8 } ; int n = arr1 . Length ; int x = 30 ; Console . WriteLine ( " Count ▁ = ▁ " + countQuadruples ( arr1 , arr2 , arr3 , arr4 , n , x ) ) ; } }


"Count quadruplets ( A , B , C , D ) till N such that sum of square of A and B is equal to that of C and D | C # program for the above approach ; Function to count the quadruples ; Counter variable ; Map to store the sum of pair ( a ^ 2 + b ^ 2 ) ; Iterate till N ; Calculate a ^ 2 + b ^ 2 ; Increment the value in map ; Check if this sum was also in a ^ 2 + b ^ 2 ; Return the count ; Driver code ; Given N ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static long countQuadraples ( long N ) { long cnt = 0 ; Dictionary < long , long > m = new Dictionary < long , long > ( ) ; for ( long a = 1 ; a <= N ; a ++ ) { for ( long b = 1 ; b <= N ; b ++ ) { long x = a * a + b * b ; if ( m . ContainsKey ( x ) ) m [ x ] = m [ x ] + 1 ; else m . Add ( x , 1 ) ; } } for ( long c = 1 ; c <= N ; c ++ ) { for ( long d = 1 ; d <= N ; d ++ ) { long x = c * c + d * d ; if ( m . ContainsKey ( x ) ) cnt += m [ x ] ; } } return cnt ; } public static void Main ( String [ ] args ) { long N = 2 ; Console . WriteLine ( countQuadraples ( N ) ) ; } }


"Count quadruplets with sum K from given array | C # program for the above approach ; Function to return the number of quadruplets having given sum ; Initialize variables ; Initialize answer ; All possible first elements ; All possible second element ; Use map to find the fourth element ; All possible third elements ; Calculate number of valid 4 th elements ; Update the twice_count ; Unordered pairs ; Return answer ; Driver Code ; Given array [ ] arr ; Given sum S ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int countSum ( int [ ] a , int n , int sum ) { int i , j , k ; int l ; int count = 0 ; for ( i = 0 ; i < n - 3 ; i ++ ) { for ( j = i + 1 ; j < n - 2 ; j ++ ) { int req = sum - a [ i ] - a [ j ] ; Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( k = j + 1 ; k < n ; k ++ ) if ( m . ContainsKey ( a [ k ] ) ) { m [ a [ k ] ] ++ ; } else { m . Add ( a [ k ] , 1 ) ; } int twice_count = 0 ; for ( k = j + 1 ; k < n ; k ++ ) { if ( m . ContainsKey ( req - a [ k ] ) ) twice_count += m [ req - a [ k ] ] ; if ( req - a [ k ] == a [ k ] ) twice_count -- ; } count += twice_count / 2 ; } } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 5 , 3 , 1 , 2 , 4 } ; int S = 13 ; int N = arr . Length ; Console . Write ( countSum ( arr , N , S ) ) ; } }


"Count quadruplets with sum K from given array | C # program for the above approach ; Function to return the number of quadruplets having the given sum ; Initialize variables ; Initialize answer ; Store the frequency of sum of first two elements ; Traverse from 0 to N - 1 , where arr [ i ] is the 3 rd element ; All possible 4 th elements ; Sum of last two element ; Frequency of sum of first two elements ; Store frequency of all possible sums of first two elements ; Return the answer ; Driver Code ; Given array [ ] arr ; Given sum S ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int countSum ( int [ ] a , int n , int sum ) { int i , j ; int count = 0 ; Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( i = 0 ; i < n - 1 ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { int temp = a [ i ] + a [ j ] ; if ( temp < sum && m . ContainsKey ( sum - temp ) ) count += m [ sum - temp ] ; } for ( j = 0 ; j < i ; j ++ ) { int temp = a [ i ] + a [ j ] ; if ( temp < sum ) if ( m . ContainsKey ( temp ) ) m [ temp ] ++ ; else m . Add ( temp , 1 ) ; } } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 5 , 3 , 1 , 2 , 4 } ; int S = 13 ; int N = arr . Length ; Console . Write ( countSum ( arr , N , S ) ) ; } }


"Count quadruplets with sum K from given array | C # program for the above approach ; Function to return the number of quadruplets with the given sum ; Initialize variables ; Initialize answer ; All possible first elements ; All possible second elements ; All possible third elements ; All possible fourth elements ; Increment counter by 1 if quadruplet sum is S ; Return the final count ; Driver Code ; Given array arr [ ] ; Given sum S ; Function Call"
Solution in C#:

class GFG { static int countSum ( int [ ] a , int n , int sum ) { int i , j , k , l ; int count = 0 ; for ( i = 0 ; i < n - 3 ; i ++ ) { for ( j = i + 1 ; j < n - 2 ; j ++ ) { for ( k = j + 1 ; k < n - 1 ; k ++ ) { for ( l = k + 1 ; l < n ; l ++ ) { if ( a [ i ] + a [ j ] + a [ k ] + a [ l ] == sum ) count ++ ; } } } } return count ; } public static void Main ( ) { int [ ] arr = { 4 , 5 , 3 , 1 , 2 , 4 } ; int S = 13 ; int N = arr . Length ; System . Console . Write ( countSum ( arr , N , S ) ) ; } }


"Count rectangles generated in a given rectangle by lines drawn parallel to X and Y axis from a given set of points | C # program to implement the above approach ; Function to get the count of ractangles ; Store distinct horizontal lines ; Store distinct Vertical lines ; Insert horizontal line passing through 0 ; Insert vertical line passing through 0. ; Insert horizontal line passing through rectangle [ 3 , 0 ] ; Insert vertical line passing through rectangle [ 3 , 1 ] ; Insert all horizontal and vertical lines passing through the given array ; Insert all horizontal lines ; Insert all vertical lines ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static int cntRect ( int [ , ] points , int N , int [ , ] rectangle ) { HashSet < int > cntHor = new HashSet < int > ( ) ; HashSet < int > cntVer = new HashSet < int > ( ) ; cntHor . Add ( 0 ) ; cntVer . Add ( 0 ) ; cntHor . Add ( rectangle [ 3 , 0 ] ) ; cntVer . Add ( rectangle [ 3 , 1 ] ) ; for ( int i = 0 ; i < N ; i ++ ) { cntHor . Add ( points [ i , 0 ] ) ; cntVer . Add ( points [ i , 1 ] ) ; } return ( cntHor . Count - 1 ) * ( cntVer . Count - 1 ) ; } public static void Main ( String [ ] args ) { int [ , ] rectangle = { { 0 , 0 } , { 0 , 5 } , { 5 , 0 } , { 5 , 5 } } ; int [ , ] points = { { 1 , 2 } , { 3 , 4 } } ; int N = points . GetLength ( 0 ) ; Console . WriteLine ( cntRect ( points , N , rectangle ) ) ; } }


"Count remaining array elements after reversing binary representation of each array element | C # program for the above approach ; Function to reverse the binary representation of a number ; Traverse bits of N from the right ; Bitwise left shift ' rev ' by 1 ; If current bit is '1' ; Bitwise right shift N by 1 ; Required number ; Function to count elements from the original array that are also present in the array formed by reversing the binary representation of each element ; Stores the reversed num ; Iterate from [ 0 , N ] ; Stores the presence of integer ; Stores count of elements present in original array ; Traverse the array ; If current number is present ; Print the answer ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int findReverse ( int N ) { int rev = 0 ; while ( N > 0 ) { rev <<= 1 ; if ( ( N & 1 ) == 1 ) rev ^= 1 ; N >>= 1 ; } return rev ; } static void countElements ( int [ ] arr , int N ) { List < int > ans = new List < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { ans . Add ( findReverse ( arr [ i ] ) ) ; } Dictionary < int , int > cnt = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { cnt [ arr [ i ] ] = 1 ; } int count = 0 ; foreach ( int i in ans ) { if ( cnt . ContainsKey ( i ) ) count ++ ; } Console . WriteLine ( count ) ; } public static void Main ( ) { int [ ] arr = { 1 , 30 , 3 , 8 , 12 } ; int N = arr . Length ; countElements ( arr , N ) ; } }


"Count rotations divisible by 4 | C # program to count all rotation divisible by 4. ; Returns count of all rotations divisible by 4 ; For single digit number ; At - least 2 digit number ( considering all pairs ) ; Considering the number formed by the pair of last digit and 1 st digit ; Driver program"
Solution in C#:

using System ; class GFG { static int countRotations ( String n ) { int len = n . Length ; if ( len == 1 ) { int oneDigit = n [ 0 ] - '0' ; if ( oneDigit % 4 == 0 ) return 1 ; return 0 ; } int twoDigit , count = 0 ; for ( int i = 0 ; i < ( len - 1 ) ; i ++ ) { twoDigit = ( n [ i ] - '0' ) * 10 + ( n [ i + 1 ] - '0' ) ; if ( twoDigit % 4 == 0 ) count ++ ; } twoDigit = ( n [ len - 1 ] - '0' ) * 10 + ( n [ 0 ] - '0' ) ; if ( twoDigit % 4 == 0 ) count ++ ; return count ; } public static void Main ( ) { String n = "4834" ; Console . Write ( " Rotations : ▁ " + countRotations ( n ) ) ; } }


"Count rotations divisible by 8 | C # program to count all rotations divisible by 8 ; function to count of all rotations divisible by 8 ; For single digit number ; For two - digit numbers ( considering all pairs ) ; first pair ; second pair ; considering all three - digit sequences ; Considering the number formed by the last digit and the first two digits ; Considering the number formed by the last two digits and the first digit ; required count of rotations ; Driver Code"
Solution in C#:

using System ; class GFG { static int countRotationsDivBy8 ( String n ) { int len = n . Length ; int count = 0 ; if ( len == 1 ) { int oneDigit = n [ 0 ] - '0' ; if ( oneDigit % 8 == 0 ) return 1 ; return 0 ; } if ( len == 2 ) { int first = ( n [ 0 ] - '0' ) * 10 + ( n [ 1 ] - '0' ) ; int second = ( n [ 1 ] - '0' ) * 10 + ( n [ 0 ] - '0' ) ; if ( first % 8 == 0 ) count ++ ; if ( second % 8 == 0 ) count ++ ; return count ; } int threeDigit ; for ( int i = 0 ; i < ( len - 2 ) ; i ++ ) { threeDigit = ( n [ i ] - '0' ) * 100 + ( n [ i + 1 ] - '0' ) * 10 + ( n [ i + 2 ] - '0' ) ; if ( threeDigit % 8 == 0 ) count ++ ; } threeDigit = ( n [ len - 1 ] - '0' ) * 100 + ( n [ 0 ] - '0' ) * 10 + ( n [ 1 ] - '0' ) ; if ( threeDigit % 8 == 0 ) count ++ ; threeDigit = ( n [ len - 2 ] - '0' ) * 100 + ( n [ len - 1 ] - '0' ) * 10 + ( n [ 0 ] - '0' ) ; if ( threeDigit % 8 == 0 ) count ++ ; return count ; } public static void Main ( ) { String n = "43262488612" ; Console . Write ( " Rotations : ▁ " + countRotationsDivBy8 ( n ) ) ; } }


"Count rotations of N which are Odd and Even | CSharp implementation of the above approach ; Function to count of all rotations which are odd and even ; Driver Code"
Solution in C#:

using System ; class Solution { static void countOddRotations ( int n ) { int odd_count = 0 , even_count = 0 ; do { int digit = n % 10 ; if ( digit % 2 == 1 ) odd_count ++ ; else even_count ++ ; n = n / 10 ; } while ( n != 0 ) ; Console . WriteLine ( " Odd ▁ = ▁ " + odd_count ) ; Console . WriteLine ( " Even ▁ = ▁ " + even_count ) ; } public static void Main ( ) { int n = 1234 ; countOddRotations ( n ) ; } }


"Count rotations required to sort given array in non | C # program for the above approach ; Function to count minimum anti - clockwise rotations required to sort the array in non - increasing order ; Stores count of arr [ i + 1 ] > arr [ i ] ; Store last index of arr [ i + 1 ] > arr [ i ] ; Traverse the given array ; If the adjacent elements are in increasing order ; Increment count ; Update index ; Print the result according to the following conditions ; Otherwise , it is not possible to sort the array ; Driver Code ; Given array ; Function Call"
Solution in C#:

using System ; class GFG { static void minMovesToSort ( int [ ] arr , int N ) { int count = 0 ; int index = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] < arr [ i + 1 ] ) { count ++ ; index = i ; } } if ( count == 0 ) { Console . Write ( "0" ) ; } else if ( count == N - 1 ) { Console . Write ( N - 1 ) ; } else if ( count == 1 && arr [ 0 ] <= arr [ N - 1 ] ) { Console . Write ( index + 1 ) ; } else { Console . Write ( " - 1" ) ; } } public static void Main ( ) { int [ ] arr = { 2 , 1 , 5 , 4 , 2 } ; int N = arr . Length ; minMovesToSort ( arr , N ) ; } }


"Count rotations which are divisible by 10 | CSharp implementation to find the count of rotations which are divisible by 10 ; Function to return the count of all rotations which are divisible by 10. ; Loop to iterate through the number ; If the last digit is 0 , then increment the count ; Driver code"
Solution in C#:

using System ; class Solution { static int countRotation ( int n ) { int count = 0 ; do { int digit = n % 10 ; if ( digit % 2 == 0 ) count ++ ; n = n / 10 ; } while ( n != 0 ) ; return count ; } public static void Main ( ) { int n = 10203 ; Console . Write ( countRotation ( n ) ) ; } }


"Count sequences of given length having non | C # program for the above approach ; Function to find the Binomial Coefficient C ( n , r ) ; Stores the value C ( n , r ) ; Update C ( n , r ) = C ( n , n - r ) ; Find C ( n , r ) iteratively ; Return the final value ; Function to find number of sequence whose prefix sum at each index is always non - negative ; Find n ; Value of C ( 2 n , n ) ; Catalan number ; Print the answer ; Driver Code ; Given M and X ; Function Call"
Solution in C#:

using System ; class GFG { static long binCoff ( long n , long r ) { long val = 1 ; int i ; if ( r > ( n - r ) ) r = ( n - r ) ; for ( i = 0 ; i < r ; i ++ ) { val *= ( n - i ) ; val /= ( i + 1 ) ; } return val ; } static void findWays ( int M , int X ) { int n = M / 2 ; long a , b ; a = binCoff ( 2 * n , n ) ; b = a / ( n + 1 ) ; Console . WriteLine ( b ) ; } public static void Main ( ) { int M = 4 ; int X = 5 ; findWays ( M , X ) ; } }


"Count sequences of length K having each term divisible by its preceding term | C # program to implement the above approach ; Stores the factors of i - th element in v [ i ] ; Function to find all the factors of N ; Iterate upto Math . Sqrt ( N ) ; Function to return the count of sequences of length K having aint terms divisible by its preceding term ; Calculate factors of i ; Initialize dp [ 0 , i ] = 0 : No subsequence of length 0 ending with i - th element exists ; Initialize dp [ 0 , i ] = 1 : Only 1 subsequence of length 1 ending with i - th element exists ; Iterate [ 2 , K ] to obtain sequences of each length ; Calculate sum of aint dp [ i - 1 , vp [ j , k ] ] ; vp [ j , k ] stores aint factors of j ; Store the sum in A [ i , j ] ; Sum of aint dp [ K , j ] obtain all K length sequences ending with j ; Driver code"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static List < int > [ ] vp = new List < int > [ 2009 ] ; static void finding_factors ( int n ) { int i ; for ( i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( i * i == n ) { vp [ n ] . Add ( i ) ; } else { vp [ n ] . Add ( i ) ; vp [ n ] . Add ( n / i ) ; } } } } static int countSeq ( int N , int K ) { int i , j , k ; int [ , ] dp = new int [ 109 , 109 ] ; for ( i = 1 ; i <= N ; i ++ ) { finding_factors ( i ) ; dp [ 0 , i ] = 0 ; dp [ 1 , i ] = 1 ; } for ( i = 2 ; i <= K ; i ++ ) { for ( j = 1 ; j <= N ; j ++ ) { int sum = 0 ; for ( k = 0 ; k < vp [ j ] . Count ; k ++ ) { sum = ( sum + dp [ i - 1 , vp [ j ] [ k ] ] ) ; } dp [ i , j ] = sum ; } } int ans = 0 ; for ( j = 1 ; j <= N ; j ++ ) { ans = ( ans + dp [ K , j ] ) ; } return ans ; } public static void Main ( String [ ] args ) { int N , K ; N = 3 ; K = 2 ; for ( int i = 0 ; i < vp . Length ; i ++ ) vp [ i ] = new List < int > ( ) ; Console . Write ( countSeq ( N , K ) + "
 " ) ; } }


"Count sequences of positive integers having product X | C # program for the above approach ; Function to print the total number of possible sequences with product X ; Precomputation of binomial coefficients ; Max length of a subsequence ; Ways dp array ; Fill i slots using all the primes ; Subtract ways for all slots that exactly fill less than i slots ; Total possible sequences ; Print the resultant count ; Driver Code ; Function call"
Solution in C#:

 using System ; class GFG { static int [ , ] bin = new int [ 3000 , 3000 ] ; static void countWays ( int [ ] arr ) { int mod = ( int ) 1e9 + 7 ; bin [ 0 , 0 ] = 1 ; for ( int i = 1 ; i < 3000 ; i ++ ) { bin [ i , 0 ] = 1 ; for ( int j = 1 ; j <= i ; j ++ ) { bin [ i , j ] = ( bin [ i - 1 , j ] + bin [ i - 1 , j - 1 ] ) % mod ; } } int n = 0 ; foreach ( int x in arr ) n += x ; int [ ] ways = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { ways [ i ] = 1 ; for ( int j = 0 ; j < arr . Length ; j ++ ) { ways [ i ] = ( ways [ i ] * bin [ arr [ j ] + i - 1 , i - 1 ] ) % mod ; } for ( int j = 1 ; j < i ; j ++ ) { ways [ i ] = ( ( ways [ i ] - bin [ i , j ] * ways [ j ] ) % mod + mod ) % mod ; } } int ans = 0 ; foreach ( int x in ways ) ans = ( ans + x ) % mod ; Console . Write ( ans + "
 " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 } ; countWays ( arr ) ; } }


"Count set bits in Bitwise XOR of all adjacent elements upto N | C # program to implement the above approach ; Function to count of set bits in Bitwise XOR of adjacent elements up to N ; Stores count of set bits by Bitwise XOR on adjacent elements of [ 0 , N ] ; Stores all possible values on right most set bit over [ 0 , N ] ; Iterate over the range [ 0 , N ] ; Update N ; Update bit_Position ; Driver Code"
Solution in C#:

using System ; class GFG { static int countXORSetBitsAdjElemRange1_N ( int N ) { int total_set_bits = 0 ; int bit_Position = 1 ; while ( N != 0 ) { total_set_bits += ( ( N + 1 ) / 2 * bit_Position ) ; N -= ( N + 1 ) / 2 ; bit_Position ++ ; } return total_set_bits ; } public static void Main ( ) { int N = 4 ; Console . Write ( countXORSetBitsAdjElemRange1_N ( N ) ) ; } }


"Count set bits in a range | C # implementation to count set bits in the given range ; Function to get no of set bits in the binary representation of ' n ' ; function to count set bits in the given range ; calculating a number ' num ' having ' r ' number of bits and bits in the range l to r are the only set bits ; returns number of set bits in the range ' l ' to ' r ' in ' n ' ; Driver code"
Solution in C#:

using System ; class GFG { static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } static int countSetBitsInGivenRange ( int n , int l , int r ) { int num = ( ( 1 << r ) - 1 ) ^ ( ( 1 << ( l - 1 ) ) - 1 ) ; return countSetBits ( n & num ) ; } public static void Main ( ) { int n = 42 ; int l = 2 , r = 5 ; Console . WriteLine ( countSetBitsInGivenRange ( n , l , r ) ) ; } }


"Count set bits in the Kth number after segregating even and odd from N natural numbers | C # implementation of the above approach ; Function to return the kth element of the Odd - Even sequence of length n ; Finding the index from where the even numbers will be stored ; Return the kth element ; Function to return the count of set bits in the kth number of the odd even sequence of length n ; Required kth number ; Return the count of set bits ; Driver code"
Solution in C#:

using System ; class GFG { static int findK ( int n , int k ) { int pos ; if ( n % 2 == 0 ) { pos = n / 2 ; } else { pos = ( n / 2 ) + 1 ; } if ( k <= pos ) { return ( k * 2 - 1 ) ; } else return ( ( k - pos ) * 2 ) ; } static int countSetBits ( int n , int k ) { int kth = findK ( n , k ) ; int count = 0 ; while ( kth > 0 ) { count += kth & 1 ; kth >>= 1 ; } return count ; } public static void Main ( String [ ] args ) { int n = 18 , k = 12 ; Console . WriteLine ( countSetBits ( n , k ) ) ; } }


"Count smaller elements on right side and greater elements on left side using Binary Index Tree | C # implementation of the above approach ; Function to return the sum of arr [ 0. . index ] . This function assumes that the array is preprocessed and partial sums of array elements are stored in BITree [ ] ; Initialize result ; Traverse ancestors of BITree [ index ] ; Add current element of BITree to sum ; Move index to parent node in getSum View ; Updates a node in Binary Index Tree ( BITree ) at given index in BITree . The given value ' val ' is added to BITree [ i ] and all of its ancestors in tree . ; Traverse all ancestors and add ' val ' ; Add ' val ' to current node of BI Tree ; Update index to that of parent in update View ; Converts an array to an array with values from 1 to n and relative order of smaller and greater elements remains same . For example , { 7 , - 90 , 100 , 1 } is converted to { 3 , 1 , 4 , 2 } ; Create a copy of arrp [ ] in temp and sort the temp array in increasing order ; Traverse all array elements ; Arrays . binarySearch ( ) returns index to the first element greater than or equal to arr [ i ] ; Function to find smaller_right array ; Convert [ ] arr to an array with values from 1 to n and relative order of smaller and greater elements remains same . For example , { 7 , - 90 , 100 , 1 } is converted to { 3 , 1 , 4 , 2 } ; Create a BIT with size equal to maxElement + 1 ( Extra one is used so that elements can be directly be used as index ) ; To store smaller elements in right side and greater elements on left side ; Traverse all elements from right . ; Get count of elements smaller than arr [ i ] ; Add current element to BIT ; Print smaller_right array ; Find all left side greater elements ; Get count of elements greater than arr [ i ] ; Add current element to BIT ; Print greater_left array ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { public static int getSum ( int [ ] BITree , int index ) { int sum = 0 ; while ( index > 0 ) { sum += BITree [ index ] ; index -= index & ( - index ) ; } return sum ; } public static void updateBIT ( int [ ] BITree , int n , int index , int val ) { while ( index <= n ) { BITree [ index ] += val ; index += index & ( - index ) ; } } public static void convert ( int [ ] arr , int n ) { int [ ] temp = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) temp [ i ] = arr [ i ] ; Array . Sort ( temp ) ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = Array . BinarySearch ( temp , arr [ i ] ) + 1 ; } } public static void findElements ( int [ ] arr , int n ) { convert ( arr , n ) ; int [ ] BIT = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) BIT [ i ] = 0 ; int [ ] smaller_right = new int [ n ] ; int [ ] greater_left = new int [ n ] ; for ( int i = n - 1 ; i >= 0 ; i -- ) { smaller_right [ i ] = getSum ( BIT , arr [ i ] - 1 ) ; updateBIT ( BIT , n , arr [ i ] , 1 ) ; } Console . Write ( " Smaller ▁ right : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( smaller_right [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; for ( int i = 1 ; i <= n ; i ++ ) BIT [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { greater_left [ i ] = i - getSum ( BIT , arr [ i ] ) ; updateBIT ( BIT , n , arr [ i ] , 1 ) ; } Console . Write ( " Greater ▁ left : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( greater_left [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 12 , 1 , 2 , 3 , 0 , 11 , 4 } ; int n = arr . Length ; findElements ( arr , n ) ; } }


"Count smaller elements on right side |  ; initialize all the counts in countSmaller array as 0 ; Utility function that prints out an array on a line ; Driver function"
Solution in C#:

using System ; class GFG { static void constructLowerArray ( int [ ] arr , int [ ] countSmaller , int n ) { int i , j ; for ( i = 0 ; i < n ; i ++ ) countSmaller [ i ] = 0 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] < arr [ i ] ) countSmaller [ i ] ++ ; } } } static void printArray ( int [ ] arr , int size ) { int i ; for ( i = 0 ; i < size ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . WriteLine ( " " ) ; } public static void Main ( ) { int [ ] arr = new int [ ] { 12 , 10 , 5 , 4 , 2 , 20 , 6 , 1 , 0 , 2 } ; int n = arr . Length ; int [ ] low = new int [ n ] ; constructLowerArray ( arr , low , n ) ; printArray ( low , n ) ; } }


"Count smaller elements present in the array for each array element | C # program for the above approach ; Function to count for each array element , the number of elements that are smaller than that element ; Stores the frequencies of array elements ; Traverse the array ; Update frequency of arr [ i ] ; Compute prefix sum of the array hash [ ] ; Traverse the array arr [ ] ; If current element is 0 ; Print the resultant count ; Driver Code"
Solution in C#:

using System ; class GFG { static void smallerNumbers ( int [ ] arr , int N ) { int [ ] hash = new int [ 100000 ] ; for ( int i = 0 ; i < N ; i ++ ) hash [ arr [ i ] ] ++ ; for ( int i = 1 ; i < 100000 ; i ++ ) { hash [ i ] += hash [ i - 1 ] ; } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) { Console . WriteLine ( "0" ) ; continue ; } Console . Write ( hash [ arr [ i ] - 1 ] + " ▁ " ) ; } } public static void Main ( string [ ] args ) { int [ ] arr = { 3 , 4 , 1 , 1 , 2 } ; int N = arr . Length ; smallerNumbers ( arr , N ) ; } }


"Count smaller elements present in the array for each array element | C # program for the above approach ; Function to count for each array element , the number of elements that are smaller than that element ; Traverse the array ; Stores the count ; Traverse the array ; Increment count ; Print the count of smaller elements for the current element ; Driver Code"
Solution in C#:

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


"Count smaller numbers whose XOR with n produces greater value | C # program to count numbers whose XOR with n produces a value more than n . ; Position of current bit in n ; Traverse bits from LSB ( least significant bit ) to MSB ; Initialize result ; If the current bit is 0 , then there are 2 ^ k numbers with current bit 1 and whose XOR with n produces greater value ; Increase position for next bit ; Reduce n to find next bit ; Driver code"
Solution in C#:

using System ; class GFG { static int countNumbers ( int n ) { int k = 0 ; int count = 0 ; while ( n > 0 ) { if ( ( n & 1 ) == 0 ) count += ( int ) ( Math . Pow ( 2 , k ) ) ; k += 1 ; n >>= 1 ; } return count ; } public static void Main ( ) { int n = 11 ; Console . WriteLine ( countNumbers ( n ) ) ; } }


"Count smaller primes on the right of each array element | C # Program for the above approach include " bits / stdJava . h " ; Function to check if a number is prime or not ; Function to update a Binary Tree ; Function to find the sum of all the elements which are less than or equal to index ; Function to find the number of smaller primes on the right for every array element ; Iterate the array in backwards ; Calculating the required number of primes ; If current array element is prime ; Update the Fenwick tree ; Driver Code ; Function call"
Solution in C#:

using System ; class GFG { static int maxn = ( int ) 1e6 + 5 ; static int [ ] BITree = new int [ maxn ] ; static bool is_prime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i * i <= n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } static void update_bitree ( int [ ] BITree , int index , int value ) { while ( index <= maxn ) { BITree [ index ] += value ; index += ( index & ( - index ) ) ; } } static int sum_bitree ( int [ ] BITree , int index ) { int s = 0 ; while ( index > 0 ) { s += BITree [ index ] ; index -= ( index & ( - index ) ) ; } return s ; } static void countSmallerPrimes ( int [ ] BITree , int [ ] ar , int N ) { int [ ] ans = new int [ N ] ; for ( int i = N - 1 ; i >= 0 ; i -- ) { ans [ i ] = sum_bitree ( BITree , ar [ i ] ) ; if ( is_prime ( ar [ i ] ) ) update_bitree ( BITree , ar [ i ] , 1 ) ; } for ( int i = 0 ; i < N ; i ++ ) Console . Write ( ans [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] ar = { 5 , 5 , 17 , 9 , 12 , 15 , 11 , 7 , 39 , 3 } ; int N = ar . Length ; countSmallerPrimes ( BITree , ar , N ) ; } }


"Count smaller primes on the right of each array element | C # program for the above approach ; Function to check if a number is prime or not ; Function to find the count of smaller primes on the right of each array element ; Stores the count of smaller primes ; If A [ j ] <= A [ i ] and A [ j ] is prime ; Increase count ; Print the count for the current element ; Driver Code ; Function call"
Solution in C#:

using System ; class GFG { static bool is_prime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } static void countSmallerPrimes ( int [ ] ar , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int count = 0 ; for ( int j = i + 1 ; j < N ; j ++ ) { if ( ar [ j ] <= ar [ i ] && is_prime ( ar [ j ] ) ) { count ++ ; } } Console . Write ( count + " ▁ " ) ; } } public static void Main ( ) { int [ ] ar = { 43 , 3 , 5 , 7 , 2 , 41 } ; int N = ar . Length ; countSmallerPrimes ( ar , N ) ; } }


"Count smaller values whose XOR with x is greater than x | C # program to find count of values whose XOR with x is greater than x and values are smaller than x ; Initialize result ; Traversing through all bits of x ; If current last bit of x is set then increment count by n . Here n is a power of 2 corresponding to position of bit ; Simultaneously calculate the 2 ^ n ; Replace x with x / 2 ; ; Driver code"
Solution in C#:

using System ; class GFG { static int countValues ( int x ) { int count = 0 , n = 1 ; while ( x != 0 ) { if ( x % 2 == 0 ) count += n ; n *= 2 ; x /= 2 ; } return count ; } public static void Main ( ) { int x = 10 ; Console . Write ( countValues ( x ) ) ; } }


"Count special palindromes in a String | C # program to count special Palindromic substring ; Function to count special Palindromic susbstring ; store count of special Palindromic substring ; it will store the count of continues same char ; traverse string character from left to right ; store same character count ; count smiler character ; Case : 1 so total number of substring that we can generate are : K * ( K + 1 ) / 2 here K is sameCharCount ; store current same char count in sameChar [ ] array ; increment i ; Case 2 : Count all odd length Special Palindromic substring ; if current character is equal to previous one then we assign Previous same character count to current one ; case 2 : odd length ; subtract all single length substring ; Driver code"
Solution in C#:

using System ; class GFG { public static int CountSpecialPalindrome ( String str ) { int n = str . Length ; int result = 0 ; int [ ] sameChar = new int [ n ] ; for ( int v = 0 ; v < n ; v ++ ) sameChar [ v ] = 0 ; int i = 0 ; while ( i < n ) { int sameCharCount = 1 ; int j = i + 1 ; while ( j < n && str [ i ] == str [ j ] ) { sameCharCount ++ ; j ++ ; } result += ( sameCharCount * ( sameCharCount + 1 ) / 2 ) ; sameChar [ i ] = sameCharCount ; i = j ; } for ( int j = 1 ; j < n ; j ++ ) { if ( str [ j ] == str [ j - 1 ] ) sameChar [ j ] = sameChar [ j - 1 ] ; if ( j > 0 && j < ( n - 1 ) && ( str [ j - 1 ] == str [ j + 1 ] && str [ j ] != str [ j - 1 ] ) ) result += Math . Min ( sameChar [ j - 1 ] , sameChar [ j + 1 ] ) ; } return result - n ; } public static void Main ( ) { String str = " abccba " ; Console . Write ( CountSpecialPalindrome ( str ) ) ; } }


"Count squares of size K inscribed in a square of size N | C # implementation of the above approach ; Function to calculate the number of squares of size K in a square of size N ; Stores the number of squares ; Driver Code ; Size of the bigger square ; Size of smaller square"
Solution in C#:

using System ; class GFG { static int No_of_squares ( int N , int K ) { int no_of_squares = 0 ; no_of_squares = ( N - K + 1 ) * ( N - K + 1 ) ; return no_of_squares ; } public static void Main ( String [ ] args ) { int N = 5 ; int K = 3 ; Console . Write ( No_of_squares ( N , K ) ) ; } }


"Count squares possible from M and N straight lines parallel to X and Y axis respectively | C # program for the above approach ; Function to count all the possible squares with given lines parallel to both the X and Y axis ; Stores the count of all possible distances in X [ ] & Y [ ] respectively ; Find distance between all pairs in the array X [ ] ; Add the count to m1 ; Find distance between all pairs in the array Y [ ] ; Add the count to m2 ; Find sum of m1 [ i ] * m2 [ i ] for same distance ; Find current count in m2 ; Add to the total count ; Return the final count ; Driver Code ; Given lines ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int numberOfSquares ( int [ ] X , int [ ] Y , int N , int M ) { Dictionary < int , int > m1 = new Dictionary < int , int > ( ) ; Dictionary < int , int > m2 = new Dictionary < int , int > ( ) ; int i , j , ans = 0 ; for ( i = 0 ; i < N ; i ++ ) { for ( j = i + 1 ; j < N ; j ++ ) { int dist = Math . Abs ( X [ i ] - X [ j ] ) ; if ( m1 . ContainsKey ( dist ) ) m1 [ dist ] ++ ; else m1 . Add ( dist , 1 ) ; } } for ( i = 0 ; i < M ; i ++ ) { for ( j = i + 1 ; j < M ; j ++ ) { int dist = Math . Abs ( Y [ i ] - Y [ j ] ) ; if ( m2 . ContainsKey ( dist ) ) m2 [ dist ] ++ ; else m2 . Add ( dist , 1 ) ; } } foreach ( KeyValuePair < int , int > entry in m1 ) { if ( m2 . ContainsKey ( entry . Key ) ) { ans += ( entry . Value * m2 [ entry . Key ] ) ; } } return ans ; } public static void Main ( ) { int [ ] X = { 1 , 3 , 7 } ; int [ ] Y = { 2 , 4 , 6 , 1 } ; int N = X . Length ; int M = Y . Length ; Console . WriteLine ( numberOfSquares ( X , Y , N , M ) ) ; } }


"Count squares with odd side length in Chessboard | C # implementation of the approach ; Function to return the count of odd length squares possible ; To store the required count ; For all odd values of i ; Add the count of possible squares of length i ; Return the required count ; Driver code"
Solution in C#:

using System ; class GFG { static int count_square ( int n ) { int count = 0 ; for ( int i = 1 ; i <= n ; i = i + 2 ) { int k = n - i + 1 ; count += ( k * k ) ; } return count ; } public static void Main ( ) { int N = 8 ; Console . WriteLine ( count_square ( N ) ) ; } }


"Count strings from given array having all characters appearing in a given string | C # program to implement the above approach ; Function to count the number of Strings from an array having all characters appearing in the String S ; Initialize a set to store all distinct characters of String S ; Traverse over String S ; Insert characters into the Set ; Stores the required count ; Traverse the array ; Traverse over String arr [ i ] ; Check if character in arr [ i , j ] is present in the String S or not ; Increment the count if all the characters of arr [ i ] are present in the String S ; Finally , print the count ; Driver code"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static int countStrings ( String S , String [ ] list ) { HashSet < char > valid = new HashSet < char > ( ) ; foreach ( char x in S . ToCharArray ( ) ) { valid . Add ( x ) ; } int cnt = 0 ; for ( int i = 0 ; i < list . Length ; i ++ ) { int j = 0 ; for ( j = 0 ; j < list [ i ] . Length ; j ++ ) { if ( valid . Contains ( list [ i ] [ j ] ) ) continue ; else break ; } if ( j == list [ i ] . Length ) cnt ++ ; } return cnt ; } public static void Main ( String [ ] args ) { String [ ] arr = { " ab " , " aab " , " abaaaa " , " bbd " } ; String S = " ab " ; Console . Write ( countStrings ( S , arr ) + "
 " ) ; } }


"Count strings having sum of ASCII values of characters equal to a Prime or Armstrong Number | C # program for the above approach ; Function to check if a number is prime number ; Define a flag variable ; Check for factors of num ; If factor is found , set flag to True and break out of loop ; Check if flag is True ; Function to calculate order of the number x ; Function to check whether the given number is Armstrong number or not ; If the condition satisfies ; Function to count Armstrong valued strings ; Stores the count of Armstrong valued strings ; Iterate over the list ; Store the value of the string ; Find value of the string ; Check if it an Armstrong number ; Function to count prime valued strings ; Store the count of prime valued strings ; Iterate over the list ; Store the value of the string ; Find value of the string ; Check if it is a Prime Number ; Driver code ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static bool isPrime ( int num ) { bool flag = false ; if ( num > 1 ) { for ( int i = 2 ; i < num ; i ++ ) { if ( ( num % i ) == 0 ) { flag = true ; break ; } } } if ( flag ) return false ; else return true ; } static int order ( int x ) { int n = 0 ; while ( x != 0 ) { n = n + 1 ; x = x / 10 ; } return n ; } static bool isArmstrong ( int x ) { int n = order ( x ) ; int temp = x ; int sum1 = 0 ; while ( temp != 0 ) { int r = temp % 10 ; sum1 = sum1 + ( int ) ( Math . Pow ( r , n ) ) ; temp = temp / 10 ; } return ( sum1 == x ) ; } static int count_armstrong ( string [ ] li ) { int c = 0 ; foreach ( string ele in li ) { int val = 0 ; foreach ( char che in ele ) val += che ; if ( isArmstrong ( val ) ) c += 1 ; } return c ; } static int count_prime ( string [ ] li ) { int c = 0 ; foreach ( string ele in li ) { int val = 0 ; foreach ( char che in ele ) val += che ; if ( isPrime ( val ) ) c += 1 ; } return c ; } public static void Main ( ) { string [ ] arr = { " geeksforgeeks " , " a " , " computer " , " science " , " portal " , " for " , " geeks " } ; Console . WriteLine ( " Number ▁ of ▁ Armstrong ▁ Strings ▁ are : ▁ " + count_armstrong ( arr ) ) ; Console . WriteLine ( " Number ▁ of ▁ Prime ▁ Strings ▁ are : ▁ " + count_prime ( arr ) ) ; } }


"Count strings that end with the given pattern | C # implementation of the approach ; Function that return true if str ends with pat ; Pattern is larger in length than the string ; We match starting from the end while patLen is greater than or equal to 0. ; If at any index str doesn 't match  with pattern ; If str ends with the given pattern ; Function to return the count of required strings ; If current string ends with the given pattern ; Driver code"
Solution in C#:

using System ; class GFG { static bool endsWith ( string str , string pat ) { int patLen = pat . Length ; int strLen = str . Length ; if ( patLen > strLen ) return false ; patLen -- ; strLen -- ; while ( patLen >= 0 ) { if ( pat [ patLen ] != str [ strLen ] ) return false ; patLen -- ; strLen -- ; } return true ; } static int countOfStrings ( string pat , int n , string [ ] sArr ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( endsWith ( sArr [ i ] , pat ) ) count ++ ; return count ; } public static void Main ( ) { string pat = " ks " ; int n = 4 ; string [ ] sArr = { " geeks " , " geeksforgeeks " , " games " , " unit " } ; Console . WriteLine ( countOfStrings ( pat , n , sArr ) ) ; } }


"Count strings with consecutive 1 's | program to count all distinct binary strings with two consecutive 1 's ; Returns count of n length binary strings with consecutive 1 's ; Count binary strings without consecutive 1 's.  See the approach discussed on  ( http:goo.gl/p8A3sW ) ; Subtract a [ n - 1 ] + b [ n - 1 ] from 2 ^ n ; Driver code"
Solution in C#:

using System ; class GFG { static int countStrings ( int n ) { int [ ] a = new int [ n ] ; int [ ] b = new int [ n ] ; a [ 0 ] = b [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { a [ i ] = a [ i - 1 ] + b [ i - 1 ] ; b [ i ] = a [ i - 1 ] ; } return ( 1 << n ) - a [ n - 1 ] - b [ n - 1 ] ; } public static void Main ( ) { Console . WriteLine ( countStrings ( 5 ) ) ; } }


"Count strings with consonants and vowels at alternate position | C # implementation of above approach ; Function to find the count of strings ; Variable to store the final result ; Loop iterating through string ; If ' $ ' is present at the even position in the string ; ' sum ' is multiplied by 21 ; If ' $ ' is present at the odd position in the string ; ' sum ' is multiplied by 5 ; Driver code ; Let the string ' str ' be s$$e$ ; Print result"
Solution in C#:

using System ; class GFG { static int countStrings ( String s ) { int sum = 1 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( i % 2 == 0 && s [ i ] == ' $ ' ) sum *= 21 ; else if ( s [ i ] == ' $ ' ) sum *= 5 ; } return sum ; } public static void Main ( ) { String str = " s $ $ e $ " ; Console . WriteLine ( countStrings ( str ) ) ; } }


"Count sub | C # implementation for above approach ; Segment tree implemented as an array ; Function to build the segment tree ; Function to query product of sub - array [ l . . r ] in O ( log n ) time ; Function to count sub - arrays whose product is divisible by K ; Query segment tree to find product % k of the sub - array [ i . . j ] ; Driver code ; Build the segment tree"
Solution in C#:

using System ; class GFG { static int MAX = 100002 ; static long [ ] tree = new long [ 4 * MAX ] ; static void build ( int node , int start , int end , int [ ] arr , int k ) { if ( start == end ) { tree [ node ] = ( 1L * arr [ start ] ) % k ; return ; } int mid = ( start + end ) >> 1 ; build ( 2 * node , start , mid , arr , k ) ; build ( 2 * node + 1 , mid + 1 , end , arr , k ) ; tree [ node ] = ( tree [ 2 * node ] * tree [ 2 * node + 1 ] ) % k ; } static long query ( int node , int start , int end , int l , int r , int k ) { if ( start > end start > r end < l ) { return 1 ; } if ( start >= l && end <= r ) { return tree [ node ] % k ; } int mid = ( start + end ) >> 1 ; long q1 = query ( 2 * node , start , mid , l , r , k ) ; long q2 = query ( 2 * node + 1 , mid + 1 , end , l , r , k ) ; return ( q1 * q2 ) % k ; } static long countSubarrays ( int [ ] arr , int n , int k ) { long count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { long product_mod_k = query ( 1 , 0 , n - 1 , i , j , k ) ; if ( product_mod_k == 0 ) { count ++ ; } } } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 6 , 2 , 8 } ; int n = arr . Length ; int k = 4 ; build ( 1 , 0 , n - 1 , arr , k ) ; Console . WriteLine ( countSubarrays ( arr , n , k ) ) ; } }


"Count sub | C # implementation of the approach ; Function to count sub - arrays whose product is divisible by K ; Calculate the product of the current sub - array ; If product of the current sub - array is divisible by K ; Driver code"
Solution in C#:

using System ; class GFG { static int countSubarrays ( int [ ] arr , int n , int K ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { long product = 1 ; for ( int x = i ; x <= j ; x ++ ) product *= arr [ x ] ; if ( product % K == 0 ) count ++ ; } } return count ; } public static void Main ( ) { int [ ] arr = { 6 , 2 , 8 } ; int n = arr . Length ; int K = 4 ; Console . WriteLine ( countSubarrays ( arr , n , K ) ) ; } }


"Count sub | C # implementation to count sub - matrices having sum divisible by the value ' k ' ; function to count all sub - arrays divisible by k ; create auxiliary hash array to count frequency of remainders ; Traverse original array and compute cumulative sum take remainder of this current cumulative sum and increase count by 1 for this remainder in mod [ ] array ; as the sum can be negative , taking modulo twice ; Initialize result ; Traverse mod [ ] ; If there are more than one prefix subarrays with a particular mod value . ; add the subarrays starting from the arr [ i ] which are divisible by k itself ; function to count all sub - matrices having sum divisible by the value ' k ' ; Variable to store the final output ; Set the left column ; Set the right column for the left column set by outer loop ; Calculate sum between current left and right for every row ' i ' ; Count number of subarrays in temp [ ] having sum divisible by ' k ' and then add it to ' tot _ count ' ; required count of sub - matrices having sum divisible by ' k ' ; Driver code"
Solution in C#:

using System ; class GFG { static int subCount ( int [ ] arr , int n , int k ) { int [ ] mod = new int [ k ] ; int cumSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { cumSum += arr [ i ] ; mod [ ( ( cumSum % k ) + k ) % k ] ++ ; } int result = 0 ; for ( int i = 0 ; i < k ; i ++ ) if ( mod [ i ] > 1 ) result += ( mod [ i ] * ( mod [ i ] - 1 ) ) / 2 ; result += mod [ 0 ] ; return result ; } static int countSubmatrix ( int [ , ] mat , int n , int k ) { int tot_count = 0 ; int left , right , i ; int [ ] temp = new int [ n ] ; for ( left = 0 ; left < n ; left ++ ) { for ( right = left ; right < n ; right ++ ) { for ( i = 0 ; i < n ; ++ i ) temp [ i ] += mat [ i , right ] ; tot_count += subCount ( temp , n , k ) ; } } return tot_count - 3 ; } static void Main ( ) { int [ , ] mat = new int [ , ] { { 5 , - 1 , 6 } , { - 2 , 3 , 8 } , { 7 , 4 , - 9 } } ; int n = 3 , k = 4 ; Console . Write ( " Count = " countSubmatrix ( mat , n , k ) ) ; } }


"Count subarrays consisting of first K natural numbers in descending order | C # program for the above approach ; Function to count subarray having the decreasing sequence K to 1 ; Traverse the array ; Check if required sequence is present or not ; Reset temp to k ; Return the count ; Driver code ; Function Call"
Solution in C#:

using System ; class GFG { static int CountSubarray ( int [ ] arr , int n , int k ) { int temp = k , count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == temp ) { if ( temp == 1 ) { count ++ ; temp = k ; } else temp -- ; } else { temp = k ; if ( arr [ i ] == k ) i -- ; } } return count ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 7 , 9 , 3 , 2 , 1 , 8 , 3 , 2 , 1 } ; int N = arr . Length ; int K = 3 ; Console . Write ( CountSubarray ( arr , N , K ) ) ; } }


"Count subarrays for every array element in which they are the minimum | C # implementation of the above approach ; Function to required count subarrays ; For storing count of subarrays ; For finding next smaller element left to a element if there is no next smaller element left to it than taking - 1. ; For finding next smaller element right to a element if there is no next smaller element right to it than taking n . ; Taking exact boundaries in which arr [ i ] is minimum ; Similarly for right side ; Driver code ; Given array arr [ ] ; Function call"
Solution in C#:

using System ; using System . Collections ; class GFG { static int [ ] countingSubarray ( int [ ] arr , int n ) { int [ ] a = new int [ n ] ; int [ ] nsml = new int [ n ] ; Array . Fill ( nsml , - 1 ) ; int [ ] nsmr = new int [ n ] ; Array . Fill ( nsmr , n ) ; Stack st = new Stack ( ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { while ( st . Count > 0 && arr [ ( int ) st . Peek ( ) ] >= arr [ i ] ) st . Pop ( ) ; nsmr [ i ] = ( st . Count > 0 ) ? ( int ) st . Peek ( ) : n ; st . Push ( i ) ; } while ( st . Count > 0 ) st . Pop ( ) ; for ( int i = 0 ; i < n ; i ++ ) { while ( st . Count > 0 && arr [ ( int ) st . Peek ( ) ] >= arr [ i ] ) st . Pop ( ) ; nsml [ i ] = ( st . Count > 0 ) ? ( int ) st . Peek ( ) : - 1 ; st . Push ( i ) ; } for ( int i = 0 ; i < n ; i ++ ) { nsml [ i ] ++ ; nsmr [ i ] -- ; int r = nsmr [ i ] - i + 1 ; int l = i - nsml [ i ] + 1 ; a [ i ] = r * l ; } return a ; } static void Main ( ) { int N = 5 ; int [ ] arr = { 3 , 2 , 4 , 1 , 5 } ; int [ ] a = countingSubarray ( arr , N ) ; Console . Write ( " [ " ) ; int n = a . Length - 1 ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( a [ i ] + " , ▁ " ) ; Console . Write ( a [ n ] + " ] " ) ; } }


"Count subarrays for every array element in which they are the minimum | C # program for the above approach ; Function to find the boundary of every element within which it is minimum ; Perform Binary Search ; Find mid m ; Update l ; Update r ; Inserting the index ; Function to required count subarrays ; Stores the indices of element ; Initialize the output array ; Left boundary , till the element is smallest ; Right boundary , till the element is smallest ; Calculate the number of subarrays based on its boundary ; Adding cnt to the ans ; Driver code ; Given array arr [ ] ; Function call"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int binaryInsert ( ArrayList boundary , int i ) { int l = 0 ; int r = boundary . Count - 1 ; while ( l <= r ) { int m = ( l + r ) / 2 ; if ( ( int ) boundary [ m ] < i ) l = m + 1 ; else r = m - 1 ; } boundary . Insert ( l , i ) ; return l ; } static int [ ] countingSubarray ( int [ ] arr , int n ) { Dictionary < int , int > index = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) index [ arr [ i ] ] = i ; ArrayList boundary = new ArrayList ( ) ; boundary . Add ( - 1 ) ; boundary . Add ( n ) ; Array . Sort ( arr ) ; int [ ] ans = new int [ n ] ; foreach ( int num in arr ) { int i = binaryInsert ( boundary , index [ num ] ) ; int l = ( int ) boundary [ i ] - ( int ) boundary [ i - 1 ] - 1 ; int r = ( int ) boundary [ i + 1 ] - ( int ) boundary [ i ] - 1 ; int cnt = l + r + l * r + 1 ; ans [ index [ num ] ] += cnt ; } return ans ; } public static void Main ( string [ ] args ) { int N = 5 ; int [ ] arr = { 3 , 2 , 4 , 1 , 5 } ; int [ ] a = countingSubarray ( arr , N ) ; Console . Write ( " [ " ) ; int n = a . Length - 1 ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( a [ i ] + " , ▁ " ) ; Console . Write ( a [ n ] + " ] " ) ; } }


"Count subarrays for every array element in which they are the minimum | Set 2 | C # program for the above approach ; Function to calculate total number of sub - arrays for each element where that element is occurring as the minimum element ; Map for storing the number of sub - arrays for each element ; Traverse over all possible subarrays ; Minimum in each subarray ; Print the result ; Driver Code ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void minSubarray ( int [ ] arr , int N ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { int mini = int . MaxValue ; for ( int j = i ; j < N ; j ++ ) { mini = Math . Min ( mini , arr [ j ] ) ; if ( mp . ContainsKey ( mini ) ) { mp [ mini ] = mp [ mini ] + 1 ; } else { mp . Add ( mini , 1 ) ; } } } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( mp [ arr [ i ] ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 1 , 4 } ; int N = arr . Length ; minSubarray ( arr , N ) ; } }


"Count subarrays for every array element in which they are the minimum | Set 2 | C # program for the above approach ; Function to count subarrays for each element where it is the minimum ; For the length of strictly larger numbers on the left of A [ i ] ; Storing x in result [ i ] ; For the length of strictly larger numbers on the right of A [ i ] ; Store x * y in result array ; Print the result ; Driver Code ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void minSubarray ( int [ ] arr , int N ) { int [ ] result = new int [ N ] ; Stack < pair > l = new Stack < pair > ( ) ; Stack < pair > r = new Stack < pair > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { int count = 1 ; while ( l . Count != 0 && l . Peek ( ) . first > arr [ i ] ) { count += l . Peek ( ) . second ; l . Pop ( ) ; } l . Push ( new pair ( arr [ i ] , count ) ) ; result [ i ] = count ; } for ( int i = N - 1 ; i >= 0 ; i -- ) { int count = 1 ; while ( r . Count != 0 && r . Peek ( ) . first >= arr [ i ] ) { count += r . Peek ( ) . second ; r . Pop ( ) ; } r . Push ( new pair ( arr [ i ] , count ) ) ; result [ i ] *= count ; } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( result [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 1 , 4 } ; int N = arr . Length ; minSubarray ( arr , N ) ; } }


"Count subarrays having a single distinct element that can be obtained from a given array | C # program for the above approach ; Function to count subarrays of single distinct element into which given array can be split ; Stores the count ; Stores frequency of array elements ; Traverse the array ; Traverse the map ; Increase count of subarrays by ( frequency - 1 ) ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void divisionalArrays ( int [ ] arr , int N ) { int sum = N ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1 ; } else { mp . Add ( arr [ i ] , 1 ) ; } } foreach ( KeyValuePair < int , int > x in mp ) { if ( ( int ) x . Value > 1 ) { sum += ( int ) x . Value - 1 ; } } Console . WriteLine ( sum ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 3 } ; int N = arr . Length ; divisionalArrays ( arr , N ) ; } }


"Count subarrays having each distinct element occurring at least twice | C # program to implement the above approach ; Function to get the count of subarrays having each element occurring at least twice ; Stores count of subarrays having each distinct element occurring at least twice ; Stores count of unique elements in a subarray ; Store frequency of each element of a subarray ; Traverse the given array ; Count frequency and check conditions for each subarray ; Update frequency ; Check if frequency of arr [ j ] equal to 1 ; Update Count of unique elements ; Update count of unique elements ; If each element of subarray occurs at least twice ; Update cntSub ; Remove all elements from the subarray ; Update cntUnique ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int cntSubarrays ( int [ ] arr , int N ) { int cntSub = 0 ; int cntUnique = 0 ; Dictionary < int , int > cntFreq = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i ; j < N ; j ++ ) { if ( cntFreq . ContainsKey ( arr [ j ] ) ) { var val = cntFreq [ arr [ j ] ] ; cntFreq . Remove ( arr [ j ] ) ; cntFreq . Add ( arr [ j ] , val + 1 ) ; } else { cntFreq . Add ( arr [ j ] , 1 ) ; } if ( cntFreq [ arr [ j ] ] == 1 ) { cntUnique ++ ; } else if ( cntFreq [ arr [ j ] ] == 2 ) { cntUnique -- ; } if ( cntUnique == 0 ) { cntSub ++ ; } } cntFreq . Clear ( ) ; cntUnique = 0 ; } return cntSub ; } public static void Main ( ) { int [ ] arr = { 1 , 1 , 2 , 2 , 2 } ; int N = arr . Length ; Console . Write ( cntSubarrays ( arr , N ) ) ; } }


"Count subarrays having even Bitwise OR | C # program for the above approach ; Function to count the number of subarrays having even Bitwise OR ; Store number of subarrays having even bitwise OR ; Store the length of the current subarray having even numbers ; Traverse the array ; If the element is even ; Increment size of the current continuous sequence of even array elements ; If arr [ i ] is odd ; If length is non zero ; Adding contribution of subarrays consisting only of even numbers ; Make length of subarray as 0 ; Add contribution of previous subarray ; Return total count of subarrays ; Driver code ; Function Call"
Solution in C#:

using System ; class GFG { static int bitOr ( int [ ] arr , int N ) { int count = 0 ; int length = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { length ++ ; } else { if ( length != 0 ) { count += ( ( length ) * ( length + 1 ) ) / 2 ; } length = 0 ; } } count += ( ( length ) * ( length + 1 ) ) / 2 ; return count ; } static void Main ( ) { int [ ] arr = { 1 , 5 , 4 , 2 , 6 } ; int N = arr . Length ; Console . Write ( bitOr ( arr , N ) ) ; } }


"Count subarrays having even Bitwise XOR | C # program for the above approach ; Function to count subarrays having even Bitwise XOR ; Store the required result ; Stores count of subarrays with even and odd XOR values ; Stores Bitwise XOR of current subarray ; Traverse the array ; Update current Xor ; If XOR is even ; Update ans ; Increment count of subarrays with even XOR ; Otherwise , increment count of subarrays with odd XOR ; Print the result ; Driver Code ; Given array ; Stores the size of the array"
Solution in C#:

using System ; class GFG { static void evenXorSubarray ( int [ ] arr , int n ) { int ans = 0 ; int [ ] freq = { 0 , 0 } ; int XOR = 0 ; for ( int i = 0 ; i < n ; i ++ ) { XOR = XOR ^ arr [ i ] ; if ( XOR % 2 == 0 ) { ans += freq [ 0 ] + 1 ; freq [ 0 ] ++ ; } else { ans += freq [ 1 ] ; freq [ 1 ] ++ ; } } Console . WriteLine ( ans ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int N = arr . Length ; evenXorSubarray ( arr , N ) ; } }


"Count subarrays having even Bitwise XOR | C # program for the above approach ; Function to count the number of subarrays having even Bitwise XOR ; Store the required result ; Generate subarrays with arr [ i ] as the first element ; Store XOR of current subarray ; Generate subarrays with arr [ j ] as the last element ; Calculate Bitwise XOR of the current subarray ; If XOR is even , increase ans by 1 ; Print the result ; Driver Code ; Given array ; Stores the size of the array ; Function Call"
Solution in C#:

using System ; class GFG { static void evenXorSubarray ( int [ ] arr , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int XOR = 0 ; for ( int j = i ; j < n ; j ++ ) { XOR = XOR ^ arr [ j ] ; if ( ( XOR & 1 ) == 0 ) ans ++ ; } } Console . WriteLine ( ans ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int N = arr . Length ; evenXorSubarray ( arr , N ) ; } }


"Count subarrays having odd Bitwise XOR | C # program for the above approach ; Function to count the number of subarrays of the given array having odd Bitwise XOR ; Stores number of odd numbers upto i - th index ; Stores number of required subarrays starting from i - th index ; Store the required result ; Find the number of subarrays having odd Bitwise XOR values starting at 0 - th index ; Check if current element is odd ; If the current value of odd is not zero , increment c_odd by 1 ; Find the number of subarrays having odd bitwise XOR value starting at ith index and add to result ; Add c_odd to result ; Print the result ; Driver Code ; Given array ; Stores the size of the array"
Solution in C#:

using System ; class GFG { static void oddXorSubarray ( int [ ] a , int n ) { int odd = 0 ; int c_odd = 0 ; int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 != 0 ) { odd = ( odd == 0 ) ? 1 : 0 ; } if ( odd != 0 ) { c_odd ++ ; } } for ( int i = 0 ; i < n ; i ++ ) { result += c_odd ; if ( a [ i ] % 2 != 0 ) { c_odd = ( n - i - c_odd ) ; } } Console . WriteLine ( result ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 4 , 7 , 9 , 10 } ; int N = arr . Length ; oddXorSubarray ( arr , N ) ; } }


"Count subarrays having product equal to the power of a given Prime Number | C # program for the above approach ; Function to check if y is a power of m or not ; Calculate log y base m and store it in a variable with integer datatype ; Calculate log y base m and store it in a variable with double datatype ; If res1 and res2 are equal , return True . Otherwise , return false ; Function to count the number of subarrays having product of elements equal to a power of m , where m is a prime number ; Stores the count of subarrays required ; Stores current sequence of consecutive array elements which are a multiple of m ; Traverse the array ; If arr [ i ] is a power of M ; Increment cnt ; Update ans ; Update cnt ; Return the count of subarrays ; Driver code ; Input"
Solution in C#:

using System ; class GFG { static bool isPower ( int m , int y ) { int res1 = ( int ) Math . Log ( y ) / ( int ) Math . Log ( m ) ; double res2 = ( int ) Math . Log ( y ) / ( int ) Math . Log ( m ) ; return ( res1 == res2 ) ; } static int numSub ( int [ ] arr , int n , int m ) { int ans = 0 ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPower ( m , arr [ i ] ) ) { cnt ++ ; ans += ( cnt * ( cnt - 1 ) ) / 2 ; } else { cnt = 0 ; } } return ans ; } public static void Main ( ) { int [ ] arr = { 1 , 1 , 1 , 3 } ; int m = 3 ; int n = arr . Length ; Console . Write ( numSub ( arr , n , m ) ) ; } }


"Count subarrays having sum of elements at even and odd positions equal | C # program for the above approach ; Function to count subarrays in which sum of elements at even and odd positions are equal ; Initialize variables ; Iterate over the array ; Check if position is even then add to sum then add it to sum ; else subtract it to sum ; Increment the count if the sum equals 0 ; Print the count of subarrays ; Driver Code ; Given array [ ] arr ; Size of the array ; Function call"
Solution in C#:

using System ; class GFG { static void countSubarrays ( int [ ] arr , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int sum = 0 ; for ( int j = i ; j < n ; j ++ ) { if ( ( j - i ) % 2 == 0 ) sum += arr [ j ] ; else sum -= arr [ j ] ; if ( sum == 0 ) count ++ ; } } Console . WriteLine ( count ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 6 , 4 , 2 } ; int n = arr . Length ; countSubarrays ( arr , n ) ; } }


"Count subarrays having total distinct elements same as original array | C # program Count total number of sub - arrays having total distinct elements same as that original array . ; Method to calculate distinct sub - array ; Count distinct elements in whole array ; Reset the container by removing all elements ; Use sliding window concept to find count of subarrays having k distinct elements . ; If window size equals to array distinct element size , then update answer ; Decrease the frequency of previous element for next sliding window ; If frequency is zero then decrease the window size ; Driver method"
Solution in C#:

using System ; using System . Collections . Generic ; class Test { static int countDistictSubarray ( int [ ] arr , int n ) { Dictionary < int , int > vis = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; ++ i ) if ( ! vis . ContainsKey ( arr [ i ] ) ) vis . Add ( arr [ i ] , 1 ) ; int k = vis . Count ; vis . Clear ( ) ; int ans = 0 , right = 0 , window = 0 ; for ( int left = 0 ; left < n ; ++ left ) { while ( right < n && window < k ) { if ( vis . ContainsKey ( arr [ right ] ) ) vis [ arr [ right ] ] = vis [ arr [ right ] ] + 1 ; else vis . Add ( arr [ right ] , 1 ) ; if ( vis [ arr [ right ] ] == 1 ) ++ window ; ++ right ; } if ( window == k ) ans += ( n - right + 1 ) ; if ( vis . ContainsKey ( arr [ left ] ) ) vis [ arr [ left ] ] = vis [ arr [ left ] ] - 1 ; if ( vis [ arr [ left ] ] == 0 ) -- window ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 1 , 3 , 2 , 3 } ; Console . WriteLine ( countDistictSubarray ( arr , arr . Length ) ) ; } }


"Count subarrays made up of elements having exactly K set bits | C # program for the above approach ; Function to count the number of set bits in an integer N ; Stores the count of set bits ; While N is non - zero ; If the LSB is 1 , then increment ans by 1 ; Return the total set bits ; Function to count the number of subarrays having made up of elements having K set bits ; Stores the total count of resultant subarrays ; Traverse the given array ; If the current element has K set bits ; Otherwise ; Increment count of subarrays ; Return total count of subarrays ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static int countSet ( int N ) { int ans = 0 ; while ( N > 0 ) { ans += N & 1 ; N >>= 1 ; } return ans ; } static int countSub ( int [ ] arr , int k ) { int ans = 0 ; int setK = 0 ; for ( int i = 0 ; i < 5 ; i ++ ) { if ( countSet ( arr [ i ] ) == k ) setK += 1 ; else setK = 0 ; ans += setK ; } return ans ; } public static void Main ( string [ ] args ) { int [ ] arr = { 4 , 2 , 1 , 5 , 6 } ; int K = 2 ; Console . WriteLine ( countSub ( arr , K ) ) ; } }


"Count subarrays of atleast size 3 forming a Geometric Progression ( GP ) | C # program for the above approach ; Function to count all the subarrays of size at least 3 forming GP ; If array size is less than 3 ; Stores the count of subarray ; Stores the count of subarray for each iteration ; Traverse the array ; Check if L [ i ] forms GP ; Otherwise , update count to 0 ; Update the final count ; Return the final count ; Driver Code ; Given array arr [ ] ; Function Call"
Solution in C#:

using System ; class GFG { static int numberOfGP ( int [ ] L , int N ) { if ( N <= 2 ) return 0 ; int count = 0 ; int res = 0 ; for ( int i = 2 ; i < N ; ++ i ) { if ( L [ i - 1 ] * L [ i - 1 ] == L [ i ] * L [ i - 2 ] ) { ++ count ; } else { count = 0 ; } res += count ; } return res ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 4 , 8 , 16 , 24 } ; int N = arr . Length ; Console . Write ( numberOfGP ( arr , N ) ) ; } }


"Count subarrays such that remainder after dividing sum of elements by K gives count of elements | C # implementation of the approach ; Function to return the number of subarrays of the given array such that the remainder when dividing the sum of its elements by K is equal to the number of its elements ; To store prefix sum ; We are dealing with zero indexed array ; Taking modulus value ; Prefix sum ; To store the required answer , the left index and the right index ; To store si - i value ; Include sum ; Increment the right index ; If subarray has at least k elements ; Return the required answer ; Driver code ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class gfg { static int sub_arrays ( int [ ] a , int n , int k ) { int [ ] sum = new int [ n + 2 ] ; for ( int i = 0 ; i < n + 2 ; i ++ ) { sum [ i ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) { a [ i ] -- ; a [ i ] %= k ; sum [ i + 1 ] += sum [ i ] + a [ i ] ; sum [ i + 1 ] %= k ; } int ans = 0 , l = 0 , r = 0 ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) { if ( ! mp . ContainsKey ( sum [ i ] ) ) mp . Add ( sum [ i ] , 0 ) ; } int temp ; for ( int i = 0 ; i < n + 1 ; i ++ ) { ans += ( int ) mp [ sum [ i ] ] ; temp = ( int ) mp [ sum [ i ] ] + 1 ; mp [ sum [ i ] ] = temp ; r ++ ; if ( r - l >= k ) { temp = ( int ) mp [ sum [ l ] ] - 1 ; mp [ sum [ i ] ] = temp ; l ++ ; } } return ans ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 4 , 2 , 3 , 5 } ; int n = a . Length ; int k = 4 ; Console . Write ( sub_arrays ( a , n , k ) ) ; } }


"Count subarrays where second highest lie before highest | C # program to count number of distinct instance where second highest number lie before highest number in all subarrays . ; Pair class ; Finding the next greater element of the array . ; Finding the previous greater element of the array . ; Wrapper Function ; Finding previous largest element ; Finding next largest element ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int MAXN = 100005 ; public class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void makeNext ( int [ ] arr , int n , int [ ] nextBig ) { Stack < pair > s = new Stack < pair > ( ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { nextBig [ i ] = i ; while ( s . Count != 0 && s . Peek ( ) . first < arr [ i ] ) s . Pop ( ) ; if ( s . Count != 0 ) nextBig [ i ] = s . Peek ( ) . second ; s . Push ( new pair ( arr [ i ] , i ) ) ; } } static void makePrev ( int [ ] arr , int n , int [ ] prevBig ) { Stack < pair > s = new Stack < pair > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { prevBig [ i ] = - 1 ; while ( s . Count != 0 && s . Peek ( ) . first < arr [ i ] ) s . Pop ( ) ; if ( s . Count != 0 ) prevBig [ i ] = s . Peek ( ) . second ; s . Push ( new pair ( arr [ i ] , i ) ) ; } } static int wrapper ( int [ ] arr , int n ) { int [ ] nextBig = new int [ MAXN ] ; int [ ] prevBig = new int [ MAXN ] ; int [ ] maxi = new int [ MAXN ] ; int ans = 0 ; makePrev ( arr , n , prevBig ) ; makeNext ( arr , n , nextBig ) ; for ( int i = 0 ; i < n ; i ++ ) if ( nextBig [ i ] != i ) maxi [ nextBig [ i ] - i ] = Math . Max ( maxi [ nextBig [ i ] - i ] , i - prevBig [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) ans += maxi [ i ] ; return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 2 , 4 } ; int n = arr . Length ; Console . WriteLine ( wrapper ( arr , n ) ) ; } }


"Count subarrays which contains both the maximum and minimum array element | C # program for the above approach ; Function to count subarray containing both maximum and minimum array elements ; If the length of the array is less than 2 ; Find the index of maximum element ; Find the index of minimum element ; If i > j , then swap the value of i and j ; Return the answer ; Function to return max_element index ; Function to return min_element index ; Driver Code ; Function call"
Solution in C#:

using System ; class GFG { static int countSubArray ( int [ ] arr , int n ) { if ( n < 2 ) return n ; int i = max_element ( arr ) ; int j = min_element ( arr ) ; if ( i > j ) { int tmp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = tmp ; } return ( i + 1 ) * ( n - j ) ; } static int max_element ( int [ ] arr ) { int idx = 0 ; int max = arr [ 0 ] ; for ( int i = 1 ; i < arr . Length ; i ++ ) { if ( max < arr [ i ] ) { max = arr [ i ] ; idx = i ; } } return idx ; } static int min_element ( int [ ] arr ) { int idx = 0 ; int min = arr [ 0 ] ; for ( int i = 1 ; i < arr . Length ; i ++ ) { if ( arr [ i ] < min ) { min = arr [ i ] ; idx = i ; } } return idx ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 1 , 2 , 3 } ; int n = arr . Length ; Console . WriteLine ( countSubArray ( arr , n ) ) ; } }


"Count subarrays with Prime sum | C # program to count subarrays with Prime sum ; Function to count subarrays with Prime sum ; USE SIEVE TO FIND ALL PRIME NUMBERS LESS THAN OR EQUAL TO max_val Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Remaining part of SIEVE ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Initialize result ; Traverse through the array ; return answer ; Driver program"
Solution in C#:

class Solution { static int primeSubarrays ( int [ ] A , int n ) { int max_val = ( int ) ( System . Math . Pow ( 10 , 7 ) ) ; bool [ ] prime = new bool [ max_val + 1 ] ; for ( int p = 0 ; p < max_val + 1 ; p ++ ) prime [ p ] = true ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= max_val ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= max_val ; i += p ) prime [ i ] = false ; } } int cnt = 0 ; for ( int i = 0 ; i < n - 1 ; ++ i ) { int val = A [ i ] ; for ( int j = i + 1 ; j < n ; ++ j ) { val += A [ j ] ; if ( prime [ val ] ) ++ cnt ; } } return cnt ; } static void Main ( ) { int [ ] A = { 1 , 2 , 3 , 4 , 5 } ; int n = A . Length ; System . Console . WriteLine ( primeSubarrays ( A , n ) ) ; } }


"Count subarrays with equal number of 1 ' s ▁ and ▁ 0' s | C # implementation to count subarrays with equal number of 1 ' s ▁ and ▁ 0' s ; function to count subarrays with equal number of 1 ' s ▁ and ▁ 0' s ; ' um ' implemented as hash table to store frequency of values obtained through cumulative sum ; Traverse original array and compute cumulative sum and increase count by 1 for this sum in ' um ' . Adds ' - 1' when arr [ i ] == 0 ; traverse the hash table ' um ' ; If there are more than one prefix subarrays with a particular sum ; add the subarrays starting from 1 st element and have equal number of 1 ' s ▁ and ▁ 0' s ; required count of subarrays ; Driver program to test above"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int countSubarrWithEqualZeroAndOne ( int [ ] arr , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; int curr_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { curr_sum += ( arr [ i ] == 0 ) ? - 1 : arr [ i ] ; if ( mp . ContainsKey ( curr_sum ) ) { var v = mp [ curr_sum ] ; mp . Remove ( curr_sum ) ; mp . Add ( curr_sum , ++ v ) ; } else mp . Add ( curr_sum , 1 ) ; } int count = 0 ; foreach ( KeyValuePair < int , int > itr in mp ) { if ( itr . Value > 1 ) count += ( ( itr . Value * ( itr . Value - 1 ) ) / 2 ) ; } if ( mp . ContainsKey ( 0 ) ) count += mp [ 0 ] ; return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 0 , 0 , 1 , 0 , 1 , 1 } ; int n = arr . Length ; Console . WriteLine ( " Count ▁ = ▁ " + countSubarrWithEqualZeroAndOne ( arr , n ) ) ; } }


"Count subarrays with same even and odd elements | C # program to find total number of even - odd subarrays present in given array ; function that returns the count of subarrays that contain equal number of odd as well as even numbers ; initialize difference and answer with 0 ; create two auxiliary hash arrays to count frequency of difference , one array for non - negative difference and other array for negative difference . Size of these two auxiliary arrays is ' n + 1' because difference can reach maximum value ' n ' as well as minimum value ' - n ' ; initialize these auxiliary arrays with 0 ; since the difference is initially 0 , we have to initialize hash_positive [ 0 ] with 1 ; for loop to iterate through whole array ( zero - based indexing is used ) ; incrementing or decrementing difference based on arr [ i ] being even or odd , check if arr [ i ] is odd ; adding hash value of ' difference ' to our answer as all the previous occurrences of the same difference value will make even - odd subarray ending at index ' i ' . After that , we will increment hash array for that ' difference ' value for its occurrence at index ' i ' . if difference is negative then use hash_negative ; else use hash_positive ; return total number of even - odd subarrays ; Driver code ; Printing total number of even - odd subarrays"
Solution in C#:

using System ; class GFG { static int countSubarrays ( int [ ] arr , int n ) { int difference = 0 ; int ans = 0 ; int [ ] hash_positive = new int [ n + 1 ] ; int [ ] hash_negative = new int [ n + 1 ] ; Array . Clear ( hash_positive , 0 , n + 1 ) ; Array . Clear ( hash_negative , 0 , n + 1 ) ; hash_positive [ 0 ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] & 1 ) == 1 ) difference ++ ; else difference -- ; if ( difference < 0 ) { ans += hash_negative [ - difference ] ; hash_negative [ - difference ] ++ ; } else { ans += hash_positive [ difference ] ; hash_positive [ difference ] ++ ; } } return ans ; } static void Main ( ) { int [ ] arr = new int [ ] { 3 , 4 , 6 , 8 , 1 , 10 , 5 , 7 } ; int n = arr . Length ; Console . Write ( " Total ▁ Number ▁ of ▁ Even - Odd " + " ▁ subarrays ▁ are ▁ " + countSubarrays ( arr , n ) ) ; } }


"Count subarrays with sum equal to its XOR value | C # program to count the number of subarrays such that Xor of all the elements of that subarray is equal to sum of the elements ; Function to count the number of subarrays such that Xor of all the elements of that subarray is equal to sum of the elements ; Maintain two pointers left and right ; Iterating through the array ; Calculate the window where the above condition is satisfied ; Count will be ( right - left ) ; Remove the previous element as it is already included ; Driver code"
Solution in C#:

using System ; class GFG { static long operation ( int [ ] arr , int N ) { int right = 0 ; int num = 0 ; long ans = 0 ; for ( int left = 0 ; left < N ; left ++ ) { while ( right < N && num + arr [ right ] == ( num ^ arr [ right ] ) ) { num += arr [ right ] ; right ++ ; } ans += right - left ; if ( left == right ) right ++ ; else num -= arr [ left ] ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int N = arr . Length ; Console . WriteLine ( operation ( arr , N ) ) ; } }


"Count subsequence of length 4 having product of the first three elements equal to the fourth element | C # program for the above approach ; Function to find the total number of subsequences satisfying the given criteria ; Stores the count of quadruplets ; Stores the frequency of product of the triplet ; Traverse the given array arr [ ] ; Consider arr [ i ] as fourth element of subsequences ; Generate all possible pairs of the array [ 0 , i - 1 ] ; Increment the frequency of the triplet ; Return the total count obtained ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int countQuadruples ( int [ ] A , int N ) { int ans = 0 ; Dictionary < int , int > freq = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( freq . ContainsKey ( A [ i ] ) ) ans += freq [ A [ i ] ] ; else freq . Add ( A [ i ] , 0 ) ; for ( int j = 0 ; j < i ; j ++ ) { for ( int k = 0 ; k < j ; k ++ ) { if ( freq . ContainsKey ( A [ i ] * A [ j ] * A [ k ] ) ) freq [ A [ i ] * A [ j ] * A [ k ] ] ++ ; else freq . Add ( A [ i ] * A [ j ] * A [ k ] , 1 ) ; } } } return ans ; } public static void Main ( ) { int [ ] arr = { 10 , 2 , 2 , 7 , 40 , 160 } ; int N = arr . Length ; Console . Write ( countQuadruples ( arr , N ) ) ; } }


"Count subsequence of length three in a given string | C # program to find number of occurrences of a subsequence of length 3 ; Function to find number of occurrences of a subsequence of length three in a string ; calculate length of string ; auxiliary array to store occurrences of first character ; auxiliary array to store occurrences of third character ; calculate occurrences of first character upto ith index from left ; calculate occurrences of third character upto ith index from right ; variable to store total number of occurrences ; loop to find the occurrences of middle element ; if middle character of subsequence is found in the string ; multiply the total occurrences of first character before middle character with the total occurrences of third character after middle character ; Driver function"
Solution in C#:

using System ; public class GfG { public static int findOccurrences ( string str1 , string substr1 ) { int n = str1 . Length ; int [ ] preLeft = new int [ n ] ; int [ ] preRight = new int [ n ] ; if ( str1 [ 0 ] == substr1 [ 0 ] ) preLeft [ 0 ] ++ ; for ( int i = 1 ; i < n ; i ++ ) { if ( str1 [ i ] == substr1 [ 0 ] ) preLeft [ i ] = preLeft [ i - 1 ] + 1 ; else preLeft [ i ] = preLeft [ i - 1 ] ; } if ( str1 [ n - 1 ] == substr1 [ 2 ] ) preRight [ n - 1 ] ++ ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( str1 [ i ] == substr1 [ 2 ] ) preRight [ i ] = preRight [ i + 1 ] + 1 ; else preRight [ i ] = preRight [ i + 1 ] ; } int counter = 0 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( str1 [ i ] == str1 [ 1 ] ) { int total = preLeft [ i - 1 ] * preRight [ i + 1 ] ; counter += total ; } } return counter ; } public static void Main ( ) { string str1 = " GFGFGYSYIOIWIN " ; string substr1 = " GFG " ; Console . WriteLine ( findOccurrences ( str1 , substr1 ) ) ; } }


"Count subsequence of length three in a given string | C # program to find number of occurrences of a subsequence of length 3 ; Function to find number of occurrences of a subsequence of length three in a string ; variable to store no of occurrences ; loop to find first character ; loop to find 2 nd character ; loop to find 3 rd character ; increment count if subsequence is found ; Driver function"
Solution in C#:

using System ; public class GfG { public static int findOccurrences ( string str1 , string substr1 ) { int counter = 0 ; for ( int i = 0 ; i < str1 . Length ; i ++ ) { if ( str1 [ i ] == substr1 [ 0 ] ) { for ( int j = i + 1 ; j < str1 . Length ; j ++ ) { if ( str1 [ j ] == substr1 [ 1 ] ) { for ( int k = j + 1 ; k < str1 . Length ; k ++ ) { if ( str1 [ k ] == substr1 [ 2 ] ) counter ++ ; } } } } } return counter ; } public static void Main ( ) { string str1 = " GFGFGYSYIOIWIN " ; string substr1 = " GFG " ; Console . WriteLine ( findOccurrences ( str1 , substr1 ) ) ; } }


"Count subsequences for every array element in which they are the maximum | C # program for the above approach ; Function to merge the subarrays arr [ l . . m ] and arr [ m + 1 , . . r ] based on indices [ ] ; If a [ indices [ l ] ] is less than a [ indices [ j ] ] , add indice [ l ] to temp ; Else add indices [ j ] ; Add remaining elements ; Add remainging elements ; Recursive function to divide the array into to parts ; Recursive call for elements before mid ; Recursive call for elements after mid ; Merge the two sorted arrays ; Function to find the number of subsequences for each element ; Sorting the indices according to array [ ] arr ; Array to store output numbers ; Initialize subseq ; B [ i ] is 2 ^ i ; Doubling the subsequences ; Print the readonly output , array [ ] B ; Driver Code ; Given array ; Given length ; Function call"
Solution in C#:

using System ; class GFG { static void merge ( int [ ] indices , int [ ] a , int l , int mid , int r ) { int [ ] temp_ind = new int [ r - l + 1 ] ; int j = mid + 1 ; int i = 0 , temp_l = l , k ; while ( l <= mid && j <= r ) { if ( a [ indices [ l ] ] < a [ indices [ j ] ] ) temp_ind [ i ++ ] = indices [ l ++ ] ; else temp_ind [ i ++ ] = indices [ j ++ ] ; } while ( l <= mid ) temp_ind [ i ++ ] = indices [ l ++ ] ; while ( j <= r ) temp_ind [ i ++ ] = indices [ j ++ ] ; for ( k = 0 ; k < i ; k ++ ) indices [ temp_l ++ ] = temp_ind [ k ] ; } static void divide ( int [ ] indices , int [ ] a , int l , int r ) { if ( l >= r ) return ; int mid = l / 2 + r / 2 ; divide ( indices , a , l , mid ) ; divide ( indices , a , mid + 1 , r ) ; merge ( indices , a , l , mid , r ) ; } static void noOfSubsequences ( int [ ] arr , int N ) { int [ ] indices = new int [ N ] ; int i ; for ( i = 0 ; i < N ; i ++ ) indices [ i ] = i ; divide ( indices , arr , 0 , N - 1 ) ; int [ ] B = new int [ N ] ; int subseq = 1 ; for ( i = 0 ; i < N ; i ++ ) { B [ indices [ i ] ] = subseq ; subseq *= 2 ; } for ( i = 0 ; i < N ; i ++ ) Console . Write ( B [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 1 } ; int N = arr . Length ; noOfSubsequences ( arr , N ) ; } }


"Count subsequences having average of its elements equal to K | C # program for the above approach ; Stores the dp states ; Function to find the count of subsequences having average K ; Base condition ; Three loops for three states ; Recurrence relation ; Stores the sum of dp [ n , j , K * j ] all possible values of j with average K and sum K * j ; Iterate over the range [ 1 , N ] ; Return the readonly count ; Driver Code"
Solution in C#:

using System ; public class GFG { static int [ , , ] dp = new int [ 101 , 101 , 1001 ] ; static int countAverage ( int n , int K , int [ ] arr ) { dp [ 0 , 0 , 0 ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int k = 0 ; k < n ; k ++ ) { for ( int s = 0 ; s <= 100 ; s ++ ) { dp [ i + 1 , k + 1 , s + arr [ i ] ] += dp [ i , k , s ] ; dp [ i + 1 , k , s ] += dp [ i , k , s ] ; } } } int cnt = 0 ; for ( int j = 1 ; j <= n ; j ++ ) { cnt += dp [ n , j , K * j ] ; } return cnt ; } public static void Main ( String [ ] args ) { int [ ] arr = { 9 , 7 , 8 , 9 } ; int K = 8 ; int N = arr . Length ; Console . Write ( countAverage ( N , K , arr ) ) ; } }


"Count subsequences having odd Bitwise OR values in an array | Java implementation for the above approach ; Function to count the subsequences having odd bitwise OR value ; Stores count of odd elements ; Stores count of even elements ; Traverse the array arr [ ] ; If element is odd ; Return the final answer ; Driver Code ; Given array arr [ ]"
Solution in C#:

using System ; class GFG { static int countSubsequences ( int [ ] arr ) { int odd = 0 ; int even = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( ( arr [ i ] & 1 ) != 0 ) odd ++ ; else even ++ ; } return ( ( 1 << odd ) - 1 ) * ( 1 << even ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 1 } ; Console . Write ( countSubsequences ( arr ) ) ; } }


"Count subsequences having odd Bitwise XOR values from an array | C # program for above approach ; Function to count the subsequences having odd bitwise XOR value ; Stores count of odd elements ; Stores count of even elements ; Traverse the array A [ ] ; If el is odd ; If count of odd elements is 0 ; Driver code ; Given array A [ ] ; Function call to count subsequences having odd bitwise XOR value"
Solution in C#:

using System ; public class GFG { public static void countSubsequences ( int [ ] A ) { int odd = 0 ; int even = 0 ; foreach ( int el in A ) { if ( el % 2 == 1 ) odd ++ ; else even ++ ; } if ( odd == 0 ) Console . WriteLine ( 0 ) ; else Console . WriteLine ( 1 << ( A . Length - 1 ) ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 1 , 3 , 4 } ; countSubsequences ( A ) ; } }


"Count subsequences in first string which are anagrams of the second string | C # implementation to count subsequences in first string which are anagrams of the second string ; Returns value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- -- * 1 ] ; function to count subsequences in first string which are anagrams of the second string ; hash tables to store frequencies of each character ; store frequency of each character of ' str1' ; store frequency of each character of ' str2' ; to store the total count of subsequences ; if character ( i + ' a ' ) exists in ' str2' ; if this character ' s ▁ frequency ▁ ▁ in ▁ ' str2 ' ▁ in ▁ less ▁ than ▁ or ▁ ▁ equal ▁ to ▁ its ▁ frequency ▁ in ▁ ▁ ' str1 ' ▁ then ▁ accumulate ▁ its ▁ ▁ contribution ▁ to ▁ the ▁ count ▁ ▁ of ▁ subsequences . ▁ If ▁ its ▁ ▁ frequency ▁ in ▁ ' str1 ' ▁ is ▁ ' n ' ▁ ▁ and ▁ in ▁ ' str2 ' ▁ is ▁ ' r ', then  its contribution will be nCr,  where C is the binomial  coefficient. ; else return 0 as there could be no subsequence which is an anagram of ' str2' ; required count of subsequences ; Driver code"
Solution in C#:

using System ; class GfG { public static int SIZE = 26 ; public static int binomialCoeff ( int n , int k ) { int res = 1 ; if ( k > n - k ) k = n - k ; for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } public static int countSubsequences ( String str , String str3 ) { int [ ] freq1 = new int [ SIZE ] ; int [ ] freq2 = new int [ SIZE ] ; char [ ] str1 = str . ToCharArray ( ) ; char [ ] str2 = str3 . ToCharArray ( ) ; int n1 = str . Length ; int n2 = str3 . Length ; for ( int i = 0 ; i < n1 ; i ++ ) freq1 [ str1 [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < n2 ; i ++ ) freq2 [ str2 [ i ] - ' a ' ] ++ ; int count = 1 ; for ( int i = 0 ; i < SIZE ; i ++ ) if ( freq2 [ i ] != 0 ) { if ( freq2 [ i ] <= freq1 [ i ] ) count = count * binomialCoeff ( freq1 [ i ] , freq2 [ i ] ) ; else return 0 ; } return count ; } public static void Main ( String [ ] argc ) { String str1 = " abacd " ; String str2 = " abc " ; Console . Write ( " Count ▁ = ▁ " + countSubsequences ( str1 , str2 ) ) ; } }


"Count subsequences which contains both the maximum and minimum array element | C # program for the above approach ; Function to calculate the count of subsequences ; Find the maximum from the array ; Find the minimum from the array ; If array contains only one distinct element ; Find the count of maximum ; Find the count of minimum ; Finding the result with given condition ; Driver Code ; Function call"
Solution in C#:

using System ; using System . Linq ; class GFG { static double countSubSequence ( int [ ] arr , int n ) { int maximum = arr . Max ( ) ; int minimum = arr . Min ( ) ; if ( maximum == minimum ) return Math . Pow ( 2 , n ) - 1 ; int i = count ( arr , maximum ) ; int j = count ( arr , minimum ) ; double res = ( Math . Pow ( 2 , i ) - 1 ) * ( Math . Pow ( 2 , j ) - 1 ) * Math . Pow ( 2 , n - i - j ) ; return res ; } static int count ( int [ ] arr , int value ) { int sum = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) if ( arr [ i ] == value ) sum ++ ; return sum ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . Length ; Console . WriteLine ( countSubSequence ( arr , n ) ) ; } }


"Count subsequences with same values of Bitwise AND , OR and XOR | function for finding count of possible subsequence ; creating a map to count the frequency of each element ; store frequency of each element ; iterate through the map ; add all possible combination for key equal zero ; add all ( odd number of elements ) possible combination for key other than zero ; Driver function"
Solution in C#:

static int countSubseq ( int [ ] arr , int n ) { int count = 0 ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) { var val = mp [ arr [ i ] ] ; mp . Remove ( arr [ i ] ) ; mp . Add ( arr [ i ] , val + 1 ) ; } else { mp . Add ( arr [ i ] , 1 ) ; } } foreach ( KeyValuePair < int , int > entry in mp ) { if ( entry . Key == 0 ) count += ( int ) ( Math . Pow ( 2 , entry . Value - 1 ) ) ; else count += ( int ) ( Math . Pow ( 2 , entry . Value - 1 ) ) ; } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 2 , 2 , 5 , 6 } ; int n = arr . Length ; Console . WriteLine ( countSubseq ( arr , n ) ) ; } }


"Count subsets consisting of each element as a factor of the next element in that subset | C # Program to implement the above approach ; Function to find number of subsets satisfying the given condition ; Stores number of required sets ; Stores maximum element of arr [ ] that defines the size of sieve ; Iterate through the arr [ ] ; If current element > maxE , then update maxE ; Declare an array sieve of size N + 1 ; Mark all elements corresponding in the array , by one as there will always exists a singleton set ; Iterate from range [ 1 , N ] ; If element is present in array ; Traverse through all its multiples <= n ; Update them if they are present in array ; Iterate from the range [ 1 , N ] ; Update the value of cnt ; Print the result ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static int mod = 1000000007 ; static void countSets ( int [ ] arr , int n ) { int cnt = 0 ; int maxE = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( maxE < arr [ i ] ) maxE = arr [ i ] ; } int [ ] sieve = new int [ maxE + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) sieve [ arr [ i ] ] = 1 ; for ( int i = 1 ; i <= maxE ; i ++ ) { if ( sieve [ i ] != 0 ) { for ( int j = i * 2 ; j <= maxE ; j += i ) { if ( sieve [ j ] != 0 ) sieve [ j ] = ( sieve [ j ] + sieve [ i ] ) % mod ; } } } for ( int i = 0 ; i <= maxE ; i ++ ) cnt = ( cnt % mod + sieve [ i ] % mod ) % mod ; Console . WriteLine ( cnt % mod ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 16 , 18 , 6 , 7 , 2 , 19 , 20 , 9 } ; int N = arr . Length ; countSets ( arr , N ) ; } }


"Count subsets having distinct even numbers | C # implementation to count subsets having even numbers only and all are distinct ; function to count the required subsets ; inserting even numbers in the set ' us ' single copy of each number is retained ; counting distinct even numbers ; total count of required subsets ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int countSubsets ( int [ ] arr , int n ) { HashSet < int > us = new HashSet < int > ( ) ; int even_count = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] % 2 == 0 ) us . Add ( arr [ i ] ) ; even_count = us . Count ; return ( int ) ( Math . Pow ( 2 , even_count ) - 1 ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 2 , 1 , 9 , 2 , 6 , 5 , 3 } ; int n = arr . Length ; Console . WriteLine ( " Number ▁ of ▁ subsets ▁ = ▁ " + countSubsets ( arr , n ) ) ; } }


"Count substring of Binary string such that each character belongs to a palindrome of size greater than 1 | C # implementation to find the substrings in binary string such that every character belongs to a palindrome ; Function to to find the substrings in binary string such that every character belongs to a palindrome ; Total substrings ; Loop to store the count of continious characters in the given string ; Subtract non special strings from answer ; Driver code ; Given string s ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static int countSubstrings ( String s ) { int n = s . Length ; int answer = ( n * ( n - 1 ) ) / 2 ; int cnt = 1 ; List < int > v = new List < int > ( ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( s [ i ] == s [ i - 1 ] ) cnt ++ ; else { v . Add ( cnt ) ; cnt = 1 ; } } if ( cnt > 0 ) v . Add ( cnt ) ; for ( int i = 0 ; i < v . Count - 1 ; i ++ ) { answer -= ( v [ i ] + v [ i + 1 ] - 1 ) ; } return answer ; } public static void Main ( String [ ] args ) { String s = "00111" ; Console . Write ( countSubstrings ( s ) ) ; } }


"Count substrings consisting of equal number of a , b , c and d | C # program to implement the above approach ; Function to count the substring with equal number of a , b , c and d ; Stores relative frequency of the characters { ' a ' , ' b ' , ' c ' , ' d ' } ; Initially , frequencies of ' a ' , ' b ' , ' c ' and ' d ' are 0. ; Stores relative frequency of ' a ' ; Stores relative frequency of ' b ' ; Stores relative frequency of ' c ' ; Stores relative frequency of ' d ' ; Stores count of substring with equal number of ' a ' , ' b ' , ' c ' and ' d ' ; Iterate over the characters of the string ; If current character is ' a ' ; Update p ; Stores minimum of { p , q , r , s } ; Update p ; Update q ; Update r ; Update s ; If current character is b ; Update q ; Stores minimum of { p , q , r , s } ; Update p ; Update q ; Update r ; Update s ; Update r ; Stores minimum of { p , q , r , s } ; Update p ; Update q ; Update r ; Update s ; Update s ; Stores minimum of { p , q , r , s } ; Update p ; Update q ; Update r ; Update s ; Update relative frequency of { p , q , r , s } ; Traverse the map ; Stores count of relative frequency ; Update cntSub ; Driver code ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int countSubstrings ( string str ) { Dictionary < Tuple < Tuple < int , int > , Tuple < int , int > > , int > mp = new Dictionary < Tuple < Tuple < int , int > , Tuple < int , int > > , int > ( ) ; if ( mp . ContainsKey ( new Tuple < Tuple < int , int > , Tuple < int , int > > ( new Tuple < int , int > ( 0 , 0 ) , new Tuple < int , int > ( 0 , 0 ) ) ) ) { mp [ new Tuple < Tuple < int , int > , Tuple < int , int > > ( new Tuple < int , int > ( 0 , 0 ) , new Tuple < int , int > ( 0 , 0 ) ) ] ++ ; } else { mp [ new Tuple < Tuple < int , int > , Tuple < int , int > > ( new Tuple < int , int > ( 0 , 0 ) , new Tuple < int , int > ( 0 , 0 ) ) ] = 1 ; } int p = 0 ; int q = 0 ; int r = 0 ; int s = 0 ; int cntSub = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( str [ i ] == ' a ' ) { p ++ ; int Y = Math . Min ( Math . Min ( s , r ) , Math . Min ( p , q ) ) ; p -= Y ; q -= Y ; r -= Y ; s -= Y ; } else if ( str [ i ] == ' b ' ) { q ++ ; int Y = Math . Min ( Math . Min ( p , q ) , Math . Min ( r , s ) ) ; p -= Y ; q -= Y ; r -= Y ; s -= Y ; } else if ( str [ i ] == ' c ' ) { r ++ ; int Y = Math . Min ( Math . Min ( p , q ) , Math . Min ( r , s ) ) ; p -= Y ; q -= Y ; r -= Y ; s -= Y ; } else if ( str [ i ] == ' d ' ) { s ++ ; int Y = Math . Min ( Math . Min ( p , q ) , Math . Min ( r , s ) ) ; p -= Y ; q -= Y ; r -= Y ; s -= Y ; } if ( mp . ContainsKey ( new Tuple < Tuple < int , int > , Tuple < int , int > > ( new Tuple < int , int > ( p , q ) , new Tuple < int , int > ( r , s ) ) ) ) { mp [ new Tuple < Tuple < int , int > , Tuple < int , int > > ( new Tuple < int , int > ( p , q ) , new Tuple < int , int > ( r , s ) ) ] ++ ; } else { mp [ new Tuple < Tuple < int , int > , Tuple < int , int > > ( new Tuple < int , int > ( p , q ) , new Tuple < int , int > ( r , s ) ) ] = 1 ; } } foreach ( KeyValuePair < Tuple < Tuple < int , int > , Tuple < int , int > > , int > e in mp ) { int freq = e . Value ; cntSub += ( freq ) * ( freq - 1 ) / 2 ; }...