#K13901. Smallest Subarray with Sum Greater Than or Equal to K
Smallest Subarray with Sum Greater Than or Equal to K
Smallest Subarray with Sum Greater Than or Equal to K
You are given an array of integers. Your task is to find the length of the smallest contiguous subarray whose sum is greater than or equal to a given target value \(k\). If no such subarray exists, output -1.
Example: For the array [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and \(k = 15\), the smallest subarray with a sum greater than or equal to 15 is [7, 8] with length 2.
inputFormat
The input consists of two lines. The first line contains two integers (n) and (k) denoting the number of elements in the array and the target sum respectively. The second line contains (n) space-separated integers representing the array elements.
outputFormat
Output a single integer representing the length of the smallest contiguous subarray whose sum is at least (k). If there is no such subarray, output -1.## sample
10 15
1 2 3 4 5 6 7 8 9 10
2