"Count pairs in an array such that the absolute difference between them is ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒ ¢ Ã ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â € š ¬ Ã …¡¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â € š ¬ Ã …¾¢¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒ ¢ Ã ¢ â ‚¬ Å ¾¢¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢¢ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒâ €¦¡¬ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬¦¡¬°¥ K | C # implementation of the approach ; Function to return the count of required pairs ; Sort the given array ; To store the required count ; Update j such that it is always > i ; Find the first element arr [ j ] such that ( arr [ j ] - arr [ i ] ) >= K This is because after this element , all the elements will have absolute difference with arr [ i ] >= k and the count of valid pairs will be ( n - j ) ; Update the count of valid pairs ; Get to the next element to repeat the steps ; Return the count ; Driver code"
Solution in C#:

using System ; class GFG { static int count ( int [ ] arr , int n , int k ) { Array . Sort ( arr ) ; int cnt = 0 ; int i = 0 , j = 1 ; while ( i < n && j < n ) { j = ( j <= i ) ? ( i + 1 ) : j ; while ( j < n && ( arr [ j ] - arr [ i ] ) < k ) j ++ ; cnt += ( n - j ) ; i ++ ; } return cnt ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . Length ; int k = 2 ; Console . Write ( count ( arr , n , k ) ) ; } }


"Count pairs in an array which have at least one digit common | C # Program to count pairs in an array with some common digit ; Returns true if the pair is valid , otherwise false ; converting integers to strings ; Iterate over the strings and check if a character in first string is also present in second string , return true ; No common digit found ; Returns the number of valid pairs ; Iterate over all possible pairs ; Driver Code to test above functions"
Solution in C#:

using System ; class GFG { static bool checkValidPair ( int num1 , int num2 ) { string s1 = num1 . ToString ( ) ; string s2 = num2 . ToString ( ) ; for ( int i = 0 ; i < s1 . Length ; i ++ ) for ( int j = 0 ; j < s2 . Length ; j ++ ) if ( s1 [ i ] == s2 [ j ] ) return true ; return false ; } static int countPairs ( int [ ] arr , int n ) { int numberOfPairs = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( checkValidPair ( arr [ i ] , arr [ j ] ) ) numberOfPairs ++ ; return numberOfPairs ; } static void Main ( ) { int [ ] arr = new int [ ] { 10 , 12 , 24 } ; int n = arr . Length ; Console . WriteLine ( countPairs ( arr , n ) ) ; } }


"Count pairs in an array whose product is composite number | C # Program to implement the above approach ; Function to check if a number is prime or not ; Check if N is multiple of i or not . ; If N is multiple of i . ; Function to get the count of pairs whose product is a composite number . ; Stores the count of pairs whose product is a composite number ; Generate all possible pairs ; Stores the product of element of current pair ; If prod is a composite number ; Driver Code"
Solution in C#:

using System ; class GFG { static bool isComposite ( int N ) { for ( int i = 2 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { return true ; } } return false ; } static int compositePair ( int [ ] arr , int N ) { int res = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { int prod = arr [ i ] * arr [ j ] ; if ( isComposite ( prod ) ) { res ++ ; } } } return res ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 2 , 2 , 8 } ; int N = arr . Length ; Console . WriteLine ( compositePair ( arr , N ) ) ; } }


"Count pairs in an array whose product is composite number | C # program to implement the above approach ; Function to get all the prime numbers in the range [ 1 , X ] ; Stores the bool value to check if a number is prime or not ; Mark all non prime numbers as false ; If i is prime number ; Mark j as a composite number ; Function to get the count of pairs whose product is a composite number ; Stores the bool value to check if a number is prime or not ; Stores the count of 1 s ; Stores the count of prime numbers ; Traverse the given array . ; Stores count of pairs whose product is not a composite number ; Stores the count of pairs whose product is composite number ; Driver code"
Solution in C#:

using System ; class GFG { public static int X = 1000000 ; public static bool [ ] getPrimeNum ( ) { bool [ ] isPrime = new bool [ X ] ; for ( int i = 0 ; i < X ; i ++ ) isPrime [ i ] = true ; isPrime [ 0 ] = false ; isPrime [ 1 ] = false ; for ( int i = 2 ; i * i <= X ; i ++ ) { if ( isPrime [ i ] == true ) { for ( int j = i * i ; j < X ; j += i ) { isPrime [ j ] = false ; } } } return isPrime ; } public static int cntPairs ( int [ ] arr , int N ) { bool [ ] isPrime = getPrimeNum ( ) ; int cntOne = 0 ; int cntPrime = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 1 ) { cntOne += 1 ; } else if ( isPrime [ i ] ) { cntPrime += 1 ; } } int cntNonComp = 0 ; cntNonComp = cntPrime * cntOne + cntOne * ( cntOne - 1 ) / 2 ; int res = 0 ; res = N * ( N - 1 ) / 2 - cntNonComp ; return res ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 2 , 2 , 8 } ; int N = arr . Length ; Console . WriteLine ( cntPairs ( arr , N ) ) ; } }


"Count pairs in array such that one element is power of another | C # program to count pairs in array such that one element is power of another ; Function to check if given number number y is power of x ; log function to calculate value ; compare to the result1 or result2 both are equal ; Function to find pairs from array ; Iterate through all pairs ; Increment count if one is the power of other ; Driver code"
Solution in C#:

using System ; public class GFG { static bool isPower ( int x , int y ) { int res1 = ( int ) ( Math . Log ( y ) / Math . Log ( x ) ) ; double res2 = Math . Log ( y ) / Math . Log ( x ) ; return ( res1 == res2 ) ; } static int countPower ( int [ ] arr , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( isPower ( arr [ i ] , arr [ j ] ) || isPower ( arr [ j ] , arr [ i ] ) ) res ++ ; return res ; } public static void Main ( ) { int [ ] a = { 16 , 2 , 3 , 9 } ; int n = a . Length ; Console . WriteLine ( countPower ( a , n ) ) ; } }


"Count pairs in array such that one element is reverse of another | C # program to count the pairs in array such that one element is reverse of another ; Function to reverse the digits of the number ; Loop to iterate till the number is greater than 0 ; Extract the last digit and keep multiplying it by 10 to get the reverse of the number ; Function to find the pairs from the array such that one number is reverse of the other ; Iterate over every element in the array and increase the frequency of the element in hash map ; Iterate over every element in the array ; Remove the current element from the hash map by decreasing the frequency to avoid counting when the number is a palindrome or when we visit its reverse ; Increment the count by the frequency of reverse of the number ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int reverse ( int num ) { int rev_num = 0 ; while ( num > 0 ) { rev_num = rev_num * 10 + num % 10 ; num = num / 10 ; } return rev_num ; } static int countReverse ( int [ ] arr , int n ) { Dictionary < int , int > freq = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( freq . ContainsKey ( arr [ i ] ) ) { freq [ arr [ i ] ] ++ ; } else { freq . Add ( arr [ i ] , 1 ) ; } } int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq . ContainsKey ( arr [ i ] ) ) { freq [ arr [ i ] ] -- ; } else { freq . Add ( arr [ i ] , - 1 ) ; } if ( freq . ContainsKey ( reverse ( arr [ i ] ) ) ) { res += freq [ reverse ( arr [ i ] ) ] ; } } return res ; } static void Main ( ) { int [ ] a = { 16 , 61 , 12 , 21 , 25 } ; int n = a . Length ; Console . WriteLine ( countReverse ( a , n ) ) ; } }


"Count pairs in array such that one element is reverse of another | C # program to count the pairs in array such that one element is reverse of another ; Function to reverse the digits of the number ; Loop to iterate till the number is greater than 0 ; Extract the last digit and keep multiplying it by 10 to get the reverse of the number ; Function to find the pairs from the such that one number is reverse of the other ; Iterate through all pairs ; Increment count if one is the reverse of other ; Driver code"
Solution in C#:

using System ; class GFG { static int reverse ( int num ) { int rev_num = 0 ; while ( num > 0 ) { rev_num = rev_num * 10 + num % 10 ; num = num / 10 ; } return rev_num ; } static int countReverse ( int [ ] arr , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( reverse ( arr [ i ] ) == arr [ j ] ) { res ++ ; } return res ; } public static void Main ( String [ ] arr ) { int [ ] a = { 16 , 61 , 12 , 21 , 25 } ; int n = a . Length ; Console . Write ( countReverse ( a , n ) ) ; } }


"Count pairs in array whose sum is divisible by 4 | C # program to count pairs whose sum divisible by '4' ; Program to count pairs whose sum divisible by '4' ; Create a frequency array to count occurrences of all remainders when divided by 4 ; Count occurrences of all remainders ; If both pairs are divisible by '4' ; If both pairs are 2 modulo 4 ; If one of them is equal to 1 modulo 4 and the other is equal to 3 modulo 4 ; Driver code"
Solution in C#:

using System ; class Count { public static int count4Divisibiles ( int [ ] arr , int n ) { int [ ] freq = { 0 , 0 , 0 , 0 } ; int i = 0 ; int ans ; for ( i = 0 ; i < n ; i ++ ) ++ freq [ arr [ i ] % 4 ] ; ans = freq [ 0 ] * ( freq [ 0 ] - 1 ) / 2 ; ans += freq [ 2 ] * ( freq [ 2 ] - 1 ) / 2 ; ans += freq [ 1 ] * freq [ 3 ] ; return ( ans ) ; } public static void Main ( ) { int [ ] arr = { 2 , 2 , 1 , 7 , 5 } ; int n = 5 ; Console . WriteLine ( count4Divisibiles ( arr , n ) ) ; } }


"Count pairs made up of an element divisible by the other from an array consisting of powers of 2 | C # program for the above approach ; Function to count the number of pairs as per the given conditions ; Initialize array set_bits as 0 ; Store the total number of required pairs ; Traverse the array arr [ ] ; Store arr [ i ] in x ; Store the position of the leftmost set bit in arr [ i ] ; Increase bit position ; Divide by 2 to shift bits in right at each step ; Count of pairs for index i till its set bit position ; Increasing count of set bit position of current elelement ; Print the answer ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static void numberOfPairs ( int [ ] arr , int N ) { int [ ] set_bits = new int [ 31 ] ; for ( int i = 0 ; i < N ; i ++ ) { set_bits [ i ] = 0 ; } int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int x = arr [ i ] ; int bitpos = - 1 ; while ( x > 0 ) { bitpos ++ ; x /= 2 ; } for ( int j = 0 ; j <= bitpos ; j ++ ) { count += set_bits [ j ] ; } set_bits [ bitpos ] ++ ; } Console . Write ( count ) ; } static public void Main ( ) { int [ ] arr = { 4 , 16 , 8 , 64 } ; int N = arr . Length ; numberOfPairs ( arr , N ) ; } }


"Count pairs of characters in a string whose ASCII value difference is K | C # implementation of the approach ; Function to return the count of required pairs of characters ; Length of the string ; To store the frequency of each character ; Update the frequency of each character ; To store the required count of pairs ; If ascii value difference is zero ; If there exists similar characters more than once ; If there exits characters with ASCII value difference as k ; Return the required count ; Driver code"
Solution in C#:

using System ; class GFG { static int MAX = 26 ; static int countPairs ( char [ ] str , int k ) { int n = str . Length ; int [ ] freq = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { freq [ str [ i ] - ' a ' ] ++ ; } int cnt = 0 ; if ( k == 0 ) { for ( int i = 0 ; i < MAX ; i ++ ) { if ( freq [ i ] > 1 ) { cnt += ( ( freq [ i ] * ( freq [ i ] - 1 ) ) / 2 ) ; } } } else { for ( int i = 0 ; i < MAX ; i ++ ) { if ( freq [ i ] > 0 && i + k < MAX && freq [ i + k ] > 0 ) { cnt += ( freq [ i ] * freq [ i + k ] ) ; } } ; } return cnt ; } public static void Main ( String [ ] args ) { String str = " abcdab " ; int k = 0 ; Console . WriteLine ( countPairs ( str . ToCharArray ( ) , k ) ) ; } }


