Unsorted ranges cannot be merged in a single pass.
Acceptance criteria
- 1.#out should show merged into 2.
Unsorted ranges cannot be merged in a single pass.
const intervals = [[8, 10], [1, 3], [2, 6]]; const merged = [intervals[0].slice()]; for (let i = 1; i < intervals.length; i++) { const [from, to] = intervals[i]; const last = merged[merged.length - 1]; if (from <= last[1]) last[1] = Math.max(last[1], to); else merged.push([from, to]); } document.querySelector('#out').textContent = "merged into " + merged.length;
Output ready when you are
Press Run to render your code, or Submit to validate your solution.