Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit

Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.

Example 1
Inputnums = [8,2,4,7], limit = 4
Output2
All valid subarrays have length at most 2, such as [2, 4] and [4, 7], so the longest size is 2.
Example 2
Inputnums = [10,1,2,4,7,2], limit = 5
Output4
The subarray [2, 4, 7, 2] is the longest since the maximum absolute difference is |2 - 7| = 5 <= 5.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^9
  • 0 <= limit <= 10^9

Asked at 15 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate