# Number of ordered pairs such that (Ai & Aj) = 0

• Difficulty Level : Hard
• Last Updated : 06 Jan, 2022

Given an array A[] of n integers, find out the number of ordered pairs such that Ai&Aj is zero, where 0<=(i,j)<n. Consider (i, j) and (j, i) to be different.

Constraints:
1<=n<=104
1<=Ai<=104

Examples:

Input : A[] = {3, 4, 2}
Output : 4
Explanation : The pairs are (3, 4) and (4, 2) which are
counted as 2 as (4, 3) and (2, 4) are considered different.

Input : A[]={5, 4, 1, 6}
Output : 4
Explanation : (4, 1), (1, 4), (6, 1) and (1, 6) are the pairs

Simple approach: A simple approach is to check for all possible pairs and count the number of ordered pairs whose bitwise & returns 0.

Below is the implementation of the above idea:

## C++

 // CPP program to calculate the number// of ordered pairs such that their bitwise// and is zero#include using namespace std; // Naive function to count the number// of ordered pairs such that their// bitwise and is 0int countPairs(int a[], int n){    int count = 0;     // check for all possible pairs    for (int i = 0; i < n; i++) {        for (int j = i + 1; j < n; j++)            if ((a[i] & a[j]) == 0)                 // add 2 as (i, j) and (j, i) are                // considered different                count += 2;    }     return count;} // Driver Codeint main(){    int a[] = { 3, 4, 2 };    int n = sizeof(a) / sizeof(a[0]);       cout << countPairs(a, n);       return 0;}

## Java

 // Java program to calculate the number// of ordered pairs such that their bitwise// and is zero class GFG {         // Naive function to count the number    // of ordered pairs such that their    // bitwise and is 0    static int countPairs(int a[], int n)    {        int count = 0;         // check for all possible pairs        for (int i = 0; i < n; i++) {            for (int j = i + 1; j < n; j++)                if ((a[i] & a[j]) == 0)                     // add 2 as (i, j) and (j, i) are                    // considered different                    count += 2;        }         return count;    }     // Driver Code    public static void main(String arg[])    {        int a[] = { 3, 4, 2 };        int n = a.length;        System.out.print(countPairs(a, n));    }} // This code is contributed by Anant Agarwal.

## Python3

 # Python3 program to calculate the number# of ordered pairs such that their# bitwise and is zero # Naive function to count the number# of ordered pairs such that their# bitwise and is 0def countPairs(a, n):    count = 0     # check for all possible pairs    for i in range(0, n):        for j in range(i + 1, n):            if (a[i] & a[j]) == 0:                 # add 2 as (i, j) and (j, i) are                # considered different                count += 2    return count # Driver Codea = [ 3, 4, 2 ]n = len(a)print (countPairs(a, n)) # This code is contributed# by Shreyanshi Arun.

## C#

 // C# program to calculate the number// of ordered pairs such that their// bitwise and is zerousing System; class GFG {         // Naive function to count the number    // of ordered pairs such that their    // bitwise and is 0    static int countPairs(int []a, int n)    {        int count = 0;         // check for all possible pairs        for (int i = 0; i < n; i++)        {            for (int j = i + 1; j < n; j++)                if ((a[i] & a[j]) == 0)                     // add 2 as (i, j) and (j, i)                    // arev considered different                    count += 2;        }         return count;    }     // Driver Code    public static void Main()    {        int []a = { 3, 4, 2 };        int n = a.Length;        Console.Write(countPairs(a, n));    }} // This code is contributed by nitin mittal.



## JavaScript

   ?>

Output:

4

Time Complexity: O(n2)

Efficient approach: An efficient approach is to use Sum over Subsets Dynamic Programming method and count the number of ordered pairs. In the SOS DP we find out the pairs whose bitwise & returned 0. Here we need to count the number of pairs.
Some key observations are the constraints, the maximum that an array element can be is 104. Calculating the mask up to (1<<15) will give us our answer. Use hashing to count the occurrence of every element. If the last bit is OFF, then relating to SOS dp, we will have a base case since there is only one possibility of OFF bit.

If the last bit is set ON, then we will have the base case as:

We add freq(mask^1) to add the other possibility of OFF bit.
Iterate over N=15 bits, which is the maximum possible.
Let’s consider the i-th bit to be 0, then no subset can differ from the mask in the i-th bit as it would mean that the numbers will have a 1 at i-th bit where the mask has a 0 which would mean that it is not a subset of the mask. Thus we conclude that the numbers now differ in the first (i-1) bits only. Hence,

Now the second case, if the i-th bit is 1, it can be divided into two non-intersecting sets. One containing numbers with i-th bit as 1 and differing from mask in the next (i-1) bits. Second containing numbers with ith bit as 0 and differing from mask

(2i) in next (i-1) bits. Hence,

2i, i-1).

DP[mask][i] stores the number of subsets of mask which differ from mask only in first i bits. Iterate for all array elements, and for every array element add the number of subsets (dp[ ( ( 1<<N ) – 1 ) ^ a[i] ][ N ]) to the number of pairs. N = maximum number of bits.

Explanation of addition of dp[ ( ( 1<<N ) – 1 ) ^ a[i] ][N] to the number of pairs: Take an example of A[i] being 5, which is 101 in binary. For better understanding, assume N=3 in this case, therefore, the reverse of 101 will be 010 which on applying bitwise & gives 0. So (1<<3) gives 1000 which on subtraction from 1 gives 111. 111

101 gives 010 which is the reversed bit.So dp[((1<<N)-1)^a[i]][N] will have the number of subsets that returns 0 on applying bitwise & operator.

Below is the implementation of the above idea: