#K73907. Smallest Subarray with Sum

    ID: 34079 Type: Default 1000ms 256MiB

Smallest Subarray with Sum

Smallest Subarray with Sum

You are given an array of positive integers and an integer S. Your task is to find the minimal length of a contiguous subarray such that the sum of its elements is greater than or equal to S. If no such subarray exists, output 0.

The problem can be formally described as finding the smallest integer l such that there exists an index i for which

\(\sum_{j=i}^{i+l-1} arr[j] \ge S\)

If no valid subarray exists, the program should return 0.

You need to read input from standard input (stdin) and output your answer to standard output (stdout).

inputFormat

The input consists of two lines:

  1. The first line contains two integers N and S, where N is the number of elements in the array, and S is the target sum.
  2. The second line contains N space-separated positive integers representing the elements of the array.

outputFormat

Output a single integer, which is the minimal length of a contiguous subarray whose sum is greater than or equal to S. If such a subarray does not exist, output 0.

## sample
6 7
2 1 5 2 3 2
2