Simple example
Input
arr = [1, 2, 3, 4, 5], K = 10
Output
4
The subarray [1,2,3,4] has sum 10 and length 4, which is the longest with sum <= 10.
Full lesson preview
Given a non-negative integer array and a target K, find the length of the longest contiguous subarray whose sum is <= K using sliding window.
Problem statement
Task
Examples
Input
arr = [1, 2, 3, 4, 5], K = 10
Output
4
The subarray [1,2,3,4] has sum 10 and length 4, which is the longest with sum <= 10.
Input format
Output format
Constraints
Samples
Input
[3,1,2,1,1], 4
Output
3
Subarray [1,2,1] has sum 4 and length 3.