Skip to content
Decoder

Missing Number

Easy

Problem

nums holds n distinct numbers taken from the range 0 to n, so exactly one number in that range is missing. Return it. Try 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
Why
n = 2, so the range is 0..2

Example 3

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

Constraints

  • n == nums.length
  • 1 <= n <= 10^4
  • 0 <= nums[i] <= n
  • All the numbers of nums are unique.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.