Problem statement
Given an array and a limit, find the longest run of neighbouring values whose biggest value minus its smallest is at most the limit.
The answer is a window, so both edges only move right. Two deques keep the window maximum and minimum, which makes the spread one subtraction instead of a rescan.
Examples
Input[8, 2, 4, 7], limit = 4
Output2
The pair 2, 4 fits, and 2, 4, 7 does not, because 7 - 2 is 5.
Input[3, 3, 3, 3], limit = 0
Output4
Every value is equal, so the spread is 0 and the whole array fits.
The walkthrough is part of Premium
The statement and the examples above stay free. The full how-to-solve walkthrough, the cost notes in plain words and the interactive step-by-step visualizer unlock with a Premium plan.
See pricing