"Count pairs of coordinates connected by a line with slope in the range [ | C # program for the above approach ; Function to find the number of pairs of points such that the line passing through them has a slope in the range [ - k , k ] ; Store the result ; Traverse through all the combination of points ; If pair satisfies the given condition ; Increment ans by 1 ; Print the result ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static void findPairs ( int [ ] x , int [ ] y , int K ) { int n = x . Length ; int ans = 0 ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = i + 1 ; j < n ; ++ j ) { if ( K * Math . Abs ( x [ i ] - x [ j ] ) >= Math . Abs ( y [ i ] - y [ j ] ) ) { ++ ans ; } } } Console . WriteLine ( ans ) ; } public static void Main ( String [ ] args ) { int [ ] X = { 2 , 1 , 0 } ; int [ ] Y = { 1 , 2 , 0 } ; int K = 1 ; findPairs ( X , Y , K ) ; } }


"Count pairs of elements such that number of set bits in their AND is B [ i ] | C # Implementation of the above approach ; Function to return the count of pairs which satisfy the given condition ; Check if the count of set bits in the AND value is B [ j ] ; Function to get no of set bits in binary representation of positive integer n ; Driver code"
Solution in C#:

using System ; class GFG { static int solve ( int [ ] A , int [ ] B , int n ) { int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { if ( countSetBits ( A [ i ] & A [ j ] ) == B [ j ] ) { cnt ++ ; } } } return cnt ; } static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } public static void Main ( String [ ] args ) { int [ ] A = { 2 , 3 , 1 , 4 , 5 } ; int [ ] B = { 2 , 2 , 1 , 4 , 2 } ; int size = A . Length ; Console . WriteLine ( solve ( A , B , size ) ) ; } }


"Count pairs of equal array elements remaining after every removal | C # program to implement the above approach ; Function to count pairs of equal elements by removing arr [ i ] from the array ; Stores total count of pairs of equal elements ; Store frequency of each distinct array element ; Traverse the array ; Update frequency of arr [ i ] ; Traverse the map ; Stores key of an element ; Traverse the array ; Stores count of pairs of equal element by removing arr [ i ] ; Driver code ; Given Array"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void pairs_after_removing ( int [ ] arr , int N ) { int cntPairs = 0 ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] ++ ; } else { mp [ arr [ i ] ] = 1 ; } } foreach ( KeyValuePair < int , int > element in mp ) { int i = element . Key ; cntPairs += mp [ i ] * ( mp [ i ] - 1 ) / 2 ; } for ( int i = 0 ; i < N ; i ++ ) { int pairs_after_arr_i_removed = cntPairs + 1 - mp [ arr [ i ] ] ; Console . Write ( pairs_after_arr_i_removed + " ▁ " ) ; } return ; } public static void Main ( ) { int [ ] arr = { 2 , 3 , 4 , 3 , 2 } ; int N = arr . Length ; pairs_after_removing ( arr , N ) ; } }


"Count pairs of indices having equal prefix and suffix sums | C # code for above approach ; Function to find the count of index pairs having equal prefix and suffix sums ; Maps indices with prefix sums ; Traverse the array ; Update prefix sum ; Update frequency in Map ; Traverse the array in reverse ; Update suffix sum ; Check if any prefix sum of equal value exists or not ; Print the obtained count ; Driver code ; Given array ; Given size ; Function Call"
Solution in C#:

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


"Count pairs of indices having sum of indices same as the sum of elements at those indices | C # program for the above approach ; Function to find all possible pairs of the given array such that the sum of arr [ i ] + arr [ j ] is i + j ; Stores the total count of pairs ; Iterate over the range ; Iterate over the range ; Print the total count ; Driver Code"
Solution in C#:

using System ; class GFG { static void countPairs ( int [ ] arr , int N ) { int answer = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( arr [ i ] + arr [ j ] == i + j ) { answer ++ ; } } } Console . Write ( answer ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 0 , 1 , 2 , 3 , 4 , 5 } ; int N = arr . Length ; countPairs ( arr , N ) ; } }


"Count pairs of leaf nodes in a Binary Tree which are at most K distance apart | C # implementation of the above approach ; Structure of a Node ; Constructor of the class ; Stores the count of required pairs ; Function to perform dfs to find pair of leaf nodes at most K distance apart ; Return empty array if node is null ; If node is a leaf node and return res ; Traverse to the left ; Traverse to the right ; Update the distance between left and right leaf node ; Count all pair of leaf nodes which are at most K distance apart ; Return res to parent node ; Driver Code ; 1 / \ 2 3 / 4 ; Given distance K ; Function Call"
Solution in C#:

using System ; class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class GFG { Node root ; static int result ; static int [ ] dfs ( Node root , int distance ) { int [ ] res ; if ( root == null ) return new int [ distance + 1 ] ; if ( root . left == null && root . right == null ) { res = new int [ distance + 1 ] ; res [ 1 ] ++ ; return res ; } int [ ] left = dfs ( root . left , distance ) ; int [ ] right = dfs ( root . right , distance ) ; res = new int [ distance + 1 ] ; for ( int i = res . Length - 2 ; i >= 1 ; i -- ) { res [ i + 1 ] = left [ i ] + right [ i ] ; } for ( int l = 1 ; l < left . Length ; l ++ ) { for ( int r = 0 ; r < right . Length ; r ++ ) { if ( l + r <= distance ) { result += left [ l ] * right [ r ] ; } } } return res ; } public static void Main ( String [ ] args ) { GFG tree = new GFG ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; result = 0 ; int K = 3 ; dfs ( tree . root , K ) ; Console . WriteLine ( result ) ; } }


"Count pairs of natural numbers with GCD equal to given number | C # program to count pair in range of natural number having GCD equal to given number . ; Return the GCD of two numbers . ; Return the count of pairs having GCD equal to g . ; Setting the value of L , R . ; For each possible pair check if GCD is 1. ; Driver code"
Solution in C#:

using System ; class GFG { static int gcd ( int a , int b ) { return b > 0 ? gcd ( b , a % b ) : a ; } static int countGCD ( int L , int R , int g ) { L = ( L + g - 1 ) / g ; R = R / g ; int ans = 0 ; for ( int i = L ; i <= R ; i ++ ) for ( int j = L ; j <= R ; j ++ ) if ( gcd ( i , j ) == 1 ) ans ++ ; return ans ; } public static void Main ( ) { int L = 1 , R = 11 , g = 5 ; Console . WriteLine ( countGCD ( L , R , g ) ) ; } }


