# Count all pairs of an array which differ in K bits

Given an array of size n and integer k, count all pairs in array which differ in exactly K bits of binary representation of both the numbers.

The input arrays have elements with small values and possibly many repetitions.

```Input: arr[] = {2, 4, 1, 3, 1}
k = 2
Output: 5
Explanation:
There are only 4 pairs which differs in
exactly 2 bits of binary representation:
(2, 4), (1, 2) [Two times] and (4, 1)
[Two times]

Input  : arr[] = {2, 1, 2, 1}
k = 2
Output :  4
```
We strongly recommend that you click here and practice it, before moving on to the solution.

Naive Approach

A brute force is to run the two loops one inside the another and select the pairs one by one and take a XOR of both elements. The result of XORed value contains a set bits which are differ in both the elements. Now we just need to count total set bits so that we compare it with value K.

```// C++ program to count all pairs with bit difference
// as k
#include<bits/stdc++.h>
using namespace std;

// Utility function to count total ones in a number
int bitCount(int n)
{
int count = 0;
while (n)
{
if (n & 1)
++count;
n >>= 1;
}
return count;
}

// Function to count pairs of K different bits
long long countPairsWithKDiff(int arr[], int n, int k)
{
long long ans = 0; // initialize final answer

for (int i = 0; i < n-1; ++i)
{
for (int j = i + 1; j < n; ++j)
{
int xoredNum = arr[i] ^ arr[j];

// Check for K differ bit
if (k == bitCount(xoredNum))
++ans;
}
}
return ans;
}

// Driver code
int main()
{
int k = 2;
int arr[] = {2, 4, 1, 3, 1};
int n = sizeof(arr)/sizeof(arr[0]);

cout << "Total pairs for k = " << k << " are = "
<< countPairsWithKDiff(arr, n, k) << "\n";

return 0;
}
```
`Output:Total pairs for k = 2 are = 5`

Time complexity: O(N2 * log MAX) where MAX is maximum element in input array.
Auxiliary space: O(1)

Efficient approach

This approach is efficient for the cases when input array has small elements and possibly many repetitions. The idea is to iterate from 0 to max(arr[i]) and for every pair(i, j) check the number of set bits in (i ^ j) and compare this with K. We can use inbuilt function of gcc( __builtin_popcount ) or precompute such array to make the check faster. If number of ones in i ^ j is equals to K then we will add the total count of both i and j.

```// Below is C++ approach of finding total k bit
// difference pairs
#include<bits/stdc++.h>
using namespace std;

// Function to calclate K bit different pairs in array
long long kBitDifferencePairs(int arr[], int n, int k)
{
// Get the maximum value among all array elemensts
int MAX = *max_element(arr, arr+n);

// Set the count array to 0, count[] stores the
// total frequency of array elements
long long count[MAX+1];
memset(count, 0, sizeof(count));

for (int i=0; i < n; ++i)
++count[arr[i]];

// Initialize result
long long ans = 0;

// For 0 bit answer will be total count of same number
if (k == 0)
{
for (int i = 0; i <= MAX; ++i)
ans += (count[i] * (count[i] - 1)) / 2;

return ans;
}

for (int i = 0; i <= MAX; ++i)
{
// if count[i] is 0, skip the next loop as it
// will not contribute the answer
if (!count[i])
continue;

for (int j = i + 1; j <= MAX; ++j)
{
//Update answer if k differ bit found
if ( __builtin_popcount(i ^ j) == k)
ans += count[i] * count[j];
}
}
return ans;
}

// Driver code
int main()
{
int k = 2;
int arr[] = {2, 4, 1, 3, 1};
int n = sizeof(arr)/sizeof(arr[0]);

cout << "Total pairs for k = " << k << " are = "
<< kBitDifferencePairs(arr, n, k) << "\n";

k = 3;
cout << "Total pairs for k = " << k << " are = "
<< kBitDifferencePairs(arr, n, k) ;
return 0;
}
```
`Output:Total pairs for k = 2 are = 5`

Time complexity: O(MAX2) where MAX is maximum element in input array.
Auxiliary space: O(MAX)

This article is contributed by Shubham Bansal. 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.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
2.5 Average Difficulty : 2.5/5.0
Based on 2 vote(s)