Count of pairs {X, Y} from an array such that sum of count of set bits in X ⊕ Y and twice the count of set bits in X & Y is M
Last Updated :
11 Oct, 2023
Given an array arr[] consisting of N non-negative integers and an integer M, the task is to find the count of unordered pairs {X, Y} of array elements that satisfies the condition setBits(X ⊕ Y) + 2 * setBits(X & Y) = M, where ⊕ denotes the Bitwise XOR and & denotes the Bitwise AND.
Examples:
Input: arr[] = {30, 0, 4, 5 }, M = 2
Output: 2
Explanation: The pairs satisfying the necessary conditions are {{3, 0}, {0, 5}}.
Input: arr[] = {1, 2, 3, 4}, M = 3
Output: 3
Explanation: The pairs satisfying the necessary conditions are {{1, 3}, {2, 3}, {3, 4}}.
Naive Approach: The simplest approach is to generate all possible pairs from the given array and for each pair, check if the necessary condition is satisfied or not. Increment the count for pairs satisfying the given conditions and finally, print the count of pairs.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int countPairs( int a[], int N, int M)
{
int count = 0;
for ( int i = 0; i < N; i++) {
for ( int j = i + 1; j < N; j++) {
int x = a[i] ^ a[j];
int y = a[i] & a[j];
if (__builtin_popcount(x)
+ 2 * __builtin_popcount(y)
== M) {
count++;
}
}
}
return count;
}
int main()
{
int arr[] = { 3, 0, 4, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
int M = 2;
cout << countPairs(arr, N, M) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
public class CountPairs {
static int countPairs( int [] a, int N, int M) {
int count = 0 ;
for ( int i = 0 ; i < N; i++) {
for ( int j = i + 1 ; j < N; j++) {
int x = a[i] ^ a[j];
int y = a[i] & a[j];
if (Integer.bitCount(x) + 2 * Integer.bitCount(y) == M) {
count++;
}
}
}
return count;
}
public static void main(String[] args) {
int arr[] = { 3 , 0 , 4 , 5 };
int N = arr.length;
int M = 2 ;
System.out.println(countPairs(arr, N, M));
}
}
|
Python3
def countSetBits(n):
count = 0
while (n):
count + = n & 1
n >> = 1
return count
def countPairs(a, N, M):
count = 0
for i in range ( 0 ,N):
for j in range (i + 1 ,N):
x = a[i] ^ a[j]
y = a[i] & a[j]
if (countSetBits(x) + 2 * countSetBits(y)) = = M:
count + = 1
return count
arr = [ 3 , 0 , 4 , 5 ]
N = len (arr)
M = 2
pairs = countPairs(arr, N, M)
print (pairs)
|
C#
using System;
namespace CountPairs {
class Program {
static int CountPairs( int [] a, int N, int M)
{
int count = 0;
for ( int i = 0; i < N; i++) {
for ( int j = i + 1; j < N; j++) {
int x = a[i] ^ a[j];
int y = a[i] & a[j];
if (CountSetBits(x) + 2 * CountSetBits(y)
== M) {
count++;
}
}
}
return count;
}
static int CountSetBits( int num)
{
int count = 0;
while (num > 0) {
count += num & 1;
num >>= 1;
}
return count;
}
static void Main( string [] args)
{
int [] arr = { 3, 0, 4, 5 };
int N = arr.Length;
int M = 2;
Console.WriteLine(CountPairs(arr, N, M));
}
}
}
|
Javascript
function countPairs(arr, M) {
let count = 0;
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
let x = arr[i] ^ arr[j];
let y = arr[i] & arr[j];
if (popcount(x) + 2 * popcount(y) === M) {
count++;
}
}
}
return count;
}
function popcount(num) {
let count = 0;
while (num > 0) {
count += num & 1;
num >>= 1;
}
return count;
}
let arr = [3, 0, 4, 5];
let M = 2;
console.log(countPairs(arr, M));
|
Output:
2
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized based on the following observations:
- From the property of Bitwise XOR:
- setBits( a⊕ b ) = setBits( a|b ) – setBits( a&b )
- setBits( a|b ) = setBits(a) + setBits(b) – setBits( a&b )
- Therefore, the given equation becomes:
- ( setBits( X|Y ) – setBits( X&Y ) )+( 2 × setBits( X&Y ) ) = M
- setBits( X ) + setBits ( Y ) – setBits( X&Y ) – setBits( X&Y ) + ( 2 × setBits ( X&Y ) ) = M
- setBits( X ) + setBits( Y ) = M
- Therefore, the task reduces to counting the pairs of elements whose sum of set bits is equal to M.
Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countsetbits( int n)
{
int count = 0;
while (n) {
count += n & 1;
n >>= 1;
}
return (count);
}
int countPairs( int a[], int N, int M)
{
for ( int i = 0; i < N; i++) {
a[i] = countsetbits(a[i]);
}
unordered_map< int , int > mp;
for ( int i = 0; i < N; i++) {
mp[a[i]]++;
}
int count = 0;
for ( int i = 0; i < N; i++) {
count += mp[M - a[i]];
if (a[i] == M - a[i]) {
count--;
}
}
return (count / 2);
}
int main()
{
int arr[] = { 3, 0, 4, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
int M = 2;
cout << countPairs(arr, N, M);
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static int countsetbits( int n)
{
int count = 0 ;
while (n != 0 )
{
count += n & 1 ;
n >>= 1 ;
}
return (count);
}
static int countPairs( int [] a, int N, int M)
{
for ( int i = 0 ; i < N; i++)
{
a[i] = countsetbits(a[i]);
}
HashMap<Integer,
Integer> mp = new HashMap<Integer,
Integer>();
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 );
}
}
int count = 0 ;
for ( int i = 0 ; i < N; i++)
{
count += mp.get(M - a[i]);
if (a[i] == M - a[i])
{
count--;
}
}
return (count / 2 );
}
public static void main(String[] args)
{
int [] arr = { 3 , 0 , 4 , 5 };
int N = arr.length;
int M = 2 ;
System.out.println(countPairs(arr, N, M));
}
}
|
Python3
def countSetBits(n):
count = 0
while (n):
count + = n & 1
n >> = 1
return count
def countPairs(arr, N, M):
for i in range ( 0 , N):
arr[i] = countSetBits(arr[i])
mp = {}
for i in range ( 0 , N):
if arr[i] in mp:
mp[arr[i]] + = 1
else :
mp[arr[i]] = 1
twice_count = 0
for i in range ( 0 , N):
if M - arr[i] in mp.keys():
twice_count + = mp[M - arr[i]]
if (M - arr[i] = = arr[i]):
twice_count - = 1
return int (twice_count / 2 )
arr = [ 3 , 0 , 4 , 5 ]
N = len (arr)
M = 2
print (countPairs(arr, N, M))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int countsetbits( int n)
{
int count = 0;
while (n != 0)
{
count += n & 1;
n >>= 1;
}
return (count);
}
static int countPairs( int [] a, int N, int M)
{
for ( int i = 0; i < N; i++)
{
a[i] = countsetbits(a[i]);
}
Dictionary< int ,
int > mp = new Dictionary< int ,
int >();
for ( int i = 0; i < N; ++i)
{
if (mp.ContainsKey(a[i]) == true )
mp[a[i]] += 1;
else
mp[a[i]] = 1;
}
int count = 0;
for ( int i = 0; i < N; i++)
{
count += mp[M - a[i]];
if (a[i] == M - a[i])
{
count--;
}
}
return (count / 2);
}
static public void Main()
{
int [] arr = { 3, 0, 4, 5 };
int N = arr.Length;
int M = 2;
Console.WriteLine(countPairs(arr, N, M));
}
}
|
Javascript
<script>
function countsetbits(n)
{
var count = 0;
while (n) {
count += n & 1;
n >>= 1;
}
return (count);
}
function countPairs(a, N, M)
{
for ( var i = 0; i < N; i++) {
a[i] = countsetbits(a[i]);
}
var mp = new Map();
for ( var i = 0; i < N; i++) {
if (mp.has(a[i]))
mp.set(a[i], mp.get(a[i])+1)
else
mp.set(a[i], 1)
}
var count = 0;
for ( var i = 0; i < N; i++) {
count += mp.get(M - a[i]);
if (a[i] == M - a[i]) {
count--;
}
}
return (count / 2);
}
var arr = [3, 0, 4, 5];
var N = arr.length;
var M = 2;
document.write( countPairs(arr, N, M));
</script>
|
Time Complexity: O(NlogN)
Auxiliary Space: O(N) as using auxiliary space for unordered_map
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...