"Count pairs of nodes having minimum distance between them equal to the difference of their distances from root | C # program for the above approach ; Stores the count of pairs ; Store the adjacency list of the connecting vertex ; Function for theto perform DFS traversal of the given tree ; Traverse the adjacency list of the current node u ; If the current node is the parent node ; Add number of ancestors , which is same as depth of the node ; Function for DFS traversal of the given tree ; Print the result ; Function to find the count of pairs such that the minimum distance between them is equal to the difference between distance of the nodes from root node ; Add edges to [ ] adj ; Driver Code"
Solution in C#:

 using System ; using System . Collections . Generic ; public class GFG { static int ans = 0 ; static List < int > [ ] adj = new List < int > [ ( int ) ( 1e5 ) + 1 ] ; static void dfsUtil ( int u , int par , int depth ) { foreach ( int it in adj [ u ] ) { if ( it != par ) { dfsUtil ( it , u , depth + 1 ) ; } } ans += depth ; } static void dfs ( int u , int par , int depth ) { dfsUtil ( u , par , depth ) ; Console . Write ( ans + "
 " ) ; } static void countPairs ( int [ , ] edges ) { for ( int i = 0 ; i < edges . GetLength ( 0 ) ; i ++ ) { int u = edges [ i , 0 ] ; int v = edges [ i , 1 ] ; adj [ u ] . Add ( v ) ; adj [ v ] . Add ( u ) ; } dfs ( 1 , 1 , 1 ) ; } public static void Main ( String [ ] args ) { int [ , ] edges = { { 1 , 2 } , { 1 , 3 } , { 2 , 4 } } ; for ( int i = 0 ; i < adj . GetLength ( 0 ) ; i ++ ) adj [ i ] = new List < int > ( ) ; countPairs ( edges ) ; } }


"Count pairs of non | C # implementation of the approach ; Pre - processing function ; Get the size of the string ; Initially mark every position as false ; For the length ; Iterate for every index with length j ; If the length is less than 2 ; If characters are equal ; Check for equal ; Function to return the number of pairs ; Create the dp table initially ; Declare the left array ; Declare the right array ; Initially left [ 0 ] is 1 ; Count the number of palindrome pairs to the left ; Initially right most as 1 ; Count the number of palindrome pairs to the right ; Count the number of pairs ; Driver code"
Solution in C#:

using System ; class GFG { static int N = 100 ; static void pre_process ( bool [ , ] dp , char [ ] s ) { int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { dp [ i , j ] = false ; } } for ( int j = 1 ; j <= n ; j ++ ) { for ( int i = 0 ; i <= n - j ; i ++ ) { if ( j <= 2 ) { if ( s [ i ] == s [ i + j - 1 ] ) { dp [ i , i + j - 1 ] = true ; } } else if ( s [ i ] == s [ i + j - 1 ] ) { dp [ i , i + j - 1 ] = dp [ i + 1 , i + j - 2 ] ; } } } } static int countPairs ( String s ) { bool [ , ] dp = new bool [ N , N ] ; pre_process ( dp , s . ToCharArray ( ) ) ; int n = s . Length ; int [ ] left = new int [ n ] ; int [ ] right = new int [ n ] ; left [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) { if ( dp [ j , i ] == true ) { left [ i ] ++ ; } } } right [ n - 1 ] = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { right [ i ] = right [ i + 1 ] ; for ( int j = n - 1 ; j >= i ; j -- ) { if ( dp [ i , j ] == true ) { right [ i ] ++ ; } } } int ans = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { ans += left [ i ] * right [ i + 1 ] ; } return ans ; } public static void Main ( String [ ] args ) { String s = " abacaba " ; Console . Write ( countPairs ( s ) ) ; } }


"Count pairs of numbers from 1 to N with Product divisible by their Sum | C # program to count pairs of numbers from 1 to N with Product divisible by their Sum ; Function to count pairs ; variable to store count ; Generate all possible pairs such that 1 <= x < y < n ; Driver code"
Solution in C#:

using System ; class GFG { static int countPairs ( int n ) { int count = 0 ; for ( int x = 1 ; x < n ; x ++ ) { for ( int y = x + 1 ; y <= n ; y ++ ) { if ( ( y * x ) % ( y + x ) == 0 ) count ++ ; } } return count ; } public static void Main ( ) { int n = 15 ; Console . WriteLine ( countPairs ( n ) ) ; } }


"Count pairs of parentheses sequences such that parentheses are balanced | C # program to count the number of pairs of balanced parentheses ; Function to count the number of pairs ; Hashing function to count the opening and closing brackets ; Traverse for all bracket sequences ; Get the string ; Counts the opening and closing required ; Traverse in the string ; If it is a opening bracket ; Closing bracket ; If openings are there , then close it ; Else increase count of closing ; If requirements of openings are there and no closing ; If requirements of closing are there and no opening ; Perfect ; Divide by two since two perfect makes one pair ; Traverse in the open and find corresponding minimum ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int countPairs ( string [ ] bracks , int num ) { Dictionary < int , int > open = new Dictionary < int , int > ( ) ; Dictionary < int , int > close = new Dictionary < int , int > ( ) ; int cnt = 0 ; for ( int i = 0 ; i < num ; i ++ ) { string s = bracks [ i ] ; int l = s . Length ; int op = 0 , cl = 0 ; for ( int j = 0 ; j < l ; j ++ ) { if ( s [ j ] == ' ( ' ) op ++ ; else { if ( op != 0 ) op -- ; else cl ++ ; } } if ( op != 0 && cl == 0 ) { if ( open . ContainsKey ( op ) ) { open [ op ] ++ ; } else { open [ op ] = 1 ; } } if ( cl != 0 && op == 0 ) { if ( close . ContainsKey ( cl ) ) { close [ cl ] ++ ; } else { close [ cl ] = 1 ; } } if ( op == 0 && cl == 0 ) cnt ++ ; } cnt /= 2 ; foreach ( KeyValuePair < int , int > it in open ) { cnt += Math . Min ( it . Value , close [ it . Value ] ) ; } return cnt ; } public static void Main ( string [ ] args ) { string [ ] bracks = { " ) ( ) ) " , " ) " , " ( ( " , " ( ( " , " ( " , " ) " , " ) " } ; int num = bracks . Length ; Console . Write ( countPairs ( bracks , num ) ) ; } }


"Count pairs of points having distance between them equal to integral values in a K | C # program for the above approach ; Function to find pairs whose distance between the points of is an integer value . ; Stores count of pairs whose distance between points is an integer ; Traverse the array , points [ ] ; Stores distance between points ( i , j ) ; Traverse all the points of current pair ; Update temp ; Update dist ; If dist is a perfect square ; Update ans ; Driver Code ; Given value of K ; Given points ; Given value of N ; Function Call"
Solution in C#:

using System ; class GFG { static void cntPairs ( int [ , ] points , int n , int K ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int dist = 0 ; for ( int k = 0 ; k < K ; k ++ ) { int temp = ( points [ i , k ] - points [ j , k ] ) ; dist += temp * temp ; } if ( Math . Sqrt ( dist ) * Math . Sqrt ( dist ) == dist ) { ans += 1 ; } } } Console . WriteLine ( ans ) ; } public static void Main ( ) { int K = 2 ; int [ , ] points = { { 1 , 2 } , { 5 , 5 } , { - 2 , 8 } } ; int n = points . GetLength ( 0 ) ; cntPairs ( points , n , K ) ; } }


"Count pairs of similar rectangles possible from a given array |  ; Get the count of all pairs of similar rectangles ; Initialize the result value and map to store the ratio to the rectangles ; Calculate the rectangular ratio and save them ; Calculate pairs of similar rectangles from its common ratio ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static int getCount ( int rows , int columns , int [ , ] sides ) { int res = 0 ; Dictionary < double , int > ratio = new Dictionary < double , int > ( ) ; for ( int i = 0 ; i < rows ; i ++ ) { double rectRatio = ( double ) sides [ i , 0 ] / sides [ i , 1 ] ; if ( ! ratio . ContainsKey ( rectRatio ) ) { ratio [ rectRatio ] = 0 ; } ratio [ rectRatio ] = ratio [ rectRatio ] + 1 ; } foreach ( KeyValuePair < double , int > p in ratio ) { int val = p . Value ; if ( val > 1 ) { res += ( val * ( val - 1 ) ) / 2 ; } } return res ; } public static void Main ( string [ ] args ) { int [ , ] A = { { 4 , 8 } , { 10 , 20 } , { 15 , 30 } , { 3 , 6 } } ; int columns = 2 ; int rows = 4 ; Console . WriteLine ( getCount ( rows , columns , A ) ) ; } }


"Count pairs of similar rectangles possible from a given array | C # program for the above approach ; Function to calculate the count of similar rectangles ; Driver code ; Input"
Solution in C#:

using System ; class GFG { static int getCount ( int rows , int columns , int [ , ] A ) { int res = 0 ; for ( int i = 0 ; i < rows ; i ++ ) { for ( int j = i + 1 ; j < rows ; j ++ ) { if ( A [ i , 0 ] * A [ j , 1 ] == A [ i , 1 ] * A [ j , 0 ] ) { res ++ ; } } } return res ; } static void Main ( ) { int [ , ] A = { { 4 , 8 } , { 10 , 20 } , { 15 , 30 } , { 3 , 6 } } ; int columns = 2 ; int rows = 4 ; Console . Write ( getCount ( rows , columns , A ) ) ; } }


"Count pairs up to N having sum equal to their XOR | C # program for the above approach ; 2D array for memoization ; Recursive Function to count pairs ( x , y ) such that x + y = x ^ y ; If the String is traversed completely ; If the current subproblem is already calculated ; If bound = 1 and s [ i ] = = '0' , only ( 0 , 0 ) can be placed ; Otherwise ; Placing ( 0 , 1 ) and ( 1 , 0 ) are equivalent . Hence , multiply by 2. ; Place ( 0 , 0 ) at the current position . ; Return the answer ;  ; Utility Function to convert N to its binary representation ; Function to count pairs ( x , y ) such that x + y = x ^ y ; Convert the number to equivalent binary representation ; Initialize dp array with - 1. ; Print answer returned by recursive function ; Driver code ; Input ; Function call"
Solution in C#:

 using System ; class GFG { static int [ , ] dp = new int [ 1000 , 2 ] ; static int IsSumEqualsXor ( int i , int n , int bound , char [ ] s ) { if ( i == n ) return 1 ; if ( dp [ i , bound ] != - 1 ) return dp [ i , bound ] ; int ans = 0 ; if ( bound != 0 && s [ i ] == '0' ) { ans = IsSumEqualsXor ( i + 1 , n , 1 , s ) ; } else { ans = 2 * IsSumEqualsXor ( i + 1 , n , bound != 0 & ( s [ i ] == '1' ) ? 1 : 0 , s ) ; ans += IsSumEqualsXor ( i + 1 , n , 0 , s ) ; } return dp [ i , bound ] = ans ; } static String reverse ( String input ) { char [ ] a = input . ToCharArray ( ) ; int l , r = a . Length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . Join ( " " , a ) ; } static String convertToBinary ( int n ) { String ans = " " ; while ( n > 0 ) { char rem = ( char ) ( n % 2 + '0' ) ; ans += ( rem ) ; n /= 2 ; } ans = reverse ( ans ) ; return ans ; } static void IsSumEqualsXorUtil ( int N ) { String s = convertToBinary ( N ) ; for ( int i = 0 ; i < dp . GetLength ( 0 ) ; i ++ ) { for ( int j = 0 ; j < dp . GetLength ( 1 ) ; j ++ ) { dp [ i , j ] = - 1 ; } } Console . Write ( IsSumEqualsXor ( 0 , s . Length , 1 , s . ToCharArray ( ) ) + "
 " ) ; } public static void Main ( String [ ] args ) { int N = 10 ; IsSumEqualsXorUtil ( N ) ; } }


"Count pairs whose Bitwise AND exceeds Bitwise XOR from a given array | C # program to implement the above approach ; Function to count pairs that satisfy the above condition ; Stores the count of pairs ; Stores the count of array elements having same positions of MSB ; Traverse the array ; Stores the index of MSB of array elements ; Calculate number of pairs ; Driver Code ; Given Input ; Function call to count pairs satisfying the given condition"
Solution in C#:

using System ; class GFG { static int cntPairs ( int [ ] arr , int N ) { int res = 0 ; int [ ] bit = new int [ 32 ] ; for ( int i = 0 ; i < N ; i ++ ) { int pos = ( int ) ( Math . Log ( arr [ i ] ) / Math . Log ( 2 ) ) ; bit [ pos ] ++ ; } for ( int i = 0 ; i < 32 ; i ++ ) { res += ( bit [ i ] * ( bit [ i ] - 1 ) ) / 2 ; } return res ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int N = arr . Length ; Console . Write ( cntPairs ( arr , N ) ) ; } }


"Count pairs whose product contains single distinct prime factor | C # program to implement the above approach ; Function to find a single distinct prime factor of N ; Stores distinct prime factors of N ; Calculate prime factor of N ; Calculate distinct prime factor ; Insert i into disPrimeFact ; Update N ; If N is not equal to 1 ; Insert N into disPrimeFact ; If N contains a single distinct prime factor ; Return single distinct prime factor of N ; If N contains more than one distinct prime factor ; Function to count pairs in the array whose product contains only single distinct prime factor ; Stores count of 1 s in the array ; mp [ i ] : Stores count of array elements whose distinct prime factor is only i ; Traverse the array arr [ ] ; If current element is 1 ; Store distinct prime factor of arr [ i ] ; If arr [ i ] contains more than one prime factor ; If arr [ i ] contains a single prime factor ; Stores the count of pairs whose product of elements contains only a single distinct prime factor ; Traverse the map mp [ ] ; Stores count of array elements whose prime factor is ( it . first ) ; Update res ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int singlePrimeFactor ( int N ) { HashSet < int > disPrimeFact = new HashSet < int > ( ) ; for ( int i = 2 ; i * i <= N ; ++ i ) { while ( N % i == 0 ) { disPrimeFact . Add ( i ) ; N /= i ; } } if ( N != 1 ) { disPrimeFact . Add ( N ) ; } if ( disPrimeFact . Count == 1 ) { foreach ( int i in disPrimeFact ) return i ; } return - 1 ; } static int cntsingleFactorPair ( int [ ] arr , int N ) { int countOf1 = 0 ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 1 ) { countOf1 ++ ; continue ; } int factorValue = singlePrimeFactor ( arr [ i ] ) ; if ( factorValue == - 1 ) { continue ; } else { if ( mp . ContainsKey ( factorValue ) ) mp [ factorValue ] = mp [ factorValue ] + 1 ; else mp . Add ( factorValue , 1 ) ; } } int res = 0 ; foreach ( KeyValuePair < int , int > ele1 in mp ) { int X = ele1 . Value ; res += countOf1 * X + ( X * ( X - 1 ) ) / 2 ; } return res ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int N = arr . Length ; Console . WriteLine ( cntsingleFactorPair ( arr , N ) ) ; } }


"Count pairs whose product modulo 10 ^ 9 + 7 is equal to 1 | C # program to implement the above approach ; Iterative Function to calculate ( x ^ y ) % MOD ; Initialize result ; Update x if it exceeds MOD ; If x is divisible by MOD ; If y is odd ; Multiply x with res ; y must be even now ; Function to count number of pairs whose product modulo 1000000007 is 1 ; Stores the count of desired pairs ; Stores the frequencies of each array element ; Traverse the array and update frequencies in hash ; Calculate modular inverse of arr [ i ] under modulo 1000000007 ; Update desired count of pairs ; If arr [ i ] and its modular inverse is equal under modulo MOD ; Updating count of desired pairs ; Return the final count ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int MOD = 1000000007 ; static long modPower ( long x , int y ) { long res = 1 ; x = x % MOD ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % MOD ; y = y / 2 ; x = ( x * x ) % MOD ; } return res ; } static int countPairs ( long [ ] arr , int N ) { int pairCount = 0 ; Dictionary < long , int > hash = new Dictionary < long , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( hash . ContainsKey ( arr [ i ] ) ) { hash . Add ( arr [ i ] , hash [ arr [ i ] ] + 1 ) ; } else { hash . Add ( arr [ i ] , 1 ) ; } } for ( int i = 0 ; i < N ; i ++ ) { long modularInverse = modPower ( arr [ i ] , MOD - 2 ) ; if ( hash . ContainsKey ( modularInverse ) ) pairCount += hash [ modularInverse ] ; if ( arr [ i ] == modularInverse ) { pairCount -- ; } } return pairCount / 2 ; } public static void Main ( ) { long [ ] arr = { 2 , 236426 , 280311812 , 500000004 } ; int N = arr . Length ; Console . WriteLine ( countPairs ( arr , N ) ) ; } }


"Count pairs whose products exist in array | A hashing based C # program to count pairs whose product exists in arr [ ] ; Returns count of pairs whose product exists in arr [ ] ; Create an empty hash - set that store all array element ; Insert all array element into set ; Generate all pairs and check is exist in ' Hash ' or not ; if product exists in set then we increment count by 1 ; return count of pairs whose product exist in array ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int countPairs ( int [ ] arr , int n ) { int result = 0 ; HashSet < int > Hash = new HashSet < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { Hash . Add ( arr [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int product = arr [ i ] * arr [ j ] ; if ( Hash . Contains ( product ) ) { result ++ ; } } } return result ; } public static void Main ( String [ ] args ) { int [ ] arr = { 6 , 2 , 4 , 12 , 5 , 3 } ; int n = arr . Length ; Console . WriteLine ( countPairs ( arr , n ) ) ; } }


"Count pairs whose products exist in array | C # program to count pairs whose product exist in array ; Returns count of pairs whose product exists in arr [ ] ; find product in an array ; if product found increment counter ; return Count of all pair whose product exist in array ; Driver Code"
Solution in C#:

using System ; class GFG { public static int countPairs ( int [ ] arr , int n ) { int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int product = arr [ i ] * arr [ j ] ; for ( int k = 0 ; k < n ; k ++ ) { if ( arr [ k ] == product ) { result ++ ; break ; } } } } return result ; } public static void Main ( string [ ] args ) { int [ ] arr = new int [ ] { 6 , 2 , 4 , 12 , 5 , 3 } ; int n = arr . Length ; Console . WriteLine ( countPairs ( arr , n ) ) ; } }


"Count pairs with Bitwise AND as ODD number | C # program to count pairs with AND giving a odd number ; Function to count number of odd pairs ; variable for counting odd pairs ; find all pairs ; find AND operation check odd or even ; return number of odd pair ; Driver Code ; calling function findOddPair and print number of odd pair"
Solution in C#:

using System ; class GFG { static int findOddPair ( int [ ] A , int N ) { int i , j ; int oddPair = 0 ; for ( i = 0 ; i < N ; i ++ ) { for ( j = i + 1 ; j < N ; j ++ ) { if ( ( A [ i ] & A [ j ] ) % 2 != 0 ) oddPair ++ ; } } return oddPair ; } public static void Main ( ) { int [ ] a = { 5 , 1 , 3 , 2 } ; int n = a . Length ; Console . WriteLine ( findOddPair ( a , n ) ) ; } }


"Count pairs with Bitwise AND as ODD number | C # program to count pairs with Odd AND ; Count total odd numbers in ; return count of even pair ; Driver Code ; calling function findOddPair and print number of odd pair"
Solution in C#:

using System ; class GFG { public static int findOddPair ( int [ ] A , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( A [ i ] % 2 == 1 ) ) { count ++ ; } } return count * ( count - 1 ) / 2 ; } public static void Main ( string [ ] args ) { int [ ] a = new int [ ] { 5 , 1 , 3 , 2 } ; int n = a . Length ; Console . WriteLine ( findOddPair ( a , n ) ) ; } }


