1

Consider the numerical 20 questions game. In this game, player 1 thinks of a number in the range 1 to n. Player 2 has to figure out this number by asking the fewest number of true/false questions.Assume that nobody cheats. i. What is an optimal strategy if n in known? ii. What is a good strategy is n is not known?

4
  • 4
    That is called binary search in CS terms. Commented Feb 21, 2013 at 13:34
  • or Dichotomic search Commented Feb 21, 2013 at 13:37
  • Binary search if n is known, otherwise the problem becomes more interesting. Commented Feb 21, 2013 at 13:39
  • 1
    @IVlad if upper bound is unknown, first doubling from 1(to find a upper bound), then halving(to find the exact number). Commented Feb 21, 2013 at 13:50

1 Answer 1

7

Please refer to this wiki: binary search for number guessing game.

If n is known, use binary search algorithm to find it, so asked questions is not more than floor(log2(n)).

If n is unknown, you can first find an upper bound by repeated doubling, then apply binary search. It's clear that asked questions is not more than 2 * floor(log2(k)) + 1, where k is the unknown selected number.

Sign up to request clarification or add additional context in comments.

2 Comments

yeah! thanks for that! but in the question, it says that, it should be done in Optimal and in Good strategy!! :(
I updated my answer to give the time complexity for both cases. They are proved to be optimal and surely good strategies.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.