AlgoInsight

DSA problem

Missing Ranges

EasyArraysPattern: Array Traversaltop600GoogleAmazonFacebookMicrosoft

You are given an inclusive range [lower, upper] and a sorted unique integer array nums, where all elements lie within the inclusive range.

A number x is considered 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 every missing number. That is, no element of nums is in any of the ranges, and each missing number is covered by exactly one of the ranges.

Each returned range [a, b] is a contiguous block of missing numbers where a is the smallest and b is the largest missing number in that block.

Examples

Example 1
Input: nums = [0,1,3,50,75], lower = 0, upper = 99
Output: [[2,2],[4,49],[51,74],[76,99]]
Explanation: The ranges are: [2,2] since 2 is missing; [4,49] covering 4 through 49; [51,74]; and [76,99]. Values 0, 1, 3, 50, 75 are present so they are excluded.
Example 2
Input: nums = [-1], lower = -1, upper = -1
Output: []
Explanation: There are no missing ranges since every number in the range is present in nums.
Example 3
Input: nums = [], lower = 1, upper = 1
Output: [[1,1]]
Explanation: The only number in the range, 1, is missing because nums is empty.

Constraints

3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.

array

Browse all DSA problems