Goldman SachsOA #21
Longest Subarray
Estimated MediumJudge-ready
Topics (AI-labelled)Two PointersPrefix & Ranges
Given an array of positive integers, find the length of the longest contiguous subarray whose sum does not exceed k.
Input
The first line contains n k (1≤n≤100000, 1≤k≤10^9); the second line contains n positive integers a[i] (1..1000).
Output
Output the maximum length of a subarray with sum ≤k; output 0 if no non-empty subarray qualifies.
Example 1
Input:
3 4
1 2 3
Output:
2
Example 2
Input:
4 4
3 1 2 3
Output:
2
Example 3
Input:
1 6
7
Output:
0
Editorial
Loading solution…