#3731

Find Missing Elements

Easy
ArrayHash TableSortingHash MapArray
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n²)
O(n)
Space
O(1)
O(n)
💡

Intuition

Time O(n)Space O(n)

This approach uses a set for fast lookups, allowing us to efficiently find missing numbers in linear time.

⚙️

Algorithm

3 steps
  1. 1Step 1: Find the minimum and maximum values in the array.
  2. 2Step 2: Create a set from the array for O(1) lookups.
  3. 3Step 3: Iterate through the range from min to max, adding missing numbers to the result list if they are not in the set.
solution.py4 lines
1def find_missing(nums):
2    min_val, max_val = min(nums), max(nums)
3    num_set = set(nums)
4    return [i for i in range(min_val, max_val + 1) if i not in num_set]

Complexity note: The time complexity is O(n) due to the single pass to create the set and another pass to find missing numbers. Space complexity is O(n) for the set.

  • 1Identify the range using min and max values.
  • 2Use a set for efficient lookups.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.