#K32767. Find the Missing Number
Find the Missing Number
Find the Missing Number
You are given an unsorted list of distinct integers taken from the set \(\{1, 2, \dots, n\}\) with exactly one number missing. Your task is to find and output this missing number.
The list will contain \(n-1\) numbers. The missing number can be found by computing the expected sum of the first \(n\) natural numbers using the formula \(S = \frac{n(n+1)}{2}\), and then subtracting the sum of the given numbers from \(S\).
Example: For the input 3 7 1 2 8 4 5
, since there are 7 numbers, we deduce that \(n=8\). The expected sum is \(1+2+\cdots+8=36\) and the actual sum is \(30\); hence the missing number is \(36-30=6\).
inputFormat
The input is provided on a single line from stdin. It consists of space-separated integers representing an unsorted list with exactly one missing number from the consecutive sequence starting from 1.
outputFormat
Output a single integer to stdout—the missing number in the sequence.
## sample3 7 1 2 8 4 5
6