Back to DSA sheet
Merge Intervals
MediumArraysGiven a list of intervals, merge every set of overlapping intervals and return the non-overlapping result.
Input format (stdin): the first line has n. Each of the next n lines has two integers: an interval's start and end. Output the merged intervals, one per line as 'start end', sorted by start.
Examples
Input: 4
1 3
2 6
8 10
15 18
Output: 1 6
8 10
15 18
[1,3] and [2,6] overlap into [1,6].
Input: 2
1 4
4 5
Output: 1 5
[1,4] and [4,5] touch and merge.
Constraints
- 1 <= n <= 10^4
- start <= end
Sheets
Blind 75Grind 75NeetCode 150NeetCode 250Striver A2Z
merge-intervals.cpp2 sample tests
Loading editor
Test results
Run the sample tests to check your solution against expected output.
Custom input (stdin)
Output
Run your code to see its output.