Algorithm to find minimum number of weightings required to find defective ball from a set of n balls

后端 未结 3 987
梦谈多话
梦谈多话 2021-01-02 22:05

Okay here is a puzzle I come across a lot of times- Given a set of 12 balls , one of which is defective (it weighs either less or more) . You are allow to weigh 3 times to f

3条回答
  •  孤独总比滥情好
    2021-01-02 22:30

    Trichotomy ! :)

    Explanation : Given a set of n balls, subdivide it in 3 sets A, B and C of n/3 balls.

    Compare A and B. If equal, then the defective ball is in C. etc.

    So, your minimum number of times is the number of times you can divide n by three (sorry, i do not know the english word for that).

提交回复
热议问题