ExamAdda Logo

Insert Interval

Easy

You are given an array of non-overlapping intervals sorted in ascending order by their start values and a new interval.

Insert the new interval into the intervals such that the intervals remain sorted and do not overlap.

Return the resulting array of intervals.

Example 1

Input

2
1 3
6 9
2 5

Output

1 5
6 9

Explanation

The new interval [2,5] overlaps with [1,3], so they are merged into [1,5].

Example 2

Input

5
1 2
3 5
6 7
8 10
12 16
4 8

Output

1 2
3 10
12 16

Explanation

The new interval [4,8] overlaps with [3,5], [6,7], and [8,10]. These intervals are merged into [3,10].

Constraints

0 ≤ intervals.length ≤ 10⁴
intervals[i].length == 2
0 ≤ start ≤ end ≤ 10⁵
Intervals are sorted by start time and are non-overlapping.

Hints:

Hint 1

Add all intervals ending before the new interval starts.

Hint 2

Merge all overlapping intervals with the new interval, then add the remaining intervals.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 6, 2026

Last updated

Aug 9, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output