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 <= 1001 <= nums[i] <= 100When 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:
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:
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
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:
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
| Case | How to Handle |
|---|---|
| First array is completely empty | Return an empty collection immediately as no elements can be present in the first array. |
| Second array is completely empty | Return all elements from the first array since none of them can exist in the second. |
| Both arrays contain duplicate values | 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 | Treat negative integers, zeros, and positive integers uniformly since hash-based lookups support all standard integer values. |
| Extremely large arrays exceeding available memory | 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 | 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 | Return an empty collection after verifying that every lookup in the second array's hash set succeeds. |
| Disjoint arrays with no overlapping elements | Return a collection containing every single element from the first array since none are found in the second. |