#K65447. Shortest Subarray with Sum at Least Target
Shortest Subarray with Sum at Least Target
Shortest Subarray with Sum at Least Target
You are given an array of integers and an integer target value \(T\). Your task is to find the length of the shortest contiguous subarray such that the sum of its elements is at least \(T\). If no such subarray exists, output \(-1\).
More formally, for an array \(A\) of length \(n\), find the minimum integer \(L\) such that there exists indices \(i\) and \(j\) satisfying \(0 \le i \le j < n\) with \(\sum_{k=i}^{j} A[k] \ge T\) and \(L = j - i + 1\). If no such \(i, j\) exist, return \(-1\).
This problem is a classic example of using the sliding window technique and is commonly solved using a two-pointer strategy.
inputFormat
The first line of the input contains a single integer \(T\) representing the number of test cases. For each test case, the first line contains two integers \(n\) and \(target\), where \(n\) is the number of elements in the array and \(target\) is the target sum. The second line contains \(n\) space-separated integers representing the elements of the array.
Constraints:
- 1 \(\leq\) T \(\leq\) 10
- 1 \(\leq\) n \(\leq\) 105
- -109 \(\leq A[i] \leq\) 109
- 1 \(\leq target \leq\) 109
outputFormat
For each test case, output one line containing a single integer: the length of the shortest subarray with a sum at least equal to \(target\), or \(-1\) if no such subarray exists.
## sample5
6 7
2 3 1 2 4 3
5 11
1 2 3 4 5
3 4
1 4 4
5 15
1 2 3 4 5
5 16
1 2 3 4 5
2
3
1
5
-1
</p>