#C3947. Minimum Size Subarray Sum
Minimum Size Subarray Sum
Minimum Size Subarray Sum
Given an array of non-negative integers nums
and a positive integer S
, find the length of the smallest contiguous subarray for which the sum is at least S
.
Mathematically, if we denote the subarray from index l to r as \(nums[l], nums[l+1], \ldots, nums[r]\), you are to find the smallest k = r - l + 1 such that:
\(\sum_{i=l}^{r} nums[i] \ge S\)
If there is no such subarray, output 0
.
This problem can be efficiently solved using the sliding window (two pointers) technique.
inputFormat
The input is read from stdin and has the following format:
- The first line contains an integer
S
, the target sum. - The second line contains an integer
n
, representing the number of elements in the array. - The third line contains
n
space-separated non-negative integers representing the arraynums
.
outputFormat
Output to stdout a single integer representing the length of the smallest contiguous subarray of which the sum is at least S
. If no such subarray exists, output 0
.
7
6
2 3 1 2 4 3
2
</p>