The Wayback Machine - https://web.archive.org/web/20220325143313/https://www.geeksforgeeks.org/count-of-pairs-with-bitwise-xor-value-greater-than-its-bitwise-and-value-set-2/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Last Updated : 25 Mar, 2022

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 totat . Becuase 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 <bits/stdc++.h>
using namespace std;
int MSB[32];
  
// return count of pairs
// whose bitwiese 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);
}
Output
2

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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!