Find All Numbers Disappeared in an Array
Problem
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.
- n == nums.length
- 1 ≤ n ≤ 10⁵
- 1 ≤ nums[i] ≤ n
Example
nums = [4,3,2,7,8,2,3,1][5,6]The array length is 8, so the numbers 1 through 8 should appear. The input contains 1,2,2,3,3,4,7,8. Numbers 5 and 6 are missing. The algorithm marks visited numbers by negating the value at the index corresponding to each number's value minus one. After marking, positive values indicate missing numbers at those indices plus one.
Approach
Straightforward Solution
A naive approach uses a hash set to record all numbers seen and then iterates from 1 to n to find missing numbers. This requires O(n) time but O(n) extra space, violating the space constraint.
Core Observation
Each number in nums is in the range [1, n], which means each number can be mapped to an index in the array (number - 1). By marking the presence of a number at its corresponding index, the problem reduces to detecting which indices remain unmarked, indicating missing numbers.
Path to Optimal
The key insight is to use the input array itself as a marker structure. By negating the value at the index corresponding to each number encountered, the algorithm marks that number as present. After processing all numbers, indices with positive values correspond to missing numbers. This approach achieves O(n) time and O(1) extra space by modifying the input array in place.
Optimal Approach
Iterate through nums, for each number num, compute index = abs(num) - 1 and negate nums[index] if it is positive. Then iterate through nums again; indices with positive values indicate missing numbers (index + 1). Collect these into the result array and return it.
Time
O(n)
The algorithm makes two passes over the array: one to mark presence and one to collect missing numbers, each O(n).
Space
O(1)
The algorithm uses no extra data structures besides the output array, modifying the input array in place for marking.
Pattern Spotlight
In-place Marking via Index Negation
Use the input array as a presence marker by negating values at indices corresponding to seen numbers, enabling O(1) extra space detection of missing elements.
Solution
| 1 | class Solution: |
| 2 | def findDisappearedNumbers(self, nums: List[int]) -> List[int]: |
| 3 | for num in nums: |
| 4 | index = abs(num) - 1 |
| 5 | nums[index] = -abs(nums[index]) |
| 6 | |
| 7 | missing_numbers = [] |
| 8 | |
| 9 | for index, num in enumerate(nums): |
| 10 | if num > 0: |
| 11 | missing_numbers.append(index + 1) |
| 12 | |
| 13 | return missing_numbers |
Step-by-Step Solution
Mark Numbers Present by Negating Corresponding Indices
| 3 | for num in nums: |
| 4 | index = abs(num) - 1 |
| 5 | nums[index] = -abs(nums[index]) |
Objective
To mark each number's presence by negating the value at the index corresponding to that number minus one.
Key Insight
By using the absolute value of each number to find its corresponding index, the algorithm safely negates the value at that index to indicate presence without losing information. Using abs ensures that previously negated values do not interfere with the marking process. This in-place marking avoids extra space and leverages the problem's constraint that numbers are in [1, n].
Interview Quick-Check
Core Logic
Negate the value at index abs(num) - 1 to mark the number as seen, using absolute values to handle previously negated entries.
Common Pitfalls & Bugs
Failing to use abs(num) can cause incorrect indexing when values have already been negated.
State & Boundaries
Only negate if the value at the target index is positive to avoid double negation.
Collect Missing Numbers from Positive Indices
| 7 | missing_numbers = [] |
| 9 | for index, num in enumerate(nums): |
| 10 | if num > 0: |
| 11 | missing_numbers.append(index + 1) |
Objective
To identify indices with positive values after marking, which correspond to missing numbers, and collect them into the result list.
Key Insight
After marking, any index with a positive value indicates that its corresponding number (index + 1) was never encountered in the array. Collecting these indices plus one yields the missing numbers. This final pass completes the solution by translating the marking back into the required output.
Interview Quick-Check
Core Logic
Iterate through nums and append index + 1 to the result if nums[index] is positive, indicating the number is missing.
State & Boundaries
Use enumerate to access both index and value for clear mapping.
Complexity
This pass is O(n) and does not use extra space beyond the output list.
Return the List of Missing Numbers
| 13 | return missing_numbers |
Objective
To return the collected list of missing numbers as the final output.
Key Insight
Returning the result list completes the function, providing the caller with all numbers in [1, n] absent from the input array. This step is straightforward but essential for correctness.
Interview Quick-Check
Core Logic
Return the list of missing numbers collected from positive indices.
Line Analysis
This solution has 1 Critical line interviewers watch for.
nums[index] = -abs(nums[index])
Negate the value at the calculated index if it is positive to mark the number as present.
Negating only positive values prevents double negation, which could revert the marking and cause incorrect results.
return missing_numbers
Return the list of missing numbers.
This returns the final answer to the caller, completing the function.
for num in nums:
Iterate through each number in the input array.
This loop processes every element to mark the presence of numbers by modifying the array in place, enabling O(1) extra space.
index = abs(num) - 1
Calculate the index corresponding to the current number's absolute value minus one.
Using abs(num) ensures that previously negated values do not affect the index calculation, preserving correctness.
missing_numbers = []
Initialize an empty list to collect missing numbers.
This list will store all numbers corresponding to indices that remain positive after marking, representing missing elements.
for index, num in enumerate(nums):
Enumerate through the array to access both index and value.
Enumerate provides the index needed to map back to the original number (index + 1) and the value to check if it was marked.
if num > 0:
Check if the current value is positive, indicating the number is missing.
Positive values mean the corresponding number was never marked as present, so it must be missing from the input.
missing_numbers.append(index + 1)
Append the missing number (index + 1) to the result list.
Adding index + 1 translates the zero-based index back to the original number domain.
Test Your Understanding
Why does negating the value at the index corresponding to a number's value correctly mark that number as present?
Because each number maps uniquely to an index (num - 1), negating the value at that index flags that the number has been seen. Positive values at the end indicate indices whose numbers were never encountered.
Related Problems
Sorting pattern
Don't just read it. Drill it.
Reconstruct Find All Numbers Disappeared in an Array from memory until it sticks. AlgoDrill blanks out key lines and makes you fill them back in, step by step.
Unlock the Find All Numbers Disappeared in an Array drill