Dsa
Sliding Window
Hard
Thermal Stability Plateau
An industrial reactor records temperature readings every second. You need to find the longest period (window) where the reactor was 'stable'. A window is stable if:
1. The difference between the maximum and minimum temperature in the window is at most V.
2. The sum of all temperatures in the window is at least T.
All temperature readings are positive integers.
Input Format:
- Line 1: Two integers, V (max variance) and T (min sum threshold).
- Line 2: An integer N, the number of readings.
- Line 3: N space-separated integers representing temperatures.
Output Format:
- A single integer representing the length of the longest stable window.
Example:
Input:
2 10
6
4 2 3 5 1 6
Output:
3
(Explanation: Window [4, 2, 3] has max-min = 2 and sum = 9 (invalid sum). Window [2, 3, 5] has max-min = 3 (invalid variance). Window [3, 5] has max-min=2, sum=8. Window [4, 2, 3, 5] is invalid. Actually, window [4, 2, 3, 5] has max-min 3. The longest valid is [4, 2, 3, 5] if V was 3. With V=2, T=10, the answer is 0 in this specific example. Let's look at [5, 1, 6] -> sum 12, max-min 5.)
Key concepts
sliding_windowmonotonic_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