# Insert in sorted and non-overlapping interval array

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

Examples :

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.

**Approach :**

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.

`// C++ Code to insert a new interval in set of sorted ` `// intervals and merge overlapping intervals that are ` `// formed as a result of insertion. ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `// Define the structure of interval ` `struct` `Interval ` `{ ` ` ` `int` `start; ` ` ` `int` `end; ` ` ` `Interval() ` ` ` `: start(0), end(0) ` ` ` `{ ` ` ` `} ` ` ` `Interval(` `int` `s, ` `int` `e) ` ` ` `: start(s), end(e) ` ` ` `{ ` ` ` `} ` `}; ` ` ` `// A subroutine to check if intervals overlap or not. ` `bool` `doesOverlap(Interval a, Interval b) ` `{ ` ` ` `return` `(min(a.end, b.end) >= max(a.start, b.start)); ` `} ` ` ` `// Function to insert new interval and ` `// merge overlapping intervals ` `vector<Interval> insertNewInterval ` `(vector<Interval>& Intervals, Interval newInterval) ` `{ ` ` ` `vector<Interval> ans; ` ` ` `int` `n = Intervals.size(); ` ` ` ` ` `// If set is empty then simply insert ` ` ` `// newInterval and return. ` ` ` `if` `(n == 0) ` ` ` `{ ` ` ` `ans.push_back(newInterval); ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` ` ` `// Case 1 and Case 2 (new interval to be ` ` ` `// inserted at corners) ` ` ` `if` `(newInterval.end < Intervals[0].start || ` ` ` `newInterval.start > Intervals[n - 1].end) ` ` ` `{ ` ` ` `if` `(newInterval.end < Intervals[0].start) ` ` ` `ans.push_back(newInterval); ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `ans.push_back(Intervals[i]); ` ` ` ` ` `if` `(newInterval.start > Intervals[n - 1].end) ` ` ` `ans.push_back(newInterval); ` ` ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Case 3 (New interval covers all existing) ` ` ` `if` `(newInterval.start <= Intervals[0].start && ` ` ` `newInterval.end >= Intervals[n - 1].end) ` ` ` `{ ` ` ` `ans.push_back(newInterval); ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Case 4 and Case 5 ` ` ` `// These two cases need to check whether ` ` ` `// intervals overlap or not. For this we ` ` ` `// can use a subroutine that will perform ` ` ` `// this function. ` ` ` `bool` `overlap = ` `true` `; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `overlap = doesOverlap(Intervals[i], newInterval); ` ` ` `if` `(!overlap) ` ` ` `{ ` ` ` `ans.push_back(Intervals[i]); ` ` ` ` ` `// Case 4 : To check if given interval ` ` ` `// lies between two intervals. ` ` ` `if` `(i < n && ` ` ` `newInterval.start > Intervals[i].end && ` ` ` `newInterval.end < Intervals[i + 1].start) ` ` ` `ans.push_back(newInterval); ` ` ` ` ` `continue` `; ` ` ` `} ` ` ` ` ` `// Case 5 : Merge Overlapping Intervals. ` ` ` `// Starting time of new merged interval is ` ` ` `// minimum of starting time of both ` ` ` `// overlapping intervals. ` ` ` `Interval temp; ` ` ` `temp.start = min(newInterval.start, ` ` ` `Intervals[i].start); ` ` ` ` ` `// Traverse the set until intervals are ` ` ` `// overlapping ` ` ` `while` `(i < n && overlap) ` ` ` `{ ` ` ` ` ` `// Ending time of new merged interval ` ` ` `// is maximum of ending time both ` ` ` `// overlapping intervals. ` ` ` `temp.end = max(newInterval.end, ` ` ` `Intervals[i].end); ` ` ` `if` `(i == n - 1) ` ` ` `overlap = ` `false` `; ` ` ` `else` ` ` `overlap = doesOverlap(Intervals[i + 1], ` ` ` `newInterval); ` ` ` `i++; ` ` ` `} ` ` ` ` ` `i--; ` ` ` `ans.push_back(temp); ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `vector<Interval> Intervals; ` ` ` `Interval newInterval; ` ` ` ` ` `newInterval.start = 1; ` ` ` `newInterval.end = 2; ` ` ` `Intervals.push_back(newInterval); ` ` ` `newInterval.start = 3; ` ` ` `newInterval.end = 5; ` ` ` `Intervals.push_back(newInterval); ` ` ` `newInterval.start = 6; ` ` ` `newInterval.end = 7; ` ` ` `Intervals.push_back(newInterval); ` ` ` `newInterval.start = 8; ` ` ` `newInterval.end = 10; ` ` ` `Intervals.push_back(newInterval); ` ` ` `newInterval.start = 12; ` ` ` `newInterval.end = 16; ` ` ` `Intervals.push_back(newInterval); ` ` ` `newInterval.start = 4; ` ` ` `newInterval.end = 9; ` ` ` ` ` `vector<Interval> ans = ` ` ` `insertNewInterval(Intervals, newInterval); ` ` ` ` ` `for` `(` `int` `i = 0; i < ans.size(); i++) ` ` ` `cout << ans[i].start << ` `", "` ` ` `<< ans[i].end << ` `"\n"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

1, 2 3, 10 12, 16

**Time Complexity : ** O(n)

**Auxiliary Space : ** O(n)

## Recommended Posts:

- 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
- 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
- Sort a nearly sorted array using STL
- Sorted merge in one array
- Ceiling in a sorted array
- Remove duplicates from sorted array
- Alternate XOR operations on sorted array
- Find the index of first 1 in a sorted array of 0's and 1's

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.