Taro Logo

Find Missing Elements

Easy
Asked by:
Tata Consultancy Services
0 views
Topics:
Arrays

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.

Example 1:

Input: nums = [1,4,2,5]

Output: [3]

Explanation:

The smallest integer is 1 and the largest is 5, so the full range should be [1,2,3,4,5]. Among these, only 3 is missing.

Example 2:

Input: nums = [7,8,6,9]

Output: []

Explanation:

The smallest integer is 6 and the largest is 9, so the full range is [6,7,8,9]. All integers are already present, so no integer is missing.

Example 3:

Input: nums = [5,1]

Output: [2,3,4]

Explanation:

The smallest integer is 1 and the largest is 5, so the full range should be [1,2,3,4,5]. The missing integers are 2, 3, and 4.

Constraints:

  • 2 <= nums.length <= 100
  • 1 <= nums[i] <= 100

Solution


Clarifying Questions

When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:

  1. Can the arrays contain duplicate values, and if so, should the output collection also include duplicates or just unique missing elements?
  2. Should the returned collection of missing elements preserve the original relative order from the first array, or does the output order not matter?
  3. What should be returned if all elements in the first array are present in the second array, or if the first array is empty?
  4. Are the input arrays guaranteed to fit entirely in memory, or should we consider streaming/external memory approaches?
  5. Can the elements in the arrays be negative numbers or zeros, and are both arrays guaranteed to contain standard 32-bit integers?

Brute Force Solution

Approach

The problem asks us to find all integers missing from a contiguous range defined by the minimum and maximum elements in a given array. We can identify the range boundaries directly by finding the smallest and largest numbers present. By checking each number in this range against a set of the input elements, we can collect and return all absent numbers in sorted order.

Here's how the algorithm would work step-by-step:

  1. Convert the input array into a set to enable fast lookups.
  2. Determine the minimum and maximum values present in the input array.
  3. Initialize an empty list to accumulate the missing numbers.
  4. Iterate through every integer from the minimum value to the maximum value.
  5. Append any integer that does not exist in the set to the result list.
  6. Return the resulting list of missing integers.

Code Implementation

def find_missing_elements(nums):
    # Store the input numbers in a hash set for constant time lookups
    num_set = set(nums)
    # Find the minimum and maximum boundaries of the range
    min_val = min(nums)
    max_val = max(nums)
    missing_elements = []
    # Iterate through all values between the minimum and maximum
    for value in range(min_val, max_val + 1):
        if value not in num_set:
            missing_elements.append(value)
    return missing_elements

Big(O) Analysis

Time Complexity
O(N + R) – Finding the minimum, maximum, and creating the set takes O(N) time, where N is the length of nums. Iterating through the range from the minimum to the maximum takes O(R) time, where R is the difference between the maximum and minimum elements.
Space Complexity
O(N) – The hash set stores the unique elements from the input array, taking O(N) additional space, excluding the space required for the output list.

Optimal Solution

Approach

This problem requires identifying all integers missing between the minimum and maximum values of a given array. By determining the lower and upper bounds, we can check every integer within this inclusive range. Any integer that is absent from the original array is collected and returned in ascending order.

Here's how the algorithm would work step-by-step:

  1. Store all elements from the input list into a hash set for efficient membership checks.
  2. Compute the minimum and maximum values present in the input array.
  3. Iterate through every integer from the minimum to the maximum value inclusive.
  4. Check whether the current integer exists in the set.
  5. Append any integers not present in the set to the result list.
  6. Return the resulting list of missing numbers.

Code Implementation

def find_missing_elements(nums: list[int]) -> list[int]:
    # Convert the input array to a set for fast lookup
    num_set = set(nums)
    # Find the minimum and maximum boundaries of the range
    min_val = min(nums)
    max_val = max(nums)
    # Collect numbers in the range that are not in the original array
    missing_elements = []
    for value in range(min_val, max_val + 1):
        if value not in num_set:
            missing_elements.append(value)
    # Return the gathered missing numbers in ascending order
    return missing_elements

Big(O) Analysis

Time Complexity
O(N + R) – Finding the minimum and maximum takes O(N) time, converting the list to a set takes O(N) time, and iterating across the range of values takes O(R) time, where N is the length of the array and R is the difference between the maximum and minimum elements.
Space Complexity
O(N) – The hash set stores N unique elements from the input array, requiring O(N) additional space.

Edge Cases

First array is completely empty
How to Handle:
Return an empty collection immediately as no elements can be present in the first array.
Second array is completely empty
How to Handle:
Return all elements from the first array since none of them can exist in the second.
Both arrays contain duplicate values
How to Handle:
Use a hash set to track elements of the second array in O(1) time and preserve duplicate counts if required by the output specification.
Arrays contain negative numbers and zeros
How to Handle:
Treat negative integers, zeros, and positive integers uniformly since hash-based lookups support all standard integer values.
Extremely large arrays exceeding available memory
How to Handle:
Process the second array into a hash set first, then stream or iterate through the first array to avoid excessive memory consumption.
Maximum or minimum 32-bit integer boundary values
How to Handle:
Rely on standard language hash implementations that safely handle extreme integer boundaries without overflow.
All elements in the first array are already present in the second array
How to Handle:
Return an empty collection after verifying that every lookup in the second array's hash set succeeds.
Disjoint arrays with no overlapping elements
How to Handle:
Return a collection containing every single element from the first array since none are found in the second.