# Insert Interval

Posted: 22 Jan, 2021

Difficulty: Easy

#### You are given a list of ‘N’ non-overlapping intervals (each interval can be represented using two integers ‘start’ and ‘end’), sorted in ascending order (based on ‘start’ values). Your task is to insert a given interval in the list, such that all the intervals are present in sorted order.

#### In case the given interval overlaps with other intervals, merge all necessary intervals to produce a list containing only non-overlapping intervals.

#### Example:

```
Let’s say the list of intervals is: [[1,3], [5,7], [8,12]] and we need to insert the interval [4,6] into the list. [4,6] must be inserted in the second position. After insertion, since [4,6] overlaps with [5,7], we merge them into one interval [4,7]. So, our resulting list is: [[1,3], [4,7], [8,12]]
```

##### Input Format:

```
The first line of input contains an integer ‘T’ representing the number of test cases.
The first of every test case contains the positive integer ‘N’ denoting the number of intervals present in the list.
Next ‘N’ lines contain two space-separated integers, ‘start’ and ‘end’, denoting the starting and the ending point of the interval present in the list.
The next line contains two space-separated integers, start and end, denoting the starting and the ending point of the interval which is to be inserted into the list.
```

##### Output Format:

```
For each test case, return the list after inserting the given interval.
```

##### Note:

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

##### Constraints:

```
1 <= T <= 10
1 <= N <= 5 * 10^4
1 <= start <= end <= 10^5
Time Limit: 1 sec
```

Approach 1

- A simple approach to solve this problem is by using a stack.
- The idea is to one by one insert the intervals into the stack. At each step, we merge the required intervals and make sure that they remain in sorted order.
- In the end, the stack contains the resulting intervals in descending order.
- So, we reverse the stack and return the resulting list.

**Algorithm:**

- We start by inserting the
**given interval**into the stack. - Now, iterate over the intervals present in the list:
- Let’s assume that
**'CURRENT'**stores the current interval in the list and**‘PREVIOUS’**stores the interval presently at the top of the stack. - If the current interval lies after the previous interval i.e.
**‘PREVIOUS.end’ < 'CURRENT.start’**, then**push**the current interval into the stack and move on to the next interval in the list. - Otherwise, pop the top element (i.e. ‘PREVIOUS’) from the stack.
- If the previous interval lies after the current interval i.e. ‘PREVIOUS.
**start’ > ‘CURRENT.end’**: then push the current and previous intervals - Otherwise, the two intervals overlap with each other, so merge them and push the new interval into the stack.

- If the previous interval lies after the current interval i.e. ‘PREVIOUS.

- Let’s assume that
- Empty the elements of the stack into a list in reverse order.
- Return the resulting list.

Approach 2

- The previous approach required extra space for the stack. We can avoid this by performing the merge operation in-place.
- The idea is to first put all the intervals less than the given interval into a new list.
- Then merge all the intervals that intersect with the given interval and add the resulting interval to the new list.
- Also, add the remaining intervals, that are greater than the given interval, to the list.
- Return the new list of intervals.

### Algorithm:

- Create an empty list to which will store our resulting list.
- Iterate over the intervals present in the list:
- If the current interval’s ending point is less than the starting point of the given interval, then add the current interval to the list.
- Otherwise, break from the loop.

- Iterate over the list from where we left off in the previous step:
- Merge all the intervals which overlap with the given interval into a single interval and add it to the list.

- Add the remaining intervals to the list.
- Return the resulting list.

SIMILAR PROBLEMS

# Party Over

Posted: 21 May, 2021

Difficulty: Easy

# Optimize Memory Usage

Posted: 25 May, 2021

Difficulty: Moderate

# Zig - Zag Array

Posted: 3 Jun, 2021

Difficulty: Easy

# Minimum swaps to sort Array

Posted: 3 Jul, 2021

Difficulty: Easy

# Sort Big List Dates

Posted: 4 Jul, 2021

Difficulty: Moderate