Dsa
Sliding Window
Easy
The Energy Budget
An electric vehicle tracks its energy consumption per mile. Negative or zero values indicate energy regained via regenerative braking. Find the longest contiguous journey where the total energy consumed does not exceed S AND the number of miles with zero or negative energy ( regenerative miles) does not exceed Z.
Input Format:
- The first line contains three integers: S (the energy limit), Z (the max regenerative miles), and N (the total miles).
- The second line contains N space-separated integers representing energy per mile.
Output Format:
- Print the length of the longest such journey.
Example:
Input:
10 1 6
2 0 4 0 5 1
Output:
4
Explanation: The segment [4, 0, 5, 1] has a sum of 10 and exactly 1 regenerative mile (0).
Key concepts
sliding_windowmulti_conditiontwo_pointers
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