209. Missing Number

Easy
Index Range Invariant
Array

Problem

Given an array nums containing n distinct numbers taken from the range [0, n], return the only number from that range that is missing.

Examples

Example 1

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

Example 2

Input: [0,1]
Output: 2

Example 3

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

1 <= nums.length <= 100000

All values are distinct.

0 <= nums[i] <= nums.length

Hints

?? The range has one more value than the array contains.

?? XOR or the arithmetic-series formula both avoid extra storage.

Expected Complexity
Time: O(n)
Space: O(1)
Follow-up

Can you solve it in O(n) time and O(1) space?

Practice Notes

Attempts: 0

Time spent: 0min

Hints used: 0

00:00
Loading...
Case 1
Case 2
Case 3
Input
[3,0,1]
Expected
2