Count pairs with set bits sum equal to K
Given an array arr[] and an integer K, the task is to count the pairs whose sum of set bits is K
Examples:
Input: arr[] = {1, 2, 3, 4, 5}, K = 4
Output: 1
(3, 5) is the only valid pair as the count
of set bits in the integers {1, 2, 3, 4, 5}
are {1, 1, 2, 1, 2} respectively.
Input: arr[] = {5, 42, 35, 22, 7}, K = 6
Output: 6
Naive approach: Initialise count = 0 and run two nested loops and check all possible pairs and check whether the sum of count bits is K. If yes then increment count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
unsigned int countSetBits( int n)
{
unsigned int count = 0;
while (n) {
n &= (n - 1);
count++;
}
return count;
}
int pairs( int arr[], int n, int k)
{
int count = 0;
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
int sum = countSetBits(arr[i])
+ countSetBits(arr[j]);
if (sum == k)
count++;
}
}
return count;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int n = sizeof (arr) / sizeof (arr[0]);
int k = 4;
cout << pairs(arr, n, k);
return 0;
}
|
Java
class GFG
{
static int countSetBits( int n)
{
int count = 0 ;
while (n > 0 )
{
n &= (n - 1 );
count++;
}
return count;
}
static int pairs( int arr[], int n, int k)
{
int count = 0 ;
for ( int i = 0 ; i < n; i++)
{
for ( int j = i + 1 ; j < n; j++)
{
int sum = countSetBits(arr[i])
+ countSetBits(arr[j]);
if (sum == k)
count++;
}
}
return count;
}
public static void main(String args[])
{
int arr[] = { 1 , 2 , 3 , 4 , 5 };
int n = arr.length;
int k = 4 ;
System.out.println(pairs(arr, n, k));
}
}
|
Python3
def countSetBits(n) :
count = 0 ;
while (n) :
n & = (n - 1 );
count + = 1
return count;
def pairs(arr, n, k) :
count = 0 ;
for i in range (n) :
for j in range (i + 1 , n) :
sum = countSetBits(arr[i]) + countSetBits(arr[j]);
if ( sum = = k) :
count + = 1 ;
return count;
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 4 , 5 ];
n = len (arr);
k = 4 ;
print (pairs(arr, n, k));
|
C#
using System;
public class GFG
{
static int countSetBits( int n)
{
int count = 0;
while (n > 0)
{
n &= (n - 1);
count++;
}
return count;
}
static int pairs( int []arr, int n, int k)
{
int count = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = i + 1; j < n; j++)
{
int sum = countSetBits(arr[i])
+ countSetBits(arr[j]);
if (sum == k)
count++;
}
}
return count;
}
public static void Main(String []args)
{
int []arr = { 1, 2, 3, 4, 5 };
int n = arr.Length;
int k = 4;
Console.WriteLine(pairs(arr, n, k));
}
}
|
Javascript
<script>
function countSetBits(n)
{
var count = 0;
while (n > 0)
{
n &= (n - 1);
count++;
}
return count;
}
function pairs(arr , n , k)
{
var count = 0;
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
var sum = countSetBits(arr[i])
+ countSetBits(arr[j]);
if (sum == k)
count++;
}
}
return count;
}
var arr = [ 1, 2, 3, 4, 5 ];
var n = arr.length;
var k = 4;
document.write(pairs(arr, n, k));
</script>
|
Time complexity: O(n2logn)
Auxiliary Space: O(1)
Efficient approach: Assume that every integer can be represented using 32 bits, create a frequency array freq[] of size 32 where freq[i] will store the count of numbers having set bits equal to i. Now run two nested loops on this frequency array, if i + j = K then count of pairs will be freq[i] * freq[j] for all such i and j.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 32
unsigned int countSetBits( int n)
{
unsigned int count = 0;
while (n) {
n &= (n - 1);
count++;
}
return count;
}
int pairs( int arr[], int n, int k)
{
int count = 0;
int f[MAX + 1] = { 0 };
for ( int i = 0; i < n; i++)
f[countSetBits(arr[i])]++;
for ( int i = 0; i <= MAX; i++) {
for ( int j = i; j <= MAX; j++) {
if (i + j == k) {
if (i == j)
count += ((f[i] * (f[i] - 1)) / 2);
else
count += (f[i] * f[j]);
}
}
}
return count;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int n = sizeof (arr) / sizeof (arr[0]);
int k = 4;
cout << pairs(arr, n, k);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int MAX = 32 ;
static int countSetBits( int n)
{
int count = 0 ;
while (n > 0 )
{
n &= (n - 1 );
count++;
}
return count;
}
static int pairs( int arr[], int n, int k)
{
int count = 0 ;
int []f = new int [MAX + 1 ];
for ( int i = 0 ; i < n; i++)
f[countSetBits(arr[i])]++;
for ( int i = 0 ; i <= MAX; i++)
{
for ( int j = i; j <= MAX; j++)
{
if (i + j == k)
{
if (i == j)
count += ((f[i] * (f[i] - 1 )) / 2 );
else
count += (f[i] * f[j]);
}
}
}
return count;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 , 5 };
int n = arr.length;
int k = 4 ;
System.out.println(pairs(arr, n, k));
}
}
|
Python3
MAX = 32
def countSetBits(n) :
count = 0 ;
while (n):
n & = (n - 1 );
count + = 1 ;
return count;
def pairs(arr, n, k):
count = 0 ;
f = [ 0 for i in range ( MAX + 1 )]
for i in range (n):
f[countSetBits(arr[i])] + = 1 ;
for i in range ( MAX + 1 ):
for j in range ( 1 , MAX + 1 ):
if (i + j = = k):
if (i = = j):
count + = ((f[i] * (f[i] - 1 )) / 2 );
else :
count + = (f[i] * f[j]);
return count;
arr = [ 1 , 2 , 3 , 4 , 5 ]
n = len (arr)
k = 4
print (pairs(arr, n, k))
|
C#
using System;
class GFG
{
static int MAX = 32;
static int countSetBits( int n)
{
int count = 0;
while (n > 0)
{
n &= (n - 1);
count++;
}
return count;
}
static int pairs( int []arr, int n, int k)
{
int count = 0;
int []f = new int [MAX + 1];
for ( int i = 0; i < n; i++)
f[countSetBits(arr[i])]++;
for ( int i = 0; i <= MAX; i++)
{
for ( int j = i; j <= MAX; j++)
{
if (i + j == k)
{
if (i == j)
count += ((f[i] * (f[i] - 1)) / 2);
else
count += (f[i] * f[j]);
}
}
}
return count;
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 3, 4, 5 };
int n = arr.Length;
int k = 4;
Console.WriteLine(pairs(arr, n, k));
}
}
|
Javascript
<script>
var MAX = 32;
function countSetBits(n)
{
var count = 0;
while (n > 0)
{
n &= (n - 1);
count++;
}
return count;
}
function pairs(arr , n , k)
{
var count = 0;
var f = Array.from({length: MAX + 1}, (_, i) => 0);
for (i = 0; i < n; i++)
f[countSetBits(arr[i])]++;
for (i = 0; i <= MAX; i++)
{
for (j = i; j <= MAX; j++)
{
if (i + j == k)
{
if (i == j)
count += ((f[i] * (f[i] - 1)) / 2);
else
count += (f[i] * f[j]);
}
}
}
return count;
}
var arr = [ 1, 2, 3, 4, 5 ];
var n = arr.length;
var k = 4;
document.write(pairs(arr, n, k));
</script>
|
Time complexity: O(MAX2)
Auxiliary Space: O(MAX)
Last Updated :
31 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...