#K2811. Smallest Subarray with Sum at Least X

    ID: 24820 Type: Default 1000ms 256MiB

Smallest Subarray with Sum at Least X

Smallest Subarray with Sum at Least X

You are given an array of integers and several queries. For each query, find the length of the smallest contiguous subarray whose sum is greater than or equal to the query value X. If no such subarray exists, output -1 for that query.

In other words, for each query value \(X\), you must find the minimum length \(L\) such that there exists indices \(i\) and \(j\) (with \(i \le j\)) satisfying

\(\sum_{k=i}^{j} arr[k] \ge X\)

If no such subarray exists, print -1 for that query.

Note: Use an efficient algorithm (e.g. sliding window) to handle large inputs.

inputFormat

The input is given via standard input (stdin) in the following format:

Line 1: An integer n representing the number of elements in the array. Line 2: n space-separated integers denoting the array elements. (If n is 0, this line will be empty.) Line 3: An integer m representing the number of queries. Line 4: m space-separated integers denoting the query values.

outputFormat

Print a single line containing m space-separated integers, where the i-th integer is the length of the smallest contiguous subarray with sum greater than or equal to the i-th query value, or -1 if no such subarray exists.## sample

10
1 2 3 4 5 6 7 8 9 10
3
15 35 100
2 5 -1