"Count pairs with Bitwise OR as Even number | C # program to count pairs with even OR ; Count total even numbers in array . ; return count of even pair ; Driver Code"
Solution in C#:

using System ; class GFG { static int findEvenPair ( int [ ] A , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) if ( ( ! ( ( A [ i ] & 1 ) > 0 ) ) ) count ++ ; return count * ( count - 1 ) / 2 ; } public static void Main ( String [ ] args ) { int [ ] A = { 5 , 6 , 2 , 8 } ; int N = A . Length ; Console . WriteLine ( findEvenPair ( A , N ) ) ; } }


"Count pairs with Bitwise XOR as EVEN number | C # program to count pairs with XOR giving a even number ; Function to count number of even pairs ; find all pairs ; return number of even pair ; Driver Code ; calling function findEvenPair and print number of even pair"
Solution in C#:

using System ; public class GFG { static int findEvenPair ( int [ ] A , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] % 2 != 0 ) count ++ ; } int totalPairs = ( N * ( N - 1 ) / 2 ) ; int oddEvenPairs = count * ( N - count ) ; return totalPairs - oddEvenPairs ; } public static void Main ( ) { int [ ] a = { 5 , 4 , 7 , 2 , 1 } ; int n = a . Length ; Console . Write ( findEvenPair ( a , n ) ) ; } }


"Count pairs with Bitwise XOR as EVEN number | C # program to count pairs with XOR giving a even number ; Function to count number of even pairs ; variable for counting even pairs ; find all pairs ; find XOR operation check even or even ; return number of even pair ; Driver Code ; calling function findevenPair and print number of even pair"
Solution in C#:

using System ; class GFG { static int findevenPair ( int [ ] A , int N ) { int i , j ; int evenPair = 0 ; for ( i = 0 ; i < N ; i ++ ) { for ( j = i + 1 ; j < N ; j ++ ) { if ( ( A [ i ] ^ A [ j ] ) % 2 == 0 ) evenPair ++ ; } } return evenPair ; } public static void Main ( ) { int [ ] A = { 5 , 4 , 7 , 2 , 1 } ; int N = A . Length ; Console . WriteLine ( findevenPair ( A , N ) ) ; } }


"Count pairs with Bitwise XOR as ODD number | C # program to count pairs with XOR giving a odd number ; Function to count number of odd pairs ; find all pairs ; return number of odd pair ; Driver Code ; calling function findOddPair and print number of odd pair"
Solution in C#:

using System ; class GFG { static int findOddPair ( int [ ] A , int N ) { int i , count = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( A [ i ] % 2 == 0 ) count ++ ; } return count * ( N - count ) ; } public static void Main ( ) { int [ ] a = { 5 , 4 , 7 , 2 , 1 } ; int n = a . Length ; Console . Write ( findOddPair ( a , n ) ) ; } }


"Count pairs with Bitwise XOR greater than both the elements of the pair | C # program for the above approach ; Function that counts the pairs whose Bitwise XOR is greater than both the elements of pair ; Stores the count of pairs ; Generate all possible pairs ; Find the Bitwise XOR ; Find the maximum of two ; If xo < mx , increment count ; Print the value of count ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static void countPairs ( int [ ] A , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { int xo = ( A [ i ] ^ A [ j ] ) ; int mx = Math . Max ( A [ i ] , A [ j ] ) ; if ( xo > mx ) { count ++ ; } } } Console . WriteLine ( count ) ; } public static void Main ( ) { int [ ] arr = { 2 , 4 , 3 } ; int N = arr . Length ; countPairs ( arr , N ) ; } }


"Count pairs with Bitwise XOR greater than both the elements of the pair | C # program for the above approach ; Function to count pairs whose XOR is greater than the pair itself ; Stores the count of pairs ; Sort the array ; Traverse the array ; If current element is 0 , then ignore it ; Traverse all the bits of element A [ i ] ; If current bit is set then update the count ; Update bits [ ] at the most significant bit of A [ i ] ; Print the count ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static void countPairs ( int [ ] A , int N ) { int count = 0 ; Array . Sort ( A ) ; int [ ] bits = new int [ 32 ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] == 0 ) { continue ; } for ( int j = 0 ; j < 32 ; j ++ ) { if ( ( ( 1 << j ) & A [ i ] ) == 0 ) { count += bits [ j ] ; } } ++ bits [ ( int ) ( ( int ) ( Math . Log ( A [ i ] ) / Math . Log ( 2 ) ) ) ] ; } Console . WriteLine ( count ) ; } public static void Main ( ) { int [ ] arr = { 2 , 4 , 3 } ; int N = arr . Length ; countPairs ( arr , N ) ; } }


"Count pairs with Bitwise | C # program to count pair with bitwise - AND as even number ; Function to count number of pairs EVEN bitwise AND ; variable for counting even pairs ; find all pairs ; find AND operation to check evenpair ; return number of even pair ; Driver Code"
Solution in C#:

using System ; class GFG { static int findevenPair ( int [ ] A , int N ) { int i , j ; int evenPair = 0 ; for ( i = 0 ; i < N ; i ++ ) { for ( j = i + 1 ; j < N ; j ++ ) { if ( ( A [ i ] & A [ j ] ) % 2 == 0 ) evenPair ++ ; } } return evenPair ; } public static void Main ( ) { int [ ] a = { 5 , 1 , 3 , 2 } ; int n = a . Length ; Console . WriteLine ( findevenPair ( a , n ) ) ; } }


"Count pairs with Bitwise | C # program to count pair with bitwise - AND as even number ; Function to count number of pairs with EVEN bitwise AND ; count odd numbers ; count odd pairs ; return number of even pair ; Driver Code"
Solution in C#:

using System ; public class GFG { static int findevenPair ( int [ ] A , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) if ( A [ i ] % 2 != 0 ) count ++ ; int oddCount = count * ( count - 1 ) / 2 ; return ( N * ( N - 1 ) / 2 ) - oddCount ; } static public void Main ( ) { int [ ] a = { 5 , 1 , 3 , 2 } ; int n = a . Length ; Console . WriteLine ( findevenPair ( a , n ) ) ; } }


"Count pairs with Even Product from two given arrays | C # program to implement the above approach ; Function to count pairs ( arr [ i ] , brr [ j ] ) whose product is an even number ; Stores count of odd numbers in arr [ ] ; Stores count of odd numbers in brr [ ] ; Traverse the array , arr [ ] ; If arr [ i ] is an odd number ; Update cntOddArr ; Traverse the array , brr [ ] ; If brr [ i ] is an odd number ; Update cntOddArr ; Return pairs whose product is an even number ; Driver Code"
Solution in C#:

using System ; class GFG { static int cntPairsInTwoArray ( int [ ] arr , int [ ] brr , int N , int M ) { int cntOddArr = 0 ; int cntOddBrr = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 1 ) { cntOddArr += 1 ; } } for ( int i = 0 ; i < M ; i ++ ) { if ( brr [ i ] % 2 == 1 ) { cntOddBrr += 1 ; } } return ( N * M ) - ( cntOddArr * cntOddBrr ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 } ; int N = arr . Length ; int [ ] brr = { 1 , 2 } ; int M = brr . Length ; Console . Write ( cntPairsInTwoArray ( arr , brr , N , M ) ) ; } }


"Count pairs with Odd XOR | C # program to count pairs in array whose XOR is odd ; A function will return number of pair whose XOR is odd ; To store count of XOR pair ; If XOR is odd increase count ; Return count ; Driver program to test countXorPair ( )"
Solution in C#:

using System ; public class CountXor { static int countXorPair ( int [ ] arr , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) if ( ( arr [ i ] ^ arr [ j ] ) % 2 == 1 ) count ++ ; } return count ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 } ; Console . WriteLine ( countXorPair ( arr , arr . Length ) ) ; } }


"Count pairs with Odd XOR | C # program to count pairs in array whose XOR is odd ; A function will return number of pair whose XOR is odd ; To store count of odd and even numbers ; Increase even if number is even otherwise increase odd ; Return number of pairs ; Driver program to test countXorPair ( )"
Solution in C#:

using System ; public class CountXor { static int countXorPair ( int [ ] arr , int n ) { int odd = 0 , even = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) even ++ ; else odd ++ ; } return odd * even ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 } ; Console . WriteLine ( countXorPair ( arr , arr . Length ) ) ; } }


"Count pairs with average present in the same array | C # implementation of the approach ; Function to return the count of valid pairs ; Frequency array Twice the original size to hold negative elements as well ; Update the frequency of each of the array element ; If say x = - 1000 then we will place the frequency of - 1000 at ( - 1000 + 1000 = 0 ) a [ 0 ] index ; To store the count of valid pairs ; Remember we will check only for ( even , even ) or ( odd , odd ) pairs of indexes as the average of two consecutive elements is a floating point number ; Driver code"
Solution in C#:

using System ; class GFG { static int N = 1000 ; static int countPairs ( int [ ] arr , int n ) { int size = ( 2 * N ) + 1 ; int [ ] freq = new int [ size ] ; for ( int i = 0 ; i < n ; i ++ ) { int x = arr [ i ] ; freq [ x + N ] ++ ; } int ans = 0 ; for ( int i = 0 ; i < size ; i ++ ) { if ( freq [ i ] > 0 ) { ans += ( ( freq [ i ] ) * ( freq [ i ] - 1 ) ) / 2 ; for ( int j = i + 2 ; j < 2001 ; j += 2 ) { if ( freq [ j ] > 0 && ( freq [ ( i + j ) / 2 ] > 0 ) ) { ans += ( freq [ i ] * freq [ j ] ) ; } } } } return ans ; } public static void Main ( ) { int [ ] arr = { 4 , 2 , 5 , 1 , 3 , 5 } ; int n = arr . Length ; Console . WriteLine ( countPairs ( arr , n ) ) ; } }


"Count pairs with equal Bitwise AND and Bitwise OR value | C # program to implement the above approach ; Function to count pairs in an array whose bitwise AND equal to bitwise OR ; Store count of pairs whose bitwise AND equal to bitwise OR ; Stores frequency of distinct elements of array ; Traverse the array ; Increment the frequency of arr [ i ] ; Traverse map ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int countPairs ( int [ ] arr , int N ) { int cntPairs = 0 ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! mp . ContainsKey ( arr [ i ] ) ) mp . Add ( arr [ i ] , 1 ) ; else mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1 ; } foreach ( KeyValuePair < int , int > freq in mp ) { cntPairs += ( freq . Value * ( freq . Value - 1 ) ) / 2 ; } return cntPairs ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 1 , 2 , 2 } ; int N = arr . Length ; Console . WriteLine ( countPairs ( arr , N ) ) ; } }


"Count pairs with given sum | C # implementation of simple method to find count of pairs with given sum . ; Prints number of pairs in arr [ 0. . n - 1 ] with sum equal to ' sum ' ; Initialize result ; Consider all possible pairs and check their sums ; Driver Code"
Solution in C#:

