Count pairs of elements such that number of set bits in their AND is B [ i ] | C ++ implementation of the 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 ] ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; 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 ( __builtin_popcount ( A [ i ] & A [ j ] ) == B [ j ] ) { cnt ++ ; } return cnt ; } int main ( ) { int A [ ] = { 2 , 3 , 1 , 4 , 5 } ; int B [ ] = { 2 , 2 , 1 , 4 , 2 } ; int size = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << solve ( A , B , size ) ; return 0 ; }