Given a set of non-overlapping intervals and a new interval, insert the interval at correct position. If the insertion results in overlapping intervals, then merge the overlapping intervals. Assume that the set of non-overlapping intervals is sorted on the basis of start time, to find correct position of insertion.
Prerequisite : Merge the intervals
Input : Set : [1, 3], [6, 9] New Interval : [2, 5] Output : [1, 5], [6, 9] The correct position to insert new interval [2, 5] is between the two given intervals. The resulting set would have been [1, 3], [2, 5], [6, 9], but the intervals [1, 3], [2, 5] are overlapping. So, they are merged together in one interval [1, 5]. Input : Set : [1, 2], [3, 5], [6, 7], [8, 10], [12, 16] New Interval : [4, 9] Output : [1, 2], [3, 10], [12, 16] First the interval is inserted between intervals [3, 5] and [6, 7]. Then overlapping intervals are merged together in one interval.
Let the new interval to be inserted is : [a, b]
Case 1 : b < (starting time of first interval in set)
In this case simply insert new interval at the beginning of the set.
Case 2 : (ending value of last interval in set) < a
In this case simply insert new interval at the end of the set.
Case 3 : a ≤ (starting value of first interval) and b ≥ (ending value of last interval)
In this case the new interval overlaps with all the intervals, i.e., it contains all the intervals. So the final answer is the new interval itself.
Case 4 : The new interval does not overlap with any interval in the set and falls between any two intervals in the set
In this case simply insert the interval in the correct position in the set. A sample test case for this is :
Input : Set : [1, 2], [6, 9] New interval : [3, 5] Output : [1, 2], [3, 5], [6, 9]
Case 5 : The new interval overlaps with the interval(s) of the set.
In this case simply merge the new interval with overlapping intervals. To have a better understanding of how to merge overlapping intervals, refer this post : Merge Overlapping Intervals
Example 2 of sample test cases above cover this case.
1, 2 3, 10 12, 16
Time Complexity : O(n)
Auxiliary Space : O(n)
- Search, insert and delete in a sorted array
- Insert minimum number in array so that sum of array becomes prime
- Sort a nearly sorted (or K sorted) array
- Search, insert and delete in an unsorted array
- Find Kth number from sorted array formed by multiplying any two numbers in the array
- Given a sorted array and a number x, find the pair in array whose sum is closest to x
- Sort an array where a subarray of a sorted array is in reverse order
- Why is it faster to process sorted array than an unsorted array ?
- Maximum in an array that can make another array sorted
- Check whether a given array is a k sorted array or not
- Sorted merge in one array
- Ceiling in a sorted array
- Sort a nearly sorted array using STL
- Check if array can be sorted with one swap
- Alternate XOR operations on sorted array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.