#C1954. Smallest Subarray with Sum at Least K
Smallest Subarray with Sum at Least K
Smallest Subarray with Sum at Least K
You are given an array of integers and a target sum k. Your task is to find the length of the smallest contiguous subarray such that the sum of its elements is greater than or equal to k. If no such subarray exists, output -1
.
More formally, given an array a[0], a[1], ..., a[n-1]
and an integer k, you need to find the minimum length L such that there exist indices start and end satisfying
If no subarray satisfies the condition, print -1
.
Input/Output: All input is taken from stdin and output is printed to stdout.
inputFormat
The first line contains two integers n
and k
where n
is the number of elements in the array and k
is the target sum.
The second line contains n
space-separated integers representing the elements of the array.
outputFormat
Output a single integer representing the length of the smallest contiguous subarray whose sum is at least k
. If no such subarray exists, output -1
.
5 15
1 2 3 4 5
5