#K80547. Find the Smallest Range with a Given Sum
Find the Smallest Range with a Given Sum
Find the Smallest Range with a Given Sum
You are given an array \( A \) of \( n \) integers and \( q \) queries. For each query, you need to find the smallest contiguous subarray (i.e., segment) whose sum is exactly equal to a given integer \( x \). Formally, you need to find the range \( [l, r] \) (using 1-indexing) such that
\( \sum_{i=l}^{r} A_i = x \)
If there are multiple subarrays satisfying the condition, output the one with the smallest length. If no valid subarray exists, output -1
.
Note: The input is read from standard input and the output should be written to standard output.
inputFormat
The first line contains an integer \( n \) denoting the number of elements in the array \( A \).
The second line contains \( n \) space-separated integers, representing the array \( A \).
The third line contains an integer \( q \) denoting the number of queries.
The fourth line contains \( q \) space-separated integers, each representing a query \( x \).
outputFormat
For each query, output one line. If a valid subarray exists, output two space-separated integers \( l \) and \( r \) (the starting and ending indices, 1-indexed) of the minimal subarray; otherwise, output -1
.
5
1 -1 2 3 -1
3
5 3 1
1 4
2 5
1 1
</p>