#K85797. Longest Subarray with Sum Constraint
Longest Subarray with Sum Constraint
Longest Subarray with Sum Constraint
You are given an array of integers and a set of query values. For each query, you need to determine the length of the longest contiguous subarray whose sum does not exceed the given query value.
Formally, given an array A of length n and a query value Q, you must find the maximum length L such that there exists indices i and j (with i ≤ j) for which:
\( \sum_{k=i}^{j} A[k] \le Q \)
You are expected to use an efficient approach, for example using a sliding window technique, to find the answer for each query.
inputFormat
The input is given in the following format from stdin:
n A1 A2 ... An m Q1 Q2 ... Qm
Where:
- n - the number of elements in the array.
- A1, A2, ..., An - the array elements, separated by spaces.
- m - the number of queries.
- Q1, Q2, ..., Qm - the query values, separated by spaces.
outputFormat
Print a single line to stdout containing m integers separated by spaces. Each integer is the length of the longest contiguous subarray for the corresponding query where the sum of the subarray is less than or equal to the query value.
## sample5
1 2 3 4 5
3
5 7 10
2 3 4