Gaurav And Sub-array

4.1

25 votes
Algorithms, Binary Search, Bit manipulation, Easy, Searching, Two pointer
Problem

You are given an array A[] consisting of N non-negative integers. Now, you need to answer Q queries of the following type given an integer K in each query.

You need to find the minimum length L of any subarray of A, such that if all elements of this subarray are represented in binary notation and concatenated to form a binary string, then no of 1's in the resulting string is at least K.  

Input Format:

The first line of the input consists of two space-separated integers N and Q.

The second line contains N space separated integers, where the ithinteger denotes A[i]

Next Q lines contains a non-negative integer K.

Output Format:

For each query out of the Q ones, print the answer on a new line. If for a paritcular query no valid subarray exists, then print -1 instead as the answer to that query. 

Constraints:

1N1000001Q50K1090A[i]109

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

For first query consider subarray A[1,1], then binary string representing A[1,1] is 01 which has one 1's.

For second query consider subarray A[1,2], then binary string is 0110 which has two 1's.

Similarly, for third query consider subarray A[1,3].

Editor Image

?