#C6262. Maximum Valid Subarray Length

    ID: 50003 Type: Default 1000ms 256MiB

Maximum Valid Subarray Length

Maximum Valid Subarray Length

You are given an array of positive integers and an integer threshold T. Your task is to find the maximum length of a contiguous subarray that can be obtained by processing the array with the following sliding window procedure:

Iterate over the array while accumulating the sum of elements. Whenever the current window sum satisfies \[ \sum_{i=l}^{r} a_i \ge T \] update your answer with the current window length, and then shrink the window from the left by removing the leftmost element. Continue this process until the entire array is processed. If no subarray satisfies the condition, output 0.

Note: Although a longer subarray might exist overall, the procedure always shrinks the window as soon as the threshold is met, and the answer is the maximum window length encountered during this process.

inputFormat

The input is read from standard input and consists of two lines:

  • The first line contains two integers n and T where n is the number of elements in the array and T is the threshold.
  • The second line contains n space-separated positive integers.

It is guaranteed that all numbers are positive integers.

outputFormat

Output a single integer — the maximum length of a contiguous subarray, determined by the procedure described above, whose sum is greater than or equal to T. If no valid subarray exists, output 0.

## sample
5 7
2 1 5 6 2
3

</p>