Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Inserting a new interval in a sorted array of intervals in JavaScript
For the purpose of this question, we define an interval as an array of two numbers where the first number is always smaller than the second number.
For example ?
[4, 6], [2, 3], [6, 8], [2, 7], [1, 8] are all examples of valid intervals.
Suppose, we have an array of intervals which is sorted according to their start times (the first elements of each interval).
The intervals in the array are non-overlapping which means that for any two arbitrary adjacent intervals:
[m, n], [x, y] m < n < x < y
Therefore, one such example of this array of intervals can be ?
const arr = [[2, 4], [5, 7], [9, 10], [13, 17]];
We are required to write a JavaScript function that takes in one such array of intervals as the first argument and a single interval as the second argument.
The function should then insert the interval at its correct position in the array and maintain the non-overlapping property of the array.
If required, we can merge two or more intervals in the array in order to keep the array intervals non-overlapping.
For example, if for the above array of intervals, the interval we need to insert is [6, 13], then the output should look like ?
const output = [[2, 4], [5, 17]];
Algorithm Overview
The solution involves three main steps:
- Add all intervals that come before the new interval (no overlap)
- Merge the new interval with any overlapping intervals
- Add all remaining intervals that come after
Example
Following is the code ?
const arr = [[2, 4], [5, 7], [9, 10], [13, 17]];
const interval = [6, 13];
const insertWithin = (arr = [], interval = []) => {
const res = [];
let ind = 0;
// Add all intervals that end before the new interval starts
while (arr[ind] && arr[ind][1]
[[2, 4], [5, 17]]
How It Works
Let's trace through the example with intervals [[2, 4], [5, 7], [9, 10], [13, 17]] and new interval [6, 13]:
- Step 1: [2, 4] ends at 4, which is less than 6 (new interval start), so add it to result
- Step 2: [5, 7] starts at 5, which is ? 13 (new interval end), so merge: start = min(6, 5) = 5, end = max(13, 7) = 13
- Step 3: [9, 10] starts at 9, which is ? 13, so merge: start = min(5, 9) = 5, end = max(13, 10) = 13
- Step 4: [13, 17] starts at 13, which is ? 13, so merge: start = min(5, 13) = 5, end = max(13, 17) = 17
- Final result: [[2, 4], [5, 17]]
Additional Example
Here's another example with no overlap:
const arr2 = [[1, 2], [3, 5], [6, 7], [8, 10]]; const interval2 = [2.5, 2.8]; console.log(insertWithin(arr2, interval2));
[[1, 2], [2.5, 2.8], [3, 5], [6, 7], [8, 10]]
Conclusion
This algorithm efficiently inserts a new interval into a sorted array of non-overlapping intervals while maintaining the sorted order and merging overlapping intervals. The time complexity is O(n) where n is the number of intervals.
