#K62752. Find the Duplicate and Missing Numbers
Find the Duplicate and Missing Numbers
Find the Duplicate and Missing Numbers
You are given an array of length (n) containing integers from (1) to (n). In this array, exactly one number occurs twice (duplicate) and another number is missing. Your task is to find these two numbers: the duplicate and the missing one.
The mathematical relations used in this problem are based on the formulas for the sum and the sum of squares of the first (n) natural numbers:
[ S = 1 + 2 + \cdots + n = \frac{n(n+1)}{2} ]
[ S_2 = 1^2 + 2^2 + \cdots + n^2 = \frac{n(n+1)(2n+1)}{6} ]
By comparing the expected sums with the actual sums from the input array, you can derive the duplicate and missing numbers.
Input is read from standard input (stdin) and output is written to standard output (stdout).
inputFormat
The first line contains an integer (n), indicating the number of elements in the array. The second line contains (n) space-separated integers, representing the array elements.
outputFormat
Print two integers separated by a space: the first is the duplicate number and the second is the missing number.## sample
5
1 2 2 4 5
2 3