Requirements
- You receive an integer array
arr together with an integer k.
- Determine the maximum length of any contiguous portion of the array whose total is at most
k.
- The usual version restricts array entries to non-negative numbers; that restriction is required for the standard sliding-window method.
longest_subarray_at_most_k(arr, k)
Notes
- Consider the case where
k < arr[0] while every value is positive: the answer may be 0 when no individual value qualifies, or 1 when the smallest value qualifies, based on the precise problem statement. Confirm that detail before implementing.
Preparation
- Similar problems include LC 209, "Minimum Size Subarray Sum" (sum ≥ target), and LC 862, "Shortest Subarray with Sum at Least K" (the negative-number version).