Missing Number Finder
easySave
ArrayBit ManipulationMath
Given an array containing n distinct numbers taken from the range 0 to n, find the one number in the range that is missing from the array. For example, if n = 3 and the array is [3, 0, 1], the missing number would be 2.
Example 1
Input
[ [ 3, 0, 1 ] ]
Output
2
Explanation. The number '2' is missing from the sequence 0 to 3.
Example 2
Input
[ [ 0, 1 ] ]
Output
2
Explanation. The number '2' is missing from the sequence 0 to 2.
Example 3
Input
[ [ 9, 6, 4, 2, 3, 5, 7, 0, 1 ] ]
Output
8
Explanation. The number '8' is missing from the sequence 0 to 9.
Example 4
Input
[ [ 0 ] ]
Output
1
Explanation. The number '1' is missing from the sequence 0 to 1.
Follow-up: Can you solve the problem in linear time and constant space complexity?
Constraints:
The provided array will always have a length of at least 1 and at most 10,000. All elements of the array will be unique.
- Views
- 2