Count pairs in an array such that both elements has equal set bits
Given an array arr [] of size N with unique elements, the task is to count the total number of pairs of elements that have equal set bits count.
Examples:
Input: arr[] = {2, 5, 8, 1, 3}
Output: 4
Set bits counts for {2, 5, 8, 1, 3} are {1, 2, 1, 1, 2}
All pairs with same set bits count are {2, 8}, {2, 1}, {5, 3}, {8, 1}
Input: arr[] = {1, 11, 7, 3}
Output: 1
Only possible pair is {11, 7}
Approach:
- Traverse the array from left to right and count total number of set bits of each integer.
- Use a map to store the number of elements with same count of set bits with set bits as key, and count as value.
- Then iterator through map elements, and calculate how many two-element pairs can be formed from n elements (for each element of the map) i.e. (n * (n-1)) / 2.
- The final result will be the sum of output from the previous step for every element of the map.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int totalPairs( int arr[], int n)
{
map< int , int > m;
for ( int i = 0; i < n; i++) {
m[__builtin_popcount(arr[i])]++;
}
map< int , int >::iterator it;
int result = 0;
for (it = m.begin(); it != m.end(); it++) {
result
+= (*it).second * ((*it).second - 1) / 2;
}
return result;
}
int main()
{
int arr[] = { 7, 5, 3, 9, 1, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << totalPairs(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int countSetBits( int n)
{
int count = 0 ;
while (n > 0 )
{
n &= (n - 1 ) ;
count++;
}
return count;
}
static int totalPairs( int arr[], int n)
{
HashMap<Integer, Integer> m = new HashMap<>();
for ( int i = 0 ; i < n; i++) {
int count = countSetBits(arr[i]);
if (m.containsKey(count))
m.put(count, m.get(count) + 1 );
else
m.put(count, 1 );
}
int result = 0 ;
for (Map.Entry<Integer, Integer> entry : m.entrySet()) {
int value = entry.getValue();
result += ((value * (value - 1 )) / 2 );
}
return result;
}
public static void main (String[] args) {
int arr[] = { 7 , 5 , 3 , 9 , 1 , 2 };
int n = arr.length;
System.out.println(totalPairs(arr, n));
}
}
|
Python3
def totalPairs(arr, n):
m = dict ()
for i in range (n):
x = bin (arr[i]).count( '1' )
m[x] = m.get(x, 0 ) + 1 ;
result = 0
for it in m:
result + = (m[it] * (m[it] - 1 )) / / 2
return result
arr = [ 7 , 5 , 3 , 9 , 1 , 2 ]
n = len (arr)
print (totalPairs(arr, n))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int countSetBits( int n)
{
int count = 0;
while (n > 0)
{
n &= (n - 1) ;
count++;
}
return count;
}
static int totalPairs( int []arr, int n)
{
Dictionary< int , int > mp = new Dictionary< int , int >();
for ( int i = 0 ; i < n; i++)
{
int count = countSetBits(arr[i]);
if (mp.ContainsKey(count))
{
var val = mp[count];
mp.Remove(count);
mp.Add(count, val + 1);
}
else
{
mp.Add(count, 1);
}
}
int result = 0;
foreach (KeyValuePair< int , int > entry in mp){
int values = entry.Value;
result += ((values * (values -1)) / 2);
}
return result;
}
public static void Main (String[] args)
{
int []arr = { 7, 5, 3, 9, 1, 2 };
int n = arr.Length;
Console.WriteLine(totalPairs(arr, n));
}
}
|
Javascript
<script>
function countSetBits(n)
{
var count = 0;
while (n > 0)
{
n &= (n - 1) ;
count++;
}
return count;
}
function totalPairs(arr, n)
{
var m = new Map();
for ( var i = 0; i < n; i++) {
if (m.has(arr[i]))
{
m.set(countSetBits(arr[i]), m.get(countSetBits(arr[i]))+1);
}
else {
m.set(countSetBits(arr[i]), 1);
}
}
var result = 0;
m.forEach((value, key) => {
result += parseInt(key * (key - 1) / 2);
});
return result;
}
var arr = [7, 5, 3, 9, 1, 2 ];
var n = arr.length;
document.write( totalPairs(arr, n));
</script>
|
Time Complexity: O(n log n), where n is the number of elements in given array
Auxiliary Space: O(n)
Last Updated :
03 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...