Longest Subarray Within a Budget

medium
Unit 10 · Sliding Windows0/3 to milestone

Given non-negative costs and a budget, return the length of the longest contiguous subarray whose sum is at most budget. Solve it in O(n) with a sliding window.

Also return the start index of the first such longest window: implement longest_within(costs, budget) returning (length, start) ((0, 0) if no single element fits).

#sliding-window#two-pointers2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Longest Subarray Within a Budget · TechGradify