Queries for number of array elements in a range with Kth Bit Set

Last Updated :

Given an array arr[] of n positive integers and q queries, in which each query have an integer k. For each query, the task is to output the minimum size of subarray whose binary representation of each elements if concatenated will have at least k number of set bits. If no such array exist, print -1.

Examples:

Input : 
n = 4, arr[] = { 1, 2, 4, 8 }, q = 3
k1 = 1 
k2 = 2
k3 = 3

Output :
1
2
3

First observe, if a concatenation of binary representation of elements in a subarray of size S have k set bits then there must be a subarray of size S + 1 which will always have more or equal to k set bits. Minimum and maximum possible value of S can be 1 and n respectively.

So, we can apply binary search for each query to find the minimum length that have at least k set bits. So, first precompute number of set bits of all integers. Now, construct the prefix sum array, where prefixsum[i] contain sum of set bits of all array elements upto ith element. Then we can apply binary search to find minumum subarray size which satisfies the condition prefixsum[i+L] – prefixsum[i] >= k. If no such subarray exist, print “-1”.

Below is C++ implementation of this approach:

#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;

// Return the number of set bits in a number
int countSetbits(int n)
{
    int cnt = 0;
    while(n)
    {
        n = n & (n - 1);
        cnt++;
    }
    return cnt;
}

// Count the number of set bits of the first N whole numbers
void precomputeSetbits(int sbit[])
{
    for(int i = 0; i < N; i++)
    {
        sbit[i] = countSetbits(i);
    }
}

// Calculate the prefix sum array of set bits
void calSum(int n, int sum[], int sbit[], int a[])
{
    sum[0] = 0;

    // Finding the prefix sum
    for(int i = 1; i <= n; i++)
    {
        sum[i] = sum[i - 1] + sbit[a[i - 1]];
    }
}

// Check if any subarray of size x has at least k set bits
bool check(int n, int x, int k, int sum[])
{
    for(int i = 0; i <= n - x; i++)
    {
        if((sum[i + x] - sum[i]) >= k)
        {
            return true;
        }
    }
    return false;
}

// Wrapper function
void wrapper(int n, int arr[], int q, int k[])
{
    int sbit[N];
    precomputeSetbits(sbit);

    int sum[N];
    calSum(n, sum, sbit, arr);

    for(int i = 0; i < q; i++)
    {
        // Binary search to find the minimum length.
        int lo = 1, hi = n, ans = -1;
        int mid;

        while (hi - lo >= 0)
        {
            mid = (lo + hi) / 2;
            if (check(n, mid, k[i], sum))
            {
                ans = mid;
                hi = mid - 1;
            }
            else
            {
                lo = mid + 1;
            }
        }
        printf("%d\n", ans);
    }
}

// Driven Program
int main()
{
    int n = 4;
    int arr[] = { 1, 2, 4, 8 };
    int q = 3;
    int k[] = { 1, 2, 3 };

    wrapper(n, arr, q, k);

    return 0;
}

Output:

1
2
3
Comment
Next Article
Mithlesh Upadhyay Published 23 Sep, 2023 · 3 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Similar Reads

  • Find Words containing ‘a’ in given String

    Given a statement(string), the task is to check if that string contains any word which contains ‘a’ in it then print that word otherwise print no any word…

    2 min read
  • Find the Nth Triacontakaihenagonal Number

    Given a number N, the task is to find Nth triacontakaihenagonal number. A triacontakaihenagonal number is class of figurate number. It has 31 – sided polygon called triacontakaihenagon.…

    1 min read
  • Check if a Number is a Tetracontagon Number

    Given an integer N, the task is to check if it is a Tetracontagon number or not. Tetracontagon number is class of figurate number. It has 40 –…

    2 min read
  • Find max rational number in given Array

    Given the Numerator and Denominator of N Rational Numbers, the task is to find the maximum Rational Number from the array. Examples: Input: N = 2 num[] =…

    2 min read
  • Check if a Number is Pentacontagon

    Given an integer N, the task is to check if it is a pentacontagon number or not. Pentacontagon number is class of figurate number. It has 50 –…

    2 min read
  • Check if a Number is Triacontakaidigon

    Given an integer N, the task is to check if it is a Triacontakaidigon number or not. Triacontakaidigon number is class of figurate number. It has 32 –…

    2 min read