Finding Missing Ranges in a Sorted Integer Array
Given a sorted unique integer array nums and a range [lower, upper], your task is to identify all the numbers within this inclusive range that are missing from the nums array. This is a common problem in data validation and gap analysis, where you need to ensure a complete sequence of expected values.
Problem Description
You are provided with a sorted array of unique integers, nums, and an inclusive integer range defined by lower and upper. Your goal is to return a list of strings representing the missing ranges. A missing range should be formatted as:
"a"if the range consists of a single numbera."a->b"if the range consists of multiple numbers fromatob(inclusive).
The nums array is guaranteed to be sorted in ascending order and contain unique integers. The lower and upper bounds are also integers.
Key Requirements
- Identify missing numbers: Determine which integers between
lowerandupper(inclusive) are not present innums. - Group into ranges: Consecutive missing numbers should be represented as a single range.
- Format output: The output should be a list of strings, where each string represents a missing range in the specified format.
Expected Behavior
- If
numsis empty, the entire range[lower, upper]is missing. - If
numscontains numbers outside the[lower, upper]range, these numbers should be ignored. - The
lowerandupperbounds themselves can be missing.
Edge Cases to Consider
numsis empty.loweris equal toupper.numscontains only numbers less thanlower.numscontains only numbers greater thanupper.numscontains all numbers within the[lower, upper]range.- The missing ranges start exactly at
loweror end exactly atupper.
Examples
Example 1:
Input: nums = [0, 1, 3, 50, 75], lower = 0, upper = 99
Output: ["2", "4->49", "51->74", "76->99"]
Explanation:
- The number 2 is missing.
- The numbers from 4 to 49 (inclusive) are missing.
- The numbers from 51 to 74 (inclusive) are missing.
- The numbers from 76 to 99 (inclusive) are missing.
Example 2:
Input: nums = [], lower = 1, upper = 1
Output: ["1"]
Explanation: The only number in the range is 1, and it's missing from the empty array.
Example 3:
Input: nums = [-1], lower = -1, upper = -1
Output: []
Explanation: The range consists of a single number -1, which is present in nums.
Example 4:
Input: nums = [-5, -3, 0, 1, 5], lower = -10, upper = 10
Output: ["-10->-6", "-4", "-2->-1", "2->4", "6->10"]
Explanation:
- The numbers from -10 to -6 are missing.
- The number -4 is missing.
- The numbers from -2 to -1 are missing.
- The numbers from 2 to 4 are missing.
- The numbers from 6 to 10 are missing.
Constraints
0 <= nums.length <= 100-10^9 <= nums[i] <= 10^9numsis sorted in ascending order.numscontains unique integers.-10^9 <= lower <= upper <= 10^9- The time complexity of your solution should be efficient, ideally O(N) where N is the length of
nums, as you'll need to iterate through the array.
Notes
- Consider how you will handle the boundaries of the
[lower, upper]range. It might be helpful to think about the "gaps" betweenlowerand the first element ofnums, between consecutive elements ofnums, and between the last element ofnumsandupper. - The problem involves large integer values, so ensure your chosen language's integer types can handle them.
- A helper function to format the range string might be useful.