Dsa
Sliding Window
Medium
The Stable Range Sum
In a physical experiment, a 'stable summit' is a period where the range of values (Max - Min) does not exceed D, and the total sum of values does not exceed S. Find the maximum length of such a period.
Input Format:
- The first line contains two integers: D (max difference) and S (max sum).
- The second line contains a space-separated sequence of integers.
Output Format:
- A single integer representing the maximum length.
Example:
Input:
2 10
1 2 3 4 5
Output:
3
(Explanation: Windows: [1,2,3] (range 2, sum 6), [2,3,4] (range 2, sum 9), [3,4,5] (range 2, sum 12 - fails sum). Max length is 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