Merge overlapping intervals

Easy
0/40
Average time to solve is 10m
profile
Contributed by
45 upvotes
Asked in companies
Chegg Inc.GrowwGoldman Sachs

Problem statement

Given 'N' number of intervals, where each interval contains two integers denoting the boundaries of the interval. The task is to merge all the overlapping intervals and return the list of merged intervals sorted in ascending order.

Two intervals will be considered to be overlapping if the starting integer of one interval is less than or equal to the finishing integer of another interval, and greater than or equal to the starting integer of that interval.

Example:
for the given 5 intervals - [1,4], [3,5], [6,8], [10,12], [8,9].
Since intervals [1,4] and [3,5] overlap with each other, we will merge them into a single interval as [1,5].

Similarly [6,8] and [8,9] overlaps, we merge them into [6,9].

Interval [10,12] does not overlap with any interval.

Final List after merging overlapping intervals: [1,5], [6,9], [10,12]
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of input contains an integer 'T' representing the number of the test case. Then the test case follows.

The first line of each test case contains an integer 'N', the number of intervals.

The second line of the test case contains 'N' integers, the starting integer of 'N' intervals.

The third line of the test case contains 'N' integers, the ending integer of 'N' intervals.
Output Format:
For each test case, print 'S' lines, each contains two single space-separated integers 'a', and 'b', where 'S' is the size of the merged array of intervals, 'a' is the start time of an interval and 'b' is the end time of the same interval.

Print the output of each test case in a separate line.
Constraints:
1 <= T <= 100
1 <= N <= 1000
0 <= start, finish <= 10^9

Where 'T' denotes the number of test cases, 'N' denotes the number of intervals respectively, 'start' and 'finish' are the starting and finishing times for intervals.   

Time Limit: 1 sec
Sample Input 1:
1
2
1 3
3 5
Sample Output 1:
1 5  
Explanation for Sample Input 1
Since these two intervals overlap at point 3 so we merge them and the new interval becomes (1,5).
Sample Input 2:
3
5
1 3 6 8 10
4 5 8 9 12
10
1 2 3 1 9 5 11 7 12 14
3 7 5 8 10 11 12 10 12 17
7
1 4 2 5 6 2 8
2 6 7 6 9 11 12
Sample Output 2:
1 5
6 9
10 12
1 12
14 17
1 12
Hint

What are the conditions when an interval needs to be merged?

Approaches (2)
Brute Force
  1. We are given the function MERGEINTERVALS(), which takes a 2D vector representing the vector of intervals and returns another 2D vector which is the vector of merged intervals.
  2. We create another function ISOVERLAP() to check if the current interval overlaps with the other interval.
  3. Now we create an empty 2D vector “RES” to store finally merged intervals and another boolean vector “VIS” to mark if the current interval is visited or not.
  4. To begin with, we mark each interval in VIS as false or unvisited.
  5. Now we run a loop for the total number of intervals and find non visited intervals.
  6. For each non-visited interval, if there exists an overlapping interval with the current interval we will merge both intervals,
  7. After merging, we update the current interval with the largest of both intervals and mark them visited.
  8. Every merged interval is then stored in the final 2D vector “RES”.
  9. Finally, we sort “RES” and return it as our answer.
Time Complexity

O(N^2), where N is the number of intervals.

 

In the worst case, for each interval, we will be checking all other intervals. For the N interval, the overall time complexity is O(N^2).

Space Complexity

O(N), where N is the number of intervals.

 

In the worst case, we will be using an extra array of size N to mark intervals visited. Hence, the overall space complexity is O(N).

Code Solution
(100% EXP penalty)
Merge overlapping intervals
Full screen
Console