#C11385. Find the Missing Number
Find the Missing Number
Find the Missing Number
You are given an unsorted array of n distinct integers, where each integer is in the range \( [0, n] \). This means that the array should contain all the numbers from \(0\) to \(n\) (inclusive) except for exactly one missing number. Your task is to find and output the missing number.
The correct missing number can be calculated by comparing the expected sum of all numbers from \(0\) to \(n\) with the actual sum of the given numbers. The expected sum is given by the formula:
\( S = \frac{n(n+1)}{2} \)
and the missing number is \( S - \text{(sum of the elements)} \).
Read the input from standard input and print the missing number to standard output.
inputFormat
The first line of input contains an integer \( n \), which represents the number of elements in the array. The second line contains \( n \) space-separated integers, representing the distinct elements of the array. These integers belong to the range \( [0, n] \) with exactly one missing number.
outputFormat
Output a single integer, the missing number, to standard output.
## sample3
3 0 1
2