#C8364. Merge Overlapping Intervals
Merge Overlapping Intervals
Merge Overlapping Intervals
You are given N intervals. Each interval is represented by two integers: the start and the end time. Two intervals overlap if the start of one is less than or equal to the end of the previous one. In mathematical terms, two intervals \([a, b]\) and \([c, d]\) are overlapping if \(c \leq b\). When two intervals overlap, they should be merged into a single interval that spans from the minimum start time to the maximum end time.
Your task is to merge all overlapping intervals and output the resulting set of intervals sorted by their start times.
Note: The intervals are given in a half-open style \([start, end]\), but you can assume that if two intervals overlap (i.e. \(c \leq b\)), they should be merged.
inputFormat
The first line of input contains a single integer N — the number of intervals.
The next N lines each contain two integers start and end, representing an interval.
Example:
3 1 3 2 4 5 7
outputFormat
The first line of output should contain a single integer M — the number of merged intervals.
The following M lines should each contain two integers indicating the start and end of a merged interval.
Example:
2 1 4 5 7## sample
3
1 3
2 4
5 7
2
1 4
5 7
</p>