#C3105. Smallest Subarray with Given Sum

    ID: 46496 Type: Default 1000ms 256MiB

Smallest Subarray with Given Sum

Smallest Subarray with Given Sum

You are given an array of positive integers and a target sum \( S \). Your task is to find the length of the smallest contiguous subarray whose sum is greater than or equal to \( S \). If no such subarray exists, return \( -1 \).

Example:

Input: S = 7, arr = [2, 3, 1, 2, 4, 3]
Output: 2

The subarray [4, 3] has a sum of 7 and is the smallest subarray meeting the criteria.

inputFormat

The first line of input contains an integer \( T \) representing the number of test cases. Each test case consists of two lines. The first line contains two integers: \( S \) (the target sum) and \( n \) (the number of elements in the array). The second line contains \( n \) space-separated positive integers representing the array.

Example:

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

outputFormat

For each test case, output a single line containing the length of the smallest contiguous subarray whose sum is at least \( S \). If such a subarray does not exist, output \( -1 \).

Example:

2
5
-1
## sample
1
7 6
2 3 1 2 4 3
2

</p>