#K11816. Smallest Subarray with Sum at Least M

    ID: 23553 Type: Default 1000ms 256MiB

Smallest Subarray with Sum at Least M

Smallest Subarray with Sum at Least M

Given an array of positive integers and an integer M, your task is to determine the length of the smallest contiguous subarray such that the sum of its elements is greater than or equal to M. If no such subarray exists, output -1.

This problem can be formalized as follows: find the smallest integer l such that there exists an index i with

[ \sum_{j=i}^{i+l-1}{a_j} \ge M ]

If no such subarray exists, return -1.

inputFormat

The first line contains two integers N and M where N (1 ≤ N ≤ 105) is the number of elements in the array and M (1 ≤ M ≤ 109) is the target sum.

The second line contains N space-separated positive integers representing the elements of the array.

outputFormat

Output a single line containing one integer: the length of the smallest contiguous subarray with a sum greater than or equal to M. If such subarray does not exist, output -1.

## sample
8 15
1 2 3 4 5 6 7 8
2