Watch 10 video solutions for Missing Ranges, a easy level problem involving Array. This walkthrough by NeetCode has 139,224 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given an inclusive range [lower, upper] and a sorted unique integer array nums, where all elements are within the inclusive range.
A number x is considered missing if x is in the range [lower, upper] and x is not in nums.
Return the shortest sorted list of ranges that exactly covers all the missing numbers. That is, no element of nums is included in any of the ranges, and each missing number is covered by one of the ranges.
Example 1:
Input: nums = [0,1,3,50,75], lower = 0, upper = 99 Output: [[2,2],[4,49],[51,74],[76,99]] Explanation: The ranges are: [2,2] [4,49] [51,74] [76,99]
Example 2:
Input: nums = [-1], lower = -1, upper = -1 Output: [] Explanation: There are no missing ranges since there are no missing numbers.
Constraints:
-109 <= lower <= upper <= 1090 <= nums.length <= 100lower <= nums[i] <= uppernums are unique.Problem Overview: You are given a sorted integer array nums and two boundaries lower and upper. The task is to report every missing range of numbers between these bounds that does not appear in the array. Each gap between consecutive values represents a missing range.
Approach 1: Brute Force Range Check (O((upper-lower)) time, O(1) space)
The most direct idea is to iterate through every integer from lower to upper and check whether it appears in the array. You maintain a pointer in nums and compare each candidate number against it. When a number is missing, start building a range until a present number appears again. This works conceptually but becomes inefficient when the numeric interval is large because the algorithm examines every value even if the array itself is small.
Approach 2: Simulation with Gap Detection (O(n) time, O(1) space)
The optimal solution scans the sorted array once and detects gaps between adjacent numbers. Track a variable prev representing the last observed value, initialized to lower - 1. For each number curr in nums, check if curr - prev > 1. When this condition holds, the interval [prev + 1, curr - 1] is a missing range. Append that range to the result list, formatting it as a single number or a range string depending on its size.
After processing the array, run one final check using a sentinel value upper + 1 to capture the last possible gap. Because the array is already sorted, every missing segment is discovered by comparing neighboring values rather than scanning the entire numeric interval. The algorithm performs a single pass over the array and stores only the resulting ranges.
This method relies purely on sequential iteration and boundary comparisons, which makes it a classic array traversal pattern. The gap-detection logic is similar to problems that analyze consecutive elements or use lightweight two-pointer style iteration across ordered data. Understanding how to detect intervals between values is useful in range-based problems and interval processing patterns.
Recommended for interviews: Interviewers expect the linear scan simulation approach. The brute force method demonstrates basic understanding of the problem but fails when the range between lower and upper becomes large. The gap-detection approach shows that you recognize the sorted property of the array and convert the problem into comparing consecutive elements, achieving O(n) time with constant space.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Range Check | O(upper - lower) | O(1) | When the numeric range is very small and implementation simplicity matters |
| Simulation with Gap Detection | O(n) | O(1) | Best for sorted arrays where you detect missing intervals between consecutive elements |