# Count pairs of elements such that number of set bits in their AND is B[i]

Given two arrays A[] and B[] of N elements each. The task is to find the number of index pairs (i, j) such that i ≤ j and F(A[i] & A[j]) = B[j] where F(X) is the count of set bits in the binary representation of X.

Examples:

Input: A[] = {2, 3, 1, 4, 5}, B[] = {2, 2, 1, 4, 2}
Output: 4
All possible pairs are (3, 3), (3, 1), (1, 1) and (5, 5)

Input: A[] = {1, 2, 3, 4, 5}, B[] = {2, 2, 2, 2, 2}
Output: 2

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Iterate through all the possible pairs (i, j) and check the count of set bits in their AND value. If the count is equal to B[j] then increment the count.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the count of pairs ` `// which satisfy the given condition ` `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++) ` ` `  `            ``// Check if the count of set bits ` `            ``// in the AND value is B[j] ` `            ``if` `(__builtin_popcount(A[i] & A[j]) == B[j]) { ` `                ``cnt++; ` `            ``} ` ` `  `    ``return` `cnt; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `A[] = { 2, 3, 1, 4, 5 }; ` `    ``int` `B[] = { 2, 2, 1, 4, 2 }; ` `    ``int` `size = ``sizeof``(A) / ``sizeof``(A); ` ` `  `    ``cout << solve(A, B, size); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `public` `class` `GFG  ` `{ ` ` `  `    ``// Function to return the count of pairs ` `    ``// which satisfy the given condition ` `    ``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++) ``// Check if the count of set bits ` `            ``// in the AND value is B[j] ` `            ``{ ` `                ``if` `(Integer.bitCount(A[i] & A[j]) == B[j])  ` `                ``{ ` `                    ``cnt++; ` `                ``} ` `            ``} ` `        ``} ` ` `  `        ``return` `cnt; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args)  ` `    ``{ ` `        ``int` `A[] = {``2``, ``3``, ``1``, ``4``, ``5``}; ` `        ``int` `B[] = {``2``, ``2``, ``1``, ``4``, ``2``}; ` `        ``int` `size = A.length; ` ` `  `        ``System.out.println(solve(A, B, size)); ` `    ``} ` `} ` ` `  `/* This code contributed by PrinciRaj1992 */`

## Python3

 `# Python3 implementation of the approach  ` ` `  `# Function to return the count of pairs  ` `# which satisfy the given condition  ` `def` `solve(A, B, n) :  ` `    ``cnt ``=` `0``;  ` ` `  `    ``for` `i ``in` `range``(n) :  ` `        ``for` `j ``in` `range``(i, n) :  ` ` `  `            ``# Check if the count of set bits  ` `            ``# in the AND value is B[j]  ` `            ``c ``=` `A[i] & A[j] ` `            ``if` `(``bin``(c).count(``'1'``) ``=``=` `B[j]) : ` `                ``cnt ``+``=` `1``;  ` `    ``return` `cnt;  ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"` `:  ` ` `  `    ``A ``=` `[ ``2``, ``3``, ``1``, ``4``, ``5` `];  ` `    ``B ``=` `[ ``2``, ``2``, ``1``, ``4``, ``2` `];  ` `     `  `    ``size ``=` `len``(A);  ` ` `  `    ``print``(solve(A, B, size));  ` ` `  `# This code is contributed  ` `# by AnkitRai01 `

## C#

 `// C# Implementation of the above approach ` `using` `System; ` ` `  `class` `GFG  ` `{  ` ` `  `    ``// Function to return the count of pairs  ` `    ``// which satisfy the given condition  ` `    ``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++)  ` `            ``// Check if the count of set bits  ` `            ``// in the AND value is B[j]  ` `            ``{  ` `                ``if` `(countSetBits(A[i] & A[j]) == B[j])  ` `                ``{  ` `                    ``cnt++;  ` `                ``}  ` `            ``}  ` `        ``}  ` ` `  `        ``return` `cnt;  ` `    ``}  ` `     `  `    ``// Function to get no of set  ` `    ``// bits in binary representation  ` `    ``// of positive integer n  ` `    ``static` `int` `countSetBits(``int` `n)  ` `    ``{  ` `        ``int` `count = 0;  ` `        ``while` `(n > 0) ` `        ``{  ` `            ``count += n & 1;  ` `            ``n >>= 1;  ` `        ``}  ` `        ``return` `count;  ` `    ``} ` `     `  `    ``// Driver code  ` `    ``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));  ` `    ``}  ` `}  ` ` `  `// This code is contributed by Princi Singh `

Output:

```4
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.