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.
Examples:
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
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.
Below is the implementation of above approach:
C++
// 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; } |
Java
// Java program to count all pairs with bit difference // as k import java.io.*; class GFG { // Utility function to count total ones in a number static int bitCount(int n) { int count = 0; while (n>0) { if ((n & 1)>0) ++count; n >>= 1; } return count; } // Function to count pairs of K different bits static long countPairsWithKDiff(int arr[], int n, int k) { 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 public static void main (String[] args) { int k = 2; int arr[] = {2, 4, 1, 3, 1}; int n =arr.length; System.out.println( "Total pairs for k = " + k + " are " + countPairsWithKDiff(arr, n, k) + "\n"); } } // This code is contributed by shs.. |
Python3
# Python 3 program to count all pairs # with bit difference as k # Utility function to count total # ones in a number def bitCount(n): count = 0 while (n): if (n & 1): count += 1 n >>= 1 return count # Function to count pairs of K different bits def countPairsWithKDiff(arr, n, k): ans = 0 # initialize final answer for i in range(0, n - 1, 1): for j in range(i + 1, n, 1): xoredNum = arr[i] ^ arr[j] # Check for K differ bit if (k == bitCount(xoredNum)): ans += 1 return ans # Driver code if __name__ == '__main__': k = 2 arr = [2, 4, 1, 3, 1] n = len(arr) print("Total pairs for k =", k, "are", countPairsWithKDiff(arr, n, k)) # This code is contributed by # Sanjit_Prasad |
C#
// C# program to count all pairs // with bit difference as k using System; class GFG { // Utility function to count // total ones in a number static int bitCount(int n) { int count = 0; while (n > 0) { if ((n & 1) > 0) ++count; n >>= 1; } return count; } // Function to count pairs of K different bits static long countPairsWithKDiff(int []arr, int n, int k) { 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 public static void Main () { int k = 2; int []arr = {2, 4, 1, 3, 1}; int n = arr.Length; Console.WriteLine( "Total pairs for k = " + k + " are " + countPairsWithKDiff(arr, n, k) + "\n"); } } // This code is contributed by shs.. |
PHP
<?php // PHP program to count all // pairs with bit difference // as k // Utility function to count // total ones in a number function bitCount($n) { $count = 0; while ($n) { if ($n & 1) ++$count; $n >>= 1; } return $count; } // Function to count pairs // of K different bits function countPairsWithKDiff($arr, $n, $k) { // initialize final answer $ans = 0; for ($i = 0; $i < $n-1; ++$i) { for ($j = $i + 1; $j < $n; ++$j) { $xoredNum = $arr[$i] ^ $arr[$j]; // Check for K differ bit if ($k == bitCount($xoredNum)) ++$ans; } } return $ans; } // Driver code $k = 2; $arr = array(2, 4, 1, 3, 1); $n = count($arr); echo "Total pairs for k = " , $k , " are " , countPairsWithKDiff($arr, $n, $k) , "\n"; // This code is contributed by anuj_67. ?> |
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.
C++
// Below is C++ approach of finding total k bit // difference pairs #include<bits/stdc++.h> using namespace std; // Function to calculate 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; } |
Java
// Below is Java approach of finding total k bit // difference pairs import java.util.*; class GFG { // Function to calculate K bit // different pairs in array static long kBitDifferencePairs(int arr[], int n, int k) { // Get the maximum value among all array elemensts int MAX = Arrays.stream(arr).max().getAsInt(); // Set the count array to 0, // count[] stores the total // frequency of array elements long []count = new long[MAX + 1]; Arrays.fill(count, 0); for (int i = 0; i < n; ++i) ++count[arr[i]]; // Initialize result 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] == 0) continue; for (int j = i + 1; j <= MAX; ++j) { // Update answer if k differ bit found if ( Integer.bitCount(i ^ j) == k) ans += count[i] * count[j]; } } return ans; } // Driver code public static void main(String[] args) { int k = 2; int arr[] = {2, 4, 1, 3, 1}; int n = arr.length; System.out.println("Total pairs for k = " + k + " are = " + kBitDifferencePairs(arr, n, k)); k = 3; System.out.println("Total pairs for k = " + k + " are = " + kBitDifferencePairs(arr, n, k)); } } // This code is contributed by Rajput-Ji |
Python3
# Below is Python3 approach of finding # total k bit difference pairs # Function to calculate K bit different # pairs in array def kBitDifferencePairs(arr, n, k): # Get the maximum value among # all array elemensts MAX = max(arr) # Set the count array to 0, count[] stores # the total frequency of array elements count = [0 for i in range(MAX + 1)] for i in range(n): count[arr[i]] += 1 # Initialize result ans = 0 # For 0 bit answer will be total # count of same number if (k == 0): for i in range(MAX + 1): ans += (count[i] * (count[i] - 1)) // 2 return ans for i in range(MAX + 1): # if count[i] is 0, skip the next loop # as it will not contribute the answer if (count[i] == 0): continue for j in range(i + 1, MAX + 1): # Update answer if k differ bit found if (bin(i ^ j).count('1') == k): ans += count[i] * count[j] return ans # Driver code k = 2arr = [2, 4, 1, 3, 1] n = len(arr) print("Total pairs for k =", k, "are", kBitDifferencePairs(arr, n, k)) k = 3print("Total pairs for k =", k, "are", kBitDifferencePairs(arr, n, k)) # This code is contributed by mohit kumar |
C#
// Below is C# approach of finding // total k bit difference pairs using System; using System.Linq; class GFG { // Function to calculate K bit // different pairs in array static long kBitDifferencePairs(int []arr, int n, int k) { // Get the maximum value among // all array elemensts int MAX = arr.Max(); // Set the count array to 0, // count[] stores the total // frequency of array elements long []count = new long[MAX + 1]; for (int i = 0; i < n; ++i) ++count[arr[i]]; // Initialize result 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] == 0) continue; for (int j = i + 1; j <= MAX; ++j) { // Update answer if k differ bit found if (BitCount(i ^ j) == k) ans += count[i] * count[j]; } } return ans; } static int BitCount(int n) { int count = 0; while (n > 0) { count += n & 1; n >>= 1; } return count; } // Driver code public static void Main(String[] args) { int k = 2; int []arr = {2, 4, 1, 3, 1}; int n = arr.Length; Console.WriteLine("Total pairs for k = " + k + " are = " + kBitDifferencePairs(arr, n, k)); k = 3; Console.WriteLine("Total pairs for k = " + k + " are = " + kBitDifferencePairs(arr, n, k)); } } // This code is contributed by PrinciRaj1992 |
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.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.

