How do you do this? The values are unsorted but are of [1..n] Example array [3,1,2,5,7,8]. Answer: 4, 6
I saw this solution in
Let x and y be the roots of a quadratic equation.
SUM = x + yPRODUCT = x * yIf we know the sum and the product, we can reconstruct the quadratic equation as:
z^2 - (SUM)z + (PRODUCT) = 0
In the algorithm you mentioned, we find the sum and the product, and from that, we reconstruct the quadratic equation using the above formula.
If you are interested in a detailed derivation, here is a reference. Read "Reconstruction of the quadratic equation from the sum and product of roots".