Find All Numbers Disappeared in an Array - Complete Solution Guide
Find All Numbers Disappeared in an Array is LeetCode problem 448, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 Follow up: Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.
Detailed Explanation
The problem asks us to find all numbers within the range [1, n] (inclusive) that are *missing* from a given array `nums` of `n` integers. Each element in the `nums` array is guaranteed to be within the same range [1, n]. The goal is to return a list of these missing numbers. For example, if `nums` is [4,3,2,7,8,2,3,1], the missing numbers are 5 and 6, since they are the only numbers within the range [1, 8] that are not present in the array.
Solution Approach
The provided solutions use a hash set (or a boolean array) to keep track of the numbers present in the input array. After iterating through the array and adding each number to the set, the code iterates from 1 to `n` (inclusive), checking if each number is present in the set. If a number is *not* present in the set, it is added to the result list. The C solution is a variation which utilizes in-place modification to achieve O(1) extra space.
Step-by-Step Algorithm
- Step 1: Create a hash set (or boolean array) to store the numbers present in the input array `nums`.
- Step 2: Iterate through the `nums` array and add each number to the hash set (or set the corresponding index in the boolean array to true).
- Step 3: Create an empty list to store the missing numbers.
- Step 4: Iterate from 1 to `n` (inclusive).
- Step 5: For each number `i` in the range [1, n], check if it is present in the hash set (or if the corresponding index in the boolean array is true).
- Step 6: If the number `i` is *not* present in the hash set (or the corresponding index in the boolean array is false), add it to the list of missing numbers.
- Step 7: Return the list of missing numbers.
Key Insights
- Insight 1: The problem leverages the fact that the numbers in the input array are within a specific range ([1, n]) which can be used as indices.
- Insight 2: Using a set or a boolean array allows efficient checking of the presence of each number in the specified range.
- Insight 3: The 'in-place' modification of the input array (as done in the C solution) is a clever way to achieve O(1) space complexity (excluding the output array), by utilizing the existing array as a hash table.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Array, Hash Table.
Companies
Asked at: Tinkoff.