v9.1.1
Menu

Missing Number

easyPROArrays & HashingNetflix DSA

Given an array containing n distinct numbers from 0, 1, 2, ..., n, find the one that is missing.

Example

Input
3,0,1
Output
2
Reasoning
  • Input: nums is [3,0,1][3, 0, 1], a list of distinct whole numbers. Its length is n=3n = 3, and that count fixes the range: the values are supposed to come from {0,1,…,n}={0,1,2,3}\{0, 1, \dots, n\} = \{0, 1, 2, 3\}.
  • That range holds n+1=4n + 1 = 4 candidates but the list only has 33 entries, so exactly one candidate is absent.
  • Math: the answer is the single element of {0,1,2,3}∖{3,0,1}\{0, 1, 2, 3\} \setminus \{3, 0, 1\}. Checking each candidate: 00 is present, 11 is present, 22 is not, 33 is present.
  • Output: the integer 2, a single number rather than a list, because there is only ever one gap.
  • Notice that nn is read from the list's length, not from its largest value. For [0,1][0, 1], n=2n = 2, so the range is {0,1,2}\{0, 1, 2\} and the missing value is 2, the top end itself, even though it is larger than every value in the list. The order of nums never affects the answer.

Constraints

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

More from Netflix DSA

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.