cswork / OA打开练习工作台

UberOA #37

Find Maximum Length Subarray

估计中等可在线判题

考点(AI 标注)二分查找前缀和区间

Given an array of positive integers and k, find the maximum length L such that every contiguous subarray of length L has a sum strictly less than k. If no positive length works, output 0.

This site has made the standard input and output explicit.

输入格式

The first line contains n k, and the second line contains n positive integers. 1≤n≤200000, 1≤a[i]≤10⁹, 1≤k≤10¹⁸.

输出格式

Output one integer, the answer.

样例 1

输入:

4 8
3 1 2 4

输出:

3

样例 2

输入:

1 9
9

输出:

0

样例 3

输入:

3 10
1 1 1

输出:

3

样例说明

Example 1: the two windows of length 3 have sums 6 and 7, both less than 8; length 4 gives 10, which fails, so the answer is 3. Example 2: the only element sums to 9, which is not strictly less, so the answer is 0. Example 3: the whole array sums to 3, less than 10, so the maximum length is 3.