Count pairs with Bitwise XOR greater than both the elements of the pair
Last Updated :
05 Jan, 2022
Given an array arr[] of size N, the task is to count the number of pairs whose Bitwise XOR is greater than both the elements in the pair.
Examples:
Input: arr[] = {2, 4, 3}
Output: 2
Explanation: There are only 2 pairs whose Bitwise XOR is greater than both the elements in the pair:
1) (2, 4): Bitwise XOR = 2 ^ 4 = 6, which is greater than both 2 and 4.
2) (4, 3): Bitwise XOR = 4 ^ 3 = 7, which is greater than both 4 and 3.
Input: arr[] = {2, 2, 2}
Output: 0
Explanation:Since all array elements are the same, Bitwise XOR of all possible pairs is 0. Therefore, no such pair exists
Approach: The simplest approach is to generate all possible pairs from the given array and count those pairs whose Bitwise XOR is greater than both the elements. Print the count after checking all the pairs only once.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void countPairs( int A[], int N)
{
int count = 0;
for ( int i = 0; i < N; i++) {
for ( int j = i + 1; j < N; j++) {
int xo = (A[i] ^ A[j]);
int mx = max(A[i], A[j]);
if (xo > mx) {
count++;
}
}
}
cout << count;
}
int main()
{
int arr[] = { 2, 4, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
countPairs(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static void countPairs( int [] A, int N)
{
int count = 0 ;
for ( int i = 0 ; i < N; i++)
{
for ( int j = i + 1 ; j < N; j++)
{
int xo = (A[i] ^ A[j]);
int mx = Math.max(A[i], A[j]);
if (xo > mx)
{
count++;
}
}
}
System.out.println(count);
}
public static void main(String[] args)
{
int [] arr = { 2 , 4 , 3 };
int N = arr.length;
countPairs(arr, N);
}
}
|
Python3
def countPairs(A, N):
count = 0
for i in range ( 0 , N):
for j in range (i + 1 , N):
xo = (A[i] ^ A[j])
mx = max (A[i], A[j])
if (xo > mx):
count + = 1
print (count)
if __name__ = = '__main__' :
arr = [ 2 , 4 , 3 ]
N = len (arr)
countPairs(arr, N)
|
C#
using System;
class GFG{
static void countPairs( int [] A, int N)
{
int count = 0;
for ( int i = 0; i < N; i++)
{
for ( int j = i + 1; j < N; j++)
{
int xo = (A[i] ^ A[j]);
int mx = Math.Max(A[i], A[j]);
if (xo > mx)
{
count++;
}
}
}
Console.WriteLine(count);
}
public static void Main()
{
int [] arr = { 2, 4, 3 };
int N = arr.Length;
countPairs(arr, N);
}
}
|
Javascript
<script>
function countPairs(A , N) {
var count = 0;
for (i = 0; i < N; i++) {
for (j = i + 1; j < N; j++) {
var xo = (A[i] ^ A[j]);
var mx = Math.max(A[i], A[j]);
if (xo > mx) {
count++;
}
}
}
document.write(count);
}
var arr = [ 2, 4, 3 ];
var N = arr.length;
countPairs(arr, N);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to use Bit Manipulation. Consider X = A^B and A < B and Let K be the Most Significant Bit of the number A. Now, If X is greater than both the element A and B if and only if the Kth bit of B is 0. If the Kth bit of an integer is already 0, then count the numbers such that the Kth bit is the MSB bit of the other integer. Below are the steps:
- Initialize a variable count to store the count of pairs and an array say bits[] of size 32 and sort the given array.
- Traverse the array and do the following:
- If the current element is 0 then check for the next element.
- Else traverse all the bits of the current element and if any bit at position j is set then increment the count by bits[j].
- After the above steps, Update the bits[log2(current element)] by 1.
- After the above steps, print the value of the count as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void countPairs( int A[], int N)
{
int count = 0;
sort(A, A + N);
int bits[32] = { 0 };
for ( int i = 0; i < N; i++) {
if (A[i] == 0) {
continue ;
}
for ( int j = 0; j < 32; j++) {
if (!((1LL << j) & A[i])) {
count += bits[j];
}
}
++bits[( int )(log2l(A[i]))];
}
cout << count;
}
int main()
{
int arr[] = { 2, 4, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
countPairs(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static void countPairs( int [] A, int N)
{
int count = 0 ;
Arrays.sort(A);
int [] bits = new int [ 32 ];
for ( int i = 0 ; i < N; i++)
{
if (A[i] == 0 )
{
continue ;
}
for ( int j = 0 ; j < 32 ; j++)
{
if ((( 1 << j) & A[i]) == 0 )
{
count += bits[j];
}
}
++bits[( int )(( int )(Math.log(A[i]) /
Math.log( 2 )))];
}
System.out.println(count);
}
public static void main(String[] args)
{
int [] arr = { 2 , 4 , 3 };
int N = arr.length;
countPairs(arr, N);
}
}
|
Python3
import math
def countPairs(A, N):
count = 0
A.sort()
bits = [ 0 ] * 32
for i in range ( 0 , N):
if (A[i] = = 0 ):
continue
for j in range ( 0 , 32 ):
if ((( 1 << j) & A[i]) = = 0 ):
count + = bits[j]
bits[( int )(math.log(A[i], 2 ))] + = 1
print (count)
if __name__ = = '__main__' :
arr = [ 2 , 4 , 3 ]
N = len (arr)
countPairs(arr, N)
|
C#
using System;
class GFG{
static void countPairs( int [] A, int N)
{
int count = 0;
Array.Sort(A);
int [] bits = new int [32];
for ( int i = 0; i < N; i++)
{
if (A[i] == 0)
{
continue ;
}
for ( int j = 0; j < 32; j++)
{
if (((1 << j) & A[i]) == 0)
{
count += bits[j];
}
}
++bits[( int )(( int )(Math.Log(A[i]) /
Math.Log(2)))];
}
Console.WriteLine(count);
}
public static void Main()
{
int [] arr = { 2, 4, 3 };
int N = arr.Length;
countPairs(arr, N);
}
}
|
Javascript
<script>
function countPairs(A , N) {
var count = 0;
A.sort();
var bits = Array(32).fill(0);
for (i = 0; i < N; i++) {
if (A[i] == 0) {
continue ;
}
for (j = 0; j < 32; j++) {
if (((1 << j) & A[i]) == 0) {
count += bits[j];
}
}
bits[parseInt(Math.log(A[i]) / Math.log(2))]++;
}
document.write(count);
}
var arr = [ 2, 4, 3 ];
var N = arr.length;
countPairs(arr, N);
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...