using System ; class GFG { public static void getPairsCount ( int [ ] arr , int sum ) { int count = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) for ( int j = i + 1 ; j < arr . Length ; j ++ ) if ( ( arr [ i ] + arr [ j ] ) == sum ) count ++ ; Console . WriteLine ( " Count ▁ of ▁ pairs ▁ is ▁ " + count ) ; } static public void Main ( ) { int [ ] arr = { 1 , 5 , 7 , - 1 , 5 } ; int sum = 6 ; getPairsCount ( arr , sum ) ; } }


"Count pairs with given sum | C # implementation of simple method to find count of pairs with given sum ; Returns number of pairs in arr [ 0. . n - 1 ] with sum equal to ' sum ' ; Store counts of all elements in map hm ; initializing value to 0 , if key not found ; iterate through each element and increment the count ( Notice that every pair is counted twice ) ; if ( arr [ i ] , arr [ i ] ) pair satisfies the condition , then we need to ensure that the count is decreased by one such that the ( arr [ i ] , arr [ i ] ) pair is not considered ; return the half of twice_count ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static int [ ] arr = new int [ ] { 1 , 5 , 7 , - 1 , 5 } ; public static int getPairsCount ( int n , int sum ) { Dictionary < int , int > hm = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! hm . ContainsKey ( arr [ i ] ) ) { hm [ arr [ i ] ] = 0 ; } hm [ arr [ i ] ] = hm [ arr [ i ] ] + 1 ; } int twice_count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( hm [ sum - arr [ i ] ] != 0 ) { twice_count += hm [ sum - arr [ i ] ] ; } if ( sum - arr [ i ] == arr [ i ] ) { twice_count -- ; } } return twice_count / 2 ; } public static void Main ( string [ ] args ) { int sum = 6 ; Console . WriteLine ( " Count ▁ of ▁ pairs ▁ is ▁ " + getPairsCount ( arr . Length , sum ) ) ; } }


"Count pairs with odd Bitwise XOR that can be removed and replaced by their Bitwise OR | C # program for the above approach ; Function to count the number of pairs required to be removed from the array and replaced by their Bitwise OR values ; Stores the count of even array elements ; Traverse the given array ; Increment the count of even array elements ; If the array contains at least one odd array element ; Otherwise , print 0 ; Driver code"
Solution in C#:

using System ; class GFG { static void countPairs ( int [ ] arr , int N ) { int even = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) even ++ ; } if ( N - even >= 1 ) { Console . WriteLine ( even ) ; return ; } Console . WriteLine ( 0 ) ; } static void Main ( ) { int [ ] arr = { 5 , 4 , 7 , 2 } ; int N = arr . Length ; countPairs ( arr , N ) ; } }


"Count pairs with product of indices equal to the product of elements present at those indices | C # program for the above approach ; Function to count the number of pairs having product of indices equal to the product of elements at that indices ; Stores the count of valid pairs ; Generate all possible pairs ; If the condition is satisfied ; Increment the count ; Return the total count ; Driver Code"
Solution in C#:

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


"Count pairs with set bits sum equal to K | C # implementation of the approach ; Function to return the count of set bits in n ; Function to return the count of required pairs ; To store the count ; Frequency array ; If current pair satisfies the given condition ; ( arr [ i ] , arr [ i ] ) cannot be a valid pair ; Driver code"
Solution in C#:

using System ; class GFG { static int MAX = 32 ; static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } static int pairs ( int [ ] arr , int n , int k ) { int count = 0 ; int [ ] f = new int [ MAX + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) f [ countSetBits ( arr [ i ] ) ] ++ ; for ( int i = 0 ; i <= MAX ; i ++ ) { for ( int j = i ; j <= MAX ; j ++ ) { if ( i + j == k ) { if ( i == j ) count += ( ( f [ i ] * ( f [ i ] - 1 ) ) / 2 ) ; else count += ( f [ i ] * f [ j ] ) ; } } } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . Length ; int k = 4 ; Console . WriteLine ( pairs ( arr , n , k ) ) ; } }


"Count pairs with set bits sum equal to K | C # implementation of the approach ; Function to return the count of set bits in n ; Function to return the count of required pairs ; To store the count ; Sum of set bits in both the integers ; If current pair satisfies the given condition ; Driver code"
Solution in C#:

using System ; public class GFG { static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } static int pairs ( int [ ] arr , int n , int k ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int sum = countSetBits ( arr [ i ] ) + countSetBits ( arr [ j ] ) ; if ( sum == k ) count ++ ; } } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . Length ; int k = 4 ; Console . WriteLine ( pairs ( arr , n , k ) ) ; } }


"Count palindrome words in a sentence | C # program to count number of palindrome words in a sentence ; Function to check if a word is palindrome ; Function to count palindrome words ; to check last word for palindrome ; to store each word ; extracting each word ; Driver code"
Solution in C#:

using System ; class GFG { public static bool checkPalin ( string word ) { int n = word . Length ; word = word . ToLower ( ) ; for ( int i = 0 ; i < n ; i ++ , n -- ) { if ( word [ i ] != word [ n - 1 ] ) { return false ; } } return true ; } public static int countPalin ( string str ) { str = str + " ▁ " ; string word = " " ; int count = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { char ch = str [ i ] ; if ( ch != ' ▁ ' ) { word = word + ch ; } else { if ( checkPalin ( word ) ) { count ++ ; } word = " " ; } } return count ; } public static void Main ( string [ ] args ) { Console . WriteLine ( countPalin ( " Madam ▁ " + " Arora ▁ teaches ▁ malayalam " ) ) ; Console . WriteLine ( countPalin ( " Nitin ▁ " + " speaks ▁ malayalam " ) ) ; } }


"Count palindromic characteristics of a String | C # program which counts different palindromic characteristics of a string . ; function which checks whether a substring str [ i . . j ] of a given string is a palindrome or not . ; P [ i , j ] = true if substring str [ i . . j ] is palindrome , else false ; palindrome of single length ; palindrome of length 2 ; Palindromes of length more then 2. This loop is similar to Matrix Chain Multiplication . We start with a gap of length 2 and fill P table in a way that gap between starting and ending indexes increases one by one by outer loop . ; Pick starting point for current gap ; Set ending point ; If current string is palindrome ; function which recursively counts if a string str [ i . . j ] is a k - palindromic string or not . ; terminating condition for a string which is a k - palindrome . ; terminating condition for a string which is not a k - palindrome . ; increases the counter for the string if it is a k - palindrome . ; mid is middle pointer of the string str [ i ... j ] . ; if length of string which is ( j - i + 1 ) is odd than we have to subtract one from mid . else if even then no change . ; if the string is k - palindrome then we check if it is a ( k + 1 ) - palindrome or not by just sending any of one half of the string to the Count_k_Palindrome function . ; Finding all palindromic substrings of given string ; counting k - palindromes for each and every substring of given string . . ; Output the number of K - palindromic substrings of a given string . ; Driver code"
Solution in C#:

using System ; class GFG { static int MAX_STR_LEN = 1000 ; static bool [ , ] P = new bool [ MAX_STR_LEN , MAX_STR_LEN ] ; static int [ ] Kpal = new int [ MAX_STR_LEN ] ; static void checkSubStrPal ( string str , int n ) { for ( int i = 0 ; i < MAX_STR_LEN ; i ++ ) { for ( int j = 0 ; j < MAX_STR_LEN ; j ++ ) P [ i , j ] = false ; Kpal [ i ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) P [ i , i ] = true ; for ( int i = 0 ; i < n - 1 ; i ++ ) if ( str [ i ] == str [ i + 1 ] ) P [ i , i + 1 ] = true ; for ( int gap = 2 ; gap < n ; gap ++ ) { for ( int i = 0 ; i < n - gap ; i ++ ) { int j = gap + i ; if ( str [ i ] == str [ j ] && P [ i + 1 , j - 1 ] ) P [ i , j ] = true ; } } } static void countKPalindromes ( int i , int j , int k ) { if ( i == j ) { Kpal [ k ] ++ ; return ; } if ( P [ i , j ] == false ) return ; Kpal [ k ] ++ ; int mid = ( i + j ) / 2 ; if ( ( j - i + 1 ) % 2 == 1 ) mid -- ; countKPalindromes ( i , mid , k + 1 ) ; } static void printKPalindromes ( string s ) { int n = s . Length ; checkSubStrPal ( s , n ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n - i ; j ++ ) countKPalindromes ( j , j + i , 1 ) ; for ( int i = 1 ; i <= n ; i ++ ) Console . Write ( Kpal [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } static void Main ( ) { string s = " abacaba " ; printKPalindromes ( s ) ; } }


"Count paths with distance equal to Manhattan distance | C # implementation of the approach ; Function to return the value of nCk ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; Function to return the number of paths ; Difference between the ' x ' coordinates of the given points ; Difference between the ' y ' coordinates of the given points ; 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 countPaths ( int x1 , int y1 , int x2 , int y2 ) { int m = Math . Abs ( x1 - x2 ) ; int n = Math . Abs ( y1 - y2 ) ; return ( binomialCoeff ( m + n , n ) ) ; } public static void Main ( ) { int x1 = 2 , y1 = 3 , x2 = 4 , y2 = 5 ; Console . Write ( countPaths ( x1 , y1 , x2 , y2 ) ) ; } }


"Count perfect power of K in a range [ L , R ] | C # implementation to find the count of numbers those are powers of K in range L to R ; Function to find the Nth root of the number ; initially guessing a random number between 0 to 9 ; Smaller eps , denotes more accuracy ; Initializing difference between two roots by int . MaxValue ; xK denotes current value of x ; loop until we reach desired accuracy ; calculating current value from previous value ; Function to count the perfect powers of K in range L to R ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { static double nthRoot ( int A , int N ) { Random r = new Random ( ) ; double xPre = r . Next ( 0 , 9 ) ; double eps = 1e-3 ; double delX = int . MaxValue ; double xK = 0 ; while ( delX > eps ) { xK = ( ( N - 1.0 ) * xPre + ( double ) A / Math . Pow ( xPre , N - 1 ) ) / ( double ) N ; delX = Math . Abs ( xK - xPre ) ; xPre = xK ; } return xK ; } static int countPowers ( int a , int b , int k ) { return ( int ) ( Math . Floor ( nthRoot ( b , k ) ) - Math . Ceiling ( nthRoot ( a , k ) ) + 1 ) ; } public static void Main ( String [ ] args ) { int a = 7 , b = 28 , k = 2 ; Console . Write ( " Count ▁ of ▁ Powers ▁ is ▁ " + countPowers ( a , b , k ) ) ; } }


"Count permutation such that sequence is non decreasing | C # implementation of the approach ; To store the factorials ; Function to update fact [ ] array such that fact [ i ] = i ! ; 0 ! = 1 ; i ! = i * ( i - 1 ) ! ; Function to return the count of possible permutations ; To store the result ; Sort the array ; Initial size of the block ; Increase the size of block ; Update the result for the previous block ; Reset the size to 1 ; Update the result for the last block ; Driver Code ; Pre - calculating factorials"
Solution in C#:

using System ; class GFG { static int N = 20 ; static int [ ] fact = new int [ N ] ; static void pre ( ) { fact [ 0 ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) { fact [ i ] = i * fact [ i - 1 ] ; } } static int CountPermutation ( int [ ] a , int n ) { int ways = 1 ; Array . Sort ( a ) ; int size = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] == a [ i - 1 ] ) { size ++ ; } else { ways *= fact [ size ] ; size = 1 ; } } ways *= fact [ size ] ; return ways ; } static public void Main ( ) { int [ ] a = { 1 , 2 , 4 , 4 , 2 , 4 } ; int n = a . Length ; pre ( ) ; Console . Write ( CountPermutation ( a , n ) ) ; } }


"Count permutations of all integers upto N that can form an acyclic graph based on given conditions | C # implementation of the above approach ; Find the count of possible graphs ; Driver Code"
Solution in C#:

using System ; class GFG { static void possibleAcyclicGraph ( int N ) { Console . Write ( ( int ) Math . Pow ( 2 , N - 1 ) ) ; return ; } public static void Main ( ) { int N = 4 ; possibleAcyclicGraph ( N ) ; } }


