ExamAdda Logo

Non Overlapping Intervals

Easy

Given an array of intervals where intervals[i] = [starti, endi], return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.

Example 1

Input

4
1 2
2 3
3 4
1 3

Output

1

Explanation

Removing [1,3] leaves the remaining intervals non-overlapping.

Example 2

Input

3
1 2
1 2
1 2

Output

2

Explanation

Two intervals need to be removed so that only one non-overlapping interval remains.

Constraints

1 ≤ intervals.length ≤ 10⁵
intervals[i].length == 2
-5 × 10⁴ ≤ starti < endi ≤ 5 × 10⁴

Hints:

Hint 1

Sort the intervals by their ending time.

Hint 2

Keep the interval with the earliest ending time whenever two intervals overlap. This leaves the maximum possible space for future 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 9, 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