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:2
Explanation:

2 is missing from [0,1,2,3].

Input:nums = [1]
Output:0
Explanation:

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:4
Explanation:

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

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!