#K52492. Minimum Subarray Length

    ID: 29321 Type: Default 1000ms 256MiB

Minimum Subarray Length

Minimum Subarray Length

You are given an array of non-negative integers and a target integer T. Your task is to find the minimum length of a contiguous subarray such that the sum of its elements is at least T. More formally, given an array a of length n and a target T, you need to find the smallest integer L for which there exist indices 0 \le l \le r < n satisfying

i=lraiT\sum_{i=l}^{r} a_i \ge T

If no such subarray exists, output -1.

Note: It is guaranteed that all elements in the array are non-negative, which enables the use of a sliding window (two-pointer) technique to solve the problem efficiently.

inputFormat

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

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

outputFormat

Output a single integer to standard output (stdout) representing the minimum length of a contiguous subarray for which the sum is at least T. If there is no such subarray, output -1.

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