Find the one number missing from an array containing distinct values from a consecutive range.
Missing Number
You are given an array nums containing n distinct integers. The values are drawn from the range [0, n], and exactly one number from that range does not appear in the array.
Your task is to return the missing number.
A solution should run efficiently in linear time and use constant extra space if possible.
Input Format
- An integer array
nums numscontains distinct integers- Each value is in the range
[0, n], wheren = nums.length - Exactly one number in
[0, n]is absent
Output Format
Return the single missing integer.
Constraints
1 <= nums.length <= $10^{5}$0 <= nums[i] <= nums.length- All values in
numsare distinct - Exactly one number in the inclusive range
[0, nums.length]is missing
Example 1
Input
nums = [3,0,1]
Output
2
Explanation
The numbers from 0 to 3 are {0,1,2,3}. The value 2 does not appear in the array.
Example 2
Input
nums = [0,1]
Output
2
Explanation
The range is {0,1,2}, so 2 is missing.
Show 1 more example
Example 3
Input
nums = [9,6,4,2,3,5,7,0,1]
Output
8
Explanation
The array contains every number from 0 to 9 except 8.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.