#K51077. Minimum Subsegment Length
Minimum Subsegment Length
Minimum Subsegment Length
You are given an array of n positive integers and a target sum S. Your task is to find the minimum length of a contiguous subarray (subsegment) such that the sum of the subarray is greater than or equal to S. If no such subarray exists, output -1.
Formally, given an array a of numbers, find the smallest integer k such that there exists an index i satisfying
If no valid subsegment exists, print -1.
Example: For n = 8
, S = 15
, and a = [1, 2, 3, 4, 5, 6, 7, 8]
, the minimum valid subsegment is [7,8]
whose length is 2.
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 positive integers separated by spaces representing the elements of the array.
outputFormat
Output a single integer, the minimum length of a contiguous subarray whose sum is greater than or equal to S. If no such subarray exists, output -1.
## sample8 15
1 2 3 4 5 6 7 8
2
</p>