Open In App

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

Last Updated : 21 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 <bits/stdc++.h>
using namespace std;
int MSB[32];
 
// 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[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;
  }
  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




<script>
       // JavaScript code for the above approach
       let MSB = new Array(32).fill(0);
 
       // return count of pairs
       // whose bitwise xor >= bitwise
       function valid_pairs(arr, N)
       {
           for (let i = 0; i < N; i++)
           {
            
               // index of MSB in arr[i]
               let MSB_index = Math.floor(Math.log2(arr[i]));
               MSB[MSB_index]++;
           }
 
           let tot = Math.floor((N * (N - 1)) / 2);
           let invalid = 0;
 
           // pairs are invalid if
           // their MSB index  are same
           for (let i = 0; i < 32; i++)
               invalid += Math.floor(((MSB[i] * 1 * (MSB[i] - 1)) / 2));
 
           let valid = tot - invalid;
           return valid;
       }
 
       // Driver Code
       let N = 3;
       let arr = [12, 4, 15]
       document.write(valid_pairs(arr, N));
 
   // This code is contributed by Potta Lokesh
   </script>


Output

2

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads