Watch 10 video solutions for Check if All the Integers in a Range Are Covered, a easy level problem involving Array, Hash Table, Prefix Sum. This walkthrough by Sandip Jana has 1,363 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a 2D integer array ranges and two integers left and right. Each ranges[i] = [starti, endi] represents an inclusive interval between starti and endi.
Return true if each integer in the inclusive range [left, right] is covered by at least one interval in ranges. Return false otherwise.
An integer x is covered by an interval ranges[i] = [starti, endi] if starti <= x <= endi.
Example 1:
Input: ranges = [[1,2],[3,4],[5,6]], left = 2, right = 5 Output: true Explanation: Every integer between 2 and 5 is covered: - 2 is covered by the first range. - 3 and 4 are covered by the second range. - 5 is covered by the third range.
Example 2:
Input: ranges = [[1,10],[10,20]], left = 21, right = 21 Output: false Explanation: 21 is not covered by any range.
Constraints:
1 <= ranges.length <= 501 <= starti <= endi <= 501 <= left <= right <= 50Problem Overview: You receive a list of integer ranges and two numbers left and right. The task is to verify whether every integer in the interval [left, right] appears in at least one of the provided ranges.
Approach 1: Direct Range Check (Time: O(n * m), Space: O(1))
Iterate through every integer from left to right. For each value, scan all ranges and check whether the number falls inside any interval [start, end]. If a number is not covered by any range, return false immediately. The key idea is brute-force verification: treat each integer independently and validate coverage using simple comparisons. This works well because the constraints are small and avoids extra memory. You only perform range comparisons using standard array traversal.
Approach 2: Boolean Array Coverage (Time: O(n + U), Space: O(U))
Create a boolean array that represents whether each integer is covered. For every range [start, end], mark all indices between them as true. After processing all ranges, iterate from left to right and check if every position is marked. If any position is false, the required interval is not fully covered. This converts repeated range checks into constant-time lookups. The idea resembles coverage tracking often used in hash table style presence checks, and it can also be optimized with a difference array and prefix sum technique when ranges become larger.
Recommended for interviews: The boolean coverage approach is usually preferred because it reduces repeated scanning of ranges and clearly demonstrates interval coverage logic. The direct range check still shows good baseline reasoning and is acceptable when constraints are small, but the coverage array highlights your ability to trade memory for faster lookups.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Direct Range Check | O(n * m) | O(1) | When constraints are small and you want the simplest brute-force logic |
| Boolean Array Coverage | O(n + U) | O(U) | Preferred when you want faster lookups for coverage checks across the interval |