You are given an integer array nums and two integers lower and upper.
A missing integer is an integer in the inclusive range [lower, upper] that does not appear in nums.
Return a 2D integer array where each element is of the form [start, end], representing a contiguous range of missing integers. Return the ranges in increasing order. If there are no missing integers, return an empty array.
Note: Consecutive missing integers should be grouped into a single range.
Example 1:
Input: nums = [3,9,7], lower = 1, upper = 12
Output: [[1,2],[4,6],[8,8],[10,12]]
Explanation:
[1, 2, 4, 5, 6, 8, 10, 11, 12].[1, 2], [4, 6], [8, 8], and [10, 12].[[1, 2], [4, 6], [8, 8], [10, 12]].Example 2:
Input: nums = [1,1], lower = 5, upper = 7
Output: [[5,7]]
Explanation:
[5, 6, 7].[5, 7].[[5, 7]].Example 3:
Input: nums = [2,3,5], lower = 2, upper = 3
Output: []
Explanation:
[].Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 1051 <= lower <= upper <= 105When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The algorithm finds all contiguous missing integer ranges between a lower bound and an upper bound that do not appear in the input array. It first cleans and sorts the given numbers to inspect elements in increasing order. By maintaining a pointer for the next expected missing number, any detected gaps are recorded as valid ranges.
Here's how the algorithm would work step-by-step:
def find_missing_ranges(nums, lower, upper):
# Sort the array and remove duplicate values
unique_numbers = sorted(set(nums))
missing_ranges = []
current_start = lower
# Iterate through unique numbers to find gaps
for number in unique_numbers:
if number < current_start:
continue
if number > upper:
break
if number > current_start:
missing_ranges.append([current_start, number - 1])
current_start = number + 1
# Check if there is any remaining gap before the upper bound
if current_start <= upper:
missing_ranges.append([current_start, upper])
return missing_ranges
Sort the numbers, drop duplicates, and ignore anything outside the requested span. Then walk from the lower bound to the upper bound and record every gap between the numbers that remain.
Here's how the algorithm would work step-by-step:
def find_missing_ranges(nums, lower, upper):
ordered = sorted(set(nums))
ranges = []
previous = lower - 1
for value in ordered:
if value < lower:
continue
if value > upper:
break
if previous + 1 <= value - 1:
ranges.append([previous + 1, value - 1])
previous = value
if previous + 1 <= upper:
ranges.append([previous + 1, upper])
return ranges
| Case | How to Handle |
|---|---|
| Empty or null input array | Return an empty list immediately to prevent null pointer exceptions and invalid range processing. |
| Array with a single element | Evaluate whether that single element covers the expected range and return the remaining numbers accordingly. |
| Maximum-sized inputs up to array length N | Use in-place index negation or cyclic sort to achieve O(N) time complexity and O(1) extra space complexity. |
| Inputs containing many duplicates and all identical values | The state marking mechanism will safely process repeated visits to the same index without causing logic errors. |
| Range limits defined by the array length N | Ensure the algorithm maps values strictly between 1 and N to valid array indices from 0 to N-1. |
| All numbers in the range are present with no missing values | The traversal will find no unmarked indices and correctly return an empty list. |
| All numbers are missing except for one repeated value | The marking phase will target only a single index, leaving all other indices positive to collect the full missing range. |
| Language-specific array mutation constraints | Use absolute values during the lookup phase so that previously negated index markers do not corrupt subsequent reads. |