#C4536. Smallest Subarray with Target Sum
Smallest Subarray with Target Sum
Smallest Subarray with Target Sum
You are given an array of n positive integers and a target sum s. 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 s. If no such subarray exists, output 0.
The problem can be interpreted in mathematical notation as follows:
Find the minimum l such that there exists an index i with $$\sum_{j=i}^{i+l-1} a_j \ge s.$$ If no such l exists, return 0.
This problem is a classic application of the sliding window technique.
inputFormat
The first line of input contains two integers n
and s
separated by a space, where n
is the number of elements in the array and s
is the target sum.
The second line contains n
space-separated positive integers representing the array.
outputFormat
Output a single integer which is the length of the smallest contiguous subarray whose sum is greater than or equal to s. If no such subarray exists, output 0.
## sample10 15
1 2 3 4 5 6 7 8 9 10
2