Count pairs whose Bitwise AND exceeds Bitwise XOR from a given array
Given an array arr[] of size N, the task is to count the number of pairs from the given array such that the Bitwise AND (&) of each pair is greater than its Bitwise XOR(^).
Examples :
Input: arr[] = {1, 2, 3, 4}
Output:1
Explanation:
Pairs that satisfy the given conditions are:
(2 & 3) > (2 ^ 3)
Therefore, the required output is 1.
Input: arr[] = {1, 4, 3, 7}
Output: 1
Explanation:
Pairs that satisfy the given conditions are:
(4 & 7) > (4 ^ 7)
Therefore, the required output is 1.
Naive Approach: The simplest approach to solve the problem is to traverse the array and generate all possible pairs from the given array. For each pair, check if its Bitwise AND( & ) is greater than its Bitwise XOR( ^ ) or not. If found to be true, then increment the count of pairs by 1. Finally, print the count of such pairs obtained.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient approach: To optimize the above approach, follow the properties of the Bitwise Operators:
1 ^ 0 = 1
0 ^ 1 = 1
1 & 1 = 1
X = b31b30…..b1b0
Y = a31b30….a1a0
If the expression {(X & Y) > (X ^ Y)} is true, then the Most Significant Bit (MSB) of both X and Y must be equal.
Total count of pairs that satisfying the condition {(X & Y) > (X ^ Y)} is equal to:
Follow the steps below to solve the problem:
- Initialize a variable, say res, to store the count of pairs that satisfy the given condition.
- Traverse the given array arr[].
- Store the positions of the Most Significant Bit (MSB) of each element of the given array.
- Initialize an array bits[], of size 32 (max no of bits)
- Iterate over each array element and perform the following steps:
- Find the Most Significant Bit (MSB) of the current array element, say j.
- Add the value stored in bits[j] to the answer.
- Increase the value of bits[j] by 1.
Below is the implementation of the above approach
C++
#include <bits/stdc++.h>
using namespace std;
int cntPairs( int arr[], int N)
{
int res = 0;
int bit[32] = { 0 };
for ( int i = 0; i < N; i++) {
int pos = log2(arr[i]);
bit[pos]++;
}
for ( int i = 0; i < 32; i++) {
res += (bit[i] * (bit[i] - 1)) / 2;
}
return res;
}
int main()
{
int arr[] = { 1, 2, 3, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << cntPairs(arr, N);
}
|
Java
import java.io.*;
class GFG{
static int cntPairs( int arr[], int N)
{
int res = 0 ;
int bit[] = new int [ 32 ];
for ( int i = 0 ; i < N; i++)
{
int pos = ( int )(Math.log(arr[i]) / Math.log( 2 ));
bit[pos]++;
}
for ( int i = 0 ; i < 32 ; i++)
{
res += (bit[i] * (bit[i] - 1 )) / 2 ;
}
return res;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 };
int N = arr.length;
System.out.println(cntPairs(arr, N));
}
}
|
Python3
import math
def cntPairs(arr, N):
res = 0
bit = [ 0 ] * 32
for i in range (N):
pos = ( int )(math.log2(arr[i]))
bit[pos] + = 1
for i in range ( 32 ):
res + = (bit[i] * (bit[i] - 1 )) / / 2
return res
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 4 ]
N = len (arr)
print (cntPairs(arr, N))
|
C#
using System;
class GFG{
static int cntPairs( int [] arr, int N)
{
int res = 0;
int [] bit = new int [32];
for ( int i = 0; i < N; i++)
{
int pos = ( int )(Math.Log(arr[i]) / Math.Log(2));
bit[pos]++;
}
for ( int i = 0; i < 32; i++)
{
res += (bit[i] * (bit[i] - 1)) / 2;
}
return res;
}
static public void Main ()
{
int [] arr = { 1, 2, 3, 4 };
int N = arr.Length;
Console.Write(cntPairs(arr, N));
}
}
|
Javascript
<script>
function cntPairs(arr, N)
{
var res = 0;
var bit = Array(32).fill(0);
var i;
for ( i = 0; i < N; i++) {
var pos = Math.ceil(Math.log2(arr[i]));
bit[pos] += 1;
}
for (i = 0; i < 32; i++) {
res += Math.ceil((bit[i] * (bit[i] - 1)) / 2);
}
return res;
}
arr = [1, 2, 3, 4];
N = arr.length;
document.write(cntPairs(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
29 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...