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:
Array has n=3 elements, so range is [0,1,2,3]. Array contains {0,1,3}. Using XOR or sum formula, This finds 2 is missing.
Input:
nums = [1]Output:
0Explanation:
The missing number is 0.
Input:
nums = [0,1,2,3,5,6,7]Output:
4Explanation:
The missing number is 4.
Constraints
- •
n == nums.length - •
1 ≤ n ≤ 10⁴ - •
0 ≤ nums[i] ≤ n