Given a range and a list thereof, while keeping the existing intervals in the list unchanged, split the additional range into sub-intervals and add them to the list, such that all the ranges in the final list are disjoint and contain all numbers present in the input ranges.
The goal is to create an insertInterval(intervals, newInterval) function which returns a new interval list if there are any changes.
Pre-condition: Interval range is sorted smallest to larger [[0, 1], [3, 5]].
Examples: Input and output:
assert.deepEqual(
insertIntervalSec([[1,5],[10,15],[20,25]], [12,27]),
[[1,5],[10,15],[15,20],[20,25],[25, 27]]
);
assert.deepEqual(
insertIntervalSec([[1,5],[10,15],[20,25]], [-3,0]),
[[-3,0],[1,5],[10,15],[20,25]]
);
assert.deepEqual(
insertIntervalSec([[1,5],[10,15],[20,25]], [-3,3]),
[[-3,1],[1,5],[10,15],[20,25]]
);
assert.deepEqual(
insertIntervalSec([[0,5],[10,15],[20,25]], [15,15]),
[[0,5],[10,15],[20,25]]
);
assert.deepEqual(
insertIntervalSec([[0,5],[10,15],[20,25]], [20,21]),
[[0,5],[10,15],[20,25]]
);
assert.deepEqual(
insertIntervalSec([[0,5],[10,15],[20,25]], [26,27]),
[[0,5],[10,15],[20,25],[26, 27]]
);
assert.deepEqual(
insertIntervalSec([[0,5],[10,15],[20,25]], [25,27]),
[[0,5],[10,15],[20,25],[25,27]]
);
assert.deepEqual(insertIntervalSec([], [25,27]), [[25,27]]);
assert.deepEqual(insertIntervalSec([[1,1]], [1,1]), [[1,1]]);
assert.deepEqual(insertIntervalSec([[1,1]], [1,3]), [[1, 1], [1, 3]]);
assert.deepEqual(insertIntervalSec([[2,2]], [1,3]), [[1, 2], [2, 2], [2, 3]]);
All programming languages are welcome.
[[1, 2], [2, 5]]
\$\endgroup\$