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

• Difficulty Level : Medium
• Last Updated : 03 Jun, 2021

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:
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:

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 using namespace std; // Function to return the count of pairs// which satisfy the given conditionint 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 codeint main(){    int A[] = { 5, 3, 2, 4, 6, 1 };    int B[] = { 2, 2, 1, 4, 2, 3 };    int size = sizeof(A) / sizeof(A);     cout << solve(A, B, size);     return 0;}

Java

 // Java implementation of the approachclass GFG{ // Function to return the count of pairs// which satisfy the given conditionstatic 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 codepublic 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

Python3

 # Python3 implementation of the approach # Function to return the count of pairs# which satisfy the given conditiondef 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 codeif __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

C#

 // C# implementation of the approachusing System; class GFG{ // Function to return the count of pairs// which satisfy the given conditionstatic 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 codepublic 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 */

Javascript


Output:
7

My Personal Notes arrow_drop_up