402. Missing Ranges

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

A number x is considered missing if it lies within [lower, upper] but is not present in nums.

Return the shortest sorted list of ranges that exactly covers all the missing numbers. No element of nums should be included in any of the ranges, and each missing number must belong to one of the ranges.

Example 1:

Input: nums = [2, 5, 8, 15, 28], lower = 1, upper = 30

Output: [[1,1], [3,4], [6,7], [9,14], [16,27], [29,30]]

Explanation: The missing ranges are:

  • [1,1]
  • [3,4]
  • [6,7]
  • [9,14]
  • [16,27]
  • [29,30]

Example 2:

Input: nums = [-10, -7, -4, 0, 3, 9], lower = -10, upper = 10

Output: [[-9,-8], [-6,-5], [-3,-1], [1,2], [4,8], [10,10]]

Explanation: The missing ranges are:

  • [-9,-8]
  • [-6,-5]
  • [-3,-1]
  • [1,2]
  • [4,8]
  • [10,10]

Now Your Turn!

Pick the correct output for the given input

Input: nums = [7], lower = 5, upper = 9

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • −10⁹ ≤ lower ≤ upper ≤ 10⁹
  • 0 ≤ nums.length ≤ 100
  • lower ≤ nums[i] ≤ upper
  • All values in nums are unique.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
vector<vector<int>> findMissingRanges(vector<int>& nums, int lower, int upper) {
// Your code goes here
}
};
Test Case

Input:

Lower
Nums
Upper