"Number of subarrays have bitwise OR >= K | C # implementation of the above approach ; Function which builds the segment tree ; Function that returns bitwise OR of segment [ L . . R ] ; Function to count requisite number of subarrays ; Check for subarrays starting with index i ; If OR of subarray [ i . . mid ] >= K , then all subsequent subarrays will have OR >= K therefore reduce high to mid - 1 to find the minimal length subarray [ i . . mid ] having OR >= K ; Increase count with number of subarrays having OR >= K and starting with index i ; Driver code ; Build segment tree ."
Solution in C#:

using System ; class GFG { static int N = 100002 ; static int [ ] tree = new int [ 4 * N ] ; static void build ( int [ ] arr , int node , int start , int end ) { if ( start == end ) { tree [ node ] = arr [ start ] ; return ; } int mid = ( start + end ) >> 1 ; build ( arr , 2 * node , start , mid ) ; build ( arr , 2 * node + 1 , mid + 1 , end ) ; tree [ node ] = tree [ 2 * node ] | tree [ 2 * node + 1 ] ; } static int query ( int node , int start , int end , int l , int r ) { if ( start > end start > r end < l ) { return 0 ; } if ( start >= l && end <= r ) { return tree [ node ] ; } int mid = ( start + end ) >> 1 ; int q1 = query ( 2 * node , start , mid , l , r ) ; int q2 = query ( 2 * node + 1 , mid + 1 , end , l , r ) ; return q1 | q2 ; } static int countSubArrays ( int [ ] arr , int n , int K ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int low = i , high = n - 1 , index = int . MaxValue ; while ( low <= high ) { int mid = ( low + high ) >> 1 ; if ( query ( 1 , 0 , n - 1 , i , mid ) >= K ) { index = Math . Min ( index , mid ) ; high = mid - 1 ; } else { low = mid + 1 ; } } if ( index != int . MaxValue ) { count += n - index ; } } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 4 , 5 } ; int n = arr . Length ; build ( arr , 1 , 0 , n - 1 ) ; int k = 6 ; Console . WriteLine ( countSubArrays ( arr , n , k ) ) ; } }


"Number of subarrays have bitwise OR >= K | C # implementation of the approach ; Function to build the segment tree ; Function to return the bitwise OR of segment [ L . . R ] ; Function to return the count of required sub - arrays ; Build segment tree ; Query segment tree for bitwise OR of sub - array [ i . . j ] ; Driver code"
Solution in C#:

using System ; class GFG { static int N = 100002 ; static int [ ] tree = new int [ 4 * N ] ; static void build ( int [ ] arr , int node , int start , int end ) { if ( start == end ) { tree [ node ] = arr [ start ] ; return ; } int mid = ( start + end ) >> 1 ; build ( arr , 2 * node , start , mid ) ; build ( arr , 2 * node + 1 , mid + 1 , end ) ; tree [ node ] = tree [ 2 * node ] | tree [ 2 * node + 1 ] ; } static int query ( int node , int start , int end , int l , int r ) { if ( start > end start > r end < l ) { return 0 ; } if ( start >= l && end <= r ) { return tree [ node ] ; } int mid = ( start + end ) >> 1 ; int q1 = query ( 2 * node , start , mid , l , r ) ; int q2 = query ( 2 * node + 1 , mid + 1 , end , l , r ) ; return q1 | q2 ; } static int countSubArrays ( int [ ] arr , int n , int K ) { build ( arr , 1 , 0 , n - 1 ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { int bitwise_or = query ( 1 , 0 , n - 1 , i , j ) ; if ( bitwise_or >= K ) count ++ ; } } return count ; } static void Main ( ) { int [ ] arr = { 3 , 4 , 5 } ; int n = arr . Length ; int k = 6 ; Console . WriteLine ( countSubArrays ( arr , n , k ) ) ; } }


"Number of subarrays have bitwise OR >= K | C # implementation of the approach ; Function to return the count of required sub - arrays ; Traverse sub - array [ i . . j ] ; 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 ++ ) { int bitwise_or = 0 ; for ( int k = i ; k <= j ; k ++ ) { bitwise_or = bitwise_or | arr [ k ] ; } if ( bitwise_or >= K ) count ++ ; } } return count ; } public static void Main ( ) { int [ ] arr = { 3 , 4 , 5 } ; int n = arr . Length ; int k = 6 ; Console . WriteLine ( countSubArrays ( arr , n , k ) ) ; } }


"Number of subarrays having absolute sum greater than K | Set | C # implementation of the approach ; Function to find required value ; Variable to store final answer ; Loop to find prefix - sum ; Sorting prefix - sum array ; Loop to find upper_bound for each element ; Returning final answer ; Driver code ; Function to find required value"
Solution in C#:

using System ; class GFG { static int findCnt ( int [ ] arr , int n , int k ) { int ans = 0 ; for ( int i = 1 ; i < n ; i ++ ) { arr [ i ] += arr [ i - 1 ] ; if ( arr [ i ] > k arr [ i ] < - 1 * k ) ans ++ ; } if ( arr [ 0 ] > k arr [ 0 ] < - 1 * k ) ans ++ ; Array . Sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) ans += n - upper_bound ( arr , 0 , n , arr [ i ] + k ) ; return ans ; } static int upper_bound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( a [ middle ] > element ) high = middle ; else low = middle + 1 ; } return low ; } public static void Main ( ) { int [ ] arr = { - 1 , 4 , - 5 , 6 } ; int n = arr . Length ; int k = 0 ; Console . WriteLine ( findCnt ( arr , n , k ) ) ; } }


"Number of subarrays having even product | C # implementation of the above approach ; Function to count subarrays with even product ; Stores count of subarrays with even product ; Traverse the array ; Initialize product ; Update product of the subarray ; Print total count of subarrays ; Driver Code ; Input ; Length of an array ; Function call to count subarrays with even product"
Solution in C#:

using System ; public class GFG { static void evenproduct ( int [ ] arr , int length ) { int count = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { int product = 1 ; for ( int j = i ; j < arr . Length ; j ++ ) { product *= arr [ j ] ; if ( product % 2 == 0 ) ++ count ; } } Console . WriteLine ( count ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 7 , 5 , 4 , 9 } ; int length = arr . Length - 1 ; evenproduct ( arr , length ) ; } }


"Number of subarrays having even product | C # program for the above approach ; Function to count subarrays with even product ; Total number of subarrays ; Counter variables ; Traverse the array ; If current element is odd ; Update count of subarrays with odd product up to index i ; Print count of subarrays with even product ; Driver Code ; Input ; Length of an array ; Function call to count even product subarrays"
Solution in C#:

using System ; class GFG { static void evenproduct ( int [ ] arr , int length ) { int total_subarray = length * ( length + 1 ) / 2 ; int total_odd = 0 ; int count_odd = 0 ; for ( int i = 0 ; i < arr . Length ; ++ i ) { if ( arr [ i ] % 2 == 0 ) { count_odd = 0 ; } else { ++ count_odd ; total_odd += count_odd ; } } Console . WriteLine ( total_subarray - total_odd ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 7 , 5 , 4 , 9 } ; int length = arr . Length ; evenproduct ( arr , length ) ; } }


"Number of subarrays having product less than K | C # program to count subarrays having product less than k . ; Counter for single element ; Multiple subarray ; If this multiple is less than k , then increment ; Driver Code"
Solution in C#:

using System ; public class GFG { static int countsubarray ( int [ ] array , int n , int k ) { int count = 0 ; int i , j , mul ; for ( i = 0 ; i < n ; i ++ ) { if ( array [ i ] < k ) count ++ ; mul = array [ i ] ; for ( j = i + 1 ; j < n ; j ++ ) { mul = mul * array [ j ] ; if ( mul < k ) count ++ ; else break ; } } return count ; } static public void Main ( ) { int [ ] array = { 1 , 2 , 3 , 4 } ; int k = 10 ; int size = array . Length ; int count = countsubarray ( array , size , k ) ; Console . WriteLine ( count ) ; } }


"Number of subarrays having sum exactly equal to k | C # program for the above approach ; Calculate all subarrays ; Calculate required sum ; Check if sum is equal to required sum"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void Main ( ) { int [ ] arr = { 10 , 2 , - 2 , - 20 , 10 } ; int k = - 10 ; int n = arr . Length ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int sum = 0 ; for ( int j = i ; j < n ; j ++ ) { sum += arr [ j ] ; if ( sum == k ) res ++ ; } } Console . WriteLine ( res ) ; } }


"Number of subarrays having sum exactly equal to k | C # program to find number of subarrays with sum exactly equal to k . ; Function to find number of subarrays with sum exactly equal to k . ; HashMap to store number of subarrays starting from index zero having particular value of sum . ; Sum of elements so far ; Add current element to sum so far . ; If currsum is equal to desired sum , then a new subarray is found . So increase count of subarrays . ; currsum exceeds given sum by currsum - sum . Find number of subarrays having this sum and exclude those subarrays from currsum by increasing count by same amount . ; Add currsum value to count of different values of sum . ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static int findSubarraySum ( int [ ] arr , int n , int sum ) { Dictionary < int , int > prevSum = new Dictionary < int , int > ( ) ; int res = 0 ; int currsum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { currsum += arr [ i ] ; if ( currsum == sum ) res ++ ; if ( prevSum . ContainsKey ( currsum - sum ) ) res += prevSum [ currsum - sum ] ; if ( ! prevSum . ContainsKey ( currsum ) ) prevSum . Add ( currsum , 1 ) ; else { int count = prevSum [ currsum ] ; prevSum [ currsum ] = count + 1 ; } } return res ; } public static void Main ( ) { int [ ] arr = { 10 , 2 , - 2 , - 20 , 10 } ; int sum = - 10 ; int n = arr . Length ; Console . Write ( findSubarraySum ( arr , n , sum ) ) ; } }


"Number of subarrays having sum of the form k ^ m , m >= 0 | C # implementation of the above approach ; partial_sum ; Function to count number of sub - arrays whose sum is k ^ p where p >= 0 ; If m [ a + b ] = c , then add c to the current sum . ; Increase count of prefix sum . ; If m [ a + b ] = c , then add c to the current sum . ; Increase count of prefix sum . ; b = k ^ p , p >= 0 ; k ^ m can be maximum equal to 10 ^ 14. ; If m [ a + b ] = c , then add c to the current sum . ; Increase count of prefix sum . ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static readonly int MAX = 100005 ; static long [ ] partial_sum ( long [ ] prefix_sum , int [ ] arr , int n ) { for ( int i = 1 ; i <= n ; i ++ ) { prefix_sum [ i ] = ( prefix_sum [ i - 1 ] + arr [ i - 1 ] ) ; } return prefix_sum ; } static int countSubarrays ( int [ ] arr , int n , int k ) { long [ ] prefix_sum = new long [ MAX ] ; prefix_sum [ 0 ] = 0 ; prefix_sum = partial_sum ( prefix_sum , arr , n ) ; int sum ; if ( k == 1 ) { sum = 0 ; Dictionary < long , int > mp = new Dictionary < long , int > ( ) ; for ( int i = n ; i >= 0 ; i -- ) { if ( mp . ContainsKey ( prefix_sum [ i ] + 1 ) ) sum += mp [ prefix_sum [ i ] + 1 ] ; if ( mp . ContainsKey ( prefix_sum [ i ] ) ) mp . Add ( prefix_sum [ i ] , mp [ prefix_sum [ i ] ] + 1 ) ; else mp . Add ( prefix_sum [ i ] , 1 ) ; } return sum ; } if ( k == - 1 ) { sum = 0 ; Dictionary < long , int > map = new Dictionary < long , int > ( ) ; for ( int i = n ; i >= 0 ; i -- ) { if ( map . ContainsKey ( prefix_sum [ i ] + 1 ) ) sum += map [ prefix_sum [ i ] + 1 ] ; if ( map . ContainsKey ( prefix_sum [ i ] - 1 ) ) sum += map [ prefix_sum [ i ] - 1 ] ; if ( map . ContainsKey ( prefix_sum [ i ] ) ) map . Add ( prefix_sum [ i ] , map [ prefix_sum [ i ] ] + 1 ) ; else map . Add ( prefix_sum [ i ] , 1 ) ; } return sum ; } sum = 0 ; long b , l = 100000000000000L ; Dictionary < long , int > m = new Dictionary < long , int > ( ) ; for ( int i = n ; i >= 0 ; i -- ) { b = 1 ; while ( true ) { if ( b > l ) break ; if ( m . ContainsKey ( prefix_sum [ i ] + b ) ) sum += m [ prefix_sum [ i ] + b ] ; b *= k ; } if ( m . ContainsKey ( prefix_sum [ i ] ) ) m . Add ( ( prefix_sum [ i ] ) , m [ prefix_sum [ i ] ] + 1 ) ; else m . Add ( ( prefix_sum [ i ] ) , 1 ) ; } return sum ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 2 , 2 , 2 } ; int n = arr . Length ; int k = 2 ; Console . Write ( countSubarrays ( arr , n , k ) ) ; } }


