#K42647. Minimum Size Subarray Sum

    ID: 27134 Type: Default 1000ms 256MiB

Minimum Size Subarray Sum

Minimum Size Subarray Sum

Given an array of n positive integers and a positive integer S, find the minimal length of a contiguous subarray of which the sum is at least S. If there is no such subarray, return 0.

You are required to solve the problem using an efficient approach with a time complexity of approximately \(O(n)\). Formally, given an array \(a_1, a_2,\dots,a_n\), find the smallest integer \(L\) such that there exists an index \(j\) (with \(1 \leq j \leq n-L+1\)) satisfying:

\[ a_j + a_{j+1} + \cdots + a_{j+L-1} \geq S \]

If no such \(L\) exists, output 0.

inputFormat

The first line of input contains two integers n and S, 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 array elements \(a_1, a_2, \dots, a_n\).

outputFormat

Print a single integer, which is the length of the smallest contiguous subarray whose sum is at least S. If no such subarray exists, print 0.

## sample
10 15
1 2 3 4 5 6 7 8 9 10
2