18. Merge Intervals
MediumSortingIntervals
Merge all overlapping intervals and print the result sorted by start. Intervals that touch (end = start) also merge.
Input format
Line 1: n Next n lines: start end
Output format
One merged interval per line: start end
Example 1
Input
4 1 3 2 6 8 10 15 18
Output
1 6 8 10 15 18
Example 2
Input
2 1 4 4 5
Output
1 5
Constraints
1 ≤ n ≤ 10⁴
Read input from standard input and print the answer to standard output. Your code is judged against 5 test cases, most of them hidden.
Loading the MyLoginn IDE…