Count subarrays made up of elements having exactly K set bits
Last Updated :
31 Mar, 2021
Given an array arr[] consisting of N integers and an integer K, the task is to count the number of subarrays possible consisting of elements having exactly K set bits.
Examples:
Input: arr[] = {4, 2, 1, 5, 6}, K = 2
Output: 3
Explanation: The subarrays made up of elements having exactly 2 set bits are {5}, {6} and {5, 6}.
Input: arr[] = {4, 2, 1, 5, 6}, K = 1
Output: 6
Naive Approach: The simplest approach to solve the problem is to generate all possible subarrays of the given array and count those subarrays made up of elements having exactly K set bits. Finally, print the count of such subarrays.
Time Complexity: O(N3log(M)), where M is the largest element in the array.
Auxiliary Space: O(1)
Efficient Approach: The idea is to keep the track of consecutive array elements with K set bits and find the count of subarrays with those consecutive sets of elements. Follow the steps below to solve the problem:
- Initialize a variable, say res as 0, to store the total count of subarrays consisting of elements having K set bits. Initialize a variable, say count as 0, to store the count of a consecutive set of elements having K set bits.
- Traverse the given array arr[] and perform the following steps:
- If the current element arr[i] has K set bits, then increment count by 1.
- Otherwise, increment the value of res by (count*(count – 1)) / 2 as the total number of subarrays formed by the previous consecutive elements and set count to 0.
- After completing the above steps, print the value of res as the resultant count of subarrays.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int countSet( int N)
{
int ans = 0;
while (N)
{
ans += N & 1;
N >>= 1;
}
return ans;
}
int countSub( int *arr, int k)
{
int ans = 0;
int setK = 0;
for ( int i = 0; i < 5; i++)
{
if (countSet(arr[i]) == k)
setK += 1;
else
setK = 0;
ans += setK;
}
return ans;
}
int main()
{
int arr[] = {4, 2, 1, 5, 6};
int K = 2;
cout<<(countSub(arr, K));
return 0;
}
|
Java
import java.util.*;
class GFG{
static int countSet( int N)
{
int ans = 0 ;
while (N > 0 )
{
ans += N & 1 ;
N >>= 1 ;
}
return ans;
}
static int countSub( int []arr, int k)
{
int ans = 0 ;
int setK = 0 ;
for ( int i = 0 ; i < 5 ; i++)
{
if (countSet(arr[i]) == k)
setK += 1 ;
else
setK = 0 ;
ans += setK;
}
return ans;
}
public static void main(String[] args)
{
int arr[] = { 4 , 2 , 1 , 5 , 6 };
int K = 2 ;
System.out.print(countSub(arr, K));
}
}
|
Python3
def countSet(N):
ans = 0
while N:
ans + = N & 1
N >> = 1
return ans
def countSub(arr, k):
ans = 0
setK = 0
for i in arr:
if countSet(i) = = k:
setK + = 1
else :
setK = 0
ans + = setK
return ans
arr = [ 4 , 2 , 1 , 5 , 6 ]
K = 2
print (countSub(arr, K))
|
C#
using System;
class GFG {
static int countSet( int N)
{
int ans = 0;
while (N > 0)
{
ans += N & 1;
N >>= 1;
}
return ans;
}
static int countSub( int [] arr, int k)
{
int ans = 0;
int setK = 0;
for ( int i = 0; i < 5; i++) {
if (countSet(arr[i]) == k)
setK += 1;
else
setK = 0;
ans += setK;
}
return ans;
}
public static void Main( string [] args)
{
int [] arr = { 4, 2, 1, 5, 6 };
int K = 2;
Console.WriteLine(countSub(arr, K));
}
}
|
Javascript
<script>
function countSet(N)
{
let ans = 0;
while (N)
{
ans += N & 1;
N >>= 1;
}
return ans;
}
function countSub(arr,k)
{
let ans = 0;
let setK = 0;
for (let i = 0; i < 5; i++)
{
if (countSet(arr[i]) == k)
setK += 1;
else
setK = 0;
ans += setK;
}
return ans;
}
let arr = [4, 2, 1, 5, 6];
let K = 2;
document.write((countSub(arr, K)));
</script>
|
Time Complexity: O(N*log(M)), where M is the largest element in the array.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...