Count pairs from an array whose Bitwise OR is greater than Bitwise AND
Last Updated :
29 Jan, 2022
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: 3
Explanation:
There are 3 such pairs: (1, 4), (4, 7), (1, 7).
1) 1 | 4 (= 5) > 1 & 4 (= 0)
2) 4 | 7 (= 7) > 4 & 7 (= 4)
3) 1 | 7 (= 7) > 1 & 7 (= 1)
Input: A[] = {3, 3, 3}
Output: 0
Explanation: There exist no such pair.
Naive Approach: The simplest approach is to generate all possible pairs from the given array and for every pair (i, j), if it satisfies the given conditions, then increment the count by 1. After checking for all the pairs, print the value of count as the result.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to count the number of
// pairs (i, j) their Bitwise OR is
// greater than Bitwise AND
void countPairs(int A[], int n)
{
// Store the required answer
int count = 0;
// Check for all possible pairs
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++)
// If the condition satisfy
// then increment count by 1
if ((A[i] | A[j])
> (A[i] & A[j])) {
count++;
}
}
// Print the answer
cout << count;
}
// Driver Code
int main()
{
int A[] = { 1, 4, 7 };
int N = sizeof(A) / sizeof(A[0]);
// Function Call
countPairs(A, N);
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to count the number of
// pairs (i, j) their Bitwise OR is
// greater than Bitwise AND
static void countPairs(int A[], int n)
{
// Store the required answer
int count = 0;
// Check for all possible pairs
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++)
// If the condition satisfy
// then increment count by 1
if ((A[i] | A[j])
> (A[i] & A[j])) {
count++;
}
}
// Print the answer
System.out.println(count);
}
// Driver Code
public static void main(String args[])
{
int A[] = { 1, 4, 7 };
int N = A.length;
// Function Call
countPairs(A, N);
}
}
// This code is contributed by splevel62.
Python3
# Python program to implement
# the above approach
# Function to count the number of
# pairs (i, j) their Bitwise OR is
# greater than Bitwise AND
def countPairs(A, n):
# Store the required answer
count = 0;
# Check for all possible pairs
for i in range(n):
for j in range(i + 1, n):
# If the condition satisfy
# then increment count by 1
if ((A[i] | A[j]) > (A[i] & A[j])):
count += 1;
# Print the answer
print(count);
# Driver Code
if __name__ == '__main__':
A = [1, 4, 7];
N = len(A);
# Function Call
countPairs(A, N);
# This code is contributed by 29AjayKumar
C#
// C# program to implement
// the above approach
using System;
class GFG
{
// Function to count the number of
// pairs (i, j) their Bitwise OR is
// greater than Bitwise AND
static void countPairs(int[] A, int n)
{
// Store the required answer
int count = 0;
// Check for all possible pairs
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++)
// If the condition satisfy
// then increment count by 1
if ((A[i] | A[j])
> (A[i] & A[j])) {
count++;
}
}
// Print the answer
Console.Write(count);
}
// Driver Code
public static void Main()
{
int[] A = { 1, 4, 7 };
int N = A.Length;
// Function Call
countPairs(A, N);
}
}
// This code is contributed by susmitakundugoaldanga.
JavaScript
<script>
// Javascript program for the above approach
// Function to count the number of
// pairs (i, j) their Bitwise OR is
// greater than Bitwise AND
function countPairs( A, n)
{
// Store the required answer
var count = 0;
// Check for all possible pairs
for (var i = 0; i < n; i++) {
for (var j = i + 1; j < n; j++)
// If the condition satisfy
// then increment count by 1
if ((A[i] | A[j])
> (A[i] & A[j])) {
count++;
}
}
// Print the answer
document.write( count);
}
// Driver Code
var A = [ 1, 4, 7 ];
var N = A.length;
// Function Call
countPairs(A, N);
</script>
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is based on the observation that the condition Ai | Aj > Ai & Aj is satisfied only when Ai and Aj are distinct. If Ai is equal to Aj, then Ai | Aj = Ai & Aj. The condition Ai | Aj < Ai & Aj is never met. Hence, the problem can be solved by subtracting the number of pairs having an equal value from the total number of pairs possible. Follow the steps below to solve the problem:
- Initialize a variable, say count, with N * (N - 1) / 2 that denotes the total number of possible pairs.
- Initialize a hashmap, say M to store the frequency of each array element.
- Traverse the array arr[] and for each array element arr[i], increment the frequency of arr[i] in M by 1.
- Now, traverse the hashmap M and for each key K, and its corresponding value X, subtract the value of (X * (X - 1))/2 from the count.
- After completing the above steps, print the value of count as the result.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to count the number of
// pairs (i, j) their Bitwise OR is
// greater than Bitwise AND
void countPairs(int A[], int n)
{
// Total number of pairs
// possible from the array
long long count = (n * (n - 1)) / 2;
// Stores frequency of each array element
unordered_map<long long, long long> ump;
// Traverse the array A[]
for (int i = 0; i < n; i++) {
// Increment ump[A[i]] by 1
ump[A[i]]++;
}
// Traverse the Hashmap ump
for (auto it = ump.begin();
it != ump.end(); ++it) {
// Subtract those pairs (i, j)
// from count which has the
// same element on index i
// and j (i < j)
long long c = it->second;
count = count - (c * (c - 1)) / 2;
}
// Print the result
cout << count;
}
// Driver Code
int main()
{
int A[] = { 1, 4, 7 };
int N = sizeof(A) / sizeof(A[0]);
// Function Call
countPairs(A, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to count the number of
// pairs (i, j) their Bitwise OR is
// greater than Bitwise AND
static void countPairs(int A[], int n)
{
// Total number of pairs
// possible from the array
int count = (n * (n - 1)) / 2;
// Stores frequency of each array element
Map<Integer,Integer> mp = new HashMap<>();
// Traverse the array A[]
for (int i = 0 ; i < n; i++){
if(mp.containsKey(A[i])){
mp.put(A[i], mp.get(A[i])+1);
}
else{
mp.put(A[i], 1);
}
}
// Traverse the Hashmap ump
for (Map.Entry<Integer,Integer> it : mp.entrySet()){
// Subtract those pairs (i, j)
// from count which has the
// same element on index i
// and j (i < j)
int c = it.getValue();
count = count - (c * (c - 1)) / 2;
}
// Print the result
System.out.print(count);
}
// Driver Code
public static void main(String[] args)
{
int A[] = { 1, 4, 7 };
int N = A.length;
// Function Call
countPairs(A, N);
}
}
// This code is contributed by shikhasingrajput
Python3
# Python 3 program for the above approach
from collections import defaultdict
# Function to count the number of
# pairs (i, j) their Bitwise OR is
# greater than Bitwise AND
def countPairs(A, n):
# Total number of pairs
# possible from the array
count = (n * (n - 1)) // 2
# Stores frequency of each array element
ump = defaultdict(int)
# Traverse the array A[]
for i in range(n):
# Increment ump[A[i]] by 1
ump[A[i]] += 1
# Traverse the Hashmap ump
for it in ump.keys():
# Subtract those pairs (i, j)
# from count which has the
# same element on index i
# and j (i < j)
c = ump[it]
count = count - (c * (c - 1)) // 2
# Print the result
print(count)
# Driver Code
if __name__ == "__main__":
A = [1, 4, 7]
N = len(A)
# Function Call
countPairs(A, N)
# This code is contributed by chitranayal.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to count the number of
// pairs (i, j) their Bitwise OR is
// greater than Bitwise AND
static void countPairs(int[] A, int n)
{
// Total number of pairs
// possible from the array
long count = (n * (n - 1)) / 2;
// Stores frequency of each array element
Dictionary<long, long> ump = new Dictionary<long, long>();
// Traverse the array A[]
for (int i = 0; i < n; i++)
{
// Increment ump[A[i]] by 1
if(ump.ContainsKey(A[i]))
{
ump[A[i]]++;
}
else{
ump[A[i]] = 1;
}
}
// Traverse the Hashmap ump
foreach(KeyValuePair<long, long> it in ump)
{
// Subtract those pairs (i, j)
// from count which has the
// same element on index i
// and j (i < j)
long c = it.Value;
count = count - (c * (c - 1)) / 2;
}
// Print the result
Console.WriteLine(count);
}
// Driver code
static void Main() {
int[] A = { 1, 4, 7 };
int N = A.Length;
// Function Call
countPairs(A, N);
}
}
// This code is contributed by divyeshrabadiya07.
JavaScript
<script>
// Javascript program for the above approach
// Function to count the number of
// pairs (i, j) their Bitwise OR is
// greater than Bitwise AND
function countPairs(A, n)
{
// Total number of pairs
// possible from the array
var count = (n * (n - 1)) / 2;
// Stores frequency of each array element
var ump = new Map();
// Traverse the array A[]
for (var i = 0; i < n; i++)
{
// Increment ump[A[i]] by 1
if(ump.has(A[i]))
{
ump.set(A[i], ump.get(A[i])+1);
}
else{
ump.set(A[i], 1);
}
}
// Traverse the Hashmap ump
for(var it of ump)
{
// Subtract those pairs (i, j)
// from count which has the
// same element on index i
// and j (i < j)
var c = it[1];
count = count - (c * (c - 1)) / 2;
}
// Print the result
document.write(count + "<br>");
}
// Driver code
var A = [1, 4, 7];
var N = A.length;
// Function Call
countPairs(A, N);
// This code is contributed by rutvik_56.
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Check if any Array pair has bitwise XOR greater than bitwise AND
Given an array arr[] of size N, the task is to find if there exists a pair in the array, such that their bitwise XOR is greater than their bitwise AND i.e. arr[i] â arr[j] > arr[i] & arr[j], (0 ⤠i < j ⤠N-1) where â represents the Bitwise XOR operator and & represents bitwise AND oper
9 min read
Count pairs with bitwise XOR exceeding bitwise AND from a given array
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: 8Explanation: Pairs that satisfy the given conditions are: (1 & 2) < (
10 min read
Count of pairs with bitwise XOR value greater than its bitwise AND value
Given an array arr that contains N positive Integers. Find the count of all possible pairs whose bitwise XOR value is greater than bitwise AND value Examples: Input : arr[]={ 12, 4, 15}Output: 2Explanation: 12 ^ 4 = 8, 12 & 4 = 4. so 12 ^ 4 > 12 & 4 4 ^ 15 = 11, 4 & 15 = 4. so 4 ^ 15
4 min read
Count pairs having bitwise XOR greater than K from a given array
Given an array arr[]of size N and an integer K, the task is to count the number of pairs from the given array such that the Bitwise XOR of each pair is greater than K. Examples: Input: arr = {1, 2, 3, 5} , K = 2 Output: 4 Explanation: Bitwise XOR of all possible pairs that satisfy the given conditio
15+ min read
Count of pairs with bitwise XOR value greater than its bitwise AND value | Set 2
Given an array arr that contains N positive Integers. Find the count of all possible pairs whose bit wise XOR value is greater than bit wise AND value Examples: Input : arr[]={ 12, 4 , 15}Output: 2Explanation: 12 ^ 4 = 8 , 12 & 4 = 4 . so 12 ^ 4 > 12 & 4 4 ^ 15 = 11, 4 & 15 = 4. so 4
6 min read
Bitwise XOR of Bitwise AND of all pairs from two given arrays
Given two arrays arr1[] and arr2[] consisting of N and M integers respectively, the task is to print the Bitwise XOR of Bitwise AND of all pairs possible by selecting an element from arr1[] and arr2[]. Examples: Input: arr1[] = {1, 2, 3}, arr2[] = {6, 5}Output: 0Explanation: Bitwise AND of the pair
10 min read
Count pairs with equal Bitwise AND and Bitwise OR value
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
6 min read
Find pair whose bitwise OR and bitwise AND follows the given condition
Given 2 arrays arr1[] and arr2[], of size N and M respectively, the task is to find a pair of value (say a and b) such that the following conditions are satisfied: (a | b) ? arr1[i] for all values of i in the range [0, N-1].(a & b) ? arr2[j] for all values of j in the range [0, M-1]. Examples: I
7 min read
Count pair of nodes with greater Bitwise AND than Bitwise XOR in given Linked List
Given a singly linked list, the task is to Count the pairs of nodes with greater Bitwise AND than Bitwise XOR. Examples: Input: list: 1->4->2->6->3Output: 2Explanation: 1st List Node Pair: (4, 6 ), Bitwise AND = 4, Bitwise XOR = 22nd List Node Pair: (2, 3), Bitwise AND = 2, Bitwise XOR =
13 min read
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