Count pairs with equal Bitwise AND and Bitwise OR value
Last Updated :
20 May, 2021
Given an array, arr[] of size N, the task is to count the number of unordered pairs such that Bitwise AND and Bitwise OR of each pair is equal.
Examples:
Input: arr[] = {1, 2, 1}
Output: 1
Explanation:
Bitwise AND value and Bitwise OR value all possible pairs are:
Bitwise AND of the pair(arr[0], arr[1]) is (arr[0] & arr[1]) = (1 & 2) = 0
Bitwise AND of the pair(arr[0], arr[1]) is (arr[0] | arr[1]) = (1 | 2) = 3
Bitwise AND of the pair(arr[0], arr[2]) is (arr[0] & arr[2]) = (1 & 2) = 1
Bitwise AND of the pair(arr[0], arr[1]) is (arr[0] | arr[1]) = (1 | 2) = 1
Bitwise AND of the pair(arr[1], arr[2]) is (arr[1] & arr[2]) = (2 & 1) = 0
Bitwise AND of the pair(arr[0], arr[1]) is arr[0] | arr[1] = (2 | 1) = 3
Therefore, the required output is 1.
Input: arr[] = {1, 2, 3, 1, 2, 2}
Output: 4
Naive Approach: The simplest approach to solve the problem is to traverse the array and generate all possible pairs of the given array. For each pair, check if Bitwise And of the pair is equal to Bitwise OR of that pair or not. If found to be true, then increment the counter. Finally, print the value of the counter.
Efficient Approach: To optimize the above approach the idea is based on the following observations:
0 & 0 = 0 and 0 | 0 = 0
0 & 1 = 0 and 0 | 1 = 1
1 & 0 = 0 and 1 | 0 = 1
1 & 1 = 1 and 1 | 1 = 1
Therefore, If both the elements of a pair are equal, only then, bitwise AND(&) and Bitwise OR(|) of the pair becomes equal.
Follow the steps below to solve the problem:
- Initialize a variable, say cntPairs to store the count of pairs whose Bitwise AND(&) value and Bitwise OR(|) value is equal.
- Create a map, say mp to store the frequency of all distinct elements of the given array.
- Traverse the given array and store the frequency of all distinct elements of the given array in mp.
- Traverse map and check if frequency, say freq is greater than 1 then update cntPairs += (freq * (freq - 1)) / 2.
- Finally, print the value of cntPairs.
Below is the implementation of the above approach:
C++
// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to count pairs in an array
// whose bitwise AND equal to bitwise OR
int countPairs(int arr[], int N)
{
// Store count of pairs whose
// bitwise AND equal to bitwise OR
int cntPairs = 0;
// Stores frequency of
// distinct elements of array
map<int, int> mp;
// Traverse the array
for (int i = 0; i < N; i++) {
// Increment the frequency
// of arr[i]
mp[arr[i]]++;
}
// Traverse map
for (auto freq: mp) {
cntPairs += (freq.second *
(freq.second - 1)) / 2;
}
return cntPairs;
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 3, 1, 2, 2 };
int N = sizeof(arr) / sizeof(arr[0]);
cout<<countPairs(arr, N);
}
Java
// Java program to implement
// the above approach
import java.io.*;
import java.util.*;
class GFG{
// Function to count pairs in an array
// whose bitwise AND equal to bitwise OR
static int countPairs(int[] arr, int N)
{
// Store count of pairs whose
// bitwise AND equal to bitwise OR
int cntPairs = 0;
// Stores frequency of
// distinct elements of array
HashMap<Integer, Integer> mp = new HashMap<>();
// Traverse the array
for(int i = 0; i < N; i++)
{
// Increment the frequency
// of arr[i]
mp.put(arr[i],
mp.getOrDefault(arr[i], 0) + 1);
}
// Traverse map
for(Map.Entry<Integer, Integer> freq : mp.entrySet())
{
cntPairs += (freq.getValue() *
(freq.getValue() - 1)) / 2;
}
return cntPairs;
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 1, 2, 3, 1, 2, 2 };
int N = arr.length;
System.out.println(countPairs(arr, N));
}
}
// This code is contributed by akhilsaini
Python3
# Python3 program to implement
# the above approach
# Function to count pairs in an array
# whose bitwise AND equal to bitwise OR
def countPairs(arr, N):
# Store count of pairs whose
# bitwise AND equal to bitwise OR
cntPairs = 0
# Stores frequency of
# distinct elements of array
mp = {}
# Traverse the array
for i in range(0, N):
# Increment the frequency
# of arr[i]
if arr[i] in mp:
mp[arr[i]] = mp[arr[i]] + 1
else:
mp[arr[i]] = 1
# Traverse map
for freq in mp:
cntPairs += int((mp[freq] *
(mp[freq] - 1)) / 2)
return cntPairs
# Driver Code
if __name__ == "__main__":
arr = [ 1, 2, 3, 1, 2, 2 ]
N = len(arr)
print(countPairs(arr, N))
# This code is contributed by akhilsaini
C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to count pairs in an array
// whose bitwise AND equal to bitwise OR
static int countPairs(int[] arr, int N)
{
// Store count of pairs whose
// bitwise AND equal to bitwise OR
int cntPairs = 0;
// Stores frequency of
// distinct elements of array
Dictionary<int,
int> mp = new Dictionary<int,
int>();
// Traverse the array
for(int i = 0; i < N; i++)
{
// Increment the frequency
// of arr[i]
if (!mp.ContainsKey(arr[i]))
mp.Add(arr[i], 1);
else
mp[arr[i]] = mp[arr[i]] + 1;
}
// Traverse map
foreach(KeyValuePair<int, int> freq in mp)
{
cntPairs += (freq.Value *
(freq.Value - 1)) / 2;
}
return cntPairs;
}
// Driver Code
public static void Main()
{
int[] arr = { 1, 2, 3, 1, 2, 2 };
int N = arr.Length;
Console.WriteLine(countPairs(arr, N));
}
}
// This code is contributed by akhilsaini
JavaScript
<script>
// JavaScript program to implement
// the above approach
// Function to count pairs in an array
// whose bitwise AND equal to bitwise OR
function countPairs(arr, N)
{
// Store count of pairs whose
// bitwise AND equal to bitwise OR
var cntPairs = 0;
// Stores frequency of
// distinct elements of array
var mp = new Map();
// Traverse the array
for (var i = 0; i < N; i++) {
// Increment the frequency
// of arr[i]
if(mp.has(arr[i]))
mp.set(arr[i], mp.get(arr[i])+1)
else
mp.set(arr[i], 1);
}
// Traverse map
mp.forEach((value, key) => {
cntPairs += parseInt((value *
(value - 1)) / 2);
});
return cntPairs;
}
// Driver Code
var arr = [1, 2, 3, 1, 2, 2 ];
var N = arr.length;
document.write( countPairs(arr, N));
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Count Number of Pairs where Bitwise AND and Bitwise XOR is Equal 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. Example: Input: N = 3, arr[] = {0,0,1}Output: 1Explanation: There is only one pair arr[0] and arr[1] as 0&0=0 and 0^0=0 Input: N = 4, arr[] = {1, 2, 4, 8}Output: 0Explanati
4 min read
Non-negative pairs with sum of Bitwise OR and Bitwise AND equal to N Given an integer N, the task is to find all non-negative pairs (A, B) such that the sum of Bitwise OR and Bitwise AND of A, B is equal to N, i.e., (A | B) + (A & B) = N. Examples: Input: N = 5Output: (0, 5), (1, 4), (2, 3), (3, 2), (4, 1), (5, 0)Explanation: All possible pairs satisfying the nec
5 min read
Count values whose Bitwise OR with A is equal to B Given two integers A and B, the task is to count possible values of X that satisfies the condition A | X = B. Note: | represents Bitwise OR operation. Examples: Input: A = 2, B = 3Output: 2Explanation: Since, 2 | 1 = 3 and 2 | 3 = 3. Therefore, the possible values of x are 1 and 3. Input: A = 5, B =
6 min read
Count pairs from an array whose Bitwise OR is greater than Bitwise AND Given an array A[] consisting of N integers, the task is to count the number of pairs (i, j) such that i < j, and Bitwise OR of A[i] and A[j] is greater than Bitwise AND of A[i] and A[j]. Examples: Input: A[] = {1, 4, 7}Output: 3Explanation: There are 3 such pairs: (1, 4), (4, 7), (1, 7).1) 1 | 4
10 min read
Count of pairs having bit size at most X and Bitwise OR equal to X Given a number X, calculate number of possible pairs (a, b) such that bitwise or of a and b is equal to X and number of bits in both a and b is less than equal to number of bits in X. Examples: Input: X = 6 Output: 9 Explanation: The possible pairs of (a, b) are (4, 6), (6, 4), (6, 6), (6, 2), (4, 2
4 min read
Count triples with Bitwise AND equal to Zero Given an array of integers A[] consisting of N integers, find the number of triples of indices (i, j, k) such that A[i] & A[j] & A[k] is 0(<0 ? i, j, k ? N and & denotes Bitwise AND operator. Examples: Input: A[]={2, 1, 3}Output: 12Explanation: The following i, j, k triples can be cho
7 min read