You are given an array of n distinct integers, each drawn from the range 0 to n inclusive. Since the range 0..n contains n + 1 values but the array has only n of them, exactly one value from 0..n is absent. Determine the missing value.
The missing value is unique, so the answer is uniquely determined.
Line 1: an integer n, the number of elements in the array.
Line 2: n space-separated distinct integers, each in the range 0..n. If n is such that the line would be empty it will still be present but blank.
A single integer: the value in 0..n that does not appear in the array.
n array values are distinct and every one lies in [0, n].0..n is missing.Example 1
Input
4 0 1 3 4
Expected
2
Explanation
The range is 0..4. The array contains 0, 1, 3, 4, so the absent value is 2.
Example 2
Input
3 3 1 2
Expected
0
Explanation
The range is 0..3. The array contains 1, 2, 3, so 0 is the missing value.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →