Count of pairs with bitwise XOR value greater than its bitwise AND value | Set 2
Last Updated :
21 Apr, 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 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++
#include <bits/stdc++.h>
using namespace std;
int MSB[32];
long long valid_pairs( int arr[], int N)
{
for ( int i = 0; i < N; i++) {
int MSB_index = log2(arr[i]);
MSB[MSB_index]++;
}
long long tot = (N * (N - 1)) / 2;
long long invalid = 0;
for ( int i = 0; i < 32; i++)
invalid += ((MSB[i] * 1LL * (MSB[i] - 1)) / 2);
long long valid = tot - invalid;
return valid;
}
int main()
{
int N = 3;
int arr[] = { 12, 4, 15 };
cout << valid_pairs(arr, N);
}
|
Java
import java.io.*;
import java.lang.*;
class GFG
{
public static int log2( int N)
{
int result = ( int )(Math.log(N) / Math.log( 2 ));
return result;
}
static int MSB[] = new int [ 32 ];
public static long valid_pairs( int arr[], int N)
{
for ( int i = 0 ; i < N; i++) {
int MSB_index = log2(arr[i]);
MSB[MSB_index]++;
}
long tot = (N * (N - 1 )) / 2 ;
long invalid = 0 ;
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));
}
}
|
Python3
import math
MSB = [ 0 for _ in range ( 32 )]
def valid_pairs(arr, N):
for i in range ( 0 , N):
MSB_index = int (math.log2(arr[i]))
MSB[MSB_index] + = 1
tot = (N * (N - 1 )) / / 2
invalid = 0
for i in range ( 0 , 32 ):
invalid + = ((MSB[i] * (MSB[i] - 1 )) / / 2 )
valid = tot - invalid
return valid
if __name__ = = "__main__" :
N = 3
arr = [ 12 , 4 , 15 ]
print (valid_pairs(arr, N))
|
C#
using System;
public class GFG{
public static int log2( int N)
{
int result = ( int )(Math.Log(N) / Math.Log(2));
return result;
}
static int [] MSB = new int [32];
public static long valid_pairs( int [] arr, int N)
{
for ( int i = 0; i < N; i++) {
int MSB_index = log2(arr[i]);
MSB[MSB_index]++;
}
long tot = (N * (N - 1)) / 2;
long invalid = 0;
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));
}
}
|
Javascript
<script>
let MSB = new Array(32).fill(0);
function valid_pairs(arr, N)
{
for (let i = 0; i < N; i++)
{
let MSB_index = Math.floor(Math.log2(arr[i]));
MSB[MSB_index]++;
}
let tot = Math.floor((N * (N - 1)) / 2);
let invalid = 0;
for (let i = 0; i < 32; i++)
invalid += Math.floor(((MSB[i] * 1 * (MSB[i] - 1)) / 2));
let valid = tot - invalid;
return valid;
}
let N = 3;
let arr = [12, 4, 15]
document.write(valid_pairs(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...