Loading...
Given an integer array nums and an integer limit, return the length of the longest non-empty contiguous subarray in which the difference between the maximum and minimum elements is at most limit.
Equivalently: the absolute difference between any two elements of the chosen subarray must be less than or equal to limit.
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: The subarray [2,4] has max - min = 2 <= 4. No subarray of length 3 or more qualifies: every such subarray contains both 8 and 2 (difference 6) or both 2 and 7 (difference 5).
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] has maximum 7 and minimum 2, and 7 - 2 = 5 <= 5. No longer subarray qualifies.
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Explanation: With limit 0, all elements of the subarray must be equal. The longest run of equal values is [2,2,2].
nums.length ≤105nums[i] ≤109limit ≤109Click "Run" to test with sample cases or "Submit" to run all tests.