← Home

448. Find All Numbers Disappeared in an Array

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
448

I like to read this solution as a small machine: keep the useful information, throw away the noise. For 448. Find All Numbers Disappeared in an Array, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are findDisappearedNumbers, exist.

Guide

Why?

The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Initialize the memory or helper structure.
  2. Process candidates in the order the invariant expects.
  3. Update the answer only when the current state is valid.
  4. Return the value that represents the fully processed input.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01/**
02Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.
03
04Find all the elements of [1, n] inclusive that do not appear in this array.
05
06Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.
07
08Example:
09
10Input:
11[4,3,2,7,8,2,3,1]
12
13Output:
14[5,6]
15**/
16
17class Solution {
18public:
19    vector<int> findDisappearedNumbers(vector<int>& nums) {
20        //Method 1(TLE)
21        /**
22        if(nums.size()==0){
23            return vector<int>();
24        }
25        int _min = nums[0], _max = nums[0];
26        vector<int> ans;
27        for(int num : nums){
28            if(num < _min){
29                for(int i = num+1; i < _min; i++){
30                    ans.push_back(i);
31                }
32                _min = num;
33            }
34            
35            if(num > _max){
36                for(int i = _max+1; i < num; i++){
37                    ans.push_back(i);
38                }
39                _max = num;
40            }
41            
42            vector<int>::iterator it = find(ans.begin(), ans.end(), num);
43            if(it!=ans.end()){
44                ans.erase(it);
45            }
46        }
47        **/
48        
49        //Method 2(TLE)
50        /**
51        if(nums.size()==0){
52            return vector<int>();
53        }
54        //if nums' values won't cover the range of [1,n]
55        for(int i = 1; i < _min; i++){
56            ans.push_back(i);
57        }
58        
59        for(int i = nums.size(); i > _max; i--){
60            ans.push_back(i);
61        }
62        
63        vector<int> ans(nums.size());
64        iota(ans.begin(), ans.end(), 1); //1 is the starting number
65        
66        for(int num : nums){
67            ans.erase(remove(ans.begin(), ans.end(), num), ans.end());
68        }
69        **/
70        
71        //Method 3
72        /**
73        if(nums.size()==0){
74            return vector<int>();
75        }
76        
77        vector<bool> exist(nums.size(), false);
78        for(int num : nums){
79            exist[num-1] = true;
80        }
81        
82        vector<int> ans;
83        for(int i = 0; i < nums.size(); i++){
84            if(exist[i]==false){
85                ans.push_back(i+1);
86            }
87        }
88        **/
89        
90        //Method 4(In-place)
91        //https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/discuss/92956/Java-accepted-simple-solution
92        vector<int> ans;
93        for(int i = 0; i < nums.size(); i++){
94            //after we seen nums[i], we make the position of nums[i]-1 negative 
95            int val = abs(nums[i])-1;
96            if(nums[val]>0){
97                //we have seen val+1 in nums
98                nums[val] *= -1;
99            }
100        }
101        
102        for(int i = 0; i < nums.size(); i++){
103            if(nums[i]>0) ans.push_back(i+1);
104        }
105        
106        return ans;
107    }
108};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.