Gaurav And Sub-array
Practice
4.1 (25 votes)
Algorithms
Binary search
Bit manipulation
Easy
Searching
Two pointer
Problem
83% Success 8054 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

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 \(i^{th}\)integer 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:

\(1 \le N \le 100000 \\ 1 \le Q \le 5 \\ 0 \le K \le 10^9 \\ 0 \le A[i] \le 10^9 \\\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
62 votes
Tags:
Basic ProgrammingBinary SearchEasySearchingapproved
Points:20
54 votes
Tags:
ApprovedBinary SearchData StructuresEasyImplementationOpenSorting
Points:20
98 votes
Tags:
AlgorithmsBinary SearchEasyMathSearching