#C10063. Minimum Contiguous Subarray Length

    ID: 39227 Type: Default 1000ms 256MiB

Minimum Contiguous Subarray Length

Minimum Contiguous Subarray Length

Given a positive integer \(S\) and an array of positive integers, 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, return 0.

Formally, for an array \(a_1, a_2, \ldots, a_n\), find the minimum length \(L\) such that there exists an index \(i\) with \(\sum_{j=i}^{i+L-1} a_j \ge S\). If no contiguous subarray satisfies this condition, output 0.

inputFormat

The input is given via standard input (stdin) with the following format:

  • The first line contains two integers: \(S\) (the target sum) and \(n\) (the number of elements in the array).
  • The second line contains \(n\) space-separated positive integers representing the array.

outputFormat

Output a single integer representing the length of the smallest contiguous subarray whose sum is \(\ge S\). If there is no such subarray, output 0.

## sample
7 6
2 3 1 2 4 3
2