#C5259. Smallest Subarray with Given Sum

    ID: 48888 Type: Default 1000ms 256MiB

Smallest Subarray with Given Sum

Smallest Subarray with Given Sum

You are given an array of positive integers and a target sum \(S\). Your task is to find the length of the smallest contiguous subarray whose sum is greater than or equal to \(S\). If no such subarray exists, output 0.

For a subarray defined by indices \(i\) to \(j\) (with \(0 \leq i \leq j < n\)), its sum is given by:

[ \text{sum} = \sum_{k=i}^{j} a_k ]

You need to determine the minimal length \(L = j - i + 1\) such that \(\text{sum} \geq S\). The input ends when \(n = 0\).

inputFormat

The input is read from stdin and consists of multiple test cases. Each test case is given in the following format:

  1. An integer \(n\) representing the number of elements in the array. If \(n = 0\), the input terminates and no further test cases should be processed.
  2. An integer \(S\) representing the target sum.
  3. A line containing \(n\) space-separated integers representing the array elements.

There may be multiple test cases before the termination line.

outputFormat

For each test case, output the length of the smallest contiguous subarray with sum greater than or equal to \(S\) on a separate line. If no such subarray exists, output 0.

Output is written to stdout.

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

3

</p>