Count pairs of elements such that number of set bits in their AND is B [ i ] | 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
function solve ( A , B , n ) { var cnt = 0 ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = i ; j < n ; j ++ ) { if ( countSetBits ( A [ i ] & A [ j ] ) == B [ j ] ) { cnt ++ ; } } } return cnt ; } function countSetBits ( n ) { var count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } var A = [ 2 , 3 , 1 , 4 , 5 ] ; var B = [ 2 , 2 , 1 , 4 , 2 ] ; var size = A . length ; document . write ( solve ( A , B , size ) ) ;