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
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 ) ) ; } }