Update appNew update is available. Click here to update.

Insert Interval

Last Updated: 22 Jan, 2021
Difficulty: Easy

PROBLEM STATEMENT

Try Problem

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 into the stack (in the same order).
      • Otherwise, the two intervals overlap with each other, so merge them and push the new interval into the stack.
  • Empty the elements of the stack into a list in reverse order.
  • Return the resulting list.

 

Try Problem