Missing Number
EasyBit Manipulation
You are given an array nums of n distinct integers, each in the range 0 to n inclusive. That range has n + 1 values, so exactly one of them is missing from nums. Return the missing value.
The array is not sorted. Aim for O(n) time and O(1) extra space.
Example 1
- Input:
- nums = [4,0,3,1]
- Output:
- 2
- Explanation
- There are 4 values, so the full range is 0 through 4. The array has 0, 1, 3 and 4, so 2 is the one missing.
Example 2
- Input:
- nums = [1,2,3]
- Output:
- 0
- Explanation
- Three values means the range is 0 through 3. Everything except 0 is present.
Example 3
- Input:
- nums = [0,1,2]
- Output:
- 3
- Explanation
- Three values means the range is 0 through 3. 0, 1 and 2 are all there, so the missing value is the top of the range, 3.
Constraints
n == nums.length1 <= n <= 10^40 <= nums[i] <= n- All values in
numsare distinct