"Count permutations of first N natural numbers having sum of adjacent elements equal to a perfect square | C # program for the above approach ; Function to count total number of permutation of the first N natural number having the sum of adjacent elements as perfect square ; Create an adjacency matrix ; Count elements whose indegree is 1 ; Generate adjacency matrix ; Find the sum of i and j ; If sum is perfect square . then move from i to j ; Add it in adjacency list of i ; If any list is of size 1 , then the indegree is 1 ; If there is no element whose indegree is 1 , then N such permutations are possible ; If there is 1 or 2 elements whose indegree is 1 , then 2 permutations are possible ; If there are more than 2 elements whose indegree is 1 , then return 0 ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int countPermutations ( int N ) { List < List < int > > adj = new List < List < int > > ( 105 ) ; for ( int i = 0 ; i < 105 ; i ++ ) adj . Add ( new List < int > ( ) ) ; int indeg = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= N ; j ++ ) { if ( i == j ) continue ; int sum = i + j ; if ( Math . Ceiling ( Math . Sqrt ( sum ) ) == Math . Floor ( Math . Sqrt ( sum ) ) ) { adj [ i ] . Add ( j ) ; } } if ( adj [ i ] . Count == 1 ) indeg ++ ; } if ( indeg == 0 ) return N ; else if ( indeg <= 2 ) return 2 ; else return 0 ; } public static void Main ( ) { int N = 17 ; Console . WriteLine ( countPermutations ( N ) ) ; } }


"Count permutations of given array that generates the same Binary Search Tree ( BST ) | C # program to implement the above approach ; Function to precompute the factorial of 1 to N ; Function to get the value of nCr ; nCr = fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; Function to count the number of ways to rearrange the array to obtain same BST ; Store the size of the array ; Base case ; Store the elements of the left subtree of BST ; Store the elements of the right subtree of BST ; Store the root node ; Push all the elements of the left subtree ; Push all the elements of the right subtree ; Store the size of leftSubTree ; Store the size of rightSubTree ; Recurrence relation ; Driver Code ; Store the size of arr ; Store the factorial up to N ; Precompute the factorial up to N"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void calculateFact ( int [ ] fact , int N ) { fact [ 0 ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) { fact [ i ] = fact [ i - 1 ] * i ; } } static int nCr ( int [ ] fact , int N , int R ) { if ( R > N ) return 0 ; int res = fact [ N ] / fact [ R ] ; res /= fact [ N - R ] ; return res ; } static int countWays ( List < int > arr , int [ ] fact ) { int N = arr . Count ; if ( N <= 2 ) { return 1 ; } List < int > leftSubTree = new List < int > ( ) ; List < int > rightSubTree = new List < int > ( ) ; int root = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] < root ) { leftSubTree . Add ( arr [ i ] ) ; } else { rightSubTree . Add ( arr [ i ] ) ; } } int N1 = leftSubTree . Count ; int N2 = rightSubTree . Count ; int countLeft = countWays ( leftSubTree , fact ) ; int countRight = countWays ( rightSubTree , fact ) ; return nCr ( fact , N - 1 , N1 ) * countLeft * countRight ; } public static void Main ( String [ ] args ) { int [ ] a = { 3 , 4 , 5 , 1 , 2 } ; List < int > arr = new List < int > ( ) ; foreach ( int i in a ) arr . Add ( i ) ; int N = a . Length ; int [ ] fact = new int [ N ] ; calculateFact ( fact , N ) ; Console . Write ( countWays ( arr , fact ) ) ; } }


"Count permutations that are first decreasing then increasing . | C # implementation of the above approach ; Function to compute a ^ n % mod ; Function to count permutations that are first decreasing and then increasing ; For n = 1 return 0 ; Calculate and return result ; Driver code"
Solution in C#:

using System ; class GFG { static int mod = 1000000007 ; static long power ( long a , long n ) { if ( n == 0 ) return 1 ; long p = power ( a , n / 2 ) % mod ; p = ( p * p ) % mod ; if ( ( n & 1 ) == 1 ) p = ( p * a ) % mod ; return p ; } static int countPermutations ( int n ) { if ( n == 1 ) { return 0 ; } return ( ( int ) power ( 2 , n - 1 ) - 2 ) % mod ; } static public void Main ( ) { int n = 5 ; Console . WriteLine ( countPermutations ( n ) ) ; } }


"Count pieces of circle after N cuts | C # program to find number of pieces of circle after N cuts ; Function to find number of pieces of circle after N cuts ; Driver program"
Solution in C#:

class solution { static int countPieces ( int N ) { return 2 * N ; } static void Main ( ) { int N = 100 ; System . Console . WriteLine ( countPieces ( N ) ) ; } }


"Count points from an array that lies inside a semi | C # program for above approach ; Traverse the array ; Stores if a point lies above the diameter or not ; Stores if the R is less than or equal to the distance between center and point ; Driver code ;"
Solution in C#:

using System ; class Gfg { public static int getPointsIns ( int x1 , int y1 , int radius , int x2 , int y2 , pair [ ] points ) { int ans = 0 ; for ( int i = 0 ; i < points . Length ; i ++ ) { bool condOne = false , condTwo = false ; if ( ( points [ i ] . b - y2 ) * ( x2 - x1 ) - ( y2 - y1 ) * ( points [ i ] . a - x2 ) >= 0 ) { condOne = true ; } if ( radius >= ( int ) Math . Sqrt ( Math . Pow ( ( y1 - points [ i ] . b ) , 2 ) + Math . Pow ( x1 - points [ i ] . a , 2 ) ) ) { condTwo = true ; } if ( condOne && condTwo ) { ans += 1 ; } } return ans ; } public static void Main ( string [ ] args ) { int X = 0 ; int Y = 0 ; int R = 5 ; int P = 5 ; int Q = 0 ; pair [ ] arr = { new pair ( 2 , 3 ) , new pair ( 5 , 6 ) , new pair ( - 1 , 4 ) , new pair ( 5 , 5 ) } ; Console . Write ( getPointsIns ( X , Y , R , P , Q , arr ) ) ; } } public class pair { public int a ; public int b ; public pair ( int a , int b ) { this . a = a ; this . b = b ; } }


"Count points which are revisited while following the path specified by a given string | C # program for the above approach ; Function to find the number of times already visited position is revisited after starting traversal from { X , Y } ; Stores the x and y temporarily ; Stores the number of times an already visited position is revisited ; Initialize hashset ; Insert the starting coordinates ; Traverse over the string ; Update the coordinates according to the current directions ; If the new { X , Y } has been visited before , then increment the count by 1 ; Otherwise ; Insert new { x , y } ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { static int count ( String S , int X , int Y ) { int N = S . Length ; int temp_x = 0 , temp_y = 0 ; int count = 0 ; HashSet < String > s = new HashSet < String > ( ) ; s . Add ( ( X + " # " + Y ) ) ; for ( int i = 0 ; i < N ; i ++ ) { temp_x = X ; temp_y = Y ; if ( S [ i ] == ' U ' ) { X ++ ; } else if ( S [ i ] == ' D ' ) { X -- ; } else if ( S [ i ] == ' R ' ) { Y ++ ; } else { Y -- ; } if ( s . Contains ( ( temp_x + X ) + " # " + ( temp_y + Y ) ) ) { count ++ ; } else { s . Add ( ( temp_x + X ) + " # " + ( temp_y + Y ) ) ; } } return count ; } public static void Main ( String [ ] args ) { String S = " RDDUDL " ; int X = 0 , Y = 0 ; Console . Write ( count ( S , X , Y ) ) ; } }


"Count positions in Binary Matrix having equal count of set bits in corresponding row and column | C # Program to implement above approach ; Function to return the count of indices in from the given binary matrix having equal count of set bits in its row and column ; Stores count of set bits in corresponding column and row ; Traverse matrix ; Since 1 contains a set bit ; Update count of set bits for current row and col ; Stores the count of required indices ; Traverse matrix ; If current row and column has equal count of set bits ; Return count of required position ; Driver Code"
Solution in C#:

using System ; class GFG { static int countPosition ( int [ , ] mat ) { int n = mat . GetLength ( 0 ) ; int m = mat . GetLength ( 1 ) ; int [ ] row = new int [ n ] ; int [ ] col = new int [ m ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( mat [ i , j ] == 1 ) { col [ j ] ++ ; row [ i ] ++ ; } } } int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( row [ i ] == col [ j ] ) { count ++ ; } } } return count ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 0 , 1 } , { 1 , 1 } } ; Console . WriteLine ( countPosition ( mat ) ) ; } }


"Count positions in a chessboard that can be visited by the Queen which are not visited by the King | C # program for the above approach ; Function to print the number of cells only visited by the queen ; Find all the moves ; Find all moves for x + 1 , y + 1 ; Find all moves for x - 1 , y - 1 ; Find all moves for x - 1 , y + 1 ; Find all moves for x + 1 , y - 1 ; Find all squares visited by King x + 1 , in same row ; x - 1 , in same row ; y + 1 , in same column ; y - 1 , in same column ; Return answer ; Driver Code ; Dimension of Board ; Position of Cell ; Function Call"
Solution in C#:

using System ; class GFG { static int Moves_Calculator ( int x , int y , int row , int col ) { int total_moves = 0 ; if ( ( row - x ) > 0 && ( col - y ) > 0 ) total_moves += Math . Min ( ( row - x ) , ( col - y ) ) ; if ( ( y - 1 ) > 0 && ( x - 1 ) > 0 ) total_moves += Math . Min ( ( y - 1 ) , ( x - 1 ) ) ; if ( ( x - 1 ) > 0 && ( col - y ) > 0 ) total_moves += Math . Min ( ( x - 1 ) , ( col - y ) ) ; if ( ( row - x ) > 0 && ( y - 1 ) > 0 ) total_moves += Math . Min ( ( row - x ) , ( y - 1 ) ) ; total_moves += ( row - 1 ) + ( col - 1 ) ; int king_moves = 0 ; if ( x + 1 <= row ) king_moves += 1 ; if ( x - 1 > 0 ) king_moves += 1 ; if ( y + 1 <= col ) king_moves += 1 ; if ( y - 1 > 0 ) king_moves += 1 ; if ( x + 1 <= row && y + 1 <= col ) king_moves += 1 ; if ( x + 1 <= row && y - 1 > 0 ) king_moves += 1 ; if ( x - 1 > 0 && y - 1 > 0 ) king_moves += 1 ; if ( x - 1 > 0 && y + 1 <= col ) king_moves += 1 ; return total_moves - king_moves ; } public static void Main ( ) { int n = 8 , m = 8 ; int x = 1 , y = 1 ; Console . WriteLine ( Moves_Calculator ( x , y , m , n ) ) ; } }


"Count positive integers with 0 as a digit and maximum ' d ' digits | C # program to find the count of natural numbers upto a given number of digits that contain atleast one zero ; Utility function to calculate the count of natural numbers upto a given number of digits that contain atleast one zero ; Sum of two GP series ; Driver Code"
Solution in C#:

using System ; class GFG { static int findCountUpto ( int d ) { int GP1_Sum = 9 * ( ( int ) ( ( Math . Pow ( 10 , d ) ) - 1 ) / 9 ) ; int GP2_Sum = 9 * ( ( int ) ( ( Math . Pow ( 9 , d ) ) - 1 ) / 8 ) ; return GP1_Sum - GP2_Sum ; } public static void Main ( ) { int d = 1 ; Console . WriteLine ( findCountUpto ( d ) ) ; d = 2 ; Console . WriteLine ( findCountUpto ( d ) ) ; d = 4 ; Console . WriteLine ( findCountUpto ( d ) ) ; } }


"Count possible N | C # program for the above approach ; Macros for modulus ; DP array for memoization ; Utility function to count N digit numbers with digit i not appearing more than max_digit [ i ] consecutively ; If number with N digits is generated ; Create a reference variable ; Check if the current state is already computed before ; Initialize ans as zero ; Check if count of previous digit has reached zero or not ; Fill current position only with digits that are unequal to previous digit ; Else set the value of count for this new digit accordingly from max_digit [ ] ; Function to count N digit numbers with digit i not appearing more than max_digit [ i ] consecutive times ; Stores the readonly count ; Print the total count ; Driver Code ; Initialize the dp array with - 1 Fill each row with - 1. ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; using System ; using System . Collections . Generic ; public class GFG { static int MOD = 1000000007 ; static int [ , , ] dp = new int [ 5005 , 12 , 12 ] ; static int findCountUtil ( int N , int [ ] maxDigit , int position , int previous , int count ) { if ( position == N ) { return 1 ; } int ans = dp [ position , previous , count ] ; if ( ans != - 1 ) { return ans ; } ans = 0 ; for ( int i = 0 ; i <= 9 ; ++ i ) { if ( count == 0 && previous != i ) { ans = ( ans + ( findCountUtil ( N , maxDigit , position + 1 , i , maxDigit [ i ] - 1 ) ) % MOD ) % MOD ; } else if ( count != 0 ) { ans = ( ans + ( findCountUtil ( N , maxDigit , position + 1 , i , ( previous == i && position != 0 ) ? count - 1 : maxDigit [ i ] - 1 ) ) % MOD ) % MOD ; } } return ans ; } static void findCount ( int N , int [ ] maxDigit ) { int position = 0 ; int previous = 0 ; int count = 1 ; int ans = findCountUtil ( N , maxDigit , position , previous , count ) ; Console . WriteLine ( ans ) ; } public static void Main ( String [ ] args ) { int N = 2 ; int [ ] maxDigit = { 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 } ; for ( int i = 0 ; i < dp . GetLength ( 0 ) ; i ++ ) { for ( int j = 0 ; j < dp . GetLength ( 1 ) ; j ++ ) { for ( int k = 0 ; k < dp . GetLength ( 2 ) ; k ++ ) dp [ i , j , k ] = - 1 ; } } findCount ( N , maxDigit ) ; } }


