Puzzle 69 |The Number Game
Ankur and Vijay are fond of playing Number games. In one of the games, Ankur has to choose a number X, such that X belongs to [1, 10000] . Vijay has to guess the chosen number as soon as possible. Ankur will let Vijay know whether his guess is smaller than, larger than or equal to the number. The warning is that Vijay loses the game if his guess is larger than Ankur’s chosen number two or more times.
Q1. How to make guesses, and how many guesses are necessary?
Q2. What if Ankur is allowed to pick a very large positive number without any given limits ? .
Both the problems can be treated in the following ways:
When Ankur chooses a number between 1 and n, here n=10000, Vijay should start guessing √n, 2√n, 3√n, 4√n, and so on. The first time his guess exceeds Ankur’s number, the range of numbers has been narrowed down to √n numbers; he then starts guessing sequentially in that range.
Ans2: If Ankur guesses an arbitrarily large number n, then Vijay may use Binary Search which mean to choose mid of 1and 10000 ie 5000 and if Ankur says it is less then choose mid term of 1 and 5000 ie 2500 and so on and at the end you will be meeting up with exact number n. It takes the time of order O(n) to complete.
This puzzle is contributed by Ujjwal Prakash. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!