Description
Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array. Use O(1) extra space and O(n) runtime.
Examples
Input:
nums = [3,0,1]Output:
2Explanation:
2 is missing from [0,1,2,3].
Input:
nums = [1]Output:
0Explanation:
n=1, so the range is [0,1]. The array contains 1, so 0 is missing.
Input:
nums = [0,1,2,3,5,6,7]Output:
4Explanation:
n=7, so the range is [0,1,2,3,4,5,6,7]. All numbers are present except 4, which is missing from the middle of the sequence.
Constraints
- •
n == nums.length - •
1 ≤ n ≤ 10⁴ - •
0 ≤ nums[i] ≤ n