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)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Search, insert and delete in a sorted array
- Largest interval in an Array that contains the given element X for Q queries
- Sort a nearly sorted (or K sorted) array
- Check if any interval completely overlaps the other
- Find the largest interval that contains exactly one of the given N integers.
- Maximum points covered after removing an Interval
- Count of available non-overlapping intervals to be inserted to make interval [0, R]
- Insert minimum number in array so that sum of array becomes prime
- Search, insert and delete in an unsorted array
- Generate all possible sorted arrays from alternate elements of two given sorted arrays
- Maximum number of partitions that can be sorted individually to make sorted
- Insert duplicate of K adjacent to it for it's every occurrence in array
- Given a sorted array and a number x, find the pair in array whose sum is closest to x
- Value of k-th index of a series formed by append and insert MEX in middle
- Check whether a given array is a k sorted array or not
- Why is it faster to process sorted array than an unsorted array ?
- Maximum in an array that can make another array sorted
- Sort an array where a subarray of a sorted array is in reverse order
- Find Kth number from sorted array formed by multiplying any two numbers in the array
- Check if an Array can be Sorted by picking only the corner Array elements
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.