[LeetCode] Find All Numbers Disappeared in an Array II

4031. Find All Numbers Disappeared in an Array II

You are given an integer array nums and two integers lower and upper.

A missing integer is an integer in the inclusive range [lower, upper] that does not appear in nums.

Return a 2D integer array where each element is of the form [start, end], representing a contiguous range of missing integers. Return the ranges in increasing order. If there are no missing integers, return an empty array.

Note: Consecutive missing integers should be grouped into a single range.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
vector<vector<int>> findDisappearedNumbers(vector<int>& nums, int lower, int upper) {
nums.push_back(lower-1);
nums.push_back(upper+1);
sort(begin(nums), end(nums));
vector<vector<int>> res;
for(int i = 0; i < nums.size() - 1; i++) {
int l = nums[i] + 1, r = nums[i+1] - 1;
if(l <= r and l >= lower and r <= upper) res.push_back({l,r});
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/03/PS/LeetCode/find-all-numbers-disappeared-in-an-array-ii/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.