# Count pairs in an array such that both elements has equal set bits

Given an array *arr* 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*. - Final result will be the sum of output from the previous step for every element of the map.

Below is the implementation of the of the above approach:

`// C++ implementation of above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count all pairs ` `// with equal set bits count ` `int` `totalPairs(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// map to store count of elements ` ` ` `// with equal number of set bits ` ` ` `map<` `int` `, ` `int` `> m; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// inbuilt function that returns the ` ` ` `// count of set bits of the number ` ` ` `m[__builtin_popcount(arr[i])]++; ` ` ` `} ` ` ` ` ` `map<` `int` `, ` `int` `>::iterator it; ` ` ` `int` `result = 0; ` ` ` `for` `(it = m.begin(); it != m.end(); it++) { ` ` ` ` ` `// there can be (n*(n-1)/2) unique two- ` ` ` `// element pairs to choose from n elements ` ` ` `result ` ` ` `+= (*it).second * ((*it).second - 1) / 2; ` ` ` `} ` ` ` ` ` `return` `result; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 7, 5, 3, 9, 1, 2 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `cout << totalPairs(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

4

## Recommended Posts:

- Count of index pairs with equal elements in an array
- Count all pairs of an array which differ in K bits
- XOR of all elements of array with set bits equal to K
- Sorting array elements with set bits equal to K
- For each element in 1st array count elements less than or equal to it in 2nd array | Set 2
- For each element in 1st array count elements less than or equal to it in 2nd array
- Count of smaller or equal elements in sorted array
- Count elements less than or equal to a given value in a sorted rotated array
- Noble integers in an array (count of greater elements is equal to value)
- Total character pairs from two strings, with equal number of set bits in their ascii value
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- Count minimum bits to flip such that XOR of A and B equal to C
- Count pairs from two sorted arrays whose sum is equal to a given value x
- Count all distinct pairs with difference equal to k
- Count elements such that there are exactly X elements with values greater than or equal to X

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.