Skip to main content

Check if All the Integers in a Range Are Covered - Solution & Explanation

EasyArrayHash TablePrefix Sum18 min readAsked at: Amazon, Bloomberg, Squarespace
Practice this problem

Problem Statement

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 <= 50
  • 1 <= starti <= endi <= 50
  • 1 <= left <= right <= 50

Approach Overview

Problem 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 1: Direct Range Check

This approach involves iterating through each number between left and right to check if it is covered by at least one range in ranges. For each number, we loop through all ranges, and as soon as we find a range that covers the number, we move to the next number. If any number isn't covered after checking all ranges, we return false.

We iterate over each number from left to right and check if it falls within any given range. If we find such a range, we proceed to the next number; otherwise, we return false.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n * m), where n is the number of integers from left to right and m is the number of ranges.

Space Complexity: O(1), no additional space is used besides the input.

Try this approach in the editor →

Approach 2: Boolean Array Coverage

This method involves using a boolean array where each index indicates whether the corresponding number is covered. After initializing the array, we iterate over each range and mark the numbers within that range as covered in the boolean array. At the end, we check if all numbers from left to right are marked as covered.

The solution constructs a boolean array initialized as false representing number coverage from 1 to 50. As it iterates over each range, it updates the array indices to true for covered numbers.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(m + n), with m as the number of ranges and n as the length of the boolean array (fixed at 50).

Space Complexity: O(1), using a constant-sized boolean array.

Try this approach in the editor →

Approach 3: Difference Array

We can use the idea of a difference array to create a difference array diff of length 52.

Next, we iterate through the array ranges. For each interval [l, r], we increment diff[l] by 1 and decrement diff[r + 1] by 1.

Then, we iterate through the difference array diff, maintaining a prefix sum s. For each position i, we increment s by diff[i]. If s \le 0 and left \le i \le right, it indicates that an integer i within the interval [left, right] is not covered, and we return false.

If we finish iterating through the difference array diff without returning false, it means that every integer within the interval [left, right] is covered by at least one interval in ranges, and we return true.

The time complexity is O(n + M), and the space complexity is O(M). Here, n is the length of the array ranges, and M is the maximum value of the interval, which in this case is M \le 50.

Code

Python

Java

C++

Go

TypeScript

JavaScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Direct Range Check

Time Complexity: O(n * m), where n is the number of integers from left to right and m is the number of ranges.

Space Complexity: O(1), no additional space is used besides the input.

Boolean Array Coverage

Time Complexity: O(m + n), with m as the number of ranges and n as the length of the boolean array (fixed at 50).

Space Complexity: O(1), using a constant-sized boolean array.

Difference Array—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Direct Range CheckO(n * m)O(1)When constraints are small and you want the simplest brute-force logic
Boolean Array CoverageO(n + U)O(U)Preferred when you want faster lookups for coverage checks across the interval

Video Solution

Leetcode 1893 Check if All the Integers in a Range Are Covered • Sandip Jana • 1,407 views views

Watch 7 more video solutions →

Frequently Asked Questions

Is Check if All the Integers in a Range Are Covered easy or hard?
Check if All the Integers in a Range Are Covered is classified as an Easy problem. The logic mainly involves iterating through ranges and verifying interval coverage using arrays or simple marking techniques.
Check if All the Integers in a Range Are Covered Python/Java solution
In Python or Java, implement either a brute-force scan for each integer or build a boolean array that marks coverage for each range. After marking, iterate from left to right and ensure every value is covered.
How to solve Check if All the Integers in a Range Are Covered in O(n)?
Use a coverage array or difference array. Mark all integers that appear in the provided ranges, then verify that every integer between left and right is marked. This approach processes each range once and performs constant-time lookups for validation.
What is the best approach for Check if All the Integers in a Range Are Covered?
The boolean array coverage approach is typically the best. Mark each number covered by the given ranges and then verify that every integer from left to right is marked. This reduces repeated range scans and runs in O(n + U) time where U is the width of the target interval.
Is Check if All the Integers in a Range Are Covered asked at Google/Amazon/Meta?
Range coverage and interval validation problems frequently appear in interviews at companies like Amazon, Google, and Meta. While this exact problem may vary, the underlying concepts of interval processing and prefix sum techniques are common interview topics.
What data structure is used in Check if All the Integers in a Range Are Covered?
The common data structure is a boolean array used to track whether each integer in the domain is covered. Some variations also use prefix sum arrays or hash-based presence checks depending on the size of the value range.
What is the time complexity of Check if All the Integers in a Range Are Covered?
The direct range check approach runs in O(n * m) time where n is the number of ranges and m is the size of the interval [left, right]. The optimized boolean coverage method runs in O(n + U) time where U = right - left + 1.

Ready to solve this problem?

Practice Check if All the Integers in a Range Are Covered with our built-in code editor and test cases.

Practice on FleetCode