Computing the smallest positive integer not covered by any of a set of intervals
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
In computational problems, efficiently computing the smallest positive integer not covered by any of a given set of intervals is a common task with numerous applications in fields like network security, game development, scheduling, and resource management. This article delves into the methods of solving this problem, discussing various approaches, their implementations, and complexities.
Problem Definition
Given a list of intervals, the task is to find the smallest positive integer that is not included in any of these intervals. Formally, you are provided with n
intervals [l_i, r_i]
(where each l_i
and r_i
are integers such that ), and you need to determine the smallest integer x
such that for every interval [l_i, r_i]
, x
is not within the range l_i ≤ x ≤ r_i
.
Example
Consider the intervals [1, 3]
, [5, 7]
, and [10, 15]
. The smallest positive integer not covered by these intervals is 4
.
Approaches
1. Brute Force Method
This method involves checking each integer from 1
upward against all intervals to see if it falls outside them.
Algorithm:
- Start with
x = 1. - For each interval
[l_i, r_i], check ifxlies outside all intervals. - If
xis outside all intervals, returnxas the answer. - Else, increment
xand repeat the checks.
Complexity:
- Time Complexity: where
nis the number of intervals andmis the smallest integer not covered, which makes this inefficient for large datasets. - Space Complexity: , as no additional space is required beyond variables for counting and indexing.
2. Sorting and Iterative Merge
To make the brute force method more efficient, we first sort the intervals and then iterate to find the smallest uncovered integer.
Algorithm:
- Sort intervals based on their starting points
l_i. - Initialize
x = 1(smallest positive integer). - Iterate through each interval
[l_i, r_i]. - If
x < l_i, thenxis outside the current interval range. Returnx. - Else, update
xtor_i + 1ifr_i + 1 > x. - Continue until all intervals are processed.
Complexity:
- Time Complexity: due to the sorting step.
- Space Complexity: if sort is done in-place.
3. Using Data Structures
For enhanced performance in dynamically added intervals or repeated queries, data structures like Binary Indexed Trees or Segment Trees can be utilized.
Segment Tree Approach
A segment tree allows for the efficient merging of intervals and querying the smallest missing integer.
Algorithm:
- Construct a segment tree where each node keeps track of coverage within its range.
- Update intervals into the tree.
- Query to find the smallest uncovered integer starting from
1by traversing the tree.
Complexity:
- Time Complexity: Building the tree takes . Each update or query operation is done in .
- Space Complexity: , as a segment tree with range covers
2nnodes (root to leaves).
Considerations
- Overlap: Intervals might overlap, making it necessary to correctly merge them when considering their combined effect.
- Large Ranges: For very large rages, efficient data structure use is critical for performance.
- Dynamic Updates: For scenarios where intervals are frequently added or removed, dynamic data structures outperform simpler methods.
Conclusion
Effective algorithms to compute the smallest positive integer not covered by any of a set of intervals are essential in optimizing computational resources. Employing sorting or data structures facilitates more efficient solutions compared to naive methods.
Here's a table summarizing the key methods and their traits:
| Approach | Time Complexity | Space Complexity | Suitable For |
| Brute Force | Small datasets | ||
| Sorting and Merging | Moderate datasets, one-time query | ||
| Segment Tree | Large datasets, repeated queries/dynamic updates |
By selecting an appropriate method, one can efficiently manage resources in practical applications that involve ranges, such as network allocation and task scheduling.
Related reading
- Concatenating red-black trees
- Concatenating/Merging/Joining two AVL trees
- Condensed matrix function to find pairs
- Confused on Miller-Rabin
- Conditional sampling of binary vectors ?
- Confused with Voronoi diagram algorithm Fortune's sweepline
- Connect nodes to maximize total edge weight
- Connected Component Labeling - Implementation

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.