Hone logo
Hone
Problems

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 number a.
  • "a->b" if the range consists of multiple numbers from a to b (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

  1. Identify missing numbers: Determine which integers between lower and upper (inclusive) are not present in nums.
  2. Group into ranges: Consecutive missing numbers should be represented as a single range.
  3. Format output: The output should be a list of strings, where each string represents a missing range in the specified format.

Expected Behavior

  • If nums is empty, the entire range [lower, upper] is missing.
  • If nums contains numbers outside the [lower, upper] range, these numbers should be ignored.
  • The lower and upper bounds themselves can be missing.

Edge Cases to Consider

  • nums is empty.
  • lower is equal to upper.
  • nums contains only numbers less than lower.
  • nums contains only numbers greater than upper.
  • nums contains all numbers within the [lower, upper] range.
  • The missing ranges start exactly at lower or end exactly at upper.

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^9
  • nums is sorted in ascending order.
  • nums contains 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" between lower and the first element of nums, between consecutive elements of nums, and between the last element of nums and upper.
  • 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.
Loading editor...
plaintext