Given an array nums
of n
integers where nums[i]
is in the range [1, n]
, return an array of all the integers in the range [1, n]
that do not appear in nums
.
Example 1:
Input: nums = [4,3,2,7,8,2,3,1]
Output: [5,6]
Example 2:
Input: nums = [1,1]
Output: [2]
Constraints:
n == nums.length
1 <= n <= 10^5
1 <= nums[i] <= n
Could you solve this in O(n) time and O(1) space?
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:
We are given a collection of numbers. Our goal is to find which numbers, within a specific range, are missing from that collection. The most straightforward way to do this is to check each number in the range individually to see if it's present in our collection.
Here's how the algorithm would work step-by-step:
def find_disappeared_numbers_brute_force(numbers):
missing_numbers = []
array_size = len(numbers)
# Iterate through the expected range of numbers
for expected_number in range(1, array_size + 1):
is_present = False
# Check if the current expected number exists in the input array
for number in numbers:
if number == expected_number:
is_present = True
break
# Add to missing list if not found
if not is_present:
# Because this number is absent, add to the result
missing_numbers.append(expected_number)
return missing_numbers
The best way to solve this problem is to cleverly use the existing list itself to keep track of which numbers are present. We can do this without needing extra space by changing the signs of numbers in the list.
Here's how the algorithm would work step-by-step:
def find_disappeared_numbers(numbers):
list_length = len(numbers)
# Mark each number as seen by negating the value at its index.
for number in numbers:
index = abs(number) - 1
if numbers[index] > 0:
numbers[index] *= -1
missing_numbers = []
# If a value is positive, then that index + 1 is missing
for index in range(list_length):
if numbers[index] > 0:
missing_numbers.append(index + 1)
return missing_numbers
Case | How to Handle |
---|---|
Null or empty input array | Return an empty list as there are no numbers to check for disappearance. |
Input array with only one element | Return a list containing all numbers from 1 to n that are not the single element in the input array. |
Input array where all numbers from 1 to n are present | Return an empty list because no numbers are missing. |
Input array contains duplicate numbers | The solution correctly identifies missing numbers even with duplicates because the marking or swapping mechanism will only be applied once per present number. |
Input array contains numbers outside the range [1, n] | Ignore numbers outside the range as they do not affect the identification of missing numbers within the [1, n] range. |
Large input array causing potential memory issues | The in-place modification approach minimizes memory usage, scaling better than approaches that use auxiliary data structures of size n. |
Input array where n is close to the maximum integer value | The in-place modification should avoid integer overflow issues as we are only modifying existing values by using their indices. |
Array contains extreme boundary values (1 and n) | The in-place marking should handle 1 and n correctly due to the index mapping from value - 1 to index. |