You're given an array nums containing n distinct numbers taken from the range [0, n]. This means if the array has 3 elements, the numbers should come from [0, 1, 2, 3].

Here's the catch: exactly one number from this range is missing from the array. Your task is to find and return that missing number.

Example: If nums = [3, 0, 1], then n = 3 and the complete range should be [0, 1, 2, 3]. The missing number is 2.

Can you solve this efficiently? There are multiple clever approaches using math, bit manipulation, and more!

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [3,0,1]
โ€บ Output: 2
๐Ÿ’ก Note: Array has 3 elements, so complete range should be [0,1,2,3]. Numbers 0, 1, 3 are present, but 2 is missing.
example_2.py โ€” Missing First
$ Input: nums = [1]
โ€บ Output: 0
๐Ÿ’ก Note: Array has 1 element, so complete range should be [0,1]. Number 1 is present, but 0 is missing.
example_3.py โ€” Missing Last
$ Input: nums = [0,1]
โ€บ Output: 2
๐Ÿ’ก Note: Array has 2 elements, so complete range should be [0,1,2]. Numbers 0 and 1 are present, but 2 is missing.

Constraints

  • n == nums.length
  • 1 โ‰ค n โ‰ค 104
  • 0 โ‰ค nums[i] โ‰ค n
  • All the numbers of nums are unique

Visualization

Tap to expand
๐Ÿ” The Missing Number Detective StoryEvidence Locker: [3, 0, 1] - One piece missing from [0,1,2,3]๐Ÿ”จ Brute Force Detective:"I'll check each possible evidence number..."O(nยฒ) - Very slow!๐Ÿ“š Hash Table Detective:"Let me catalog what I have first..."O(n) time, O(n) space๐Ÿง  Mathematical Detective:"Eureka! Expected sum - Actual sum = Missing number!"O(n) time, O(1) space โญExpectedSum: 6-ActualSum: 4=2๐ŸŽฏ Investigation Methods:Brute Force: Check everything repeatedlyHash Table: Smart cataloging systemMathematics: Brilliant insight!๐Ÿ† Winner: Math Formulaโ€ข Fastest: O(n) timeโ€ข Most efficient: O(1) spaceโ€ข Elegant: Uses Gauss's formulaโ€ข Interview gold: Shows mathematical thinking
Understanding the Visualization
1
Crime Scene Analysis
You know there should be n+1 pieces of evidence numbered 0 through n, but you only found n pieces.
2
The Brute Force Method
Check each possible evidence number by searching through your entire collection - slow but thorough.
3
The Hash Table Method
Create a quick-reference catalog of all evidence you have, then check which number isn't in your catalog.
4
The Mathematical Breakthrough
Use Gauss's discovery: the sum of numbers 0+1+2+...+n equals n(n+1)/2. The missing evidence number equals the difference between expected and actual sums!
Key Takeaway
๐ŸŽฏ Key Insight: Sometimes the most elegant solution comes from mathematical properties rather than complex algorithms. Gauss's sum formula transforms this from a search problem into a simple arithmetic calculation!
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
89.5K Views
Very High Frequency
~15 min Avg. Time
2.3K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen