Skip to main content
LeetCode 1272, Medium. Topics: Array. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 35 parametrized pytest cases, and a playground notebook:

Problem

A set of real numbers can be represented as the union of several disjoint intervals, where each interval is in the form [a, b). A real number x is in the set if one of its intervals [a, b) contains x (i.e. a <= x < b). You are given a sorted list of disjoint intervals intervals representing a set of real numbers as described above, where intervals[i] = [ai, bi] represents the interval [ai, bi). You are also given another interval toBeRemoved. Return the set of real numbers with the interval toBeRemoved removed from* intervals*. In other words, return the set of real numbers such that every x in the set is in intervals but not in toBeRemoved. Your answer should be a sorted list of disjoint intervals as described above.

Examples

Example 1
Example 2

Constraints

  • 1 <= intervals.length <= 10^4
  • -10^9 <= ai < bi <= 10^9

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026