"Count possible binary strings of length N without P consecutive 0 s and Q consecutive 1 s | C # program to implement the above approach ; Function to check if a string satisfy the given condition or not ; Stores the length of string ; Stores the previous character of the string ; Stores the count of consecutive equal characters ; Traverse the string ; If current character is equal to the previous character ; If count of consecutive 1 s is more than Q ; If count of consecutive 0 s is more than P ; Reset value of cnt ; If count of consecutive 1 s is more than Q ; If count of consecutive 0 s is more than P ; Function to count all distinct binary strings that satisfy the given condition ; Stores the length of str ; If length of str is N ; If str satisfy the given condition ; If str does not satisfy the given condition ; Append a character '0' at end of str ; Append a character '1' at end of str ; Return total count of binary strings ; Driver Code"
Solution in C#:

using System ; class GFG { static bool checkStr ( string str , int P , int Q ) { int N = str . Length ; char prev = str [ 0 ] ; int cnt = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( str [ i ] == prev ) { cnt ++ ; } else { if ( prev == '1' && cnt >= Q ) { return false ; } if ( prev == '0' && cnt >= P ) { return false ; } cnt = 1 ; } prev = str [ i ] ; } if ( prev == '1' && cnt >= Q ) { return false ; } if ( prev == '0' && cnt >= P ) { return false ; } return true ; } static int cntBinStr ( string str , int N , int P , int Q ) { int len = str . Length ; if ( len == N ) { if ( checkStr ( str , P , Q ) ) return 1 ; return 0 ; } int X = cntBinStr ( str + '0' , N , P , Q ) ; int Y = cntBinStr ( str + '1' , N , P , Q ) ; return X + Y ; } public static void Main ( ) { int N = 5 , P = 2 , Q = 3 ; Console . WriteLine ( cntBinStr ( " " , N , P , Q ) ) ; } }


"Count possible decodings of a given Digit Sequence | Set 2 | C # implementation to count the possible decodings of the given digit sequence ; Function to count the number of ways to decode the given digit sequence ; Array to store the dp states ; Case of empty String ; Condition to check if the first character of String is 0 ; Base case for single length String ; Bottom - up dp for the String ; Previous character ; Current character ; Case to include the current digit as a single digit for decoding the String ; Case to include the current character as two - digit for decoding the String ; Condition to check if the current character is " * " ; Condition to check if the current character is less than or equal to 26 ; Condition to check if the previous digit is equal to " * " ; Driver Code ; Function Call"
Solution in C#:

 using System ; class GFG { static int waysToDecode2 ( char [ ] s ) { int n = s . Length ; int [ ] dp = new int [ n + 1 ] ; dp [ 0 ] = 1 ; if ( s [ 0 ] == '0' ) return 0 ; dp [ 1 ] = ( ( s [ 0 ] == ' * ' ) ? 9 : 1 ) ; for ( int i = 2 ; i <= n ; i ++ ) { char first = s [ i - 2 ] ; char second = s [ i - 1 ] ; if ( second == ' * ' ) { dp [ i ] += 9 * dp [ i - 1 ] ; } else if ( second > '0' ) { dp [ i ] += dp [ i - 1 ] ; } if ( first == '1' first == '2' ) { if ( second == ' * ' ) { if ( first == '1' ) { dp [ i ] += 9 * dp [ i - 2 ] ; } else if ( first == '2' ) { dp [ i ] += 6 * dp [ i - 2 ] ; } } else if ( ( ( first - '0' ) * 10 + ( second - '0' ) ) <= 26 ) { dp [ i ] += dp [ i - 2 ] ; } } else if ( first == ' * ' ) { if ( second == ' * ' ) { dp [ i ] += 15 * dp [ i - 2 ] ; } else if ( second <= '6' ) { dp [ i ] += 2 * dp [ i - 2 ] ; } else { dp [ i ] += dp [ i - 2 ] ; } } } return dp [ n ] ; } public static void Main ( String [ ] args ) { String str = "12*3" ; Console . Write ( waysToDecode2 ( str . ToCharArray ( ) ) + "
 " ) ; } }


"Count possible moves in the given direction in a grid | C # implementation of the above approach ; Function to return the count of possible steps in a single direction ; It can cover infinite steps ; We are approaching towards X = N ; We are approaching towards X = 1 ; Function to return the count of steps ; Take the minimum of both moves independently ; Update count and current positions ; Driver code"
Solution in C#:

using System ; class GFG { static int steps ( int cur , int x , int n ) { if ( x == 0 ) return int . MaxValue ; if ( x > 0 ) return Math . Abs ( ( n - cur ) / x ) ; else return Math . Abs ( ( cur - 1 ) / x ) ; } static int countSteps ( int curx , int cury , int n , int m , int [ , ] moves ) { int count = 0 ; int k = moves . GetLength ( 0 ) ; for ( int i = 0 ; i < k ; i ++ ) { int x = moves [ i , 0 ] ; int y = moves [ i , 1 ] ; int stepct = Math . Min ( steps ( curx , x , n ) , steps ( cury , y , m ) ) ; count += stepct ; curx += stepct * x ; cury += stepct * y ; } return count ; } public static void Main ( String [ ] args ) { int n = 4 , m = 5 , x = 1 , y = 1 ; int [ , ] moves = { { 1 , 1 } , { 1 , 1 } , { 0 , - 2 } } ; Console . Write ( countSteps ( x , y , n , m , moves ) ) ; } }


"Count possible permutations of given array satisfying the given conditions | C # Program to implement the above approach ; Function to get the value of binomial coefficient ; Stores the value of binomial coefficient ; Since C ( N , R ) = C ( N , N - R ) ; Calculate the value of C ( N , R ) ; Function to get the count of permutations of the array that satisfy the condition ; Stores count of permutations of the array that satisfy the given condition ; Stores the value of C ( 2 N , N ) ; Stores the value of catalan number ; Return answer ; Driver Code"
Solution in C#:

using System ; class GFG { static int binCoff ( int N , int R ) { int res = 1 ; if ( R > ( N - R ) ) { R = ( N - R ) ; } for ( int i = 0 ; i < R ; i ++ ) { res *= ( N - i ) ; res /= ( i + 1 ) ; } return res ; } static int cntPermutation ( int N ) { int cntPerm ; int C_2N_N = binCoff ( 2 * N , N ) ; cntPerm = C_2N_N / ( N + 1 ) ; return cntPerm ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int N = arr . Length ; Console . WriteLine ( cntPermutation ( N / 2 ) ) ; } }


"Count possible removals to make absolute difference between the sum of odd and even indexed elements equal to K | C # program for the above approach ; Function to check if difference between the sum of odd and even indexed elements after removing the first element is K or not ; Stores the sum of elements at odd and even indices ; Return 1 if difference is K ; Function to check if difference between the sum of odd and even indexed elements after removing the second element is K or not ; Stores the sum of elements at odd and even indices ; Return 1 if difference is K ; Function to count number of elements to be removed to make sum of differences between odd and even indexed elements equal to K ; Size of given array ; Base Conditions ; Stores prefix and suffix sums ; Base assignments ; Store prefix sums of even indexed elements ; Store prefix sums of odd indexed elements ; Similarly , store suffix sums of elements at even and odd indices ; Stores the count of possible removals ; Traverse and remove the ith element ; If the current element is excluded , then previous index ( i - 1 ) points to ( i + 2 ) and ( i - 2 ) points to ( i + 1 ) ; Find count when 0 th element is removed ; Find count when 1 st element is removed ; Count gives the required answer ; Driver Code ; Function call"
Solution in C#:

using System ; class GFG { static int findCount0th ( int [ ] arr , int N , int K ) { int oddsum = 0 , evensum = 0 ; for ( int i = 1 ; i < N ; i += 2 ) { oddsum += arr [ i ] ; } for ( int i = 2 ; i < N ; i += 2 ) { evensum += arr [ i ] ; } if ( Math . Abs ( oddsum - evensum ) == K ) return 1 ; else return 0 ; } static int findCount1st ( int [ ] arr , int N , int K ) { int evensum = arr [ 0 ] , oddsum = 0 ; for ( int i = 3 ; i < N ; i += 2 ) { evensum += arr [ i ] ; } for ( int i = 2 ; i < N ; i += 2 ) { oddsum += arr [ i ] ; } if ( Math . Abs ( oddsum - evensum ) == K ) return 1 ; else return 0 ; } static int countTimes ( int [ ] arr , int K ) { int N = ( int ) arr . Length ; if ( N == 1 ) return 1 ; if ( N < 3 ) return 0 ; if ( N == 3 ) { int cnt = 0 ; cnt += ( Math . Abs ( arr [ 0 ] - arr [ 1 ] ) == K ? 1 : 0 ) + ( Math . Abs ( arr [ 2 ] - arr [ 1 ] ) == K ? 1 : 0 ) + ( Math . Abs ( arr [ 0 ] - arr [ 2 ] ) == K ? 1 : 0 ) ; return cnt ; } int [ ] prefix = new int [ N + 2 ] ; int [ ] suffix = new int [ N + 2 ] ; for ( int i = 0 ; i < N + 2 ; i ++ ) { prefix [ i ] = 0 ; } for ( int i = 0 ; i < N + 2 ; i ++ ) { suffix [ i ] = 0 ; } prefix [ 0 ] = arr [ 0 ] ; prefix [ 1 ] = arr [ 1 ] ; suffix [ N - 1 ] = arr [ N - 1 ] ; suffix [ N - 2 ] = arr [ N - 2 ] ; for ( int i = 2 ; i < N ; i += 2 ) { prefix [ i ] = arr [ i ] + prefix [ i - 2 ] ; } for ( int i = 3 ; i < N ; i += 2 ) { prefix [ i ] = arr [ i ] + prefix [ i - 2 ] ; } for ( int i = N - 3 ; i >= 0 ; i -= 2 ) { suffix [ i ] = arr [ i ] + suffix [ i + 2 ] ; } for ( int i = N - 4 ; i >= 0 ; i -= 2 ) { suffix [ i ] = arr [ i ] + suffix [ i + 2 ] ; } int count = 0 ; for ( int i = 2 ; i < N ; i ++ ) { if ( Math . Abs ( prefix [ i - 1 ] + suffix [ i + 2 ] - prefix [ i - 2 ] - suffix [ i + 1 ] ) == K ) { count ++ ; } } count += findCount0th ( arr , N , K ) ; count += findCount1st ( arr , N , K ) ; return count ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 4 , 5 , 6 } ; int K = 2 ; Console . WriteLine ( countTimes ( arr , K ) ) ; } }


"Count possible splits of sum N into K integers such that the minimum is at least P | C # program for the above approach ; Function that finds the value of the Binomial Coefficient C ( n , k ) ; Stores the value of Binomial Coefficient in bottom up manner ; Base Case ; Find the value using previously stored values ; Return the value of C ( N , K ) ; Function that count the number of ways to divide N into K integers >= P such that their sum is N ; Update the value of N ; Find the binomial coefficient recursively ; Driver Code ; Given K , N , and P"
Solution in C#:

using System ; class GFG { static int binomialCoeff ( int n , int k ) { int [ , ] C = new int [ n + 1 , k + 1 ] ; int i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= Math . Min ( i , k ) ; j ++ ) { if ( j == 0 j == i ) C [ i , j ] = 1 ; else C [ i , j ] = C [ i - 1 , j - 1 ] + C [ i - 1 , j ] ; } } return C [ n , k ] ; } static int waysToSplitN ( int k , int n , int P ) { int new_N = n - k * P ; return binomialCoeff ( new_N + k - 1 , new_N ) ; } public static void Main ( ) { int K = 3 , N = 8 , P = 2 ; Console . Write ( waysToSplitN ( K , N , P ) ) ; } }


