← Home

268. Missing Number

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
bit manipulationC++Markdown
268

The trick here is to name the state correctly, then let the implementation follow. For 268. Missing Number, the solution in this repository is mainly a bit manipulation solution.

Guide

What?

Before optimizing anything, pin down what information is still useful after each move. 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: bit manipulation.

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 missingNumber, full.

Guide

Why?

The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.

  • 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. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

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//Runtime: 1080 ms, faster than 6.59% of C++ online submissions for Missing Number.
02//Memory Usage: 10.3 MB, less than 9.80% of C++ online submissions for Missing Number.
03
04class Solution {
05public:
06    int missingNumber(vector<int>& nums) {
07        vector<int> full(nums.size()+1);
08        iota(full.begin(), full.end(), 0);
09        
10        for(int num : nums){
11            full.erase(remove(full.begin(), full.end(), num), full.end());
12        }
13        
14        return full[0];
15    }
16};
17
18//https://leetcode.com/problems/missing-number/discuss/69777/C%2B%2B-solution-using-bit-manipulation
19//Runtime: 24 ms, faster than 80.72% of C++ online submissions for Missing Number.
20//Memory Usage: 9.9 MB, less than 70.59% of C++ online submissions for Missing Number.
21class Solution {
22public:
23    int missingNumber(vector<int>& nums) {
24        int result = 0;
25        //exclusive-or from (a) [0 to n] and (b) [all number in "nums"]
26        //same numbers in (a) and (b) will counteract each other
27        //so it will remain 0 ^ the missing number = the missing nuber
28        for(int i = 0; i < nums.size(); i++){
29            result ^= nums[i];
30            result ^= i;
31        }
32        result ^= nums.size();
33        return result;
34    }
35};

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.