Find Missing Elements (LeetCode 3731): Solution and Explanation

Easy
Array
Hash Table
Sorting

nums once held every integer from its smallest to its largest value, and some values in between were removed. Return the removed values in increasing order.

Intuition

The smallest and largest values are still in the array, so they mark the two ends of the original range. Nothing outside [min, max] can be missing.

Draw that range as a number line and put a peg on every value that is still present. The missing numbers are exactly the empty spots.

A hash set answers "is there a peg at v?" in O(1), and sweeping the line from left to right produces the empty spots already sorted.

Approach: Hash set plus range sweep

  1. Compute lo = min(nums) and hi = max(nums).
  2. Insert every number into a set seen.
  3. For each v from lo + 1 to hi - 1, append v to missing if it is not in seen.
  4. Return missing.

Step-by-step example

nums = [1,4,2,5]
  1. The smallest value is 1 and the largest is 5, so the line runs from 1 to 5.
  2. Pegs go on 1, 2, 4 and 5.
  3. Sweep 2, 3, 4. 2 has a peg, 3 does not, so missing = [3]. 4 has a peg.
  4. Return [3].

Code: Python, Java, C++

1class Solution:
2    def findMissingElements(self, nums: List[int]) -> List[int]:
3        lo, hi = min(nums), max(nums)
4        seen = set(nums)
5        missing = []
6        for v in range(lo + 1, hi):
7            if v not in seen:
8                missing.append(v)
9        return missing
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n + R)

Building the set and finding min and max take O(n). The sweep visits R = max - min values, each with an O(1) set lookup. With values up to 100, R is at most 99.

Space

O(n)

The set holds the n input values. The output list is not counted.

Other approaches

Sort and scan gaps (time O(n log n + R), space O(1) besides sorting)

Sort nums, then for each neighbouring pair (a, b) append every integer strictly between them. No set is needed, and the output is sorted automatically.

Boolean array (time O(n + R), space O(max value))

Because values are at most 100, a fixed array of 101 flags can replace the hash set. Same sweep, smaller constant factor.

Common mistakes

  • Sweeping from 1 instead of from the smallest value. Numbers below the minimum were never part of the range.
  • Checking membership with a linear search on the array inside the sweep, which turns the solution into O(n * R).
  • Returning the missing values in the order they were found by an unsorted scan instead of in increasing order.

Frequently asked questions

Why can the sweep skip lo and hi themselves?

The smallest and largest values are guaranteed to be in nums, so only the values strictly between them can be missing.

Is the output always sorted with this approach?

Yes. The sweep goes from lo + 1 upward, so every missing value is appended in increasing order.

What if nothing is missing?

Every value in the range has a peg, the sweep appends nothing, and the answer is an empty list.