Bitwise AND and XOR pair counting
Last Updated :
16 Sep, 2023
Given an integer array arr[] of size N, the task is to count the number of pairs whose BITWISE AND and BITWISE XOR are equal.
Examples:
Input: N = 3, arr[] = [0, 0, 1]
Output: 1
Explanation: All possible pairs from the array are pair = [(0, 0), (0, 1), (1, 0)].
- we can see that pair = (0, 0), 0&0 == 0 and 0^0 == 0 this pair stratified the given condition so, we increase our answer by += 1
- for pair = (0, 1), 0&1 == 0 and 0^1 == 1, we can see that these are not equal we can’t increase our ans.
- we check for last also, in last also they are not equal.
So, our answer is 1. Because only one condition stratified the given condition.
Input: N = 4, arr[] = {1, 2, 4, 8}
Output: 0
Explanation: There are no pairs satisfying the condition.
Approach: This can be solved with the following idea:
The idea behind this approach is that we can use a dictionary to count the frequency of each element in the array. Then, we can iterate through all possible pairs of elements and check if their bitwise XOR is equal to their bitwise AND. If it is, we add the product of their frequencies to a counter. Finally, we return half the value of the counter since each pair is counted twice.
Below are the steps involved in the implementation of the code:
- Create a dictionary freq to keep track of the frequency of each element in the array.
- Iterate through all possible pairs of elements in the dictionary.
- For each pair, check whether their bitwise XOR (^) is equal to their bitwise AND (&).
- If the condition is true, add the product of the frequencies of the two elements in the pair to a counter.
- After iterating through all pairs, return half the value of the counter since each pair is counted twice.
Below is the implementation of the code:
C++
#include<bits/stdc++.h>
using namespace std;
void countPairs( int n, int arr[])
{
unordered_map< int , int > freq;
for ( int i = 0; i < n; i++)
{
freq[arr[i]]++;
}
int ans = 0;
for ( auto it1 = freq.begin(); it1 != freq.end(); it1++)
{
for ( auto it2 = freq.begin(); it2 != freq.end(); it2++)
{
int i = it1->first;
int j = it2->first;
if ((i ^ j) == (i & j))
{
if (i == j)
{
ans += (it1->second) * (it2->second - 1);
}
else
{
ans += (it1->second) * (it2->second);
}
}
}
}
cout << ans / 2 << endl;
}
int main()
{
int n = 3;
int arr[] = {0, 0, 1};
countPairs(n, arr);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void countPairs( int n, int arr[])
{
HashMap<Integer, Integer> freq = new HashMap<Integer, Integer>();
for ( int i = 0 ; i < n; i++)
{
freq.put(arr[i], freq.getOrDefault(arr[i], 0 ) + 1 );
}
int ans = 0 ;
for (Map.Entry<Integer, Integer> it1 : freq.entrySet())
{
for (Map.Entry<Integer, Integer> it2 : freq.entrySet())
{
int i = it1.getKey();
int j = it2.getKey();
if ((i ^ j) == (i & j))
{
if (i == j)
{
ans += (it1.getValue()) * (it2.getValue() - 1 );
}
else
{
ans += (it1.getValue()) * (it2.getValue());
}
}
}
}
System.out.println(ans / 2 );
}
public static void main (String[] args)
{
int n = 3 ;
int arr[] = { 0 , 0 , 1 };
countPairs(n, arr);
}
}
|
Python
def countPairs(n, arr):
freq = {}
for i in arr:
freq[i] = freq.get(i, 0 ) + 1
ans = 0
for i in freq.keys():
for j in freq.keys():
if i ^ j = = i & j:
if i = = j:
ans + = freq[i] * (freq[j] - 1 )
else :
ans + = freq[i] * freq[j]
print (ans / / 2 )
n = 3
arr = [ 0 , 0 , 1 ]
countPairs(n, arr)
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static void CountPairs( int n, int [] arr)
{
Dictionary< int , int > freq
= new Dictionary< int , int >();
for ( int i = 0; i < n; i++) {
if (freq.ContainsKey(arr[i])) {
freq[arr[i]]++;
}
else {
freq[arr[i]] = 1;
}
}
int ans = 0;
foreach ( var kvp1 in freq)
{
foreach ( var kvp2 in freq)
{
int i = kvp1.Key;
int j = kvp2.Key;
if ((i ^ j) == (i & j)) {
if (i == j) {
ans += kvp1.Value
* (kvp2.Value - 1);
}
else {
ans += kvp1.Value * kvp2.Value;
}
}
}
}
Console.WriteLine(ans / 2);
}
static void Main()
{
int n = 3;
int [] arr = { 0, 0, 1 };
CountPairs(n, arr);
}
}
|
Javascript
<script>
function countPairs(n, arr) {
const freq = new Map();
for (let i = 0; i < n; i++) {
if (!freq.has(arr[i])) {
freq.set(arr[i], 1);
} else {
freq.set(arr[i], freq.get(arr[i]) + 1);
}
}
let ans = 0;
for (const [i, freq1] of freq) {
for (const [j, freq2] of freq) {
if ((i ^ j) === (i & j)) {
if (i === j) {
ans += freq1 * (freq2 - 1);
} else {
ans += freq1 * freq2;
}
}
}
}
document.write(Math.floor(ans / 2));
}
const n = 3;
const arr = [0, 0, 1];
countPairs(n, arr);
</script>
|
Time complexity: O(n2)
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...