Menu
CoddyTech

Missing Number

You get a list nums of n distinct integers, each between 0 and n. The range from 0 to n holds n+1 numbers, so exactly one of them is not in the list. Return that missing number.

Function

missingNumber(nums: integer-array) → integer
numsinteger-array
n distinct integers from the range 0 to n, in any order
Returnsinteger
the one number from 0 to n that is not in nums

Constraints

  • n == nums.length
  • 1 ≤ n ≤ 104
  • 0 ≤ nums[i] ≤ n
  • All the values in nums are distinct.

Examples

Input
nums = [4, 2, 0, 1]
Output
3
Explanation
The list has 4 values, so the range is 0 to 4. It holds 0, 1, 2 and 4, and 3 is the only number with no match.

lock icon+13 hidden tests on Submit

challenge icon

Follow-up

If the list came sorted, could you find the missing number in O(log n) time with binary search?

Reset code
def missingNumber(nums):
    # Write code here
Test cases

Case 1

Case 2

Case 3

Input

nums = [4, 2, 0, 1]

Expected

3