"Number of subarrays whose minimum and maximum are same | Program to count number of subarrays having same minimum and maximum . ; calculate the no of contiguous subarrays which has the same minimum and maximum ; stores the answer ; loop to traverse from 0 - n ; start checking subarray from next element ; traverse for finding subarrays ; if the elements are same then we check further and keep a count of same numbers in ' r ' ; the no of elements in between r and i with same elements . ; the no . of subarrays that can be formed between i and r ; again start checking from the next index ; returns answer ; Driver program"
Solution in C#:

using System ; class Subarray { static int calculate ( int [ ] a , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int r = i + 1 ; for ( int j = r ; j < n ; j ++ ) { if ( a [ i ] == a [ j ] ) r += 1 ; else break ; } int d = r - i ; ans += ( d * ( d + 1 ) / 2 ) ; i = r - 1 ; } return ans ; } public static void Main ( ) { int [ ] a = { 2 , 4 , 5 , 3 , 3 , 3 } ; Console . WriteLine ( calculate ( a , a . Length ) ) ; } }


"Number of submatrices with OR value 1 | C # program to count number of submatrices with OR value 1 ; Function to find required prefix - count for each row from right to left ; Function to find the count of submatrices with OR value 1 ; Array to store prefix count of zeros from right to left for bool array ; Variable to store the count of submatrices with OR value 0 ; Loop to evaluate each column of the prefix matrix uniquely . For each index of a column we will try to determine the number of sub - matrices starting from that index and has all 1 s ; First part of pair will be the value of inserted element . Second part will be the count of the number of elements . Pushed before with a greater value ; Variable to store the number of submatrices with all 0 s ; Return the final answer ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int n = 3 ; class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void findPrefixCount ( int [ , ] p_arr , bool [ , ] arr ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = n - 1 ; j >= 0 ; j -- ) { if ( arr [ i , j ] ) continue ; if ( j != n - 1 ) p_arr [ i , j ] += p_arr [ i , j + 1 ] ; p_arr [ i , j ] += ( arr [ i , j ] == false ? 1 : 0 ) ; } } static int matrixOrValueOne ( bool [ , ] arr ) { int [ , ] p_arr = new int [ n , n ] ; findPrefixCount ( p_arr , arr ) ; int count_zero_submatrices = 0 ; for ( int j = 0 ; j < n ; j ++ ) { int i = n - 1 ; Stack < pair > q = new Stack < pair > ( ) ; int to_sum = 0 ; while ( i >= 0 ) { int c = 0 ; while ( q . Count != 0 && q . Peek ( ) . first > p_arr [ i , j ] ) { to_sum -= ( q . Peek ( ) . second + 1 ) * ( q . Peek ( ) . first - p_arr [ i , j ] ) ; c += q . Peek ( ) . second + 1 ; q . Pop ( ) ; } to_sum += p_arr [ i , j ] ; count_zero_submatrices += to_sum ; q . Push ( new pair ( p_arr [ i , j ] , c ) ) ; i -- ; } } return ( n * ( n + 1 ) * n * ( n + 1 ) ) / 4 - count_zero_submatrices ; } public static void Main ( String [ ] args ) { bool [ , ] arr = { { false , false , false } , { false , true , false } , { false , false , false } } ; Console . WriteLine ( matrixOrValueOne ( arr ) ) ; } }


"Number of subsequences as " ab " in a string repeated K times | C # code to find number of subsequences of " ab " in the string S which is repeated K times . ; Count of ' a ' s ; Count of ' b ' s ; occurrence of " ab " s in string S ; Add following two : 1 ) K * ( Occurrences of " ab " in single string ) 2 ) a is from one string and b is from other . ; Driver code"
Solution in C#:

using System ; class GFG { static int countOccurrences ( string s , int K ) { int n = s . Length ; int C = 0 , c1 = 0 , c2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' a ' ) c1 ++ ; if ( s [ i ] == ' b ' ) { c2 ++ ; C += c1 ; } } return C * K + ( K * ( K - 1 ) / 2 ) * c1 * c2 ; } public static void Main ( ) { string S = " abcb " ; int k = 2 ; Console . WriteLine ( countOccurrences ( S , k ) ) ; } }


"Number of subsequences in a given binary string divisible by 2 | C # implementation of the approach ; Function to return the count of the required subsequences ; To store the final answer ; Multiplier ; Loop to find the answer ; Condition to update the answer ; updating multiplier ; Driver code"
Solution in C#:

using System ; class GFG { static int countSubSeq ( string str , int len ) { int ans = 0 ; int mul = 1 ; for ( int i = 0 ; i < len ; i ++ ) { if ( str [ i ] == '0' ) ans += mul ; mul *= 2 ; } return ans ; } static public void Main ( ) { string str = "10010" ; int len = str . Length ; Console . WriteLine ( countSubSeq ( str , len ) ) ; } }


"Number of subsequences in a string divisible by n | C # program to count subsequences of a string divisible by n ; Returns count of subsequences of str divisible by n . ; division by n can leave only n remainder [ 0. . n - 1 ] . dp [ i ] [ j ] indicates number of subsequences in string [ 0. . i ] which leaves remainder j after division by n . ; Filling value for first digit in str ; start a new subsequence with index i ; exclude i 'th character from all the  current subsequences of string [0...i-1] ; include i 'th character in all the current  subsequences of string [0...i-1] ; Driver code"
Solution in C#:

using System ; class GFG { static int countDivisibleSubseq ( string str , int n ) { int len = str . Length ; int [ , ] dp = new int [ len , n ] ; dp [ 0 , ( str [ 0 ] - '0' ) % n ] ++ ; for ( int i = 1 ; i < len ; i ++ ) { dp [ i , ( str [ i ] - '0' ) % n ] ++ ; for ( int j = 0 ; j < n ; j ++ ) { dp [ i , j ] += dp [ i - 1 , j ] ; dp [ i , ( j * 10 + ( str [ i ] - '0' ) ) % n ] += dp [ i - 1 , j ] ; } } return dp [ len - 1 , 0 ] ; } public static void Main ( ) { String str = "1234" ; int n = 4 ; Console . Write ( countDivisibleSubseq ( str , n ) ) ; } }


"Number of subsequences of maximum length K containing no repeated elements | C # implementation of the approach ; Returns number of subsequences of maximum length k and contains no repeated element ; Sort the array a [ ] ; Store the frequencies of all the distinct element in the vector arr ; count is the the number of such subsequences ; Create a 2 - d array dp [ n + 1 ] [ m + 1 ] to store the intermediate result ; Initialize the first row to 1 ; Update the dp [ ] [ ] array based on the recurrence relation ; Return the number of subsequences ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int countSubSeq ( int [ ] a , int n , int k ) { Array . Sort ( a ) ; List < int > arr = new List < int > ( ) ; int count , x ; for ( int i = 0 ; i < n ; ) { count = 1 ; x = a [ i ] ; i ++ ; while ( i < n && a [ i ] == x ) { count ++ ; i ++ ; } arr . Add ( count ) ; } int m = arr . Count ; n = Math . Min ( m , k ) ; count = 1 ; int [ , ] dp = new int [ n + 1 , m + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) dp [ 0 , i ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = m ; j >= 0 ; j -- ) { if ( j > m - i ) dp [ i , j ] = 0 ; else { dp [ i , j ] = dp [ i , j + 1 ] + arr [ j ] * dp [ i - 1 , j + 1 ] ; } } count = count + dp [ i , 0 ] ; } return count ; } public static void Main ( String [ ] args ) { int [ ] a = { 2 , 2 , 3 , 3 , 5 } ; int n = a . Length ; int k = 3 ; Console . WriteLine ( countSubSeq ( a , n , k ) ) ; } }


"Number of subsequences of the form a ^ i b ^ j c ^ k | C # program to count subsequences of the form a ^ i b ^ j c ^ k ; Returns count of subsequences of the form a ^ i b ^ j c ^ k ; Initialize counts of different subsequences caused by different combination of ' a ' ; Initialize counts of different subsequences caused by different combination of ' a ' and different combination of ' b ' ; Initialize counts of different subsequences caused by different combination of ' a ' , ' b ' and ' c ' ; Traverse all characters of given string ; If current character is ' a ' , then there are following possibilities : a ) Current character begins a new subsequence . b ) Current character is part of aCount subsequences c ) Current character is not part of aCount subsequences . ; If current character is ' b ' , then there are following possibilities : a ) Current character begins a new subsequence of b 's with aCount subsequences. b) Current character is part of bCount subsequences. c) Current character is not part of  bCount subsequences. ; If current character is ' c ' , then there are following possibilities : a ) Current character begins a new subsequence of c 's with bCount subsequences. b) Current character is part of cCount subsequences. c) Current character is not part of  cCount subsequences. ; Driver code"
Solution in C#:

using System ; public class GFG { static int countSubsequences ( String s ) { int aCount = 0 ; int bCount = 0 ; int cCount = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == ' a ' ) aCount = ( 1 + 2 * aCount ) ; else if ( s [ i ] == ' b ' ) bCount = ( aCount + 2 * bCount ) ; else if ( s [ i ] == ' c ' ) cCount = ( bCount + 2 * cCount ) ; } return cCount ; } public static void Main ( ) { String s = " abbc " ; Console . Write ( countSubsequences ( s ) ) ; } }


"Number of subsequences with negative product | C # implementation of the approach ; Function to return the count of all the subsequences with negative product ; To store the count of positive elements in the array ; To store the count of negative elements in the array ; If the current element is positive ; If the current element is negative ; For all the positive elements of the array ; For all the negative elements of the array ; Driver code"
Solution in C#:

using System ; class GFG { static int cntSubSeq ( int [ ] arr , int n ) { int pos_count = 0 ; int neg_count = 0 ; int result ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) pos_count ++ ; if ( arr [ i ] < 0 ) neg_count ++ ; } result = ( int ) Math . Pow ( 2 , pos_count ) ; if ( neg_count > 0 ) result *= ( int ) Math . Pow ( 2 , neg_count - 1 ) ; else result = 0 ; return result ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , - 4 , - 1 , 6 } ; int n = arr . Length ; Console . Write ( cntSubSeq ( arr , n ) ) ; } }


"Number of subsequences with positive product | C # implementation of the approach ; Function to return the count of all the subsequences with positive product ; To store the count of positive elements in the array ; To store the count of negative elements in the array ; If the current element is positive ; If the current element is negative ; For all the positive elements of the array ; For all the negative elements of the array ; For the empty subsequence ; Driver code"
Solution in C#:

using System ; class GFG { static int cntSubSeq ( int [ ] arr , int n ) { int pos_count = 0 ; int neg_count = 0 ; int result ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) pos_count ++ ; if ( arr [ i ] < 0 ) neg_count ++ ; } result = ( int ) Math . Pow ( 2 , pos_count ) ; if ( neg_count > 0 ) result *= ( int ) Math . Pow ( 2 , neg_count - 1 ) ; result -= 1 ; return result ; } public static void Main ( ) { int [ ] arr = { 2 , - 3 , - 1 , 4 } ; int n = arr . Length ; Console . Write ( cntSubSeq ( arr , n ) ) ; } }


"Number of subsets with a given AND value | C # implementation of the approach ; To store states of DP ; Function to return the required count ; Base case ; If the state has been solved before return the value of the state ; Setting the state as solved ; Recurrence relation ; Driver code"
Solution in C#:

using System ; class GFG { static int maxN = 20 ; static int maxM = 64 ; static int [ , ] dp1 = new int [ maxN , maxM ] ; static bool [ , ] v1 = new bool [ maxN , maxM ] ; static int findCnt ( int [ ] arr , int i , int curr , int n , int m ) { if ( i == n ) { return ( curr == m ? 1 : 0 ) ; } if ( v1 [ i , curr ] ) return dp1 [ i , curr ] ; v1 [ i , curr ] = true ; dp1 [ i , curr ] = findCnt ( arr , i + 1 , curr , n , m ) + findCnt ( arr , i + 1 , ( curr & arr [ i ] ) , n , m ) ; return dp1 [ i , curr ] ; } public static void Main ( String [ ] args ) { int [ ] arr = { 0 , 0 , 0 } ; int n = arr . Length ; int m = 0 ; Console . WriteLine ( findCnt ( arr , 0 , ( ( 1 << 6 ) - 1 ) , n , m ) ) ; } }


"Number of subsets with a given OR value | C # implementation of the approach ; To store states of DP ; Function to return the required count ; Base case ; If the state has been solved before return the value of the state ; Setting the state as solved ; Recurrence relation ; Driver code"
Solution in C#:

