#K37467. Find the Missing Number
Find the Missing Number
Find the Missing Number
You are given an array containing n distinct numbers taken from the range \(0\) to \(n\). In other words, the array contains exactly \(n\) numbers, while the complete range should have \(n+1\) numbers. Your task is to find the one missing number from the array.
For example:
- For the input array
[3, 0, 1]
, the array length is3
, and the numbers should be in the range \(0\) to \(3\). The missing number is \(2\). - For the input array
[0, 1]
, the missing number is \(2\) since the full range is \(0\) to \(2\).
You can use the formula for the sum of the first \(n\) natural numbers, given by \(S = \frac{n(n+1)}{2}\), where \(n\) is the number of elements that should be in the complete range. The difference between this expected sum and the sum of the given array elements gives the missing number.
inputFormat
The input is read from standard input. The first line contains a single integer \(n\) representing the number of elements in the array. The second line contains \(n\) space-separated integers, which are the elements of the array.
Note that although the array has \(n\) elements, the full range of numbers should be from \(0\) to \(n\) (inclusive), meaning one number in this range is missing.
outputFormat
Output the missing number to standard output.
## sample3
3 0 1
2
</p>