#K1636. Missing Number Finder
Missing Number Finder
Missing Number Finder
You are given a positive integer n and an array of unique integers. The array contains numbers in the range \(1\) to \(n\). Ideally, if one number is missing, the array length is \(n-1\); if no number is missing, the array length is \(n\). Your task is to find the missing number if it exists, or print 0 if the array is complete.
Recall that the sum of the first \(n\) natural numbers is given by the formula:
[ S = \frac{n(n+1)}{2} ]
You can use this formula to compute the expected sum, then subtract the sum of the given array elements. If the difference is 0, then no number is missing; otherwise, the difference is the missing number.
inputFormat
The input is read from stdin and consists of two lines:
- The first line contains an integer
n
representing the upper bound of the number range. - The second line contains space-separated integers which are the elements of the array. The array will contain either \(n-1\) numbers (if one number is missing) or \(n\) numbers (if no number is missing).
outputFormat
Output the missing number to stdout. If no number is missing (i.e. the array is complete), output 0.
## sample8
1 2 4 6 3 7 8
5