189. Find Missing Numbers In Range

Asked in

Find Missing Numbers In Range
You are given a sorted list of unique integers nums and two integers lower and upper.

The range from lower to upper, inclusive, contains all valid numbers that should be considered.

Your task is to find every number in this inclusive range that does not appear in nums.

Instead of returning each missing number separately, consecutive missing numbers must be grouped together as ranges.

Each missing range should be returned as a string in the format "start,end", where start is the first missing number in that range and end is the last missing number in that range.

If only one number is missing, it should still be represented as "x,x".

Only numbers between lower and upper, inclusive, should be considered. Numbers in nums that are outside this range should be ignored.

If nums is empty, then the entire range from lower to upper is missing.

Method Signature

List<String> findMissingRanges(List<Integer> nums, int lower, int upper)
  • Returns all missing ranges inside the inclusive range [lower, upper].
  • Each returned string must use comma-separated format "start,end".
  • The returned ranges must be sorted by increasing start value.

Input Format

  • nums is a sorted list of unique integers.
  • lower is the smallest value in the inclusive range.
  • upper is the largest value in the inclusive range.

Output Format

  • Return a List<String>.
  • Each string represents one missing range using format "start,end".
  • If there are no missing numbers in the range, return an empty list.

Important Details

  • If there are missing numbers before the first valid number in nums, add the range from lower to that number minus 1.
  • If there is a gap between two consecutive valid numbers, add the missing range between them.
  • If there are missing numbers after the last valid number in nums, add the range from that number plus 1 to upper.
  • Values in nums smaller than lower or greater than upper should not create missing ranges outside [lower, upper].

Constraints

  • 0 ≤ nums.size() ≤ 10,000
  • -1,000,000,000 ≤ lower ≤ upper ≤ 1,000,000,000
  • -1,000,000,000 ≤ nums[i] ≤ 1,000,000,000
  • nums is sorted in strictly increasing order.
  • All values in nums are unique.
  • The method must never receive null as a parameter value.

Examples

Example 1

Input: findMissingRanges(nums = [0, 1, 4, 8, 10], lower = 0, upper = 12)
Output: ["2,3", "5,7", "9,9", "11,12"]
Explanation: Numbers 2 and 3 are missing together, numbers 5 through 7 are missing together, 9 is missing alone, and numbers 11 through 12 are missing after the last value.

Example 2

Input: findMissingRanges(nums = [5, 6, 7], lower = 1, upper = 10)
Output: ["1,4", "8,10"]
Explanation: The values before 5 are missing from 1 to 4, and the values after 7 are missing from 8 to 10.

Example 3

Input: findMissingRanges(nums = [], lower = 3, upper = 6)
Output: ["3,6"]
Explanation: Since nums is empty, every number from 3 to 6 is missing.

Example 4

Input: findMissingRanges(nums = [1, 2, 3, 4], lower = 1, upper = 4)
Output: []
Explanation: Every number in the range [1, 4] already appears in nums, so there are no missing ranges.

Example 5

Input: findMissingRanges(nums = [-5, -2, 0, 1, 3, 8, 20], lower = 0, upper = 5)
Output: ["2,2", "4,5"]
Explanation: Values -5, -2, 8, and 20 are outside the range [0, 5] and are ignored. Inside the range, only 2, 4, and 5 are missing.




Please use Laptop/Desktop or any other large screen to add/edit code.