Dsa
Intervals
Easy
Interval Length Calculation
You need to calculate the total length of time covered by a set of intervals in a schedule. Some intervals may overlap, and you must ensure that each unit of time is only counted once.
Your program should read from standard input. The first line contains an integer n, indicating the number of intervals. The next n lines each contain two space-separated integers indicating the start and end of an interval. Output the total length of time covered by these intervals without double counting any overlapping time.
Input Format:
- An integer n (1 ≤ n ≤ 10000)
- n lines, each containing two integers (start, end) (0 ≤ start < end ≤ 10^9)
Output Format:
- The total length of time covered by the intervals as an integer.
Example:
Input:
3
1 3
2 5
6 8
Output:
6
Key concepts
intervalslengthaggregation
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