"Count possible values of K such that X | C # program for the above approach ; Function to count integers K satisfying given equation ; Calculate the absoluter difference between a and b ; Iterate till sqrt of the difference ; Return the count ; Driver Code"
Solution in C#:

using System ; class GFG { static int condition ( int a , int b ) { int d = Math . Abs ( a - b ) , count = 0 ; for ( int i = 1 ; i <= Math . Sqrt ( d ) ; i ++ ) { if ( d % i == 0 ) { if ( d / i == i ) count += 1 ; else count += 2 ; } } return count ; } public static void Main ( String [ ] args ) { int x = 2 , y = 6 ; Console . WriteLine ( condition ( x , y ) ) ; } }


"Count possible ways to construct buildings | C # program to count all possible way to construct buildings ; Returns count of possible ways for N sections ; Base case ; 2 for one side and 4 for two sides ; countB is count of ways with a building at the end , countS is count of ways with a space at the end , prev_countB and prev_countS are previous values of countB and countS respectively . Initialize countB and countS for one side ; Use the above recursive formula for calculating countB and countS using previous values ; Result for one side is sum of ways ending with building and ending with space ; Result for 2 sides is square of result for one side ; Driver Code"
Solution in C#:

using System ; class GFG { static int countWays ( int N ) { if ( N == 1 ) return 4 ; int countB = 1 , countS = 1 , prev_countB , prev_countS ; for ( int i = 2 ; i <= N ; i ++ ) { prev_countB = countB ; prev_countS = countS ; countS = prev_countB + prev_countS ; countB = prev_countS ; } int result = countS + countB ; return ( result * result ) ; } public static void Main ( ) { int N = 3 ; Console . Write ( countWays ( N ) ) ; } }


"Count prime numbers that can be expressed as sum of consecutive prime numbers | C # program for the above approach ; Function to check if a number is prime or not ; Base Case ; Iterate till [ 5 , Math . Sqrt ( N ) ] to detect primality of numbers ; If N is divisible by i or i + 2 ; Return 1 if N is prime ; Function to count the prime numbers which can be expressed as sum of consecutive prime numbers ; Initialize count as 0 ; Stores prime numbers ; If i is prime ; Initialize the sum ; Find all required primes upto N ; Add it to the sum ; Return the readonly count ; Driver Code ; Given number N ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int isprm ( int n ) { if ( n <= 1 ) return 0 ; if ( n <= 3 ) return 1 ; if ( n % 2 == 0 n % 3 == 0 ) return 0 ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) return 0 ; } return 1 ; } static int countprime ( int n ) { int count = 0 ; List < int > primevector = new List < int > ( ) ; for ( int i = 2 ; i <= n ; i ++ ) { if ( isprm ( i ) == 1 ) { primevector . Add ( i ) ; } } int sum = primevector [ 0 ] ; for ( int i = 1 ; i < primevector . Count ; i ++ ) { sum += primevector [ i ] ; if ( sum > n ) break ; if ( isprm ( sum ) == 1 ) { count ++ ; } } return count ; } public static void Main ( String [ ] args ) { int N = 45 ; Console . Write ( countprime ( N ) ) ; } }


"Count prime numbers up to N that can be represented as a sum of two prime numbers | C # program for the above approach ; Function to store all prime numbers up to N using Sieve of Eratosthenes ; Set 0 and 1 as non - prime ; If p is prime ; Set all multiples of p as non - prime ; Function to count prime numbers up to N that can be represented as the sum of two prime numbers ; Stores all the prime numbers ; Update the prime array ; Create a dp array of size n + 1 ; Update dp [ 1 ] = 0 ; Iterate over the range [ 2 , N ] ; Add the previous count value ; Increment dp [ i ] by 1 if i and ( i - 2 ) are both prime ; Print the result ; Driver code"
Solution in C#:

using System ; class GFG { static void SieveOfEratosthenes ( int n , bool [ ] prime ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) { prime [ i ] = false ; } } } } static void countPrime ( int n ) { bool [ ] prime = new bool [ n + 1 ] ; for ( int i = 0 ; i < prime . Length ; i ++ ) { prime [ i ] = true ; } SieveOfEratosthenes ( n , prime ) ; int [ ] dp = new int [ n + 1 ] ; for ( int i = 0 ; i < dp . Length ; i ++ ) { dp [ i ] = 0 ; } dp [ 1 ] = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { dp [ i ] += dp [ i - 1 ] ; if ( prime [ i ] == true && prime [ i - 2 ] == true ) { dp [ i ] ++ ; } } Console . Write ( dp [ n ] ) ; } static void Main ( ) { int N = 6 ; countPrime ( N ) ; } }


"Count prime pairs whose difference is also a Prime Number | C # program to implement the above approach ; Function to find all prime numbers in the range [ 1 , N ] ; isPrime [ i ] : Stores if i is a prime number or not ; Calculate all prime numbers up to Max using Sieve of Eratosthenes ; If P is a prime number ; Set all multiple of P as non - prime ; Update isPrime ; Function to count pairs of prime numbers in the range [ 1 , N ] whose difference is prime ; Function to count pairs of prime numbers whose difference is also a prime number ; isPrime [ i ] : Stores if i is a prime number or not ; Iterate over the range [ 2 , N ] ; If i and i - 2 is a prime number ; Update cntPairs ; Driver Code"
Solution in C#:

using System ; class GFG { public static bool [ ] SieveOfEratosthenes ( int N ) { bool [ ] isPrime = new bool [ N + 1 ] ; for ( int i = 0 ; i < N + 1 ; i ++ ) { isPrime [ i ] = true ; } isPrime [ 0 ] = false ; isPrime [ 1 ] = false ; for ( int p = 2 ; p * p <= N ; p ++ ) { if ( isPrime [ p ] ) { for ( int i = p * p ; i <= N ; i += p ) { isPrime [ i ] = false ; } } } return isPrime ; } public static int cntPairsdiffOfPrimeisPrime ( int N ) { int cntPairs = 0 ; bool [ ] isPrime = SieveOfEratosthenes ( N ) ; for ( int i = 2 ; i <= N ; i ++ ) { if ( isPrime [ i ] && isPrime [ i - 2 ] ) { cntPairs += 2 ; } } return cntPairs ; } public static void Main ( ) { int N = 5 ; Console . WriteLine ( cntPairsdiffOfPrimeisPrime ( N ) ) ; } }


"Count prime triplets upto N having sum of first two elements equal to the third number | C # Program to implement the above approach ; Boolean array to mark prime numbers ; To count the prime triplets having the sum of the first two numbers equal to the third element ; Function to count prime triplets having sum of the first two elements equal to the third element ; Sieve of Eratosthenes ; Checks for the prime numbers having difference equal to2 ; Update count of triplets ; Print the answer ; Driver Code"
Solution in C#:

using System ; class GFG { static int MAX = 1000001 ; static bool [ ] prime = new bool [ MAX ] ; static int [ ] cntTriplet = new int [ MAX ] ; static void primeTriplet ( int N ) { for ( int i = 0 ; i <= N ; i ++ ) { prime [ i ] = true ; } prime [ 0 ] = prime [ 1 ] = false ; for ( int i = 2 ; i * i <= N ; i ++ ) { if ( prime [ i ] ) { for ( int j = i * i ; j <= N ; j += i ) { prime [ j ] = false ; } } } for ( int i = 3 ; i <= N ; i ++ ) { if ( prime [ i ] && prime [ i - 2 ] ) { cntTriplet [ i ] = cntTriplet [ i - 1 ] + 1 ; } else { cntTriplet [ i ] = cntTriplet [ i - 1 ] ; } } Console . WriteLine ( cntTriplet [ N ] ) ; } public static void Main ( String [ ] args ) { int N = 7 ; primeTriplet ( N ) ; } }


"Count primes that can be expressed as sum of two consecutive primes and 1 | C # implementation of the approach ; To check if a number is prime or not ; To store possible numbers ; Function to return all prime numbers ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; Function to count all possible prime numbers that can be expressed as the sum of two consecutive primes and one ; All possible prime numbers below N ; Driver code"
Solution in C#:

using System ; using System . Collections ; class GfG { static int N = 100005 ; static bool [ ] isprime = new bool [ N ] ; static bool [ ] can = new bool [ N ] ; static ArrayList SieveOfEratosthenes ( ) { for ( int a = 0 ; a < N ; a ++ ) { isprime [ a ] = true ; } for ( int p = 2 ; p * p < N ; p ++ ) { if ( isprime [ p ] == true ) { for ( int i = p * p ; i < N ; i += p ) isprime [ i ] = false ; } } ArrayList primes = new ArrayList ( ) ; for ( int i = 2 ; i < N ; i ++ ) if ( isprime [ i ] ) primes . Add ( i ) ; return primes ; } static int Prime_Numbers ( int n ) { ArrayList primes = SieveOfEratosthenes ( ) ; for ( int i = 0 ; i < primes . Count - 1 ; i ++ ) if ( ( int ) primes [ i ] + ( int ) primes [ i + 1 ] + 1 < N ) can [ ( int ) primes [ i ] + ( int ) primes [ i + 1 ] + 1 ] = true ; int ans = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { if ( can [ i ] && isprime [ i ] == true ) { ans ++ ; } } return ans ; } static void Main ( ) { int n = 50 ; Console . WriteLine ( Prime_Numbers ( n ) ) ; } }


"Count quadruples ( i , j , k , l ) in an array such that i < j < k < l and arr [ i ] = arr [ k ] and arr [ j ] = arr [ l ] | C # program for the above approach ; Function to count total number of required tuples ; Initialize unordered map ; Find the pairs ( j , l ) such that arr [ j ] = arr [ l ] and j < l ; Elements are equal ; Update the count ; Add the frequency of arr [ l ] to val ; Update the frequency of element arr [ j ] ; Return the answer ; Driver code ; Given array [ ] arr ; Function call"
Solution in C#:

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


"Count quadruples from four sorted arrays whose sum is equal to a given value x | C # implementation to count quadruples from four sorted arrays whose sum is equal to a given value x ; count pairs from the two sorted array whose sum is equal to the given ' value ' ; traverse ' arr1 [ ] ' from left to right traverse ' arr2 [ ] ' from right to left ; if the ' sum ' is equal to ' value ' , then increment ' l ' , decrement ' r ' and increment ' count ' ; if the ' sum ' is greater than ' value ' , then decrement r ; else increment l ; required count of pairs ; function to count all quadruples from four sorted arrays whose sum is equal to a given value x ; generate all pairs from arr1 [ ] and arr2 [ ] ; calculate the sum of elements in the pair so generated ; count pairs in the 3 rd and 4 th array having value ' x - p _ sum ' and then accumulate it to ' count ' ; required count of quadruples ; Driver program to test above ; four sorted arrays each of size ' n '"
Solution in C#:

using System ; public class GFG { static int countPairs ( int [ ] arr1 , int [ ] arr2 , int n , int value ) { int count = 0 ; int l = 0 , r = n - 1 ; while ( l < n & r >= 0 ) { int sum = arr1 [ l ] + arr2 [ r ] ; if ( sum == value ) { l ++ ; r -- ; count ++ ; } else if ( sum > value ) r -- ; else l ++ ; } return count ; } static int countQuadruples ( int [ ] arr1 , int [ ] arr2 , int [ ] arr3 , int [ ] arr4 , int n , int x ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) { int p_sum = arr1 [ i ] + arr2 [ j ] ; count += countPairs ( arr3 , arr4 , n , x - p_sum ) ; } return count ; } static public void Main ( ) { int [ ] arr1 = { 1 , 4 , 5 , 6 } ; int [ ] arr2 = { 2 , 3 , 7 , 8 } ; int [ ] arr3 = { 1 , 4 , 6 , 10 } ; int [ ] arr4 = { 2 , 4 , 7 , 8 } ; int n = arr1 . Length ; int x = 30 ; Console . Write ( " Count ▁ = ▁ " + countQuadruples ( arr1 , arr2 , arr3 , arr4 , n , x ) ) ; } }