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

前端 未结 30 1798
时光说笑
时光说笑 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:53

    Can you check if every number exists? If yes you may try this:

    S = sum of all numbers in the bag (S < 5050)
    Z = sum of the missing numbers 5050 - S

    if the missing numbers are x and y then:

    x = Z - y and
    max(x) = Z - 1

    So you check the range from 1 to max(x) and find the number

提交回复
热议问题