using System ; class GFG { static int maxN = 20 ; static int maxM = 64 ; static int [ , ] dp = new int [ maxN , maxM ] ; static Boolean [ , ] v = new Boolean [ maxN , maxM ] ; static int findCnt ( int [ ] arr , int i , int curr , int n , int m ) { if ( i == n ) { return ( curr == m ? 1 : 0 ) ; } if ( v [ i , curr ] ) return dp [ i , curr ] ; v [ i , curr ] = true ; dp [ i , curr ] = findCnt ( arr , i + 1 , curr , n , m ) + findCnt ( arr , i + 1 , ( curr arr [ i ] ) , n , m ) ; return dp [ i , curr ] ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 2 } ; int n = arr . Length ; int m = 3 ; Console . WriteLine ( findCnt ( arr , 0 , 0 , n , m ) ) ; } }


"Number of subsets with same AND , OR and XOR values in an Array | C # implementation to find the number of subsets with equal bitwise AND , OR and XOR values ; Function to find the number of subsets with equal bitwise AND , OR and XOR values ; Traverse through all the subsets ; Finding the subsets with the bits of ' i ' which are set ; Computing the bitwise AND ; Computing the bitwise OR ; Computing the bitwise XOR ; Comparing all the three values ; Driver Code"
Solution in C#:

using System ; class GFG { static int mod = 1000000007 ; static int countSubsets ( int [ ] a , int n ) { int answer = 0 ; for ( int i = 0 ; i < ( 1 << n ) ; i ++ ) { int bitwiseAND = - 1 ; int bitwiseOR = 0 ; int bitwiseXOR = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( i & ( 1 << j ) ) == 0 ) { if ( bitwiseAND == - 1 ) bitwiseAND = a [ j ] ; else bitwiseAND &= a [ j ] ; bitwiseOR |= a [ j ] ; bitwiseXOR ^= a [ j ] ; } } if ( bitwiseAND == bitwiseOR && bitwiseOR == bitwiseXOR ) answer = ( answer + 1 ) % mod ; } return answer ; } public static void Main ( String [ ] args ) { int N = 6 ; int [ ] A = { 1 , 3 , 2 , 1 , 2 , 1 } ; Console . Write ( countSubsets ( A , N ) ) ; } }


"Number of subsets with same AND , OR and XOR values in an Array | C # program to find the number of subsets with equal bitwise AND , OR and XOR values ; Function to find the number of subsets with equal bitwise AND , OR and XOR values ; Precompute the modded powers of two for subset counting ; Loop to iterate and find the modded powers of two for subset counting ; Map to store the frequency of each element ; Loop to compute the frequency ; For every element > 0 , the number of subsets formed using this element only is equal to 2 ^ ( frequency [ element ] - 1 ) . And for 0 , we have to find all the subsets , so 2 ^ ( frequency [ element ] ) - 1 ; If element is greater than 0 ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int mod = 1000000007 ; static int countSubsets ( int [ ] a , int n ) { int answer = 0 ; int [ ] powerOfTwo = new int [ 100005 ] ; powerOfTwo [ 0 ] = 1 ; for ( int i = 1 ; i < 100005 ; i ++ ) powerOfTwo [ i ] = ( powerOfTwo [ i - 1 ] * 2 ) % mod ; Dictionary < int , int > frequency = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( frequency . ContainsKey ( a [ i ] ) ) { frequency [ a [ i ] ] = frequency [ a [ i ] ] + 1 ; } else { frequency . Add ( a [ i ] , 1 ) ; } foreach ( KeyValuePair < int , int > el in frequency ) { if ( el . Key != 0 ) answer = ( answer % mod + powerOfTwo [ el . Value - 1 ] ) % mod ; else answer = ( answer % mod + powerOfTwo [ el . Value ] - 1 + mod ) % mod ; } return answer ; } public static void Main ( String [ ] args ) { int N = 6 ; int [ ] A = { 1 , 3 , 2 , 1 , 2 , 1 } ; Console . Write ( countSubsets ( A , N ) ) ; } }


"Number of subsets with sum divisible by M | Set 2 | C # implementation of the approach ; To store the states of DP ; Function to find the required count ; Base case ; If the state has been solved before return the value of the state ; Setting the state as solved ; Recurrence relation ; Driver code"
Solution in C#:

using System ; class GFG { static int maxN = 20 ; static int maxM = 10 ; static int [ , ] dp = new int [ maxN , maxM ] ; static bool [ , ] v = new bool [ maxN , maxM ] ; static int findCnt ( int [ ] arr , int i , int curr , int n , int m ) { if ( i == n ) { if ( curr == 0 ) return 1 ; else return 0 ; } if ( v [ i , curr ] ) return dp [ i , curr ] ; v [ i , curr ] = true ; return dp [ i , curr ] = findCnt ( arr , i + 1 , curr , n , m ) + findCnt ( arr , i + 1 , ( curr + arr [ i ] ) % m , n , m ) ; } public static void Main ( ) { int [ ] arr = { 3 , 3 , 3 , 3 } ; int n = arr . Length ; int m = 6 ; Console . WriteLine ( findCnt ( arr , 0 , 0 , n , m ) - 1 ) ; } }


"Number of subsets with sum divisible by m | C # program which returns the Number of sub sequences ( or subsets ) which are divisible by m . ; Use Dynamic Programming to find sum of subsequences . ; Find sum pf array elements ; dp [ i ] [ j ] would be > 0 if arr [ 0. . i - 1 ] has a subsequence with sum equal to j . ; There is always sum equals zero ; Fill up the dp table ; Initialize the counter ; Check if the sum exists ; check sum is divisible by m ; Driver Code"
Solution in C#:

 using System ; class GFG { static int sumSubSequence ( int [ ] arr , int len , int m ) { int sum = 0 ; foreach ( int x in arr ) { sum += x ; } int [ , ] dp = new int [ len + 1 , sum + 1 ] ; for ( int i = 0 ; i <= len ; i ++ ) dp [ i , 0 ] ++ ; for ( int i = 1 ; i <= len ; i ++ ) { dp [ i , arr [ i - 1 ] ] ++ ; for ( int j = 1 ; j <= sum ; j ++ ) { if ( dp [ i - 1 , j ] > 0 ) { dp [ i , j ] ++ ; dp [ i , j + arr [ i - 1 ] ] ++ ; } } } int count = 0 ; for ( int j = 1 ; j <= sum ; j ++ ) if ( dp [ len , j ] > 0 ) if ( j % m == 0 ) count += dp [ len , j ] ; return count ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 2 , 3 } ; int m = 3 ; int len = arr . Length ; Console . Write ( sumSubSequence ( arr , len , m ) + "
 " ) ; } }


"Number of substrings divisible by 6 in a string of integers | C # program to calculate number of substring divisible by 6. ; Return the number of substring divisible by 6 and starting at index i in s [ ] and previous sum of digits modulo 3 is m . ; End of the string . ; If already calculated , return the stored value . ; Converting into integer . ; Increment result by 1 , if current digit is divisible by 2 and sum of digits is divisible by 3. And recur for next index with new modulo . ; Returns substrings divisible by 6. ; For storing the value of all states . ; If string contain 0 , increment count by 1. ; Else calculate using recursive function . Pass previous sum modulo 3 as 0. ; Driver code"
Solution in C#:

using System ; class GFG { static int MAX = 100002 ; static int f ( int i , int m , char [ ] s , int [ , ] memoize ) { if ( i == s . Length ) { return 0 ; } if ( memoize [ i , m ] != - 1 ) { return memoize [ i , m ] ; } int x = s [ i ] - '0' ; int ans = ( ( ( ( x + m ) % 3 == 0 ) && ( x % 2 == 0 ) ) ? 1 : 0 ) + f ( i + 1 , ( m + x ) % 3 , s , memoize ) ; return memoize [ i , m ] = ans ; } static int countDivBy6 ( char [ ] s ) { int n = s . Length ; int [ , ] memoize = new int [ n + 1 , 3 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { for ( int j = 0 ; j < 3 ; j ++ ) { memoize [ i , j ] = - 1 ; } } int ans = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == '0' ) { ans ++ ; } else { ans += f ( i , 0 , s , memoize ) ; } } return ans ; } public static void Main ( String [ ] args ) { char [ ] s = "4806" . ToCharArray ( ) ; Console . WriteLine ( countDivBy6 ( s ) ) ; } }


"Number of substrings divisible by 8 but not by 3 | C # program to count substrings which are divisible by 8 but not by 3 ; Function that returns count of substrings divisible by 8 but not by 3 ; Iterating the string . ; Prefix sum of number of substrings whose sum of digits mudolo 3 is 0 , 1 , 2. ; Iterating the string . ; Since single digit 8 is divisible by 8 and not by 3. ; Taking two digit number . ; 10 th position ; Complete 2 digit number ; Taking 3 digit number . ; 100 th position ; 10 th position ; Complete 3 digit number . ; If number formed is divisible by 8 then last 3 digits are also divisible by 8. Then all the substring ending at this index is divisible . ; But those substring also contain number which are not divisible by 3 so remove them . ; driver program"
Solution in C#:

using System ; class GFG { static int count ( String s , int len ) { int MAX = 1000 ; int cur = 0 , dig = 0 ; int [ ] sum = new int [ MAX ] ; int [ , ] dp = new int [ MAX , 3 ] ; dp [ 0 , 0 ] = 1 ; for ( int i = 1 ; i <= len ; i ++ ) { dig = ( int ) ( s [ i - 1 ] ) - 48 ; cur += dig ; cur %= 3 ; sum [ i ] = cur ; dp [ i , 0 ] = dp [ i - 1 , 0 ] ; dp [ i , 1 ] = dp [ i - 1 , 1 ] ; dp [ i , 2 ] = dp [ i - 1 , 2 ] ; dp [ i , sum [ i ] ] ++ ; } int ans = 0 , dprev = 0 , value = 0 , dprev2 = 0 ; for ( int i = 1 ; i <= len ; i ++ ) { dig = ( int ) ( s [ i - 1 ] ) - 48 ; if ( dig == 8 ) ans ++ ; if ( i - 2 >= 0 ) { dprev = ( int ) ( s [ i - 2 ] ) - 48 ; value = dprev * 10 + dig ; if ( ( value % 8 == 0 ) && ( value % 3 != 0 ) ) ans ++ ; } if ( i - 3 >= 0 ) { dprev2 = ( int ) ( s [ i - 3 ] ) - 48 ; dprev = ( int ) ( s [ i - 2 ] ) - 48 ; value = dprev2 * 100 + dprev * 10 + dig ; if ( value % 8 != 0 ) continue ; ans += ( i - 2 ) ; ans -= ( dp [ i - 3 , sum [ i ] ] ) ; } } return ans ; } public static void Main ( String [ ] args ) { String str = "6564525600" ; int len = str . Length ; Console . Write ( count ( str , len ) ) ; } }


"Number of substrings having an equal number of lowercase and uppercase letters | C # program for the above approach ; Function to find the count of substrings having an equal number of uppercase and lowercase characters ; Stores the count of prefixes having sum S considering uppercase and lowercase characters as 1 and - 1 ; Stores the count of substrings having equal number of lowercase and uppercase characters ; Stores the sum obtained so far ; If the character is uppercase ; Otherwise ; If currsum is 0 ; If the current sum exists in the Dictionary prevSum ; Increment the resultant count by 1 ; Update the frequency of the current sum by 1 ; Return the resultant count of the subarrays ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int countSubstring ( String S , int N ) { Dictionary < int , int > prevSum = new Dictionary < int , int > ( ) ; int res = 0 ; int currentSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] >= ' A ' && S [ i ] <= ' Z ' ) { currentSum ++ ; } else currentSum -- ; if ( currentSum == 0 ) res ++ ; if ( prevSum . ContainsKey ( currentSum ) ) { res += prevSum [ currentSum ] ; prevSum [ currentSum ] = prevSum [ currentSum ] + 1 ; } else prevSum . Add ( currentSum , 1 ) ; } return res ; } public static void Main ( String [ ] args ) { String S = " gEEk " ; Console . WriteLine ( countSubstring ( S , S . Length ) ) ; } }


"Number of substrings of a string | C # program to count number of substrings of a string ; Driver Code"
Solution in C#:

using System ; class GFG { static int countNonEmptySubstr ( string str ) { int n = str . Length ; return n * ( n + 1 ) / 2 ; } public static void Main ( ) { string s = " abcde " ; Console . Write ( countNonEmptySubstr ( s ) ) ; } }


"Number of substrings of one string present in other | C # program to count number of substrings of s1 present in s2 . ; s3 stores all substrings of s1 ; check the presence of s3 in s2 ; Driver code"
Solution in C#:

using System ; class GFG { static int countSubstrs ( String s1 , String s2 ) { int ans = 0 ; for ( int i = 0 ; i < s1 . Length ; i ++ ) { String s3 = " " ; char [ ] s4 = s1 . ToCharArray ( ) ; for ( int j = i ; j < s1 . Length ; j ++ ) { s3 += s4 [ j ] ; if ( s2 . IndexOf ( s3 ) != - 1 ) ans ++ ; } } return ans ; } public static void Main ( String [ ] args ) { String s1 = " aab " , s2 = " aaaab " ; Console . WriteLine ( countSubstrs ( s1 , s2 ) ) ; } }


"Number of substrings with count of each character as k | C # program to count number of substrings with counts of distinct characters as k . ; Returns true if all values in freq [ ] are either 0 or k . ; Returns count of substrings where frequency of every present character is k ; Pick a starting point ; Initialize all frequencies as 0 for this starting point ; One by one pick ending points ; Increment frequency of current char ; If frequency becomes more than k , we can 't have more substrings  starting with i ; If frequency becomes k , then check other frequencies as well . ; Driver code"
Solution in C#:

using System ; class GFG { static int MAX_CHAR = 26 ; static bool check ( int [ ] freq , int k ) { for ( int i = 0 ; i < MAX_CHAR ; i ++ ) if ( freq [ i ] != 0 && freq [ i ] != k ) return false ; return true ; } static int substrings ( String s , int k ) { for ( int i = 0 ; i < s . Length ; i ++ ) { int [ ] freq = new int [ MAX_CHAR ] ; for ( int j = i ; j < s . Length ; j ++ ) { int index = s [ j ] - ' a ' ; freq [ index ] ++ ; if ( freq [ index ] > k ) break ; else if ( freq [ index ] == k && check ( freq , k ) == true ) res ++ ; } } return res ; } public static void Main ( String [ ] args ) { String s = " aabbcc " ; int k = 2 ; Console . WriteLine ( substrings ( s , k ) ) ; s = " aabbc " ; k = 2 ; Console . WriteLine ( substrings ( s , k ) ) ; } }


"Number of substrings with each character occurring even times | C # program for the above approach ; Function to count substrings having even frequency of each character ; Stores the count of a character ; Stores bitmask ; Stores the count of substrings with even count of each character ; Traverse the string S ; Flip the ord ( i ) - 97 bits in pre ; Increment the count by hash [ pre ] ; Increment count of pre in hash ; Return the total count obtained ; Driver code"
Solution in C#:

using System . IO ; using System ; using System . Collections . Generic ; class GFG { static int subString ( string s , int n ) { Dictionary < int , int > hash = new Dictionary < int , int > ( ) ; hash [ 0 ] = 1 ; int pre = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { pre ^= ( 1 << ( int ) ( s [ i ] ) - 97 ) ; if ( hash . ContainsKey ( pre ) ) count += hash [ pre ] ; else count += 0 ; if ( hash . ContainsKey ( pre ) ) hash [ pre ] = hash [ pre ] + 1 ; else hash . Add ( pre , 1 ) ; } return count ; } static void Main ( ) { String S = " abbaa " ; int N = S . Length ; Console . WriteLine ( subString ( S , N ) ) ; } }


"Number of substrings with each character occurring even times | C # program for the above approach ; Function to count substrings having even frequency of each character ; Stores the total count of substrings ; Traverse the range [ 0 , N ] : ; Traverse the range [ i + 1 , N ] ; Stores the substring over the range of indices [ i , len ] ; Stores the frequency of characters ; Count frequency of each character ; Traverse the dictionary ; If any of the keys have odd count ; Otherwise ; Return count ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { static int subString ( string s , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int len = i + 1 ; len <= n ; len ++ ) { string test_str = s . Substring ( i , len - i ) ; Dictionary < char , int > res = new Dictionary < char , int > ( ) ; foreach ( char keys in test_str . ToCharArray ( ) ) { if ( ! res . ContainsKey ( keys ) ) res . Add ( keys , 0 ) ; res [ keys ] ++ ; } int flag = 0 ; foreach ( KeyValuePair < char , int > keys in res ) { if ( keys . Value % 2 != 0 ) { flag = 1 ; break ; } } if ( flag == 0 ) count += 1 ; } } return count ; } static public void Main ( ) { string S = " abbaa " ; int N = S . Length ; Console . WriteLine ( subString ( S , N ) ) ; } }


"Number of substrings with length divisible by the number of 1 's in it | C # program to count number of subString under given condition ; Function return count of such substring ; Mark 1 at those indices where '1' appears ; Take prefix sum ; Iterate through all the subStrings ; Driver Code"
Solution in C#:

using System ; class GFG { static int countOfSubStrings ( String s ) { int n = s . Length ; int [ ] prefix_sum = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '1' ) prefix_sum [ i ] = 1 ; } for ( int i = 1 ; i < n ; i ++ ) prefix_sum [ i ] += prefix_sum [ i - 1 ] ; int answer = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { int countOfOnes = prefix_sum [ j ] - ( i - 1 >= 0 ? prefix_sum [ i - 1 ] : 0 ) ; int length = j - i + 1 ; if ( countOfOnes > 0 && length % countOfOnes == 0 ) answer ++ ; } } return answer ; } public static void Main ( String [ ] args ) { String S = "1111100000" ; Console . Write ( countOfSubStrings ( S ) ) ; } }


