CodeSpeek

Missing Number

Easy · Bit Manipulation

You are given an array containing n distinct numbers taken from the range 0 to n inclusive, but one number in that range is missing from the array. Return the missing number. The array is not necessarily sorted.

Examples

Input:  nums = [3,0,1]
Output: 2
Why:    n is 3, the full range is 0..3, and 2 is the only value not present.
Input:  nums = [0,1]
Output: 2
Why:    n is 2, the full range is 0..2, and 2 is missing.
Input:  nums = [9,6,4,2,3,5,7,0,1]
Output: 8
Why:    n is 9, the full range is 0..9, and 8 is the only value not present.

Constraints

1 <= nums.length <= 10^4, 0 <= nums[i] <= nums.length, all values in nums are distinct

Practise it by voice

Describe the solution out loud and the interviewer writes exactly what you say, asks when you are vague, and runs the tests in your browser.

Practise Missing Number

This statement is written for CodeSpeek. The problem is part of the NeetCode 150 list; Watch NeetCode's explanation of Missing Number. Reference solutions from the NeetCode repository (MIT) are used to verify our tests.