#C595. Find the Missing Number
Find the Missing Number
Find the Missing Number
You are given an array of n distinct integers taken from the range \([0, n]\) (inclusive) with exactly one number missing. Your task is to find the missing number.
You can solve this problem by using the formula for the sum of the first \(n\) natural numbers: \(\frac{n(n+1)}{2}\). Compute the expected sum of numbers from 0 to \(n\), then subtract the actual sum of the array. The difference is the missing number.
inputFormat
The input is read from standard input (stdin). The first line contains an integer (n) representing the number of elements in the array. The second line contains (n) space-separated integers. Note: If (n = 0), the second line is omitted.
outputFormat
Output to standard output (stdout) a single integer which is the missing number in the range [0, n].## sample
3
3 0 1
2
</p>