#C6967. Find the Missing and Duplicate Number
Find the Missing and Duplicate Number
Find the Missing and Duplicate Number
You are given an array of n integers. The array is supposed to contain all the numbers from 1 to n exactly once. However, due to an error, one of the numbers is missing and another number is duplicated. Your task is to find the missing number and the duplicated number.
The input is given via standard input and the output should be printed on standard output. You are required to output the missing number and the duplicated number separated by a space.
Note: All formulas below are in LaTeX format.
Let \(S = 1 + 2 + \cdots + n\), then the expected sum is \(S = \frac{n(n+1)}{2}\). Also, the sum of squares is \(S_{sq} = 1^2 + 2^2 + \cdots + n^2\), where \(S_{sq} = \frac{n(n+1)(2n+1)}{6}\). If the actual sum of the array is \(S_a\) and the actual sum of squares is \(S_{a,sq}\), then we have:
\(\text{missing} - \text{duplicate} = S - S_a\) and \(\text{missing}^2 - \text{duplicate}^2 = S_{sq} - S_{a,sq}\).
Solve these equations to get the required values.
inputFormat
The input consists of two lines:
- The first line contains an 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 the array contains numbers from 1 to n with exactly one duplicate and one missing number.
outputFormat
Output a single line containing two integers separated by a space. The first integer is the missing number and the second is the duplicated number.
## sample6
4 3 6 2 1 6
5 6
</p>