# Insert Interval

#### You are given a 2-dimensional array ‘Intervals’ containing a list of non-overlapping intervals sorted by their start time. You are given an interval ‘newInterval’. Your task is to insert the given interval at the correct position and merge all necessary intervals to produce a list with only mutually exclusive intervals.

##### For Example:

```
Consider 'Intervals' = [[1, 3], [5, 7], [8, 12]], and 'newInterval' = [4, 6]
The interval [4, 6] overlaps with [5, 7]. Therefore we can merge the intervals and produce an interval [4, 7]. Hence the answer [[1,3], [4,7], [8,12]]
```

##### Input Format:

```
The first line of the input contains a single integer, 'T,’ denoting the number of test cases.
The first line of each test case contains a single integer, ‘N’, denoting the number of intervals.
The following ‘N’ lines of the test case contain two space-separated integers, ‘Intervals[i][0]’ and ‘Intervals[i][1]’, denoting the start and the end of the ‘i-th’ interval.
The last line of the test case contains two space-separated integers, ‘newInterval[0]’ and ‘newInterval[1]’, denoting the interval to be inserted into the list of intervals.
```

##### Output Format:

```
For each test case, print the intervals sorted by their start time. Each interval is to be printed in a separate line in a space-separated manner.
Print the output of each test case in a separate line.
```

##### Note:

```
You do not need to print anything. It has already been taken care of. Just implement the function.
```

##### Constraints:

```
1 <= T <= 5
0 <= N <= 10^5
0 <= Intervals[i][0], Intervals[i][1] <= 10^10
0 <= newInterval[0], newInterval[1] <= 10^10
Time Limit: 1 sec
```

In this approach, we will insert the **newInterval** in the given array of intervals at the correct position according to **newInterval[0]**. After inserting, we will check if the **newInterval** overlaps with other intervals. If an interval overlaps with the other interval, we will merge them into one interval.

The conditions we have to check while inserting the **newInterval **are-:

- The
**newInterval**is to be added at either the start or the end of the array. - The
**newInterval**overlaps the whole array, meaning the**newInterval[0]**is less than the start of the first interval in the array, i.e.,**Interval[0][0]**. The**newInterval[1]**is greater than the last interval in the**Intervals**array, i.e.,**Interval[N - 1][1].** - The
**newInterval**is to be inserted in the middle of the array. Then we will have to check if the interval**newInterval**overlaps with other intervals or not.

We will create a function **doesOverlap(interval1, interva2)**, which returns a boolean if the given intervals **intervals1** and **interval2** overlap with each other or not.

Algorithm:

- The function
**doesOverlap(interval1, interval2):**- Check if the minimum of
**interval1[0]**and**interval2[0]**is greater than the maximum of**interval1[1]**and**interval2[1],**then return**True**. Otherwise, we will return**False**.

- Check if the minimum of
- Initialize an empty array
**ans.** - Check if
**N**is equal to**0.**- Insert
**newInterval**into**ans**. - Return
**ans**.

- Insert
- If
**newInterval[0]**is greater than the**intervals[0][1]**or**newInterval[1]**is greater than**intervals[N - 1][1].**- If
**newInterval[1]**is less than**intervals[0][0]**.- Insert
**newInterval**into**ans**.

- Insert
- Iterate
**interval**through the array**intervals.**- Insert
**interval**into**ans**.

- Insert
- If
**newInterval[0]**is less than**intervals[N - 1][0]**- Insert
**newInterval**into**ans**.

- Insert
- Return
**ans**.

- If
- If
**newInterval[0]**is less than or equal to**intervals[0][0]**and**newInterval[1]**is greater than**intervals[N - 1][1]**- Then insert
**newInterval**into**ans**. - Return
**ans.**

- Then insert
- Set the variable
**overlap**to**True** - Set
**index**equal to**0** - While
**index**is less than**N**- Set
**overlap**equal to**doesOverlap(intervals[index], newInterval).** - If
**overlap**is**False**- Insert
**interval**into**ans**. - If
**newInterval[0]**is less than**intervals[index][1]**and**newInterval[1]**is greater than**intervals[index+1][0]**, then insert**newInterval**into**ans** - Increment
**index**by 1. - We will continue the loop.

- Insert
- Initialize an empty array
**mergedInterval** - Insert minimum of
**newInterval[0]**and**intervals[index][0]** - While
**index**is less than**N**and**overlap**is**True**- If
**i**is equal to**N - 1**, then set**overlap**to**False**. - Otherwise set
**overlap**to**doesOverlap(intervals[index + 1], newInterval)**. - Increase
**index**by**1**

- If
- Insert
**mergedInterval**into**ans**.

- Set
- Finally, return the array
**ans.**

In this approach, we will maintain a stack, and we will insert each interval in the stack one at a time by checking if the current interval overlaps with the **newInterval.**

If the **newInterval** overlaps with the first interval, we merge the two intervals and insert them into the stack. Otherwise, we can simply insert the first interval and **newInterval **into the stack.

We will traverse through all remaining intervals. We will iterate the **index** from **1** to **N - 1**

- We will set the last interval variable
**lastInterval**as the last interval inserted in the stack and remove the last interval from the stack. - We will check if the
**Intervals[index]**overlaps with the**lastInterval**, then merge the intervals and insert the merged interval into the stack. Otherwise, we will insert**Intervals[index]**and**lastInterval**in the stack.

We will create a function **doesOverlap(interval1, interva2)**, which returns a boolean if the given intervals **intervals1** and **interval2** overlap with each other or not.

Algorithm:

- The function
**doesOverlap(interval1, interval2):**- Check if the minimum of
**interval1[0]**and**interval2[0]**is greater than the maximum of**interval1[1]**and**interval2[1],**then return**True**. Otherwise, we will return**False**.

- Check if the minimum of

- Initialize a stack
**finalIntervals,**and insert**intervals[0]**into the**finalIntervals**. - Set the variable
**top**as the top element of**finalIntervals.** - If
**newInterval[0]**is less than**top[1]**- Delete the top element in
**finalIntervals** - Set
**top[0]**as minimum of**newInterval[0]**and**top[0].** - Set
**top[1]**as maximum of**newInterval[1]**and**top[1].** - Insert
**top**into**finalIntervals**stack.

- Delete the top element in
- Otherwise, insert
**newInterval**into**finalIntervals**stack. - Iterate
**index**from 1 to the**N - 1.**- Set
**lastInterval**as the top element of**finalIntervals**array and delete the top element from**finalIntervals** - If
**doesOverlap(lastInterval,interval[index])**is**True**,- Set
**lastInterval[0]**as minimum of**lastInterval[0]**and**intervals[index][0].** - Set
**lastInterval[1]**as maximum of**lastInterval[1]**and**intervals[index][1].**

- Set
- Otherwise if
**lastInterval[0]**is less than**intervals[index][0]**,- Insert the
**lastInterval**in**finalIntervals**stack. - Insert the
**intervals[index]**in**finalIntervals**stack.

- Insert the
- Otherwise
**lastInterval[0]**is greater than**intervals[index][0]**,- Insert the
**intervals[index]**in**finalIntervals**stack. - Insert the
**lastInterval**in**finalIntervals**stack.

- Insert the

- Set
- Set the variable
**ans**as an empty array. - Iterate till the stack is not empty.
- Insert the top element of the stack into the array
**ans**and remove the top element.

- Insert the top element of the stack into the array
- In the end, we will reverse the array
**ans**and return the array**ans**.