Count of pairs in an Array with same number of set bits
Last Updated :
15 Jun, 2021
Given an array arr containing N integers, the task is to count the possible number of pairs of elements with the same number of set bits.
Examples:
Input: N = 8, arr[] = {1, 2, 3, 4, 5, 6, 7, 8}
Output: 9
Explanation:
Elements with 1 set bit: 1, 2, 4, 8
Elements with 2 set bits: 3, 5, 6
Elements with 3 set bits: 7
Hence, {1, 2}, {1, 4}, {1, 8}, {2, 4}, {2, 8}, {4, 8}, {3, 5}, {3, 6}, and {5, 6} are the possible such pairs.
Input: N = 12, arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
Output: 22
Approach:
- Precompute and store the set bits for all numbers up to the maximum element of the array in bitscount[]. For all powers of 2, store 1 at their respective index. After that, compute the set bits count for the remaining elements by the relation:
bitscount[i] = bitscount[previous power of 2] + bitscount[i – previous power of 2]
- Store the frequency of set bits in the array elements in a Map.
- Add the number of possible pairs for every set bit count. If X elements have the same number of set bits, the number of possible pairs among them is X * (X – 1) / 2.
- Print the total count of such pairs.
The below code is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int countPairs( int arr[], int N)
{
int maxm = *max_element(arr, arr + N);
int i, k;
int bitscount[maxm + 1] = { 0 };
for (i = 1; i <= maxm; i *= 2)
bitscount[i] = 1;
for (i = 1; i <= maxm; i++) {
if (bitscount[i] == 1)
k = i;
if (bitscount[i] == 0) {
bitscount[i]
= bitscount[k]
+ bitscount[i - k];
}
}
map< int , int > setbits;
for ( int i = 0; i < N; i++) {
setbits[bitscount[arr[i]]]++;
}
int ans = 0;
for ( auto it : setbits) {
ans += it.second
* (it.second - 1) / 2;
}
return ans;
}
int main()
{
int N = 12;
int arr[] = { 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12 };
cout << countPairs(arr, N);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG{
static int countPairs( int []arr, int N)
{
int maxm = arr[ 0 ];
for ( int j = 1 ; j < N; j++)
{
if (maxm < arr[j])
{
maxm = arr[j];
}
}
int i, k = 0 ;
int [] bitscount = new int [maxm + 1 ];
Arrays.fill(bitscount, 0 );
for (i = 1 ; i <= maxm; i *= 2 )
bitscount[i] = 1 ;
for (i = 1 ; i <= maxm; i++)
{
if (bitscount[i] == 1 )
k = i;
if (bitscount[i] == 0 )
{
bitscount[i] = bitscount[k] +
bitscount[i - k];
}
}
Map<Integer, Integer> setbits = new HashMap<>();
for ( int j = 0 ; j < N; j++)
{
setbits.put(bitscount[arr[j]],
setbits.getOrDefault(
bitscount[arr[j]], 0 ) + 1 );
}
int ans = 0 ;
for ( int it : setbits.values())
{
ans += it * (it - 1 ) / 2 ;
}
return ans;
}
public static void main(String[] args)
{
int N = 12 ;
int []arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 ,
8 , 9 , 10 , 11 , 12 };
System.out.println(countPairs(arr, N));
}
}
|
Python3
def countPairs(arr, N):
maxm = max (arr)
i = 0
k = 0
bitscount = [ 0 for i in range (maxm + 1 )]
i = 1
while i < = maxm:
bitscount[i] = 1
i * = 2
for i in range ( 1 , maxm + 1 ):
if (bitscount[i] = = 1 ):
k = i
if (bitscount[i] = = 0 ):
bitscount[i] = (bitscount[k] +
bitscount[i - k])
setbits = dict ()
for i in range (N):
if bitscount[arr[i]] in setbits:
setbits[bitscount[arr[i]]] + = 1
else :
setbits[bitscount[arr[i]]] = 1
ans = 0
for it in setbits.values():
ans + = it * (it - 1 ) / / 2
return ans
if __name__ = = '__main__' :
N = 12
arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ,
8 , 9 , 10 , 11 , 12 ]
print (countPairs(arr, N))
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
class GFG{
static int countPairs( int []arr, int N)
{
int maxm = - int .MaxValue;
for ( int j = 0; j < N; j++)
{
if (maxm < arr[j])
{
maxm = arr[j];
}
}
int i, k = 0;
int []bitscount = new int [maxm + 1];
Array.Fill(bitscount, 0);
for (i = 1; i <= maxm; i *= 2)
bitscount[i] = 1;
for (i = 1; i <= maxm; i++)
{
if (bitscount[i] == 1)
k = i;
if (bitscount[i] == 0)
{
bitscount[i] = bitscount[k] +
bitscount[i - k];
}
}
Dictionary< int ,
int > setbits = new Dictionary< int ,
int >();
for ( int j = 0; j < N; j++)
{
if (setbits.ContainsKey(bitscount[arr[j]]))
{
setbits[bitscount[arr[j]]]++;
}
else
{
setbits[bitscount[arr[j]]] = 1;
}
}
int ans = 0;
foreach (KeyValuePair< int , int > it in setbits)
{
ans += it.Value * (it.Value - 1) / 2;
}
return ans;
}
public static void Main( string [] args)
{
int N = 12;
int []arr = { 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12 };
Console.Write(countPairs(arr, N));
}
}
|
Javascript
<script>
function countPairs(arr, N)
{
var maxm = arr.reduce((a,b)=>Math.max(a,b));
var i, k;
var bitscount = Array(maxm+1).fill(0);
for (i = 1; i <= maxm; i *= 2)
bitscount[i] = 1;
for (i = 1; i <= maxm; i++) {
if (bitscount[i] == 1)
k = i;
if (bitscount[i] == 0) {
bitscount[i]
= bitscount[k]
+ bitscount[i - k];
}
}
var setbits = new Map();
for ( var i = 0; i < N; i++) {
if (setbits.has(bitscount[arr[i]]))
setbits.set(bitscount[arr[i]],
setbits.get(bitscount[arr[i]])+1)
else
setbits.set(bitscount[arr[i]], 1)
}
var ans = 0;
setbits.forEach((value, key) => {
ans += value
* (value - 1) / 2;
});
return ans;
}
var N = 12;
var arr = [1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12];
document.write( countPairs(arr, N));
</script>
|
Share your thoughts in the comments
Please Login to comment...