Skip to the editor
JSGroundwork
JSGroundwork handwritten · web dev
←Back to Sliding window

Ready to read

JSJavaScript⑂Git◎Interview prepΣDSA in JSSDSystem Design
More topics15›
</>HTML{ }CSS⚛ReactNNext.jsNeNest.jsTSTypeScriptNoNode.js🐳DockerDBSQL & Databases✓Testing🔒Web Security☁Cloud & DevOps◈GraphQL◆Redis☸Kubernetes
☰‹›
Description
All topics›JavaScript›Sliding window

Longest Continuous Subarray With Absolute Diff <= Limit

advancedlayer B5 · Sliding window5 tests

Given an integer array nums and an integer limit, return the length of the longest contiguous subarray in which the absolute difference between any two elements is <= limit.

  • Checking every pair is unnecessary: the condition is equivalent to max(window) - min(window) <= limit
  • An empty array yields 0; a single element always qualifies
  • Target complexity: O(n). A sorted structure or repeated re-scanning of the window gives O(n log n) or O(n^2) — the intended answer keeps both the running max and the running min in amortised O(1) per step

Stuck?

All exercisesPlayground
1234567891011121314
loading the editor…

Nothing yet — hit Run and whatever you log shows up here.

Submit to see how you did.

⌘/Ctrl + Enter run⌘/Ctrl + S save⌘/Ctrl + / comment⌘/Ctrl + F find/replace⌘/Ctrl + D select next matchAlt + click multi-cursorTab indent · ⇧Tab outdentEsc leave fullscreen