 GeeksforGeeks App
Open App Browser
Continue

# Count of pairs with bitwise XOR value greater than its bitwise AND value | Set 2

Given an array arr that contains N positive Integers. Find the count of all possible pairs whose bit wise XOR value is greater than bit wise AND value

Examples:

Input : arr[]={ 12, 4 , 15}
Output:  2
Explanation: 12 ^ 4 = 8 ,  12 & 4 = 4 . so 12 ^ 4 > 12 & 4
4 ^ 15 = 11,   4 & 15 = 4. so 4 ^ 15  > 4 & 15 .
So , above two are valid pairs { 12,4 } ,{4, 15}  .

Input:  arr[] ={ 1, 1, 3, 3 }
Output: 4

Naive Approach: For the brute force approach to solve this problem, please refer to the SET 1 of this article.

Time Complexity: O(N*N)
Auxiliary Space: O(1)

Efficient Approach: The efficient approach to solve this problem is based on following observation:

Observation:

The observation is that if the index of  MSB ( Most Significant bit, also known as leftmost set bit), of any two elements is the same then it is guaranteed that the bitwise XOR of those two elements is less than their bitwise AND ., because 1^1=0, it will unset the MSB.  while 1&1=1, MSB will remain set.

Illustration:

If a = 1010, b = 1000,

Then a & b= 1000 and a ^ b = 0010

So if the MSB index is the same in both then their bitwise XOR will always be less than bitwise AND . Vice versa if the MSB index is different then their bitwise XOR value will be greater than the bitwise AND value. Consider the below example to understand this sentence.

Follow the below steps to solve this problem:

• Construct an MSB array, MSB[ i ] will represent the count of the elements whose MSB bit’s index is i.
• Remove (MSB[ i ]*(MSB[ i ]-1))/2 from total . Because the pair whose MSB is same is not a valid pair .
• Return the valid count.

Below is the Implementation of the above approach:

## C++

 `// C++ program to Count number of pairs ``//whose bit wise XOR value is greater``//than bit wise AND value` `#include ``using` `namespace` `std;``int` `MSB;` `// return count of pairs``// whose bitwise xor >= bitwise``long` `long` `valid_pairs(``int` `arr[], ``int` `N)``{``    ``for` `(``int` `i = 0; i < N; i++) {``        ``// index of MSB in arr[i]``        ``int` `MSB_index = log2(arr[i]);``        ``MSB[MSB_index]++;``    ``}` `    ``long` `long` `tot = (N * (N - 1)) / 2;` `    ``long` `long` `invalid = 0;` `    ``// pairs are invalid if``    ``// their MSB index  are same``    ``for` `(``int` `i = 0; i < 32; i++)``        ``invalid += ((MSB[i] * 1LL * (MSB[i] - 1)) / 2);` `    ``long` `long` `valid = tot - invalid;``    ``return` `valid;``}` `//Driver Code``int` `main()``{``    ``int` `N = 3;``    ``int` `arr[] = { 12, 4, 15 };``    ``cout << valid_pairs(arr, N);``}`

## Java

 `// Java program to Count number of pairs``// whose bit wise XOR value is greater``// than bit wise AND value``import` `java.io.*;``import` `java.lang.*;` `class` `GFG``{` `  ``// Function to calculate the``  ``// log base 2 of an integer``  ``public` `static` `int` `log2(``int` `N)``  ``{` `    ``// calculate log2 N indirectly``    ``// using log() method``    ``int` `result = (``int``)(Math.log(N) / Math.log(``2``));``    ``return` `result;``  ``}``  ``static` `int` `MSB[] = ``new` `int``[``32``];` `  ``// return count of pairs``  ``// whose bitwise xor >= bitwise``  ``public` `static` `long` `valid_pairs(``int` `arr[], ``int` `N)``  ``{``    ``for` `(``int` `i = ``0``; i < N; i++) {``      ``// index of MSB in arr[i]``      ``int` `MSB_index = log2(arr[i]);``      ``MSB[MSB_index]++;``    ``}` `    ``long` `tot = (N * (N - ``1``)) / ``2``;` `    ``long` `invalid = ``0``;` `    ``// pairs are invalid if``    ``// their MSB index  are same``    ``for` `(``int` `i = ``0``; i < ``32``; i++)``      ``invalid += (((``long``)MSB[i] * (MSB[i] - ``1``)) / ``2``);` `    ``long` `valid = tot - invalid;``    ``return` `valid;``  ``}``  ``public` `static` `void` `main(String[] args)``  ``{``    ``int` `N = ``3``;``    ``int` `arr[] = { ``12``, ``4``, ``15` `};``    ``System.out.print(valid_pairs(arr, N));``  ``}``}` `// This code is contributed by Rohit Pradhan`

## Python3

 `# python3 program to Count number of pairs``# whose bit wise XOR value is greater``# than bit wise AND value``import` `math` `MSB ``=` `[``0` `for` `_ ``in` `range``(``32``)]` `# return count of pairs``# whose bitwise xor >= bitwise``def` `valid_pairs(arr, N):` `    ``for` `i ``in` `range``(``0``, N):``        ``# index of MSB in arr[i]``        ``MSB_index ``=` `int``(math.log2(arr[i]))``        ``MSB[MSB_index] ``+``=` `1` `    ``tot ``=` `(N ``*` `(N ``-` `1``)) ``/``/` `2` `    ``invalid ``=` `0` `    ``# pairs are invalid if``    ``# their MSB index are same``    ``for` `i ``in` `range``(``0``, ``32``):``        ``invalid ``+``=` `((MSB[i] ``*` `(MSB[i] ``-` `1``)) ``/``/` `2``)` `    ``valid ``=` `tot ``-` `invalid``    ``return` `valid` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:` `    ``N ``=` `3``    ``arr ``=` `[``12``, ``4``, ``15``]``    ``print``(valid_pairs(arr, N))` `# This code is contributed by rakeshsahni`

## C#

 `// C# program to Count number of pairs``// whose bit wise XOR value is greater``// than bit wise AND value``using` `System;` `public` `class` `GFG{` `  ``// Function to calculate the``  ``// log base 2 of an integer``  ``public` `static` `int` `log2(``int` `N)``  ``{` `    ``// calculate log2 N indirectly``    ``// using log() method``    ``int` `result = (``int``)(Math.Log(N) / Math.Log(2));``    ``return` `result;``  ``}``  ``static` `int``[] MSB = ``new` `int``;` `  ``// return count of pairs``  ``// whose bitwise xor >= bitwise``  ``public` `static` `long` `valid_pairs(``int``[] arr, ``int` `N)``  ``{``    ``for` `(``int` `i = 0; i < N; i++) {``      ``// index of MSB in arr[i]``      ``int` `MSB_index = log2(arr[i]);``      ``MSB[MSB_index]++;``    ``}` `    ``long` `tot = (N * (N - 1)) / 2;` `    ``long` `invalid = 0;` `    ``// pairs are invalid if``    ``// their MSB index  are same``    ``for` `(``int` `i = 0; i < 32; i++)``      ``invalid += (((``long``)MSB[i] * (MSB[i] - 1)) / 2);` `    ``long` `valid = tot - invalid;``    ``return` `valid;``  ``}``  ``static` `public` `void` `Main (){` `    ``int` `N = 3;``    ``int``[] arr = { 12, 4, 15 };``    ``Console.Write(valid_pairs(arr, N));``  ``}``}` `// This code is contributed by hrithikgarg03188.`

## Javascript

 ``

Output

`2`

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up