"Number of substrings with length divisible by the number of 1 's in it | C # program to count number of subString under given condition ; Function return count of such substring ; Selection of adequate x value ; Store where 1 's are located ; If there are no ones , then answer is 0 ; For ease of implementation ; Count storage ; Iterate on all k values less than fixed x ; Keeps a count of 1 's occured  during String traversal ; Iterate on String and modify the totCount ; If this character is 1 ; Add to the readonly sum / count ; Increase totCount at exterior position ; Reduce totCount at index + k * n ; Slightly modified prefix sum storage ; Number of 1 's till i-1 ; Traversing over String considering each position and finding bounds and count using the inequalities ; Calculating bounds for l and r ; If valid then add to answer ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int countOfSubStrings ( String s ) { int n = s . Length ; int x = ( int ) Math . Sqrt ( n ) ; List < int > ones = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '1' ) ones . Add ( i ) ; } if ( ones . Count == 0 ) return 0 ; ones . Add ( n ) ; int [ ] totCount = new int [ n * x + n ] ; int sum = 0 ; for ( int k = 0 ; k <= x ; k ++ ) { int now = 0 ; totCount [ k * n ] ++ ; for ( int j = 1 ; j <= n ; j ++ ) { if ( s [ j - 1 ] == '1' ) now ++ ; int index = j - k * now ; sum += totCount [ index + k * n ] ; totCount [ index + k * n ] ++ ; } now = 0 ; totCount [ k * n ] -- ; for ( int j = 1 ; j <= n ; j ++ ) { if ( s [ j - 1 ] == '1' ) now ++ ; int index = j - k * now ; totCount [ index + k * n ] -- ; } } int [ ] prefix_sum = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) prefix_sum [ i ] = - 1 ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { prefix_sum [ i ] = cnt ; if ( s [ i ] == '1' ) cnt ++ ; } for ( int k = 0 ; k < n ; k ++ ) { for ( int j = 1 ; j <= ( n / x ) && prefix_sum [ k ] + j <= cnt ; j ++ ) { int l = ones [ prefix_sum [ k ] + j - 1 ] - k + 1 ; int r = ones [ prefix_sum [ k ] + j ] - k ; l = Math . Max ( l , j * ( x + 1 ) ) ; if ( l <= r ) { sum += r / j - ( l - 1 ) / j ; } } } return sum ; } public static void Main ( String [ ] args ) { String S = "1111100000" ; Console . Write ( countOfSubStrings ( S ) ) ; } }


"Number of substrings with odd decimal value in a binary string | C # program to count substrings with odd decimal value ; Function to count number of substrings with odd decimal representation ; auxiliary array to store count of 1 's before ith index ; store count of 1 's before  i-th index ; variable to store answer ; Traverse the string reversely to calculate number of odd substrings before i - th index ; Driver Code"
Solution in C#:

using System ; class GFG { static int countSubstr ( string s ) { int n = s . Length ; int [ ] auxArr = new int [ n ] ; if ( s [ 0 ] == '1' ) auxArr [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( s [ i ] == '1' ) auxArr [ i ] = auxArr [ i - 1 ] + 1 ; else auxArr [ i ] = auxArr [ i - 1 ] ; } int count = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) if ( s [ i ] == '1' ) count += auxArr [ i ] ; return count ; } public static void Main ( ) { string s = "1101" ; Console . WriteLine ( countSubstr ( s ) ) ; } }


"Number of subtrees having odd count of even numbers | C # implementation to find number of subtrees having odd count of even numbers ; A binary tree Node ; Helper function that allocates a new Node with the given data and NULL left and right pointers . ;  ; Returns count of subtrees having odd count of even numbers ; base condition ; count even nodes in left subtree ; Add even nodes in right subtree ; Check if root data is an even number ; if total count of even numbers for the subtree is odd ; Total count of even nodes of the subtree ; A wrapper over countRec ( ) ; Driver Code ; binary tree formation ; 2 ; / \ ; 1 3 ; / \ / \ ; 4 10 8 5 ; / ; 6"
Solution in C#:

using System ; class GFG { public class Node { public int data ; public Node left , right ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } public class P { public int pcount = 0 ; } static int countRec ( Node root , P p ) { if ( root == null ) return 0 ; int c = countRec ( root . left , p ) ; c += countRec ( root . right , p ) ; if ( root . data % 2 == 0 ) c += 1 ; if ( c % 2 != 0 ) ( p . pcount ) ++ ; return c ; } static int countSubtrees ( Node root ) { P p = new P ( ) ; countRec ( root , p ) ; return p . pcount ; } public static void Main ( String [ ] args ) { Node root = newNode ( 2 ) ; root . left = newNode ( 1 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 10 ) ; root . right . left = newNode ( 8 ) ; root . right . right = newNode ( 5 ) ; root . left . right . left = newNode ( 6 ) ; Console . WriteLine ( " Count ▁ = ▁ " + countSubtrees ( root ) ) ; } }


"Number of swaps to sort when only adjacent swapping allowed | C # program to count number of swaps required to sort an array when only swapping of adjacent elements is allowed . ; This function merges two sorted arrays and returns inversion count in the arrays . ; i is index for left subarray ; j is index for right subarray ; k is index for resultant merged subarray ; this is tricky -- see above / explanation diagram for merge ( ) ; Copy the remaining elements of left subarray ( if there are any ) to temp ; Copy the remaining elements of right subarray ( if there are any ) to temp ; Copy back the merged elements to original array ; An auxiliary recursive function that sorts the input array and returns the number of inversions in the array . ; Divide the array into two parts and call _mergeSortAndCountInv ( ) for each of the parts ; Inversion count will be sum of inversions in left - part , right - part and number of inversions in merging ; Merge the two parts ; This function sorts the input array and returns the number of inversions in the array ; Driver Code"
Solution in C#:

using System ; class GFG { static int merge ( int [ ] arr , int [ ] temp , int left , int mid , int right ) { int inv_count = 0 ; int i = left ; int j = mid ; int k = left ; while ( ( i <= mid - 1 ) && ( j <= right ) ) { if ( arr [ i ] <= arr [ j ] ) temp [ k ++ ] = arr [ i ++ ] ; else { temp [ k ++ ] = arr [ j ++ ] ; inv_count = inv_count + ( mid - i ) ; } } while ( i <= mid - 1 ) temp [ k ++ ] = arr [ i ++ ] ; while ( j <= right ) temp [ k ++ ] = arr [ j ++ ] ; for ( i = left ; i <= right ; i ++ ) arr [ i ] = temp [ i ] ; return inv_count ; } static int _mergeSort ( int [ ] arr , int [ ] temp , int left , int right ) { int mid , inv_count = 0 ; if ( right > left ) { mid = ( right + left ) / 2 ; inv_count = _mergeSort ( arr , temp , left , mid ) ; inv_count += _mergeSort ( arr , temp , mid + 1 , right ) ; inv_count += merge ( arr , temp , left , mid + 1 , right ) ; } return inv_count ; } static int countSwaps ( int [ ] arr , int n ) { int [ ] temp = new int [ n ] ; return _mergeSort ( arr , temp , 0 , n - 1 ) ; } public static void Main ( ) { int [ ] arr = { 1 , 20 , 6 , 4 , 5 } ; int n = arr . Length ; Console . Write ( " Number ▁ of ▁ swaps ▁ is ▁ " + countSwaps ( arr , n ) ) ; } }


"Number of times Maximum and minimum value updated during traversal of array | C # implementation to find the number of times minimum and maximum value updated during the traversal of the array ; Function to find the number of times minimum and maximum value updated during the traversal of the given array ; Increment i if new highest value occurs Increment j if new lowest value occurs ; Driver Code"
Solution in C#:

 using System ; class GFG { static void maxUpdated ( int [ ] arr ) { int h_score = arr [ 0 ] ; int l_score = arr [ 0 ] ; int i = 1 , j = 1 ; foreach ( int n in arr ) { if ( h_score < n ) { h_score = n ; i ++ ; } if ( l_score > n ) { l_score = n ; j ++ ; } } Console . Write ( " Number ▁ of ▁ times ▁ maximum ▁ value ▁ " ) ; Console . Write ( " updated ▁ = ▁ " + i + "
 " ) ; Console . Write ( " Number ▁ of ▁ times ▁ minimum ▁ value ▁ " ) ; Console . Write ( " updated ▁ = ▁ " + j ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 10 , 5 , 20 , 22 } ; maxUpdated ( arr ) ; } }


"Number of times a number can be replaced by the sum of its digits until it only contains one digit | C # program to count number of times we need to add digits to get a single digit . ; Here the count variable store how many times we do sum of digits and temporary_sum always store the temporary sum we get at each iteration . ; In this loop we always compute the sum of digits in temporary_ sum variable and convert it into string str till its length become 1 and increase the count in each iteration . ; computing sum of its digits ; converting temporary_sum into string str again . ; increase the count ; Driver code"
Solution in C#:

using System ; class GFG { static int NumberofTimes ( String str ) { int temporary_sum = 0 , count = 0 ; while ( str . Length > 1 ) { temporary_sum = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) temporary_sum += ( str [ i ] - '0' ) ; str = temporary_sum + " " ; count ++ ; } return count ; } public static void Main ( ) { String s = "991" ; Console . Write ( NumberofTimes ( s ) ) ; } }


"Number of times an array can be partitioned repetitively into two subarrays with equal sum | C # program for the above approach ; Recursion Function to calculate the possible splitting ; If there are less than two elements , we cannot partition the sub - array . ; Iterate from the start to end - 1. ; Recursive call to the left and the right sub - array . ; If there is no such partition , then return 0 ; Function to find the total splitting ; Prefix array to store the prefix - sum using 1 based indexing ; Store the prefix - sum ; Function Call to count the number of splitting ; Driver Code ; Given array ; Function call"
Solution in C#:

using System ; class GFG { static int splitArray ( int start , int end , int [ ] arr , int [ ] prefix_sum ) { if ( start >= end ) return 0 ; for ( int k = start ; k < end ; ++ k ) { if ( ( prefix_sum [ k ] - prefix_sum [ start - 1 ] ) == ( prefix_sum [ end ] - prefix_sum [ k ] ) ) { return 1 + splitArray ( start , k , arr , prefix_sum ) + splitArray ( k + 1 , end , arr , prefix_sum ) ; } } return 0 ; } static void solve ( int [ ] arr , int n ) { int [ ] prefix_sum = new int [ n + 1 ] ; prefix_sum [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; ++ i ) { prefix_sum [ i ] = prefix_sum [ i - 1 ] + arr [ i - 1 ] ; } Console . Write ( splitArray ( 1 , n , arr , prefix_sum ) ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 12 , 3 , 3 , 0 , 3 , 3 } ; int N = arr . Length ; solve ( arr , N ) ; } }


"Number of times the largest Perfect Cube can be subtracted from N | C # implementation of the approach ; Function to return the count of steps ; Variable to store the count of steps ; Iterate while N > 0 ; Get the largest perfect cube and subtract it from N ; Increment steps ; Return the required count ; Driver code"
Solution in C#:

using System ; class GFG { static int countSteps ( int n ) { int steps = 0 ; while ( n > 0 ) { int largest = ( int ) Math . Pow ( n , ( double ) 1 / 3 ) ; n -= ( largest * largest * largest ) ; steps ++ ; } return steps ; } public static void Main ( String [ ] args ) { int n = 150 ; Console . Write ( countSteps ( n ) ) ; } }


"Number of times the largest perfect square number can be subtracted from N | C # implementation of the approach ; Function to return the count of steps ; Variable to store the count of steps ; Iterate while N > 0 ; Get the largest perfect square and subtract it from N ; Increment steps ; Return the required count ; Driver code"
Solution in C#:

using System ; class GfG { static int countSteps ( int n ) { int steps = 0 ; while ( n > 0 ) { int largest = ( int ) Math . Sqrt ( n ) ; n -= ( largest * largest ) ; steps ++ ; } return steps ; } public static void Main ( ) { int n = 85 ; Console . WriteLine ( countSteps ( n ) ) ; } }


"Number of trailing zeroes in base B representation of N ! | C # program to find the number of trailing zeroes in base B representation of N ! ; To find the power of a prime p in factorial N ; calculating floor ( n / r ) and adding to the count ; increasing the power of p from 1 to 2 to 3 and so on ; returns all the prime factors of k ; vector to store all the prime factors along with their number of occurrence in factorization of B ; Returns largest power of B that divides N ! ; calculating minimum power of all the prime factors of B ; Driver code"
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 int findPowerOfP ( int N , int p ) { int count = 0 ; int r = p ; while ( r <= N ) { count += ( N / r ) ; r = r * p ; } return count ; } static List < pair > primeFactorsofB ( int B ) { List < pair > ans = new List < pair > ( ) ; for ( int i = 2 ; B != 1 ; i ++ ) { if ( B % i == 0 ) { int count = 0 ; while ( B % i == 0 ) { B = B / i ; count ++ ; } ans . Add ( new pair ( i , count ) ) ; } } return ans ; } static int largestPowerOfB ( int N , int B ) { List < pair > vec = new List < pair > ( ) ; vec = primeFactorsofB ( B ) ; int ans = int . MaxValue ; for ( int i = 0 ; i < vec . Count ; i ++ ) ans = Math . Min ( ans , findPowerOfP ( N , vec [ i ] . first ) / vec [ i ] . second ) ; return ans ; } public static void Main ( String [ ] args ) { Console . WriteLine ( largestPowerOfB ( 5 , 2 ) ) ; Console . WriteLine ( largestPowerOfB ( 6 , 9 ) ) ; } }


"Number of trailing zeros in N * ( N | C # implementation of the approach ; Function to return the count of trailing 0 s in the given function ; If n is odd ; If n is even ; Find the trailing zeros in n / 2 factorial ; Return the required answer ; Driver code"
Solution in C#:

using System ; class GFG { static int findTrailingZeros ( int n ) { if ( ( n & 1 ) == 1 ) return 0 ; else { int ans = 0 ; n /= 2 ; while ( n != 0 ) { ans += n / 5 ; n /= 5 ; } return ans ; } } public static void Main ( String [ ] args ) { int n = 12 ; Console . WriteLine ( findTrailingZeros ( n ) ) ; } }


"Number of trees whose sum of degrees of all the vertices is L | C # implementation of the approach ; Iterative Function to calculate ( x ^ y ) in O ( log y ) ; Initialize result ; If y is odd , multiply x with result ; y must be even now y = y / 2 ; Function to return the count of required trees ; number of nodes ; Return the result ; Driver code"
Solution in C#:

using System ; class GFG { static long power ( int x , long y ) { long res = 1 ; while ( y > 0 ) { if ( y == 1 ) res = ( res * x ) ; y = y >> 1 ; x = ( x * x ) ; } return res ; } static long solve ( int L ) { int n = L / 2 + 1 ; long ans = power ( n , n - 2 ) ; return ans ; } static public void Main ( ) { int L = 6 ; Console . WriteLine ( solve ( L ) ) ; } }


"Number of triangles after N moves | C # program to find middle of three distinct numbers to calculate the number of equilateral triangles ; function to calculate number of triangles in Nth step ; Driver code"
Solution in C#:

using System ; class Triangle { public static double numberOfTriangles ( int n ) { double ans = 2 * ( Math . Pow ( 3 , n ) ) - 1 ; return ans ; } public static void Main ( ) { int n = 2 ; Console . WriteLine ( numberOfTriangles ( n ) ) ; } }


"Number of triangles formed by joining vertices of n | C # program to implement the above problem ; Function to find the number of triangles ; print the number of triangles ; Driver code ; initialize the number of sides of a polygon"
Solution in C#:

using System ; class GFG { static void findTriangles ( int n ) { int num ; num = n * ( n - 4 ) ; Console . WriteLine ( num ) ; } public static void Main ( ) { int n ; n = 6 ; findTriangles ( n ) ; } }


"Number of triangles formed by joining vertices of n | C # program to implement the above problem ; Function to find the number of triangles ; print the number of triangles having two side common ; print the number of triangles having no side common ; Driver code ; initialize the number of sides of a polygon"
Solution in C#:

using System ; class GFG { static void findTriangles ( int n ) { int num = n ; Console . Write ( num + " ▁ " ) ; Console . WriteLine ( num * ( num - 4 ) * ( num - 5 ) / 6 ) ; } public static void Main ( ) { int n ; n = 6 ; findTriangles ( n ) ; } }


"Number of triangles formed from a set of points on three lines | C # program to find the possible number of triangles that can be formed from set of points on three lines ; Returns factorial of a number ; calculate c ( n , r ) ; Driver code"
Solution in C#:

using System ; class GFG { static int factorial ( int n ) { int fact = 1 ; for ( int i = 2 ; i <= n ; i ++ ) fact = fact * i ; return fact ; } static int ncr ( int n , int r ) { return factorial ( n ) / ( factorial ( r ) * factorial ( n - r ) ) ; } public static void Main ( ) { int m = 3 , n = 4 , k = 5 ; int totalTriangles = ncr ( m + n + k , 3 ) - ncr ( m , 3 ) - ncr ( n , 3 ) - ncr ( k , 3 ) ; Console . WriteLine ( totalTriangles ) ; } }


"Number of triangles in a plane if no more than two points are collinear | C # program to find the number of triangles in a plane if no more then two points are collinear . ; Function to find number of triangles in a plane . ; Formula to find number of triangle nC3 = n * ( n - 1 ) * ( n - 2 ) / 6 ; Driver code"
Solution in C#:

using System ; class GFG { static int countNumberOfTriangles ( int n ) { return n * ( n - 1 ) * ( n - 2 ) / 6 ; } public static void Main ( ) { int n = 4 ; Console . WriteLine ( countNumberOfTriangles ( n ) ) ; } }


"Number of triangles possible with given lengths of sticks which are powers of 2 | C # implementation of the approach ; Function to return the number of positive area triangles ; To store the count of total triangles ; To store the count of pairs of sticks with equal lengths ; Back - traverse and count the number of triangles ; Count the number of pairs ; If we have one remaining stick and we have a pair ; Count 1 triangle ; Reduce one pair ; Count the remaining triangles that can be formed ; Driver code"
Solution in C#:

using System ; class GFG { static int countTriangles ( int [ ] a , int n ) { int cnt = 0 ; int pairs = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { pairs += a [ i ] / 2 ; if ( a [ i ] % 2 == 1 && pairs > 0 ) { cnt += 1 ; pairs -= 1 ; } } cnt += ( 2 * pairs ) / 3 ; return cnt ; } public static void Main ( ) { int [ ] a = { 1 , 2 , 2 , 2 , 2 } ; int n = a . Length ; Console . WriteLine ( countTriangles ( a , n ) ) ; } }


"Number of triplets such that each value is less than N and each pair sum is a multiple of K | C # implementation of the approach ; Function to return the number of triplets ; Initializing the count array ; Storing the frequency of each modulo class ; If K is odd ; If K is even ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static int NoofTriplets ( int N , int K ) { int [ ] cnt = new int [ K ] ; Array . Fill ( cnt , 0 , cnt . Length , 0 ) ; for ( int i = 1 ; i <= N ; i += 1 ) { cnt [ i % K ] += 1 ; } if ( ( K & 1 ) != 0 ) { return cnt [ 0 ] * cnt [ 0 ] * cnt [ 0 ] ; } else { return ( cnt [ 0 ] * cnt [ 0 ] * cnt [ 0 ] + cnt [ K / 2 ] * cnt [ K / 2 ] * cnt [ K / 2 ] ) ; } } static public void Main ( ) { int N = 3 , K = 2 ; Console . Write ( NoofTriplets ( N , K ) ) ; } }


"Number of turns to reach from one node to other in binary tree | A C # Program to count number of turns in a Binary Tree . ; A Binary Tree Node ; Constructor ; Utility function to find the LCA of two given values n1 and n2 . ; Base case ; If either n1 or n2 matches with root 's key, report the presence by  returning root (Note that if a key  is ancestor of other, then the  ancestor key becomes LCA ; Look for keys in left and right subtrees ; If both of the above calls return Non - NULL , then one key is present in once subtree and other is present in other , So this node is the LCA ; Otherwise check if left subtree or right subtree is LCA ; function count number of turn need to reach given node from it 's LCA we have two way to ; if found the key value in tree ; Case 1 : ; Case 2 : ; Function to find nodes common to given two nodes ; there is no path between these two node ; case 1 : ; count number of turns needs to reached the second node from LCA ; count number of turns needs to reached the first node from LCA ; case 2 : ; count number of turns needs to reached the second node from LCA ; count number of turns needs to reached the first node from LCA1 ; Driver program to test above functions ; Let us create binary tree given in the above example"
Solution in C#:

using System ; public class Turns_to_reach_another_node { public static int Count ; public class Node { public Node left , right ; public int key ; public Node ( int key ) { this . key = key ; left = null ; right = null ; } } public static Node findLCA ( Node root , int n1 , int n2 ) { if ( root == null ) { return null ; } if ( root . key == n1 root . key == n2 ) { return root ; } Node left_lca = findLCA ( root . left , n1 , n2 ) ; Node right_lca = findLCA ( root . right , n1 , n2 ) ; if ( left_lca != null && right_lca != null ) { return root ; } return ( left_lca != null ) ? left_lca : right_lca ; } public static bool CountTurn ( Node root , int key , bool turn ) { if ( root == null ) { return false ; } if ( root . key == key ) { return true ; } if ( turn == true ) { if ( CountTurn ( root . left , key , turn ) ) { return true ; } if ( CountTurn ( root . right , key , ! turn ) ) { Count += 1 ; return true ; } } else { if ( CountTurn ( root . right , key , turn ) ) { return true ; } if ( CountTurn ( root . left , key , ! turn ) ) { Count += 1 ; return true ; } } return false ; } public static int NumberOfTurn ( Node root , int first , int second ) { Node LCA = findLCA ( root , first , second ) ; if ( LCA == null ) { return - 1 ; } Count = 0 ; if ( LCA . key != first && LCA . key != second ) { if ( CountTurn ( LCA . right , second , false ) || CountTurn ( LCA . left , second , true ) ) { ; } if ( CountTurn ( LCA . left , first , true ) || CountTurn ( LCA . right , first , false ) ) { ; } return Count + 1 ; } if ( LCA . key == first ) { CountTurn ( LCA . right , second , false ) ; CountTurn ( LCA . left , second , true ) ; return Count ; } else { CountTurn ( LCA . right , first , false ) ; CountTurn ( LCA . left , first , true ) ; return Count ; } } public static void Main ( string [ ] args ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right . left = new Node ( 6 ) ; root . right...


"Number of unique rectangles formed using N unit squares | C # program to count rotationally equivalent rectangles with n unit squares ; height >= length is maintained ; driver code"
Solution in C#:

using System ; class GFG { static int countRect ( int n ) { int ans = 0 ; for ( int length = 1 ; length <= Math . Sqrt ( n ) ; ++ length ) for ( int height = length ; height * length <= n ; ++ height ) ans ++ ; return ans ; } public static void Main ( ) { int n = 5 ; Console . Write ( countRect ( n ) ) ; } }


"Number of unique triplets whose XOR is zero | C # program to count the number of unique triplets whose XOR is 0 ; function to count the number of unique triplets whose xor is 0 ; To store values that are present ; stores the count of unique triplets ; traverse for all i , j pairs such that j > i ; xor of a [ i ] and a [ j ] ; if xr of two numbers is present , then increase the count ; returns answer ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int countTriplets ( int [ ] a , int n ) { List < int > s = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) s . Add ( a [ i ] ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int xr = a [ i ] ^ a [ j ] ; if ( s . Exists ( item => item == xr ) && xr != a [ i ] && xr != a [ j ] ) count ++ ; } } return count / 3 ; } static void Main ( ) { int [ ] a = new int [ ] { 1 , 3 , 5 , 10 , 14 , 15 } ; int n = a . Length ; Console . Write ( countTriplets ( a , n ) ) ; } }


"Number of unmarked integers in a special sieve | C # Program to determine the number of unmarked integers in a special sieve ; Driver Code"
Solution in C#:

using System ; class GFG { static int countUnmarked ( int N ) { if ( N % 2 == 0 ) return N / 2 ; else return N / 2 + 1 ; } public static void Main ( ) { int N = 4 ; Console . WriteLine ( " Number ▁ of ▁ unmarked ▁ " + " elements : ▁ " + countUnmarked ( N ) ) ; } }


"Number of valid indices in the permutation of first N natural numbers | C # implementation of the approach ; Function to return the number of i 's  such that Pi <= Pj for all 1 <= j <= i  in the permutation of first N natural numbers ; To store the count of such indices ; Store the mini value ; For all the elements ; Return the required answer ; Driver code"
Solution in C#:

using System ; class GFG { static int INT_MAX = int . MaxValue ; static int min_index ( int [ ] p , int n ) { int ans = 0 ; int mini = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { if ( p [ i ] <= mini ) mini = p [ i ] ; if ( mini == p [ i ] ) ans ++ ; } return ans ; } public static void Main ( ) { int [ ] P = { 4 , 2 , 5 , 1 , 3 } ; int n = P . Length ; Console . WriteLine ( min_index ( P , n ) ) ; } }


"Number of visible boxes after putting one inside another | C # program to count number of visible boxes . ; return the minimum number of visible boxes ; New Queue of integers . ; sorting the array ; traversing the array ; checking if current element is greater than or equal to twice of front element ; Pushing each element of array ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int minimumBox ( int [ ] arr , int n ) { Queue < int > q = new Queue < int > ( ) ; Array . Sort ( arr ) ; q . Enqueue ( arr [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { int now = q . Peek ( ) ; if ( arr [ i ] >= 2 * now ) q . Dequeue ( ) ; q . Enqueue ( arr [ i ] ) ; } return q . Count ; } public static void Main ( ) { int [ ] arr = { 4 , 1 , 2 , 8 } ; int n = arr . Length ; Console . WriteLine ( minimumBox ( arr , n ) ) ; } }


"Number of ways N can be divided into four parts to construct a rectangle | C # implementation of the approach ; Function to return the required number of ways ; Driver code"
Solution in C#:

using System ; class GFG { static int cntWays ( int n ) { if ( n % 2 == 1 ) { return 0 ; } else { return ( n - 2 ) / 4 ; } } public static void Main ( String [ ] args ) { int n = 18 ; Console . WriteLine ( cntWays ( n ) ) ; } }


"Number of ways a convex polygon of n + 2 sides can split into triangles by connecting vertices | C # program to find the nth catalan number ; Returns value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; A Binomial coefficient based function to find nth catalan number in O ( n ) time ; Calculate value of 2 nCn ; return 2 nCn / ( n + 1 ) ; Driver code"
Solution in C#:

using System ; class GFG { static long binomialCoeff ( int n , int k ) { long res = 1 ; if ( k > n - k ) k = n - k ; for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } static long catalan ( int n ) { long c = binomialCoeff ( 2 * n , n ) ; return c / ( n + 1 ) ; } public static void Main ( ) { int n = 3 ; Console . WriteLine ( catalan ( n ) ) ; } }


"Number of ways in which N can be represented as the sum of two positive integers | C # implementation of the approach ; Function to return the number of distinct ways to represent n as the sum of two integers ; Driver code"
Solution in C#:

using System ; class GFG { static int ways ( int n ) { return n / 2 ; } public static void Main ( ) { int n = 2 ; Console . WriteLine ( ways ( n ) ) ; } }


"Number of ways in which an item returns back to its initial position in N swaps in array of size K | C # program to find the number of ways in which an item returns back to its initial position in N swaps in an array of size K ; Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y / 2 ; Function to return the number of ways ; Base case ; Recursive case F ( n ) = ( k - 1 ) ^ ( n - 1 ) - F ( n - 1 ) . ; Driver code ; Function calling"
Solution in C#:

using System ; class GFG { static int mod = 1000000007 ; public static int power ( int x , int y ) { int p = mod ; int res = 1 ; x = x % p ; while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } public static int solve ( int n , int k ) { if ( n == 1 ) return 0 ; return ( power ( ( k - 1 ) , n - 1 ) % mod - solve ( n - 1 , k ) + mod ) % mod ; } public static void Main ( string [ ] args ) { int n = 4 , k = 5 ; Console . Write ( solve ( n , k ) ) ; } }


"Number of ways in which the substring in range [ L , R ] can be formed using characters out of the range | C # implementation of the approach ; Function to return the number of ways to form the sub - string ; Initialize a hash - table with 0 ; Iterate in the string and count the frequency of characters that do not lie in the range L and R ; Out of range characters ; Stores the final number of ways ; Iterate for the sub - string in the range L and R ; If exists then multiply the number of ways and decrement the frequency ; If does not exist the sub - string cannot be formed ; Return the answer ; Driver code"
Solution in C#:

using System ; class GfG { static int calculateWays ( String s , int n , int l , int r ) { int [ ] freq = new int [ 26 ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( i < l i > r ) freq [ s [ i ] - ' a ' ] ++ ; } int ways = 1 ; for ( int i = l ; i <= r ; i ++ ) { if ( freq [ s [ i ] - ' a ' ] != 0 ) { ways = ways * freq [ s [ i ] - ' a ' ] ; freq [ s [ i ] - ' a ' ] -- ; } else { ways = 0 ; break ; } } return ways ; } public static void Main ( ) { String s = " cabcaab " ; int n = s . Length ; int l = 1 , r = 3 ; Console . WriteLine ( calculateWays ( s , n , l , r ) ) ; } }


"Number of ways of choosing K equal substrings of any length for every query | C # code to print level order traversal in sorted order ; Function to generate all the sub - strings ; Length of the string ; Generate all sub - strings ; Count the occurrence of every sub - string ; Compute the Binomial Coefficient ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Function to return the result for a query ; Iterate for every unique sub - string ; Count the combinations ; Driver code ; Get all the sub - strings Store the occurrence of all the sub - strings ; Pre - computation ; Queries ; Perform queries"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int maxlen = 100 ; public static void generateSubStrings ( String s , Dictionary < String , int > mpp ) { int l = s . Length ; for ( int i = 0 ; i < l ; i ++ ) { String temp = " " ; for ( int j = i ; j < l ; j ++ ) { temp += s [ j ] ; if ( mpp . ContainsKey ( temp ) ) { mpp [ temp ] = ++ mpp [ temp ] ; } else mpp . Add ( temp , 1 ) ; } } } public static void binomialCoeff ( int [ , ] C ) { int i , j ; for ( i = 1 ; i < 100 ; i ++ ) { for ( j = 0 ; j < 100 ; j ++ ) { if ( j == 0 j == i ) C [ i , j ] = 1 ; else C [ i , j ] = C [ i - 1 , j - 1 ] + C [ i - 1 , j ] ; } } } public static int answerQuery ( Dictionary < String , int > mpp , int [ , ] C , int k ) { int ans = 0 ; foreach ( KeyValuePair < String , int > entry in mpp ) { if ( entry . Value >= k ) ans += C [ entry . Value , k ] ; } return ans ; } public static void Main ( String [ ] args ) { String s = " aabaab " ; Dictionary < String , int > mpp = new Dictionary < String , int > ( ) ; generateSubStrings ( s , mpp ) ; int [ , ] C = new int [ maxlen , maxlen ] ; binomialCoeff ( C ) ; int [ ] queries = { 2 , 3 , 4 } ; int q = queries . Length ; for ( int i = 0 ; i < q ; i ++ ) Console . WriteLine ( answerQuery ( mpp , C , queries [ i ] ) ) ; } }


"Number of ways of distributing N identical objects in R distinct groups with no groups empty | C # implementation of the above approach ; Function to return the value of ncr effectively ; Initialize the answer ; Divide simultaneously by i to avoid overflow ; Function to return the number of ways to distribute N identical objects in R distinct objects ; Driver code"
Solution in C#:

using System ; class GFG { static int ncr ( int n , int r ) { int ans = 1 ; for ( int i = 1 ; i <= r ; i += 1 ) { ans *= ( n - r + i ) ; ans /= i ; } return ans ; } static int NoOfDistributions ( int N , int R ) { return ncr ( N - 1 , R - 1 ) ; } static public void Main ( ) { int N = 4 ; int R = 3 ; Console . WriteLine ( NoOfDistributions ( N , R ) ) ; } }


"Number of ways of distributing N identical objects in R distinct groups | C # implementation of the above approach ; Function to return the value of ncr effectively ; Initialize the answer ; Divide simultaneously by i to avoid overflow ; Function to return the number of ways to distribute N identical objects in R distinct objects ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { static int ncr ( int n , int r ) { int ans = 1 ; for ( int i = 1 ; i <= r ; i += 1 ) { ans *= ( n - r + i ) ; ans /= i ; } return ans ; } static int NoOfDistributions ( int N , int R ) { return ncr ( N + R - 1 , R - 1 ) ; } static public void Main ( ) { int N = 4 , R = 3 ; Console . WriteLine ( NoOfDistributions ( N , R ) ) ; } }


"Number of ways of scoring R runs in B balls with at most W wickets | C # implementation of the approach ; Function to return the number of ways to score R runs in B balls with at most W wickets ; If the wickets lost are more ; If runs scored are more ; If condition is met ; If no run got scored ; Already visited state ; If scored 0 run ; If scored 1 run ; If scored 2 runs ; If scored 3 runs ; If scored 4 runs ; If scored 6 runs ; If scored no run and lost a wicket ; Memoize and return ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static int mod = 1000000007 ; static int RUNMAX = 300 ; static int BALLMAX = 50 ; static int WICKETMAX = 10 ; static int CountWays ( int r , int b , int l , int R , int B , int W , int [ , , ] dp ) { if ( l > W ) return 0 ; if ( r > R ) return 0 ; if ( b == B && r == R ) return 1 ; if ( b == B ) return 0 ; if ( dp [ r , b , l ] != - 1 ) return dp [ r , b , l ] ; int ans = 0 ; ans += CountWays ( r , b + 1 , l , R , B , W , dp ) ; ans = ans % mod ; ans += CountWays ( r + 1 , b + 1 , l , R , B , W , dp ) ; ans = ans % mod ; ans += CountWays ( r + 2 , b + 1 , l , R , B , W , dp ) ; ans = ans % mod ; ans += CountWays ( r + 3 , b + 1 , l , R , B , W , dp ) ; ans = ans % mod ; ans += CountWays ( r + 4 , b + 1 , l , R , B , W , dp ) ; ans = ans % mod ; ans += CountWays ( r + 6 , b + 1 , l , R , B , W , dp ) ; ans = ans % mod ; ans += CountWays ( r , b + 1 , l + 1 , R , B , W , dp ) ; ans = ans % mod ; return dp [ r , b , l ] = ans ; } static void Main ( ) { int R = 40 , B = 10 , W = 4 ; int [ , , ] dp = new int [ RUNMAX , BALLMAX , WICKETMAX ] ; for ( int i = 0 ; i < RUNMAX ; i ++ ) for ( int j = 0 ; j < BALLMAX ; j ++ ) for ( int k = 0 ; k < WICKETMAX ; k ++ ) dp [ i , j , k ] = - 1 ; Console . WriteLine ( CountWays ( 0 , 0 , 0 , R , B , W , dp ) ) ; } }


"Number of ways of writing N as a sum of 4 squares | C # implementation of above approach ; Number of ways of writing n as a sum of 4 squares ; sum of odd and even factor ; iterate from 1 to the number ; if i is the factor of n ; if factor is even ; if factor is odd ; n / i is also a factor ; if factor is even ; if factor is odd ; if n is odd ; if n is even ; Driver code"
Solution in C#:

using System ; class GFG { static int sum_of_4_squares ( int n ) { int i , odd = 0 , even = 0 ; for ( i = 1 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( i % 2 == 0 ) even += i ; else odd += i ; if ( ( n / i ) != i ) { if ( ( n / i ) % 2 == 0 ) even += ( n / i ) ; else odd += ( n / i ) ; } } } if ( n % 2 == 1 ) return 8 * ( odd + even ) ; else return 24 * ( odd ) ; } static public void Main ( ) { int n = 4 ; Console . WriteLine ( sum_of_4_squares ( n ) ) ; } }


"Number of ways such that only K bars are visible from the left | C # implementation for the above approach ; dp array ; Function to calculate the number of permutations of N , where only K bars are visible from the left . ; If subproblem has already been calculated , return ; Only ascending order is possible ; N is placed at the first position The nest N - 1 are arranged in ( N - 1 ) ! ways ; Recursing ; Driver code ; Input ; Initialize dp array ; Function call"
Solution in C#:

using System ; public class GFG { static int [ , ] dp = new int [ 1005 , 1005 ] ; static int KvisibleFromLeft ( int N , int K ) { if ( dp [ N , K ] != - 1 ) return dp [ N , K ] ; if ( N == K ) return dp [ N , K ] = 1 ; if ( K == 1 ) { int ans = 1 ; for ( int i = 1 ; i < N ; i ++ ) ans *= i ; return dp [ N , K ] = ans ; } return dp [ N , K ] = KvisibleFromLeft ( N - 1 , K - 1 ) + ( N - 1 ) * KvisibleFromLeft ( N - 1 , K ) ; } public static void Main ( String [ ] args ) { int N = 5 , K = 2 ; for ( int i = 0 ; i < 1005 ; i ++ ) { for ( int j = 0 ; j < 1005 ; j ++ ) { dp [ i , j ] = - 1 ; } } Console . Write ( KvisibleFromLeft ( N , K ) ) ; } }


"Number of ways such that only K bars are visible from the left | C # program for the above approach ; Function to calculate the number of permutations of N , where only K bars are visible from the left . ; Only ascending order is possible ; N is placed at the first position The nest N - 1 are arranged in ( N - 1 ) ! ways ; Recursing ; Driver code ; Input ; Function call"
Solution in C#:

using System ; class GFG { static int KvisibleFromLeft ( int N , int K ) { if ( N == K ) return 1 ; if ( K == 1 ) { int ans = 1 ; for ( int i = 1 ; i < N ; i ++ ) ans *= i ; return ans ; } return KvisibleFromLeft ( N - 1 , K - 1 ) + ( N - 1 ) * KvisibleFromLeft ( N - 1 , K ) ; } public static void Main ( String [ ] args ) { int N = 5 , K = 2 ; Console . Write ( KvisibleFromLeft ( N , K ) ) ; } }


"Number of ways to arrange 2 * N persons on the two sides of a table with X and Y persons on opposite sides | C # implementation for the above approach ; Function to returns factorial of n ; Function to find nCr ; Function to find the number of ways to arrange 2 * N persons ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { static int factorial ( int n ) { if ( n <= 1 ) return 1 ; return n * factorial ( n - 1 ) ; } static int nCr ( int n , int r ) { return factorial ( n ) / ( factorial ( n - r ) * factorial ( r ) ) ; } static int NumberOfWays ( int n , int x , int y ) { return nCr ( 2 * n - x - y , n - x ) * factorial ( n ) * factorial ( n ) ; } public static void Main ( String [ ] args ) { int n = 5 , x = 4 , y = 2 ; Console . WriteLine ( NumberOfWays ( n , x , y ) ) ; } }


"Number of ways to arrange K different objects taking N objects at a time | C # implementation of the approach ; Function to return n ! % p ; long res = 1 ; Initialize result ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; x = x % p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Returns n ^ ( - 1 ) mod p ; Returns nCr % p using Fermat 's little theorem. ; Base case ; Fill factorial array so that we can find all factorial of r , n and n - r ; Function to return the number of ways to arrange K different objects taking N objects at a time ; Driver Code ; Function call"
Solution in C#:

using System ; class GFG { static long mod = ( long ) ( 1e9 + 7 ) ; static long factorial ( long n , long p ) { for ( int i = 2 ; i <= n ; i ++ ) res = ( res * i ) % p ; return res ; } static long power ( long x , long y , long p ) { while ( y > 0 ) { if ( ( y & 1 ) == 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } static long modInverse ( long n , long p ) { return power ( n , p - 2 , p ) ; } static long nCrModP ( long n , long r , long p ) { if ( r == 0 ) return 1 ; long [ ] fac = new long [ ( int ) n + 1 ] ; fac [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) fac [ i ] = fac [ i - 1 ] * i % p ; return ( fac [ ( int ) n ] * modInverse ( fac [ ( int ) r ] , p ) % p * modInverse ( fac [ ( int ) n - ( int ) r ] , p ) % p ) % p ; } static long countArrangements ( long n , long k , long p ) { return ( factorial ( n , p ) * nCrModP ( k , n , p ) ) % p ; } public static void Main ( ) { long N = 5 , K = 8 ; Console . WriteLine ( countArrangements ( N , K , mod ) ) ; } }


"Number of ways to arrange N items under given constraints | C # program to find number of ways to arrange items under given constraint ; method returns number of ways with which items can be arranged ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; declare dp array to store result up to ith colored item ; variable to keep track of count of items considered till now ; loop over all different colors ; populate next value using current value and stated relation ; return value stored at last index ; Driver code"
Solution in C#:

using System ; class GFG { static int waysToArrange ( int N , int K , int [ ] k ) { int [ , ] C = new int [ N + 1 , N + 1 ] ; int i , j ; for ( i = 0 ; i <= N ; i ++ ) { for ( j = 0 ; j <= i ; j ++ ) { if ( j == 0 j == i ) C [ i , j ] = 1 ; else C [ i , j ] = ( C [ i - 1 , j - 1 ] + C [ i - 1 , j ] ) ; } } int [ ] dp = new int [ K + 1 ] ; int count = 0 ; dp [ 0 ] = 1 ; for ( i = 0 ; i < K ; i ++ ) { dp [ i + 1 ] = ( dp [ i ] * C [ count + k [ i ] - 1 , k [ i ] - 1 ] ) ; count += k [ i ] ; } return dp [ K ] ; } static void Main ( ) { int N = 4 ; int [ ] k = new int [ ] { 2 , 2 } ; int K = k . Length ; Console . Write ( waysToArrange ( N , K , k ) ) ; } }


"Number of ways to choose elements from the array such that their average is K | C # implementation of the above approach ; This dp array is used to store our values so that we don 't have to calculate same  values again and again ; Base cases Index can 't be less than 0 ; No element is picked hence average cannot be calculated ; If remainder is non zero , we cannot divide the sum by count i . e . the average will not be an integer ; If we find an average return 1 ; If we have already calculated this function simply return it instead of calculating it again ; If we don 't pick the current element  simple recur for index -1 ; If we pick the current element add it to our current sum and increment count by 1 ; Store the value for the current function ; Function to return the number of ways ; Push - 1 at the beginning to make it 1 - based indexing ; Initialize dp array by - 1 ; Call recursive function waysutil to calculate total ways ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int MAX_INDEX = 51 ; static int MAX_SUM = 2505 ; static int [ , , ] dp = new int [ MAX_INDEX , MAX_SUM , MAX_INDEX ] ; static int waysutil ( int index , int sum , int count , List < int > arr , int K ) { if ( index < 0 ) { return 0 ; } if ( index == 0 ) { if ( count == 0 ) { return 0 ; } int remainder = sum % count ; if ( remainder != 0 ) { return 0 ; } int average = sum / count ; if ( average == K ) { return 1 ; } } if ( dp [ index , sum , count ] != - 1 ) { return dp [ index , sum , count ] ; } int dontpick = waysutil ( index - 1 , sum , count , arr , K ) ; int pick = waysutil ( index - 1 , sum + arr [ index ] , count + 1 , arr , K ) ; int total = pick + dontpick ; dp [ index , sum , count ] = total ; return total ; } static int ways ( int N , int K , int [ ] arr ) { List < int > Arr = new List < int > ( ) ; Arr . Add ( - 1 ) ; for ( int i = 0 ; i < N ; ++ i ) { Arr . Add ( arr [ i ] ) ; } for ( int i = 0 ; i < MAX_INDEX ; i ++ ) { for ( int j = 0 ; j < MAX_SUM ; j ++ ) { for ( int l = 0 ; l < MAX_INDEX ; l ++ ) { dp [ i , j , l ] = - 1 ; } } } int answer = waysutil ( N , 0 , 0 , Arr , K ) ; return answer ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 6 , 2 , 8 , 7 , 6 , 5 , 9 } ; int N = arr . Length ; int K = 5 ; Console . WriteLine ( ways ( N , K , arr ) ) ; } }


"Number of ways to color N | C # program for the above approach ; Function to count the ways to color block ; For storing powers of 2 ; For storing binomial coefficient values ; Calculating binomial coefficient using DP ; Calculating powers of 2 ; Sort the indices to calculate length of each section ; Initialise answer to 1 ; Find the length of each section ; Merge this section ; Return the readonly count ; Driver Code ; Number of blocks ; Number of colored blocks ; Function call"
Solution in C#:

using System ; class GFG { static int mod = 1000000007 ; static int waysToColor ( int [ ] arr , int n , int k ) { int [ ] powOf2 = new int [ 500 ] ; int [ , ] c = new int [ 500 , 500 ] ; for ( int i = 0 ; i <= n ; i ++ ) { c [ i , 0 ] = 1 ; for ( int j = 1 ; j <= i ; j ++ ) { c [ i , j ] = ( c [ i - 1 , j ] + c [ i - 1 , j - 1 ] ) % mod ; } } powOf2 [ 0 ] = powOf2 [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { powOf2 [ i ] = powOf2 [ i - 1 ] * 2 % mod ; } int rem = n - k ; arr [ k ++ ] = n + 1 ; Array . Sort ( arr ) ; int answer = 1 ; for ( int i = 0 ; i < k ; i ++ ) { int x = arr [ i ] - ( i - 1 >= 0 ? arr [ i - 1 ] : 0 ) - 1 ; answer *= c [ rem , x ] % mod * ( i != 0 && i != k - 1 ? powOf2 [ x ] : 1 ) % mod ; rem -= x ; } return answer ; } public static void Main ( String [ ] args ) { int N = 6 ; int K = 3 ; int [ ] arr = { 1 , 2 , 6 , 0 } ; Console . Write ( waysToColor ( arr , N , K ) ) ; } }


"Number of ways to color N | C # program for the above approach ; Recursive function to count the ways ; Base case ; Initialise answer to 0 ; Color each uncolored block according to the given condition ; If any block is uncolored ; Check if adjacent blocks are colored or not ; Color the block ; recursively iterate for next uncolored block ; Uncolored for the next recursive call ; Return the final count ; Function to count the ways to color block ; Mark which blocks are colored in each recursive step ; Function call to count the ways ; Driver Code ; Number of blocks ; Number of colored blocks ; Function call"
Solution in C#:

using System ; class GFG { static int mod = 1000000007 ; static int countWays ( int [ ] colored , int count , int n ) { if ( count == n ) { return 1 ; } int answer = 0 ; for ( int i = 1 ; i < n + 1 ; i ++ ) { if ( colored [ i ] == 0 ) { if ( colored [ i - 1 ] == 1 colored [ i + 1 ] == 1 ) { colored [ i ] = 1 ; answer = ( answer + countWays ( colored , count + 1 , n ) ) % mod ; colored [ i ] = 0 ; } } } return answer ; } static int waysToColor ( int [ ] arr , int n , int k ) { int [ ] colored = new int [ n + 2 ] ; for ( int i = 0 ; i < k ; i ++ ) { colored [ arr [ i ] ] = 1 ; } return countWays ( colored , k , n ) ; } public static void Main ( ) { int N = 6 ; int K = 3 ; int [ ] arr = { 1 , 2 , 6 } ; Console . Write ( waysToColor ( arr , N , K ) ) ; } }


"Number of ways to color boundary of each block of M * N table | C # program to count the number of ways to color boundary of each block of M * N table . ; Function to compute all way to fill the boundary of all sides of the unit square ; Count possible ways to fill all upper and left side of the rectangle M * N ; Count possible ways to fill all side of the all squares unit size ; Driver code ; Number of rows ; Number of columns"
Solution in C#:

using System ; class GFG { static int CountWays ( int N , int M ) { int count = 1 ; count = ( int ) Math . Pow ( 3 , M + N ) ; count *= ( int ) Math . Pow ( 2 , M * N ) ; return count ; } static void Main ( ) { int N = 3 ; int M = 2 ; Console . Write ( CountWays ( N , M ) ) ; } }


"Number of ways to convert a character X to a string Y | C # implementation of the approach ; Function to find the modular - inverse ; While power > 1 ; Updating s and a ; Updating power ; Return the final answer ; Function to return the count of ways ; To store the final answer ; To store pre - computed factorials ; Computing factorials ; Loop to find the occurrences of x and update the ans ; Multiplying the answer by ( n - 1 ) ! ; Return the final answer ; Driver code"
Solution in C#:

using System ; class GFG { static int MOD = 1000000007 ; static long modInv ( long a ) { long p = MOD - 2 ; long s = 1 ; while ( p != 1 ) { if ( p % 2 == 1 ) s = ( s * a ) % MOD ; a = ( a * a ) % MOD ; p /= 2 ; } return ( a * s ) % MOD ; } static long findCnt ( char x , String y ) { long ans = 0 ; long [ ] fact = new long [ y . Length + 1 ] ; for ( int i = 0 ; i < y . Length + 1 ; i ++ ) fact [ i ] = 1 ; for ( int i = 1 ; i <= y . Length ; i ++ ) fact [ i ] = ( fact [ i - 1 ] * i ) % MOD ; for ( int i = 0 ; i < y . Length ; i ++ ) { if ( y [ i ] == x ) { ans += ( modInv ( fact [ i ] ) * modInv ( fact [ y . Length - i - 1 ] ) ) % MOD ; ans %= MOD ; } } ans *= fact [ ( y . Length - 1 ) ] ; ans %= MOD ; return ans ; } public static void Main ( ) { char x = ' a ' ; string y = " xxayy " ; Console . WriteLine ( findCnt ( x , y ) ) ; } }


"Number of ways to cut a stick of length N into in even length at most K units long pieces | C # program for the above approach ; Recursive function to count the total number of ways ; Base case if no - solution exist ; Condition if a solution exist ; Check if already calculated ; Initialize counter ; Recursive call ; Store the answer ; Return the answer ; Driver code"
Solution in C#:

using System ; class GFG { static int solve ( int n , int k , int mod , int [ ] dp ) { if ( n < 0 ) return 0 ; if ( n == 0 ) return 1 ; if ( dp [ n ] != - 1 ) return dp [ n ] ; int cnt = 0 ; for ( int i = 2 ; i <= k ; i += 2 ) { cnt = ( cnt % mod + solve ( n - i , k , mod , dp ) % mod ) % mod ; } dp [ n ] = cnt ; return cnt ; } public static void Main ( String [ ] args ) { int mod = ( int ) ( 1e9 + 7 ) ; int n = 4 , k = 2 ; int [ ] dp = new int [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) dp [ i ] = - 1 ; int ans = solve ( n , k , mod , dp ) ; Console . WriteLine ( ans ) ; } }


"Number of ways to distribute N Paper Set among M students | C # implementation of the approach ; Function to return n ! % 1000000007 ; To store the factorial ; Find the factorial ; Function to return the count of possible ways ; Driver code"
Solution in C#:

using System ; class GFG { static int MOD = 1000000007 ; static int factMod ( int n ) { int fact = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { fact *= ( i % MOD ) ; fact %= MOD ; } return fact ; } static int countWays ( int n , int m ) { return factMod ( m ) ; } public static void Main ( ) { int n = 2 , m = 2 ; Console . Write ( countWays ( n , m ) ) ; } }


"Number of ways to divide an array into K equal sum sub | C # implementation of the approach ; Array to store the states of DP ; Array to check if a state has been solved before ; To store the sum of the array elements ; Function to find the sum of all the array elements ; Function to return the number of ways ; If sum is not divisible by k answer will be zero ; Base case ; To check if a state has been solved before ; Sum of all the numbers from the beginning of the array ; Setting the current state as solved ; Recurrence relation ; Returning solved state ; Driver code ; Function call to find the sum of the array elements ; Print the number of ways"
Solution in C#:

using System ; class GFG { static int max_size = 20 ; static int max_k = 20 ; static int [ , ] dp = new int [ max_size , max_k ] ; static Boolean [ , ] v = new Boolean [ max_size , max_k ] ; static int sum = 0 ; static void findSum ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; } static int cntWays ( int [ ] arr , int i , int ck , int k , int n , int curr_sum ) { if ( sum % k != 0 ) return 0 ; if ( i != n && ck == k + 1 ) return 0 ; if ( i == n ) { if ( ck == k + 1 ) return 1 ; else return 0 ; } if ( v [ i , ck ] ) return dp [ i , ck ] ; curr_sum += arr [ i ] ; v [ i , ck ] = true ; dp [ i , ck ] = cntWays ( arr , i + 1 , ck , k , n , curr_sum ) ; if ( curr_sum == ( sum / k ) * ck ) dp [ i , ck ] += cntWays ( arr , i + 1 , ck + 1 , k , n , curr_sum ) ; return dp [ i , ck ] ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , - 1 , 1 , - 1 , 1 , - 1 } ; int n = arr . Length ; int k = 2 ; findSum ( arr , n ) ; Console . WriteLine ( cntWays ( arr , 0 , 1 , k , n , 0 ) ) ; } }


"Number of ways to erase exactly one element in the Binary Array to make XOR zero | C # program to find the number of ways to erase exactly one element from this array to make XOR zero ; Function to find the number of ways ; Calculate the number of 1 ' s ▁ and ▁ 0' s ; Considering the 4 cases ; Driver code"
Solution in C#:

using System ; class GFG { static int no_of_ways ( int [ ] a , int n ) { int count_0 = 0 , count_1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) count_0 ++ ; else count_1 ++ ; } if ( count_1 % 2 == 0 ) return count_0 ; else return count_1 ; } public static void Main ( ) { int n = 4 ; int [ ] a1 = { 1 , 1 , 0 , 0 } ; Console . WriteLine ( no_of_ways ( a1 , n ) ) ; n = 5 ; int [ ] a2 = { 1 , 1 , 1 , 0 , 0 } ; Console . WriteLine ( no_of_ways ( a2 , n ) ) ; n = 5 ; int [ ] a3 = { 1 , 1 , 0 , 0 , 0 } ; Console . WriteLine ( no_of_ways ( a3 , n ) ) ; n = 6 ; int [ ] a4 = { 1 , 1 , 1 , 0 , 0 , 0 } ; Console . WriteLine ( no_of_ways ( a4 , n ) ) ; } }


