15% off all plans & roadmaps Use code
See plansLeetCode 1438 · Solution & editorial
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.
Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit is a Sliding Window problem, in the Variable Size (Condition-Based) subpattern. The pattern covers fixed-size, variable-size, and character frequency matching window techniques
Master variable-size sliding window for condition-based problems like longest substring without repeating characters.
The reference solution runs in O(n) time and O(n) space.
Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit has been reported in interviews at Google.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit is a Sliding Window problem, in the Variable Size (Condition-Based) subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.