#C8291. Smallest Subarray Length
Smallest Subarray Length
Smallest Subarray Length
You are given an array of n positive integers and a target sum k. Your task is to find the length of the smallest contiguous subarray whose sum is greater than or equal to \( k \). If no such subarray exists, output -1.
Explanation: For each test case, you need to identify the minimal length l such that there exists a contiguous subarray of length l with its elements summing to at least \( k \). You may assume that the array contains only positive integers, which allows the use of the sliding window technique to efficiently solve the problem.
Input Format (stdin): The first line contains an integer T denoting the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers n and k — the number of elements in the array and the target sum, respectively. The second line contains n space-separated positive integers representing the array.
Output Format (stdout): For each test case, output a single integer on a new line representing the length of the smallest contiguous subarray whose sum is greater than or equal to \( k \). If such a subarray does not exist, output -1.
inputFormat
The input is read from stdin and is structured as follows:
- The first line contains an integer T, the number of test cases.
- For each test case, the first line contains two space-separated integers n and k, where n is the number of elements in the array and k is the target sum.
- The second line of each test case contains n space-separated integers representing the array.
outputFormat
For each test case, print a single integer on a new line representing the length of the smallest contiguous subarray with a sum greater than or equal to \( k \). If no such subarray exists, print -1.
## sample1
5 11
1 2 3 4 5
3
</p>