"Number of ways to form a heap with n distinct integers | C # program to count max heaps with n distinct keys ; dp [ i ] = number of max heaps for i distinct integers ; nck [ i ] [ j ] = number of ways to choose j elements form i elements , no order ; log2 [ i ] = floor of logarithm of base 2 of i ; to calculate nCk ; calculate l for give value of n ; number of elements that are actually present in last level ( hth level ) ( 2 ^ h - 1 ) ; if more than half - filled ; return ( 1 << h ) - 1 ; ( 2 ^ h ) - 1 ; find maximum number of heaps for n ; function to initialize arrays ; for each power of two find logarithm ; driver function"
Solution in C#:

using System ; class GFG { static int [ ] dp = new int [ MAXN ] ; static int [ , ] nck = new int [ MAXN , MAXN ] ; static int [ ] log2 = new int [ MAXN ] ; public static int choose ( int n , int k ) { if ( k > n ) return 0 ; if ( n <= 1 ) return 1 ; if ( k == 0 ) return 1 ; if ( nck [ n , k ] != - 1 ) return nck [ n , k ] ; int answer = choose ( n - 1 , k - 1 ) + choose ( n - 1 , k ) ; nck [ n , k ] = answer ; return answer ; } public static int getLeft ( int n ) { if ( n == 1 ) return 0 ; int h = log2 [ n ] ; int last = n - ( ( 1 << h ) - 1 ) ; if ( last >= ( numh / 2 ) ) else return ( 1 << h ) - 1 - ( ( numh / 2 ) - last ) ; } public static int numberOfHeaps ( int n ) { if ( n <= 1 ) return 1 ; if ( dp [ n ] != - 1 ) return dp [ n ] ; int left = getLeft ( n ) ; int ans = ( choose ( n - 1 , left ) * numberOfHeaps ( left ) ) * ( numberOfHeaps ( n - 1 - left ) ) ; dp [ n ] = ans ; return ans ; } public static int solve ( int n ) { for ( int i = 0 ; i <= n ; i ++ ) dp [ i ] = - 1 ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= n ; j ++ ) nck [ i , j ] = - 1 ; int currLog2 = - 1 ; int currPower2 = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( currPower2 == i ) { currLog2 ++ ; currPower2 *= 2 ; } log2 [ i ] = currLog2 ; } return numberOfHeaps ( n ) ; } static void Main ( ) { int n = 10 ; Console . Write ( solve ( n ) ) ; } }


"Number of ways to form a number with maximum Ks in it | C # program for the above approach ; Function to calculate number of ways a number can be formed that has the maximum number of Ks ; Convert to string ; Calculate length of subarrays that can contribute to the answer ; Count length of subarray where adjacent digits add up to K ; Current subarray can contribute to the answer only if it is odd ; Return the answer ; Driver code ; Input ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int noOfWays ( int N , int K ) { string S = N . ToString ( ) ; int ans = 1 ; for ( int i = 1 ; i < S . Length ; i ++ ) { int count = 1 ; while ( i < S . Length && ( int ) S [ i ] - 48 + ( int ) S [ i - 1 ] - 48 == K ) { count ++ ; i ++ ; } if ( count % 2 == 1 ) ans *= ( count + 1 ) / 2 ; } return ans ; } public static void Main ( ) { int N = 1454781 ; int K = 9 ; Console . Write ( noOfWays ( N , K ) ) ; } }