Module 12 · Intervals · Insert Interval

Insert interval

The intervals are already sorted and apart, so one pass is enough: those wholly before the new interval are copied as they are, the ones it overlaps are merged into it, and once it is placed the rest are copied after it.

The top lane holds the intervals, the middle one newInterval (it stretches as it takes others in), and the bottom lane fills with mergedIntervals.