Count of pairs in Array such that bitwise AND of XOR of pair and X is 0
Last Updated :
28 Jul, 2021
Given an array arr[] consisting of N positive integers and a positive integer X, the task is to find the number of pairs (i, j) such that i < j and (arr[i]^arr[j] )&X is 0.
Examples:
Input: arr[] = {1, 3, 4, 2}, X = 2
Output: 2
Explanation:
Following are the possible pairs from the given array:
- (0, 2): The value of (arr[0]^arr[2])&X is (1^4)&2 = 0.
- (1, 3): The value of (arr[1]^arr[3])&X is (3^2)&2 = 0.
Therefore, the total count of pairs is 2.
Input: arr[] = {3, 2, 5, 4, 6, 7}, X = 6
Output: 3
Naive Approach: The simple approach to solve the given problem is to generate all possible pairs of the given array and count those pairs (i, j) that satisfy the given criteria i.e., i < j and (arr[i]^arr[j] )&X is 0.. After checking for all the pairs, print the total count obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countOfPairs( int arr[], int N, int X)
{
int count = 0;
for ( int i = 0; i < N - 1; i++) {
for ( int j = i + 1; j < N; j++) {
if (((arr[i] ^ arr[j]) & X) == 0)
count++;
}
}
return count;
}
int main()
{
int arr[] = { 3, 2, 5, 4, 6, 7 };
int X = 6;
int N = sizeof (arr) / sizeof (arr[0]);
cout << countOfPairs(arr, N, X);
return 0;
}
|
Java
class GFG
{
public static int countOfPairs( int arr[], int N, int X)
{
int count = 0 ;
for ( int i = 0 ; i < N - 1 ; i++) {
for ( int j = i + 1 ; j < N; j++) {
if (((arr[i] ^ arr[j]) & X) == 0 )
count++;
}
}
return count;
}
public static void main(String args[])
{
int arr[] = { 3 , 2 , 5 , 4 , 6 , 7 };
int X = 6 ;
int N = arr.length;
System.out.println(countOfPairs(arr, N, X));
}
}
|
Python3
def countOfPairs(arr, N, X):
count = 0
for i in range (N - 1 ):
for j in range (i + 1 , N):
if (((arr[i] ^ arr[j]) & X) = = 0 ):
count + = 1
return count
if __name__ = = '__main__' :
arr = [ 3 , 2 , 5 , 4 , 6 , 7 ]
X = 6
N = len (arr)
print (countOfPairs(arr, N, X))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int countOfPairs( int []arr, int N, int X)
{
int count = 0;
for ( int i = 0; i < N - 1; i++)
{
for ( int j = i + 1; j < N; j++)
{
if (((arr[i] ^ arr[j]) & X) == 0)
count++;
}
}
return count;
}
public static void Main()
{
int []arr = { 3, 2, 5, 4, 6, 7 };
int X = 6;
int N = arr.Length;
Console.Write(countOfPairs(arr, N, X));
}
}
|
Javascript
<script>
function countOfPairs(arr, N, X)
{
let count = 0;
for (let i = 0; i < N - 1; i++)
{
for (let j = i + 1; j < N; j++)
{
if (((arr[i] ^ arr[j]) & X) == 0)
count++;
}
}
return count;
}
let arr = [3, 2, 5, 4, 6, 7];
let X = 6;
let N = arr.length;
document.write(countOfPairs(arr, N, X));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized by observing the given equation. So, to perform (A[i]^A[j]) & X == 0, then unset bits in the answer of (A[i]^A[j]) at the same position where the X has set bits in its binary representation is required.
For Example, If X = 6 => 110, so to make (answer & X) == 0 where answer = A[i]^A[j], the answer should be 001 or 000. So to get the 0 bit in the answer (in the same position as the set bit the X has), it is required to have the same bit in A[i] and A[j] at that position as the Bitwise XOR of the same bit (1^1 = 0 and 0^0 = 0) gives 0.
By closely looking at the relation between X and A[i] and A[j], it is found that X&A[i] == X&A[j]. Therefore, the idea is to find the frequency of the array elements having value arr[i]&X and any two numbers with the same value can be made as a pair. Follow the steps below to solve the problem:
- Initialize an unordered map, say M to store the count of numbers having a particular value arr[i]^X.
- Iterate over the range [0, N] using the variable i and increase the count of the value of arr[i]&X in the unordered map M.
- Initialize the variable count as 0 to store the resultant count of pairs.
- Iterate over the map M using the variable m and add the value of (m.second)*(m.second – 1)/2 to the variable count.
- After completing the above steps, print the value of count as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countOfPairs( int arr[], int N, int X)
{
int count = 0;
unordered_map< int , int > M;
for ( int i = 0; i < N; i++) {
M[(arr[i] & X)]++;
}
for ( auto m : M) {
int p = m.second;
count += p * (p - 1) / 2;
}
return count;
}
int main()
{
int arr[] = { 3, 2, 5, 4, 6, 7 };
int X = 6;
int N = sizeof (arr) / sizeof (arr[0]);
cout << countOfPairs(arr, N, X);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int countOfPairs( int [] arr, int N, int X)
{
int count = 0 ;
HashMap<Integer,
Integer> M = new HashMap<Integer,
Integer>();
for ( int i = 0 ; i < N; i++)
{
if (M.containsKey(arr[i] & X))
M.put((arr[i] & X),
M.get(arr[i] & X) + 1 );
else
M.put(arr[i] & X, 1 );
}
for (Integer entry : M.keySet())
{
int p = M.get(entry);
count += p * (p - 1 ) / 2 ;
}
return count;
}
public static void main(String[] args)
{
int [] arr = { 3 , 2 , 5 , 4 , 6 , 7 };
int X = 6 ;
int N = arr.length;
System.out.print(countOfPairs(arr, N, X));
}
}
|
Python3
def countOfPairs(arr, N, X):
count = 0
M = dict ()
for i in range ( 0 , N):
k = arr[i] & X
if k in M:
M[k] + = 1
else :
M[k] = 1
for m in M.keys():
p = M[m]
count + = p * (p - 1 ) / / 2
return count
if __name__ = = '__main__' :
arr = [ 3 , 2 , 5 , 4 , 6 , 7 ]
X = 6
N = len (arr)
print (countOfPairs(arr, N, X))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int countOfPairs( int []arr, int N, int X)
{
int count = 0;
Dictionary< int , int > M = new Dictionary< int , int >();
for ( int i = 0; i < N; i++) {
if (M.ContainsKey(arr[i] & X))
M[(arr[i] & X)]++;
else
M.Add(arr[i] & X,1);
}
foreach (KeyValuePair< int , int > entry in M)
{
int p = entry.Value;
count += p * (p - 1) / 2;
}
return count;
}
public static void Main()
{
int []arr = { 3, 2, 5, 4, 6, 7 };
int X = 6;
int N = arr.Length;
Console.Write(countOfPairs(arr, N, X));
}
}
|
Javascript
<script>
function countOfPairs(arr, N, X)
{
let count = 0;
let M = new Map();
for (let i = 0; i < N; i++) {
if (M.has(arr[i] & X)) {
M.set(arr[i] & X, M.get(arr[i] & X) + 1);
} else {
M.set(arr[i] & X, 1);
}
}
for (let m of M) {
let p = m[1];
count += (p * (p - 1)) / 2;
}
return count;
}
let arr = [3, 2, 5, 4, 6, 7];
let X = 6;
let N = arr.length;
document.write(countOfPairs(arr, N, X));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...