Find All Numbers Disappeared in an Array
Find All Numbers Disappeared in an Array is a Easy Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Array/Matrix Manipulation Patterns pattern, in the Array - Cyclic Sort subpattern.
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 <= 105
- 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.
Topics and companies
Topics: Array, Hash Table.
Reported in interviews at Amazon.
How to practise Find All Numbers Disappeared in an Array on Thita.ai
Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(1) space.
Open Find All Numbers Disappeared in an Array in the code editor, or read the Find All Numbers Disappeared in an Array editorial for a worked solution with its approach and complexity analysis.
Where Find All Numbers Disappeared in an Array sits in the DSA pattern sheet
- Array/Matrix Manipulation Patterns pattern guide
- Array - Cyclic Sort subpattern
- The full DSA patterns sheet
- All coding practice problems
Problems related to Find All Numbers Disappeared in an Array
Other problems that use the same Array - Cyclic Sort and Array/Matrix Manipulation Patterns techniques:
- Find All Duplicates in an Array — Medium, same subpattern
- First Missing Positive — Hard, same subpattern
- Add Binary — Easy, same pattern
- Add to Array-Form of Integer — Easy, same pattern
- Diagonal Traverse — Medium, same pattern
- Game of Life — Medium, same pattern
Preparing your workspace
Progress is saved as you study.