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 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 same number of set bits, the number of possible pairs among them is
**X * (X – 1) / 2**. - Print the total count of such pairs.

Below code is the implementation of the above approach:

## C++

`// C++ Program to count ` `// possible number of pairs ` `// of elements with same ` `// number of set bits. ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the ` `// count of Pairs ` `int` `countPairs(` `int` `arr[], ` `int` `N) ` `{ ` ` ` `// Get the maximum element ` ` ` `int` `maxm = *max_element(arr, arr + N); ` ` ` ` ` `int` `i, k; ` ` ` `// Array to store count of bits ` ` ` `// of all elements upto maxm ` ` ` `int` `bitscount[maxm + 1] = { 0 }; ` ` ` ` ` `// Store the set bits ` ` ` `// for powers of 2 ` ` ` `for` `(i = 1; i <= maxm; i *= 2) ` ` ` `bitscount[i] = 1; ` ` ` `// Compute the set bits for ` ` ` `// the remaining elements ` ` ` `for` `(i = 1; i <= maxm; i++) { ` ` ` `if` `(bitscount[i] == 1) ` ` ` `k = i; ` ` ` `if` `(bitscount[i] == 0) { ` ` ` `bitscount[i] ` ` ` `= bitscount[k] ` ` ` `+ bitscount[i - k]; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Store the frequency ` ` ` `// of respective counts ` ` ` `// of set bits ` ` ` `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; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

22

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- Count all pairs of an array which differ in K bits
- Count pairs in an array such that both elements has equal set bits
- Count pairs of elements such that number of set bits in their AND is B[i]
- Count pairs of elements such that number of set bits in their OR is B[i]
- Program to count number of set bits in an (big) array
- Count pairs with set bits sum equal to K
- Count number of pairs in array having sum divisible by K | SET 2
- Count number of distinct pairs whose sum exists in the given array
- Check if bits of a number has count of consecutive set bits in increasing order
- Total distinct pairs from two arrays such that second number can be obtained by inverting bits of first
- Total character pairs from two strings, with equal number of set bits in their ascii value
- Sort an array according to count of set bits
- Sort an array according to count of set bits | Set 2
- Count of elements in an Array whose set bits are in a multiple of K
- Count unset bits of a number
- Count total bits in a number
- Count Set-bits of number using Recursion
- Count integers in an Array which are multiples their bits counts
- Count number of bits changed after adding 1 to given N

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.