#K76447. Find the Missing Number
Find the Missing Number
Find the Missing Number
You are given an integer n and an array of n-1 distinct integers. The array contains unique integers chosen from the set \(\{1, 2, \ldots, n\}\), meaning that exactly one number is missing. Your task is to determine the missing number quickly and efficiently. A common approach is to use the formula for the sum of the first \(n\) natural numbers: \(\text{total} = \frac{n(n+1)}{2}\). By subtracting the sum of the array elements from the total, the missing number can be found.
Example: If n = 5
and the array is [2, 3, 1, 5]
, then the sum of all numbers from 1 to 5 is \(15\), and the sum of the array is \(11\); hence, the missing number is \(15 - 11 = 4\).
inputFormat
The input is read from stdin and consists of two lines:
- The first line contains a single integer
n
indicating the number of elements that should be present from \(1\) to \(n\). - The second line contains
n-1
space-separated integers representing the elements of the array.
outputFormat
The output is written to stdout and should be a single integer: the missing number.
## sample5
2 3 1 5
4