Insert Interval

Posted: 29 Jan, 2020
Difficulty: Moderate

PROBLEM STATEMENT

Try Problem

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
Approach 1

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-:

  1. The newInterval is to be added at either the start or the end of the array.
  2. 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].
  3. 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.
  • Initialize an empty array ans.
  • Check if N is equal to 0.
    • Insert newInterval into ans.
    • Return ans.
  • 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.
    • Iterate interval through the array intervals.
      • Insert interval into ans.
    • If newInterval[0] is less than intervals[N - 1][0]
      • Insert newInterval into ans.
    • Return ans.
  • 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.
  • 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.
    • 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
    • Insert mergedInterval into ans.
  • Finally, return the array ans.
Try Problem