0

I was looking at the problem and the discussion here: Easy interview question got harder: given numbers 1..100, find the missing number(s)

One of the user provided a solution using following equation. k1 + k2 = x

k1^2 + k2^2 = y

Substituting provides (x-k2)^2 + k2^2 = y

I am trying to solve this equation further and come up with a C program to solve the problem of finding duplicates. Inspite of spending lot of time I couldn't solve this equation to get k1 or k2 one side. I always ended up with k1 or k2 on both side of equation.

Any help is appreciated.

Community
  • 1
  • 1
user1150393
  • 139
  • 1
  • 15

1 Answers1

1

Expand the equation

(x - k2)^2 + k2^2 = y

and get

x^2 - 2xk2 + 2k2^2 = y

or

2k2^2 - 2xk2 + x^2 - y = 0

Now use the formula for solving the quadratic equation az^2 + bz + c = 0 which is (-b +/- sqrt(b^2 - 4ac))/2a. Only that in our case z=k2. So

k2 = (2x +/- sqrt(4x^2 - 8(x^2 - y))) / 4

or

k2 = (x +/- sqrt(x^2 - 2(x^2 - y))) / 2

   = (x +/- sqrt(2y - x^2)) / 2

and you can put

k2 = (x + sqrt(2y - x^2)) / 2
k1 = (x - sqrt(2y - x^2)) / 2.
Leandro Caniglia
  • 14,495
  • 4
  • 29
  • 51