Dsa
Sliding Window
Easy
The Stable Sensor
You are monitoring a high-precision sensor that records integers representing atmospheric pressure changes. To identify a 'stable' period, you must find the length of the longest contiguous sequence of readings where the difference between the maximum and minimum values in that sequence is no more than K.
Input Format:
- The first line contains two integers: K (the maximum allowed difference) and N (the number of readings).
- The second line contains N space-separated integers representing the sensor readings.
Output Format:
- Print a single integer representing the length of the longest stable contiguous sequence.
Example:
Input:
3 8
10 12 15 11 10 18 20 21
Output:
3
Explanation: The sequence [18, 20, 21] has max 21 and min 18 (diff 3 <= 3). [12, 11, 10] is also length 3.
Key concepts
sliding_windowdequemonotonic_queue
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now