Count pairs with bitwise XOR exceeding bitwise AND from a given array
Last Updated :
07 Feb, 2023
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 less than its bitwise XOR(^).
Examples:
Input: arr[] = {1, 2, 3, 4, 5}
Output: 8
Explanation:
Pairs that satisfy the given conditions are:
(1 & 2) < (1 ^ 2)
(1 & 3) < (1 ^ 3)
(1 & 4) < (1 ^ 4)
(1 & 5) < (1 ^ 5)
(2 & 4) < (2 ^ 4)
(2 & 5) < (2 ^ 5)
(3 & 4) < (3 ^ 4)
(3 & 5) < (3 ^ 5)
Therefore, the required output is 8.
Input: arr[] = {1, 4, 3, 7, 10}
Output: 9
Approach: The simplest approach is to traverse the array and generate all possible pairs from the given array. For each pair, check if its bitwise AND(&) is less than the bitwise XOR(^) of that pair 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 satisfy the condition{(X & Y) > (X ^ Y)} are:
bit[i] stores the count of array elements whose position of most significant bit(MSB) is i.
Therefore, total count of pairs that satisfy the given condition{(X & Y) < (X ^ Y)}
= [{N * (N – 1) /2} – {
}]
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.
- Store the position of most significant bit of each element of the given array.
- Finally, evaluate the result by the above mentioned formula and print the result.
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;
}
res = (N * (N - 1)) / 2 - res;
return res;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
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 ;
}
res = (N * (N - 1 )) / 2 - res;
return res;
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 , 4 , 5 , 6 };
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 ( 0 , N):
pos = int (math.log(arr[i], 2 ))
bit[pos] = bit[pos] + 1
for i in range ( 0 , 32 ):
res = res + int ((bit[i] *
(bit[i] - 1 )) / 2 )
res = int ((N * (N - 1 )) / 2 - res)
return res
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 4 , 5 , 6 ]
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;
}
res = (N * (N - 1)) / 2 - res;
return res;
}
public static void Main()
{
int [] arr = { 1, 2, 3, 4, 5, 6 };
int N = arr.Length;
Console.Write(cntPairs(arr, N));
}
}
|
Javascript
<script>
function cntPairs(arr, N)
{
let res = 0;
let bit = new Array(32).fill(0);
for (let i = 0; i < N; i++)
{
let pos = parseInt(Math.log(arr[i]) /
Math.log(2));;
bit[pos]++;
}
for (let i = 0; i < 32; i++)
{
res += parseInt((bit[i]
* (bit[i] - 1)) / 2);
}
res = parseInt((N * (N - 1)) / 2) - res;
return res;
}
let arr = [ 1, 2, 3, 4, 5, 6 ];
let N = arr.length;
document.write(cntPairs(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Method 2 : Bitwise and is greater than bitwise xor if and only if most significant bit is equal.
- Create a bits[] array of size 32 (max no of bits)
- Initialize ans to 0.
- We will traverse the array from the start and for each number,
- Find its most significant bit and say it is j.
- Add the value stored in bits[j] array to the ans. (for the current element bits[j] number of pairs can be formed)
- Now increase the value of bits[j] by 1.
- Now total number of pairs = n*(n-1)/2. Subtract the ans from it.
C++
#include <bits/stdc++.h>
using namespace std;
int findCount( int arr[], int N)
{
int ans = 0;
int bits[32] = { 0 };
for ( int i = 0; i < N; i++) {
int val = log2l(arr[i]);
ans += bits[val];
bits[val]++;
}
return N * (N - 1) / 2 - ans;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << findCount(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static int findCount( int arr[], int N)
{
int ans = 0 ;
int bits[] = new int [ 32 ];
for ( int i = 0 ; i < N; i++)
{
int val = ( int )(Math.log(arr[i]) /
Math.log( 2 ));
ans += bits[val];
bits[val]++;
}
return N * (N - 1 ) / 2 - ans;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 , 5 , 6 };
int N = arr.length;
System.out.println(findCount(arr, N));
}
}
|
Python3
import math
def findCount(arr, N):
ans = 0
bits = [ 0 ] * 32
for i in range (N):
val = int (math.log2(arr[i]))
ans + = bits[val]
bits[val] + = 1
return (N * (N - 1 ) / / 2 - ans)
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 4 , 5 , 6 ]
N = len (arr)
print (findCount(arr, N))
|
C#
using System;
class GFG{
static int findCount( int [] arr, int N)
{
int ans = 0;
int [] bits = new int [32];
for ( int i = 0; i < N; i++)
{
int val = ( int )(Math.Log(arr[i]) /
Math.Log(2));
ans += bits[val];
bits[val]++;
}
return N * (N - 1) / 2 - ans;
}
public static void Main()
{
int [] arr = { 1, 2, 3, 4, 5, 6 };
int N = arr.Length;
Console.Write(findCount(arr, N));
}
}
|
Javascript
<script>
function findCount(arr, N)
{
let ans = 0;
let bits = new Array(32).fill(0);
for (let i = 0; i < N; i++)
{
let val = parseInt(Math.log(arr[i]) /
Math.log(2));
ans += bits[val];
bits[val]++;
}
return parseInt(N * (N - 1) / 2) - ans;
}
let arr = [ 1, 2, 3, 4, 5, 6 ];
let N = arr.length;
document.write(findCount(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...