Let's say that you have numbers 1,...,n
. Now, denote their sum 1 + 2 + ... + n
as S
. If we denote the missing number as j
and the duplicit one as k
, we will get for the modified sum S' = S - j + k
, so that's one equation for two variables. We can repeat the same procedure, but this time, summing second powers, thus S_2 = 1 + 4 + ... + n^2
. For the sequence with one missing and one duplicit number, the result will be S_2' = S_2 - j*j + k*k
. Thus we get two equations for two variables.
In total, we have:
S' = S - j + k
S_2' = S_2 - j*j + k*k
therefore
k - j = S' - S =: a
k*k - j*j = S_2' - S_2 =: b
where we introduced the symbols a
and b
to simplify the notation.
Now, k*k - j*j = (k - j)*(k + j)
, thus:
k - j = a
k*k - j*j = a * (k + j) = b
summing both equations gives:
2*k = b/a + a
2*j = b/a - a
For your particular example:
S = 1 + 2 + 3 + 4 + 5 = 15
S_2 = 1 + 4 + 9 + 16 + 25 = 55
For the series with one missing and one duplicit element:
S' = 5 + 2 + 1 + 4 + 5 = 17
S_2' = 25 + 4 + 1 + 16 + 25 = 71
then:
a = S' - S = 2
b = S_2' - S_2 = 16
therefore:
2*k = 8 + 2 = 10
2*j = 8 - 2 = 6
which gives:
k = 5, j = 3
In practice (as noted by @HermanTheGermanHesse), one can obtain closed-form expressions for S
as well as S_2
in terms of polynomials in n
(so-called Faulhaber's formulae), namely: S = n*(n+1)/2
and S_2 = n*(n+1)*(2*n+1)/6
. Therefore it is just sufficient to go once over the input data, accumulate S'
and S_2'
, and use the formulae for k
and j
given above...