0

Given an array of first n natural numbers with one element missing and one duplicate element. figure out both numbers. I was asked this question in one of the interviews.

For example, the sample array is

5, 2, 1, 4, 5
where n=5

Answer for the given array should be:

missing element = 3
duplicate element = 5

Is it possible to come-up with the solution without iterating the array.

I was able to come-up with a solution having complexity O(nlogn), without extra space. Does any O(n) solution exist (without extra space)?

Prometheus
  • 549
  • 1
  • 7
  • 18

1 Answers1

4

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...

ewcz
  • 12,819
  • 1
  • 25
  • 47
  • 1
    From [here](https://brilliant.org/wiki/sum-of-n-n2-or-n3/): S = (n(n+1))/2 S_2 = (n(n+1)(2n+1))/6 So you don't have to calculatete it step by step ;) – HermanTheGermanHesse Jul 30 '18 at 09:37
  • 1
    @HermanTheGermanHesse indeed, that should simplify things! :) I will include it into the answer... – ewcz Jul 30 '18 at 11:32