Insert a closed interval
Insert one closed interval into sorted pairwise-disjoint closed intervals, merging any overlaps including touching endpoints. Return lists and leave inputs unchanged.
left, right = 2, 7
other = [6, 9]
print(min(left, other[0]), max(right, other[1]))Reason about the state
Append intervals wholly before the insertion, expand the insertion across overlaps, then append the untouched suffix. Each interval is examined once. Before writing code, identify what each variable means after one iteration. Walk through the example and explain why the next transition preserves that meaning. Then check the smallest permitted input and a case with repeated values. A correct answer follows the contract for every input, including the boundary cases in the checks. Keep any input mutation within the stated rules; return a fresh result when the task asks for one.
Your task
Insert one closed interval into sorted pairwise-disjoint closed intervals, merging any overlaps including touching endpoints. Return lists and leave inputs unchanged.
The explanation opens here once every check passes.