You are given an inclusive range [lower, upper] and a sorted integer array nums of unique values, all of which lie inside that range.
A number x is 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.
Function Signature
def find_missing_ranges(nums: list[int], lower: int, upper: int) -> list[list[int]]:
Rules
-
Each range is a two-element list
[start, end]
with
start <= end
, both ends inclusive. A single missing number
x
is written
[x, x]
.
-
Return the ranges in ascending order. Return an empty list when no number is missing.
Constraints
-
-10^9 <= lower <= upper <= 10^9
-
0 <= len(nums) <= 100
-
lower <= nums[i] <= upper
-
nums
is sorted in ascending order and all its values are unique.
Examples
Input: nums = [0, 1, 3, 50, 75], lower = 0, upper = 99
Output: [[2, 2], [4, 49], [51, 74], [76, 99]]
The numbers 2, 4 to 49, 51 to 74 and 76 to 99 are absent from nums. Each gap becomes one range, and 2 is a lone missing value, so it is written [2, 2].
Input: nums = [-1], lower = -1, upper = -1
Output: []
The range holds only -1, which is in nums, so nothing is missing.
Input: nums = [], lower = -3, upper = 2
Output: [[-3, 2]]
With an empty array every number from -3 to 2 is missing, and one range covers them all.