Statistics

Problem Statement for "GuessTheNumber"

Problem Statement

A popular guessing game is "Guess the number", where one person selects a number in a known range, and another person tries to guess that number. After each guess, the first person reveals whether the guess was correct, too high or too low.

Pretty soon one learns the best strategy, which is to guess the middle number among those not yet ruled out. If there is no single middle number, then there are two numbers to choose from. In that case, we choose the smallest of those numbers.

The algorithm can be described like this:

Init lower and upper bound
Repeat
  x = (lower bound + upper bound)/2  (round down if necessary)
  Make guess x
  If x is too low, set lower bound to x+1
  If x is too high, set upper bound to x-1
Until x is correct

For instance, assume that the lower and upper bound initally are 1 and 9, respectively. The middle number in this range is 5. If this is "too low", the new bounds become 6 and 9. This range contains four numbers, and there is thus no single middle number. The two numbers in the middle are 7 and 8, and the smallest of these is 7, so our next guess then becomes 7.

Create a class GuessTheNumber which contains the method noGuesses which takes an int upper, the initial upper bound of the range (the inital lower bound is always 1), and an int answer, the number selected by the first person. The method should return an int representing the total number of guesses required for the second person to guess the correct number using the method described above.

Definition

Class:
GuessTheNumber
Method:
noGuesses
Parameters:
int, int
Returns:
int
Method signature:
int noGuesses(int upper, int answer)
(be sure your method is public)

Constraints

  • upper will be between 1 and 1000, inclusive.
  • answer will be between 1 and upper, inclusive.

Examples

  1. 9

    6

    Returns: 3

    The first guess will be (1+9)/2=5, which is too low. The new lower bound then becomes 5+1=6. The second guess then becomes (6+9)/2=7, which is too high. The new upper bound then becomes 7-1=6. The third guess is then of course (6+6)/2)=6, which is correct. So, three guesses were required, and the method thus returns 3.

  2. 1000

    750

    Returns: 2

    The first guess will be 500, the second guess 750.

  3. 643

    327

    Returns: 7

    The guesses are 322, 483, 402, 362, 342, 332 and finally 327, so the method returns 7.

  4. 157

    157

    Returns: 8

    Here the guesses are 79, 118, 138, 148, 153, 155, 156 and finally 157. The method thus returns 8.

  5. 263

    215

    Returns: 8

  6. 701

    258

    Returns: 9

  7. 753

    643

    Returns: 10

  8. 257

    234

    Returns: 8

  9. 712

    680

    Returns: 9

  10. 844

    390

    Returns: 10

  11. 109

    61

    Returns: 4

  12. 331

    190

    Returns: 9

  13. 170

    43

    Returns: 7

  14. 918

    720

    Returns: 8

  15. 973

    836

    Returns: 6

  16. 981

    908

    Returns: 8

  17. 550

    293

    Returns: 9

  18. 165

    50

    Returns: 8

  19. 552

    468

    Returns: 9

  20. 191

    106

    Returns: 7

  21. 636

    561

    Returns: 10

  22. 945

    81

    Returns: 10

  23. 863

    249

    Returns: 7

  24. 364

    66

    Returns: 8

  25. 711

    55

    Returns: 6

  26. 877

    265

    Returns: 9

  27. 930

    518

    Returns: 10

  28. 676

    588

    Returns: 10

  29. 191

    108

    Returns: 4

  30. 135

    86

    Returns: 7

  31. 630

    481

    Returns: 10

  32. 728

    431

    Returns: 10

  33. 744

    191

    Returns: 7

  34. 105

    71

    Returns: 7

  35. 750

    621

    Returns: 6

  36. 257

    13

    Returns: 8

  37. 573

    33

    Returns: 9

  38. 491

    347

    Returns: 8

  39. 120

    46

    Returns: 6

  40. 696

    162

    Returns: 10

  41. 328

    144

    Returns: 8

  42. 498

    411

    Returns: 9

  43. 597

    250

    Returns: 10

  44. 357

    102

    Returns: 7

  45. 94

    46

    Returns: 7

  46. 8

    3

    Returns: 3

  47. 510

    45

    Returns: 8

  48. 353

    113

    Returns: 8

  49. 784

    179

    Returns: 10

  50. 802

    219

    Returns: 9

  51. 331

    180

    Returns: 9

  52. 196

    117

    Returns: 7

  53. 964

    136

    Returns: 9

  54. 428

    323

    Returns: 9

  55. 107

    48

    Returns: 6

  56. 213

    61

    Returns: 8

  57. 511

    344

    Returns: 6

  58. 659

    395

    Returns: 10

  59. 332

    327

    Returns: 6

  60. 848

    677

    Returns: 10

  61. 198

    10

    Returns: 7

  62. 439

    79

    Returns: 9

  63. 19

    2

    Returns: 3

  64. 128

    64

    Returns: 1

  65. 1

    1

    Returns: 1

  66. 2

    1

    Returns: 1

  67. 2

    2

    Returns: 2

  68. 2

    2

    Returns: 2

  69. 1

    1

    Returns: 1

  70. 157

    157

    Returns: 8

  71. 3

    3

    Returns: 2


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: