Skip to main content

Missing Ranges - Solution & Explanation

EasyPremiumFree on FleetCodeArray6 min readAsked at: Meta, Google
Practice this problem

Problem Statement

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 <= 109
  • 0 <= nums.length <= 100
  • lower <= nums[i] <= upper
  • All the values of nums are unique.

Approach Overview

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.

Solution

We can simulate the problem directly according to the requirements.

The time complexity is O(n), where n is the length of the array nums. Ignoring the space consumption of the answer, the space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Range CheckO(upper - lower)O(1)When the numeric range is very small and implementation simplicity matters
Simulation with Gap DetectionO(n)O(1)Best for sorted arrays where you detect missing intervals between consecutive elements

Video Solution

LeetCode 163. Missing Ranges Explanation and Solution • happygirlzt • 4,473 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Missing Ranges easy or hard?
Missing Ranges is classified as an Easy problem. The challenge lies in handling edge cases such as gaps at the beginning or end of the range and formatting single-number ranges versus multi-number ranges correctly.
How to solve Missing Ranges in O(n)?
Initialize a variable such as prev = lower - 1 and iterate through the array, treating upper + 1 as a final sentinel. For each number curr, check if curr - prev > 1. When the condition holds, record the range [prev + 1, curr - 1]. Update prev to curr and continue scanning.
What is the best approach for Missing Ranges?
The most efficient approach is a linear scan simulation that detects gaps between consecutive values. Track the previous number and compare it with the current number in the sorted array. Whenever the difference is greater than one, a missing range exists. This method runs in O(n) time with O(1) extra space.
What data structure is used in Missing Ranges?
The problem primarily uses arrays and simple integer variables for boundary tracking. No complex data structures are required. The algorithm relies on comparing consecutive values and storing resulting ranges in a list.
What is the time complexity of Missing Ranges?
The optimal solution runs in O(n) time where n is the number of elements in the array. The algorithm performs a single pass and checks the gap between adjacent numbers. Space complexity is O(1) excluding the output list of ranges.
Missing Ranges Python or Java solution approach?
Both Python and Java implementations follow the same logic: iterate through the array, track the previous value, and append missing intervals when a gap is detected. The solution maintains O(n) time complexity and constant auxiliary space regardless of language.
Is Missing Ranges asked at Google, Amazon, or Meta?
Missing Ranges appears in interview preparation sets for companies like Google and Meta because it tests array traversal, boundary handling, and edge cases. The problem evaluates whether candidates can detect intervals efficiently using a single pass over sorted data.

Ready to solve this problem?

Practice Missing Ranges with our built-in code editor and test cases.

Practice on FleetCode