#K4501. Smallest Subarray With Sum
Smallest Subarray With Sum
Smallest Subarray With Sum
You are given an array of integers nums
and an integer k
. Your task is to find the length L of the smallest contiguous subarray such that its sum is greater than or equal to k
. If no such subarray exists, output -1.
Formally, you need to find the minimum L (with L ≥ 1) such that there exists an index i with
$\displaystyle \sum_{j=i}^{i+L-1} nums[j] \ge k$
If there exists no subarray satisfying the condition, return -1.
inputFormat
The input is given via stdin in the following format:
a1 a2 a3 ... an k
Where:
n
is a positive integer representing the number of elements in the array.a1, a2, ..., an
are the integers of the arraynums
, separated by spaces.k
is the target sum.
outputFormat
Output a single integer to stdout which is the length of the smallest contiguous subarray with sum greater than or equal to k
. If no such subarray exists, output -1.
5
1 2 3 4 5
11
3
</p>