Problem of the day
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]]
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.
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
2
3
1 3
5 7
8 12
4 6
3
1 3
5 7
8 12
4 10
1 3
4 7
8 12
1 3
4 12
For the first test case,
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]].
For the second test case,
The interval [4, 10] overlaps with [5, 7] and [8, 12]. Therefore we can merge the intervals and produce an interval [4, 12]. Hence the answer is [[1, 3], [4, 12]].
2
2
2 3
5 7
1 4
2
1 2
6 9
3 5
1 4
5 7
1 2
3 5
6 9
Think of all the conditions to insert an interval.
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-:
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:
O(N), Where N is the number of intervals given in the array.
We are iterating through each interval once, which will take O(N) time. Hence the overall time complexity becomes O(N).
O(N), Where N is the number of intervals given in the array.
The O(N) space complexity is used to store the intervals in the array. Hence the overall space complexity becomes O(N).