2Brute2Best
🔥0

Longest Continuous Subarray With Absolute Diff ≤ Limit

Medium·LeetCode #1438 Pro OnlyBasic Reinforcement
LeetCode
01

Problem Statement

You are given an integer array nums and a number limit.

Find the longest contiguous subarray in which every pair of elements differs by at most limit. In other words, the absolute difference between any two elements of the subarray must be ≤ limit.

Return the length of that subarray.

Input

An integer array nums of length n, and an integer limit.

Output

A single number: the length of the longest subarray where any two elements differ by at most limit.

Example 1
Input: nums = [8, 2, 4, 7], limit = 4
Output: 2

[2, 4] (difference 2) and [4, 7] (difference 3) work. Any subarray of length 3 has a pair that differs by more than 4, e.g. [2, 4, 7] has 7 − 2 = 5.

Example 2
Input: nums = [10, 1, 2, 4, 7, 2], limit = 5
Output: 4

[2, 4, 7, 2] has largest 7 and smallest 2, so the biggest difference is 5, which is allowed.

Example 3
Input: nums = [4, 2, 2, 2, 4, 4, 2, 2], limit = 0
Output: 3

With limit 0, all elements must be equal. The longest run of equal values is [2, 2, 2].

Constraints
  • • 1 ≤ n ≤ 10^5
  • • 1 ≤ nums[i] ≤ 10^9
  • • 0 ≤ limit ≤ 10^9
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“longest contiguous subarray”
→ The answer is a window, and its size is the answer → variable sliding window.
“any two elements differ by at most limit”
→ The worst pair is max and min, so validity is max − min ≤ limit.
“max and min of a changing window”
→ Elements leave from the left, so a single max/min variable won't do → monotonic deques.
03

Approach Complexity Ladder

Brute Force — every start, grow to the rightO(N²)O(1)

For each start, extend the end while tracking max and min; stop when the spread is too big

Better — sliding window + multisetO(N log N)O(N)

Variable window; a sorted multiset gives the window's min and max

Optimal — sliding window + two monotonic dequesO(N)O(N)

Decreasing deque for the max, increasing deque for the min, shrink while max − min > limit

Brute Force Intuition

Fix a start. Move the end to the right one step at a time, keeping a running max and min. While max − min ≤ limit, the subarray is good — update the answer. As soon as it breaks, stop: a longer subarray from this start can only be worse.

Recognition rule for this sub-pattern:

You need the max or min of the current window at every step. A deque keeps only elements that could still become the max/min.
Remember this · Sliding Window
Trigger
Contiguous subarray / substring + longest, shortest, maximum, count, or "of size K".
Think
Can I keep a valid range and move it, instead of checking every subarray from scratch?
Key question
When is my current window valid?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Sliding Window translate into production architectures and low-level memory mechanics.