# Merge overlapping intervals

Posted: 8 Jan, 2021

Difficulty: Easy

#### 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]
```

##### 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
```

Approach 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.
- We create another function ISOVERLAP() to check if the current interval overlaps with the other interval.
- 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.
- To begin with, we mark each interval in VIS as false or unvisited.
- Now we run a loop for the total number of intervals and find non visited intervals.
- For each non-visited interval, if there exists an overlapping interval with the current interval we will merge both intervals,
- After merging, we update the current interval with the largest of both intervals and mark them visited.
- Every merged interval is then stored in the final 2D vector “RES”.
- Finally, we sort “RES” and return it as our answer.

Approach 2

Our last approach was very simple and easy, but it’s time complexity was high. We can improve our solution by sorting the intervals and then combine them

- 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.
- We will first sort the intervals by non-decreasing order of their start integers.
- We will create a 2D vector “RES” which will be our resultant vector storing all the merged intervals. Now, we will add the first interval to our resultant list.
- Now, we will run a loop for the total number of intervals and for each interval, we will check whether the current interval is overlapping with the last interval in our resultant list or not.
- If it overlaps with the last interval, we will update the end integer of the last interval in the list by the maximum of the end integers of both overlapping intervals.
- If it doesn't overlap, we will add the current interval to the “RES” vector.
- Finally, we will return our resultant vector “RES” as our final answer.