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[] = {5, 3, 2, 4, 6, 1}, B[] = {2, 2, 1, 4, 2, 3}

Output:7

All possible pairs are (5, 5), (3, 3), (2, 2),

(2, 6), (4, 6), (6, 6) and (6, 1).

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

Output:4

**Approach:** Iterate through all the possible pairs (i, j) and check the count of set bits in their OR 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 <bits/stdc++.h> ` `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 OR value is B[j] ` ` ` `if` `(__builtin_popcount(A[i] | A[j]) == B[j]) { ` ` ` `cnt++; ` ` ` `} ` ` ` ` ` `return` `cnt; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `A[] = { 5, 3, 2, 4, 6, 1 }; ` ` ` `int` `B[] = { 2, 2, 1, 4, 2, 3 }; ` ` ` `int` `size = ` `sizeof` `(A) / ` `sizeof` `(A[0]); ` ` ` ` ` `cout << solve(A, B, size); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `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 OR 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[] = { ` `5` `, ` `3` `, ` `2` `, ` `4` `, ` `6` `, ` `1` `}; ` ` ` `int` `B[] = { ` `2` `, ` `2` `, ` `1` `, ` `4` `, ` `2` `, ` `3` `}; ` ` ` `int` `size = A.length; ` ` ` ` ` `System.out.println(solve(A, B, size)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## 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 OR value is B[j] ` ` ` `if` `(` `bin` `(A[i] | A[j]).count(` `'1'` `) ` `=` `=` `B[j]) : ` ` ` `cnt ` `+` `=` `1` `; ` ` ` ` ` `return` `cnt ` ` ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `A ` `=` `[ ` `5` `, ` `3` `, ` `2` `, ` `4` `, ` `6` `, ` `1` `]; ` ` ` `B ` `=` `[ ` `2` `, ` `2` `, ` `1` `, ` `4` `, ` `2` `, ` `3` `]; ` ` ` `size ` `=` `len` `(A); ` ` ` ` ` `print` `(solve(A, B, size)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the 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 OR value is B[j] ` ` ` `if` `(bitCount(A[i] | A[j]) == B[j]) ` ` ` `{ ` ` ` `cnt++; ` ` ` `} ` ` ` ` ` `return` `cnt; ` `} ` ` ` `static` `int` `bitCount(` `long` `x) ` `{ ` ` ` `// To store the count ` ` ` `// of set bits ` ` ` `int` `setBits = 0; ` ` ` `while` `(x != 0) ` ` ` `{ ` ` ` `x = x & (x - 1); ` ` ` `setBits++; ` ` ` `} ` ` ` `return` `setBits; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String []args) ` `{ ` ` ` `int` `[]A = { 5, 3, 2, 4, 6, 1 }; ` ` ` `int` `[]B = { 2, 2, 1, 4, 2, 3 }; ` ` ` `int` `size = A.Length; ` ` ` ` ` `Console.WriteLine(solve(A, B, size)); ` `} ` `} ` ` ` `/* This code is contributed by PrinciRaj1992 */` |

*chevron_right*

*filter_none*

**Output:**

7

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count pairs of elements such that number of set bits in their AND is B[i]
- Count pairs from 1 to N such that their Sum is divisible by their XOR
- Count pairs in an array such that both elements has equal set bits
- Total character pairs from two strings, with equal number of set bits in their ascii value
- Replace the odd positioned elements with their cubes and even positioned elements with their squares
- Count ordered pairs of positive numbers such that their sum is S and XOR is K
- Count the pairs in an array such that the difference between them and their indices is equal
- Count of pairs in an array such that the highest power of 2 that divides their product is 1
- Count of pairs upto N such whose LCM is not equal to their product for Q queries
- Find number of pairs in an array such that their XOR is 0
- Find the number of pairs such that their gcd is equals to 1
- Count of pairs in an Array with same number of set bits
- Total distinct pairs from two arrays such that second number can be obtained by inverting bits of first
- Count integers in an Array which are multiples their bits counts
- Find a number X such that (X XOR A) is minimum and the count of set bits in X and B are equal
- Count pairs with set bits sum equal to K
- Check if array can be sorted by swapping pairs with GCD of set bits count equal to that of the smallest array element
- Find maximum number of elements such that their absolute difference is less than or equal to 1
- Number of ways to choose elements from the array such that their average is K
- Largest number M having bit count of N such that difference between their OR and XOR value is maximized

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.