#C11247. Find The Missing Number
Find The Missing Number
Find The Missing Number
You are given an array of distinct numbers that contains exactly n-1 elements taken from the range 0
to n-1
. In other words, one number from that range is missing. Your task is to determine the missing number.
The missing number can be computed by using the formula for the sum of the first n natural numbers, i.e., \(\frac{n(n-1)}{2}\), where n = length(array) + 1
since one number is missing.
Example: For the array [3, 1, 0], the array length is 3, so n equals 4. The sum of numbers from 0 to 3 is \(\frac{4\times3}{2} = 6\). The sum of the array is 4, so the missing number is 6 - 4 = 2.
inputFormat
The input consists of a single line of space-separated integers representing the array of numbers. The array has exactly n-1
integers, representing distinct numbers from 0 to n-1 with one number missing.
outputFormat
Output a single integer which is the missing number from the array. The output should end with a newline character.
## sample3 1 0
2
</p>