Dsa
Intervals
Hard
Solar Array Efficiency
A series of solar panels are placed along a 1D axis. Each panel i covers the interval [S_i, E_i]. However, the system has a power-density limitation: if more than K panels overlap at any specific coordinate, a surge protector triggers, and NONE of the panels at that specific coordinate produce power. If the number of panels at a coordinate is between 1 and K (inclusive), power is produced effectively across the physical length of that segment. Calculate the total length of the axis that is producing power.
Input Format:
The first line contains two integers: K (the maximum allowed overlap) and N (the number of panels).
The next N lines each contain two integers: S_i and E_i.
Output Format:
A single integer representing the total length of the segments where 1 <= panels <= K.
Example:
Input:
1 2
1 5
4 8
Output:
6
(Explanation: The segment [1, 4] has 1 panel, [4, 5] has 2 panels, and [5, 8] has 1 panel. Since K=1, only [1, 4] and [5, 8] produce power. Total length = 3 + 3 = 6.)
Key concepts
sweep-lineintervals
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