Looking to create a new array of arrays that contain the start and end numbers of intervals within a defined range without any overlaps. This scenario originated from managing timestamps of objects on a server.
For instance, if we have:
const intervals = [
[3, 5],
[7, 9],
];
const range = [2, 11];
The expected output would be [[2, 3], [5, 7], [9, 11]]
.
The intervals are sorted by start date without overlaps due to pre-processing according to this answer:
I have a working solution for this case but need help expanding it for other scenarios such as:
- Intervals:
[[3, 5]];
Range:[4, 8];
Output:[[5, 8]]
- Intervals:
[[7, 11]];
Range:[2, 8];
Output:[[2, 7]]
- Intervals:
[[3, 5], [6, 8]];
Range:[4, 7];
Output:[[5, 6]]
- Intervals:
[[5, 10], [15, 20], [25, 30]];
Range:[0, 35];
Output:[[0, 5], [10, 15], [20, 25], [30, 35]]
The current code checks for partial overlaps in cached dates before merging queries. Considering how to handle multiple cases efficiently with conditional statements or a universal function.