163. Missing Ranges
EasySolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to find all the contiguous ranges of missing integers within a given inclusive range [lower, upper]. We are provided a sorted array nums containing unique integers that are also within this range. The goal is to produce a list of ranges that exactly cover all the numbers missing from nums.
Solution
Missing Ranges in Python
class Solution:
def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:
# In Python, integers have arbitrary precision, so no overflow checks are needed
# for `num + 1`.
result = []
next_expected = lower
# Iterate through the numbers in nums
for num in nums:
# If the current number is greater than what we expect, there's a gap.
if next_expected < num:
# Add the missing range [next_expected, num - 1].
result.append([next_expected, num - 1])
# The number `num` is present, so the next expected missing number
# would start from num + 1.
next_expected = num + 1
# After iterating through all numbers, check if there's a final missing range
# from the last number processed up to upper.
if next_expected <= upper:
result.append([next_expected, upper])
return result
Missing Ranges in Java
class Solution {
public List<List<Integer>> findMissingRanges(int[] nums, int lower, int upper) {
List<List<Integer>> result = new ArrayList<>();
// Use long to prevent overflow, as num + 1 can exceed Integer.MAX_VALUE.
long nextExpected = (long)lower;
// Iterate through the numbers in nums
for (int num : nums) {
// If the current number is greater than what we expect, there's a gap.
if (nextExpected < num) {
// Add the missing range [nextExpected, num - 1].
result.add(Arrays.asList((int)nextExpected, num - 1));
}
// The number `num` is present, so the next expected missing number
// would start from num + 1.
nextExpected = (long)num + 1;
}
// After iterating through all numbers, check if there's a final missing range
// from the last number processed up to upper.
if (nextExpected <= upper) {
result.add(Arrays.asList((int)nextExpected, upper));
}
return result;
}
}
Missing Ranges in C++
class Solution {
public:
vector<vector<int>> findMissingRanges(vector<int>& nums, int lower, int upper) {
vector<vector<int>> result;
// Use long long to prevent overflow, as num + 1 can exceed INT_MAX.
long long next_expected = (long long)lower;
// Iterate through the numbers in nums
for (int num : nums) {
// If the current number is greater than what we expect, there's a gap.
if (next_expected < num) {
// Add the missing range [next_expected, num - 1].
result.push_back({(int)next_expected, num - 1});
}
// The number `num` is present, so the next expected missing number
// would start from num + 1.
next_expected = (long long)num + 1;
}
// After iterating through all numbers, check if there's a final missing range
// from the last number processed up to upper.
if (next_expected <= upper) {
result.push_back({(int)next_expected, upper});
}
return result;
}
};
Missing Ranges in TypeScript
function findMissingRanges(nums: number[], lower: number, upper: number): number[][] {
const result: number[][] = [];
// JavaScript/TypeScript 'number' type is a 64-bit float, which can safely
// represent integers up to 2^53. The problem's constraints (10^9) are well
// within this limit, so no special overflow handling is needed.
let nextExpected = lower;
// Iterate through the numbers in nums
for (const num of nums) {
// If the current number is greater than what we expect, there's a gap.
if (nextExpected < num) {
// Add the missing range [nextExpected, num - 1].
result.push([nextExpected, num - 1]);
}
// The number `num` is present, so the next expected missing number
// would start from num + 1.
nextExpected = num + 1;
}
// After iterating through all numbers, check if there's a final missing range
// from the last number processed up to upper.
if (nextExpected <= upper) {
result.push([nextExpected, upper]);
}
return result;
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.