Count pairs whose Bitwise AND exceeds Bitwise XOR from a given array
Last Updated :
29 Mar, 2022
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 greater than its Bitwise XOR(^).
Examples :
Input: arr[] = {1, 2, 3, 4}
Output:1
Explanation:
Pairs that satisfy the given conditions are:
(2 & 3) > (2 ^ 3)
Therefore, the required output is 1.
Input: arr[] = {1, 4, 3, 7}
Output: 1
Explanation:
Pairs that satisfy the given conditions are:
(4 & 7) > (4 ^ 7)
Therefore, the required output is 1.
Naive Approach: The simplest approach to solve the problem is to traverse the array and generate all possible pairs from the given array. For each pair, check if its Bitwise AND( & ) is greater than its Bitwise XOR( ^ ) or not. If found to be true, then increment the count of pairs by 1. Finally, print the count of such pairs obtained.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient approach: To optimize the above approach, follow the properties of the Bitwise Operators:
1 ^ 0 = 1
0 ^ 1 = 1
1 & 1 = 1
X = b31b30…..b1b0
Y = a31b30….a1a0
If the expression {(X & Y) > (X ^ Y)} is true, then the Most Significant Bit (MSB) of both X and Y must be equal.
Total count of pairs that satisfying the condition {(X & Y) > (X ^ Y)} is equal to:
\Sigma_{n=0}^{31}(^{bit[i]}_{\ \ \ 2})
Follow the steps below to solve the problem:
- Initialize a variable, say res, to store the count of pairs that satisfy the given condition.
- Traverse the given array arr[].
- Store the positions of the Most Significant Bit (MSB) of each element of the given array.
- Initialize an array bits[], of size 32 (max no of bits)
- Iterate over each array element and perform the following steps:
- Find the Most Significant Bit (MSB) of the current array element, say j.
- Add the value stored in bits[j] to the answer.
- Increase the value of bits[j] by 1.
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 that
// satisfy the above condition
int cntPairs(int arr[], int N)
{
// Stores the count of pairs
int res = 0;
// Stores the count of array
// elements having same
// positions of MSB
int bit[32] = { 0 };
// Traverse the array
for (int i = 0; i < N; i++) {
// Stores the index of
// MSB of array elements
int pos = log2(arr[i]);
bit[pos]++;
}
// Calculate number of pairs
for (int i = 0; i < 32; i++) {
res += (bit[i] * (bit[i] - 1)) / 2;
}
return res;
}
// Driver Code
int main()
{
// Given Input
int arr[] = { 1, 2, 3, 4 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function call to count pairs
// satisfying the given condition
cout << cntPairs(arr, N);
}
Java
// Java program to implement
// the above approach
import java.io.*;
class GFG{
// Function to count pairs that
// satisfy the above condition
static int cntPairs(int arr[], int N)
{
// Stores the count of pairs
int res = 0;
// Stores the count of array
// elements having same
// positions of MSB
int bit[] = new int[32];
// Traverse the array
for(int i = 0; i < N; i++)
{
// Stores the index of
// MSB of array elements
int pos = (int)(Math.log(arr[i]) / Math.log(2));
bit[pos]++;
}
// Calculate number of pairs
for(int i = 0; i < 32; i++)
{
res += (bit[i] * (bit[i] - 1)) / 2;
}
return res;
}
// Driver Code
public static void main(String[] args)
{
// Given Input
int arr[] = { 1, 2, 3, 4 };
int N = arr.length;
// Function call to count pairs
// satisfying the given condition
System.out.println(cntPairs(arr, N));
}
}
// This code is contributed by Dharanendra L V.
Python3
# Python3 program to implement
# the above approach
import math
# Function to count pairs that
# satisfy the above condition
def cntPairs(arr, N):
# Stores the count of pairs
res = 0
# Stores the count of array
# elements having same
# positions of MSB
bit = [0] * 32
# Traverse the array
for i in range(N):
# Stores the index of
# MSB of array elements
pos = (int)(math.log2(arr[i]))
bit[pos] += 1
# Calculate number of pairs
for i in range(32):
res += (bit[i] * (bit[i] - 1)) // 2
return res
# Driver Code
if __name__ == "__main__":
# Given Input
arr = [1, 2, 3, 4]
N = len(arr)
# Function call to count pairs
# satisfying the given condition
print(cntPairs(arr, N))
# This code is contributed by ukasp
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to count pairs that
// satisfy the above condition
static int cntPairs(int[] arr, int N)
{
// Stores the count of pairs
int res = 0;
// Stores the count of array
// elements having same
// positions of MSB
int[] bit = new int[32];
// Traverse the array
for(int i = 0; i < N; i++)
{
// Stores the index of
// MSB of array elements
int pos = (int)(Math.Log(arr[i]) / Math.Log(2));
bit[pos]++;
}
// Calculate number of pairs
for(int i = 0; i < 32; i++)
{
res += (bit[i] * (bit[i] - 1)) / 2;
}
return res;
}
// Driver Code
static public void Main ()
{
// Given Input
int[] arr = { 1, 2, 3, 4 };
int N = arr.Length;
// Function call to count pairs
// satisfying the given condition
Console.Write(cntPairs(arr, N));
}
}
// This code is contributed by avijitmondal1998
JavaScript
<script>
// Javascript program to implement
// the above approach
// Function to count pairs that
// satisfy the above condition
function cntPairs(arr, N)
{
// Stores the count of pairs
var res = 0;
// Stores the count of array
// elements having same
// positions of MSB
var bit = Array(32).fill(0);
var i;
// Traverse the array
for( i = 0; i < N; i++) {
// Stores the index of
// MSB of array elements
var pos = Math.ceil(Math.log2(arr[i]));
bit[pos] += 1;
}
// Calculate number of pairs
for (i = 0; i < 32; i++) {
res += Math.ceil((bit[i] * (bit[i] - 1)) / 2);
}
return res;
}
// Driver Code
// Given Input
arr = [1, 2, 3, 4];
N = arr.length;
// Function call to count pairs
// satisfying the given condition
document.write(cntPairs(arr, N));
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Bitwise OR of all unordered pairs from a given array Given an array arr[] of size N, the task is to find the Bitwise XOR of all possible unordered pairs from the given array. Examples: Input: arr[] = {1, 5, 3, 7} Output: 7 Explanation: All possible unordered pairs are (1, 5), (1, 3), (1, 7), (5, 3), (5, 7), (3, 7) Bitwise OR of all possible pairs are
8 min read
Count of even set bits between XOR of two arrays Given two arrays A[] and B[] having N and M positive elements respectively. The task is to count the number of elements in array A with even number of set bits in XOR for every element of array B. Examples: Input: A[] = { 4, 2, 15, 9, 8, 8 }, B[] = { 3, 4, 22 } Output: 2 4 4 Explanation: Binary repr
11 min read
Count integers in an Array which are multiples their bits counts Given an array arr[] of N elements, the task is to count all the elements which are a multiple of their set bits count.Examples: Input : arr[] = { 1, 2, 3, 4, 5, 6 } Output : 4 Explanation : There numbers which are multiple of their setbits count are { 1, 2, 4, 6 }. Input : arr[] = {10, 20, 30, 40}
9 min read
Sum of Bitwise-OR of all subarrays of a given Array | Set 2 Give an array of positive integers. The task is to find the total sum after performing the bitwise OR operation on all the sub-arrays of the given array. Examples: Input : arr[] = {1, 2, 3, 4, 5} Output : 71 Input : arr[] = {6, 5, 4, 3, 2} Output : 84 Explanation: Simple Approach: A simple approach
12 min read
Number of subarrays such that XOR of one half is equal to the other Given an array of N numbers, the task is to find the number of sub-arrays (size of the sub-array should be an even number) of the given array such that after dividing the sub-array in two equal halves, bitwise XOR of one half of the sub-array will be equal to bitwise XOR of the other half. Examples:
9 min read