[LeetCode] Find Missing Elements

3731. Find Missing Elements

You are given an integer array nums consisting of unique integers.

Originally, nums contained every integer within a certain range. However, some integers might have gone missing from the array.

The smallest and largest integers of the original range are still present in nums.

Return a sorted list of all the missing integers in this range. If no integers are missing, return an empty list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
vector<int> findMissingElements(vector<int>& nums) {
sort(rbegin(nums), rend(nums));
vector<int> res;
int now = nums.back();
while(nums.size()) {
if(nums.back() == now) nums.pop_back();
else res.push_back(now);
now++;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/11/21/PS/LeetCode/find-missing-elements/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.