#C4695. Minimum Subarray Length

    ID: 48261 Type: Default 1000ms 256MiB

Minimum Subarray Length

Minimum Subarray Length

You are given an array of n integers and an integer s. Your task is to find the minimum length of a contiguous subarray of which the sum is at least s. If no such subarray exists, output 0.

Formally, given an array nums, you are to find the minimum positive integer l such that there exists an index i with $$ \sum_{j=i}^{i+l-1} nums[j] \ge s, $$ where the summation is taken over a contiguous segment of the array. If no such segment exists that satisfies the condition, output 0.

This problem is a classic sliding window challenge.

inputFormat

The input is read from stdin and it consists of two lines:

  • The first line 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 integers representing the array elements, separated by spaces.

Constraints:

  • 1 ≤ n ≤ 105
  • 1 ≤ array elements ≤ 109

outputFormat

Output a single integer to stdout representing the minimum length of a contiguous subarray whose sum is at least s. If no such subarray exists, output 0.

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