Missing Number

Tries & Bit Manipulation, problem 4 of 8

Missing Number

Easy

LC #268

bit manipulationxormath

Not attempted yet

nums holds n distinct numbers taken from the range 0..n (that's n + 1 candidates), so exactly one is missing. Return it.

Aim for O(n) time and O(1) extra space.

Example 1

Input: nums = [3, 0, 1]
Output: 2

Example 2

Input: nums = [0, 1]
Output: 2

n = 2, so the range is 0..2 and 2 is missing.

Example 3

Input: nums = [9, 6, 4, 2, 3, 5, 7, 0, 1]
Output: 8

Constraints

  • 1 ≤ n ≤ 10⁵
  • all values distinct, each in 0..n

Python

Loading draft…

Test results

8 tests available

No results yet

Run tests your code against the examples; Submit runs the hidden tests too.

3 examples, 5 hidden

Run examples, then submit all tests.