Easy interview question got harder: given numbers 1..100, find the missing number(s) given exactly k are missing

前端 未结 30 1299
时光说笑
时光说笑 2020-11-22 07:02

I had an interesting job interview experience a while back. The question started really easy:

Q1: We have a bag containing numbers

30条回答
  •  忘了有多久
    2020-11-22 07:56

    We can solve Q2 by summing both the numbers themselves, and the squares of the numbers.

    We can then reduce the problem to

    k1 + k2 = x
    k1^2 + k2^2 = y
    

    Where x and y are how far the sums are below the expected values.

    Substituting gives us:

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

    Which we can then solve to determine our missing numbers.

提交回复
热议问题