Problem Statement
In this problem a number has been chosen uniformly, at random between 1 and range, inclusive. A number of people have already guessed some numbers, and those numbers are given as elements of guesses. It is now your turn, and there are numLeft people left after you. Your task is to guess a number such that, if everyone after you follows the best strategy possible (guesses the number that maximizes his or her chance of winning), your chance of winning after everyone has guessed is maximized. Furthermore, if there is more than one way to achieve the best chance of winning, you should pick the lowest number, and assume that everyone after you does the same.
For example, if range = 1000, guesses = {500}, and numLeft = 1, you should pick 501. This gives the person after you 2 ways to win with a probability of 499/1000. He or she can pick either 499 or 502. You can assume that he or she will pick the lower of these, 499. This gives you a 500/1000 = 50% chance winning. Picking any other number would cause you to do worse. Note that a tie does not count as a win, and we are only concerned with maximizing the chance of winning.
Definition
- Class:
- NumberGuessing
- Method:
- bestGuess
- Parameters:
- int, int[], int
- Returns:
- int
- Method signature:
- int bestGuess(int range, int[] guesses, int numLeft)
- (be sure your method is public)
Notes
- Assume the people ahead of you, when looking for the best guess, will employ the same strategy as you are using. Namely, they will assume everyone ahead of them is employing the best strategy, and choose accordingly to maximize the probability of winning.
Constraints
- range will be between 1 and 1,000,000, inclusive.
- guesses will contain between 0 and 10 elements, inclusive.
- Each element of guesses will be between 1 and range, inclusive.
- Each element of guesses will be distinct.
- numLeft will be between 0 and 6, inclusive.
- range^numLeft (where '^' denotes exponentiation) will be less than or equal to 1,000,000.
- range will be at least 1 + numLeft + the number of elements in guesses.
Examples
1000
{500}
1
Returns: 501
The example from above.
1000000
{}
1
Returns: 500000
1000
{}
2
Returns: 750
Your first intuition might be that you should guess 500 in this case, since that is right in the middle. However, lets consider what would happen if you were to guess 500. The person after you would guess 501 to maximize his or her chance of winning, and the last person would then pick 499, to maximize his or her chance of winning. This leaves you winning only if the number that was originally picked is exactly 500, at a probability of 1/1000. It turns out that your probability is maximized if you guess 750. The next person then guesses 250, and the last person guesses 251.
100
{27,80}
1
Returns: 26
20
{}
4
Returns: 8
20
{8}
3
Returns: 13
20
{8,13}
2
Returns: 18
20
{8,13,18}
1
Returns: 3
20
{8,13,18,3}
0
Returns: 2
1000000
{}
1
Returns: 500000
10
{}
6
Returns: 9
10
{9}
5
Returns: 6
10
{9,6}
4
Returns: 1
10
{9,6,1}
3
Returns: 2
10
{9,6,1,2}
2
Returns: 3
10
{9,6,1,2,3}
1
Returns: 4
10
{9,6,1,2,3,4}
0
Returns: 5
99
{}
3
Returns: 50
99
{50}
2
Returns: 83
99
{50,83}
1
Returns: 17
99
{50,83,17}
0
Returns: 16
100
{}
3
Returns: 84
100
{84}
2
Returns: 51
100
{84,51}
1
Returns: 17
100
{84,51,17}
0
Returns: 18
298771
{251230,275984,79583,295940}
0
Returns: 79584
202854
{}
1
Returns: 101427
483114
{317503,134086,267077,66718,178552,185571,349609,313749}
1
Returns: 416397
720732
{270364,706055,718771,231002,572272,687510,406934,477034}
1
Returns: 77001
12
{}
5
Returns: 8
25
{9,25,3,2,1,18}
3
Returns: 13
47
{22,1,36,6,45}
3
Returns: 15
50
{28,16,41,42}
3
Returns: 5
61
{32,44,3,22,60,18,50,27,15,53}
3
Returns: 10
29
{9}
3
Returns: 25
6
{6}
4
Returns: 1
15
{7,5,9,1}
4
Returns: 14
53
{40,24,15,18,12}
3
Returns: 49
38
{2,8}
3
Returns: 25
22
{7}
4
Returns: 15
86
{23}
3
Returns: 74
1
{}
0
Returns: 1
1000000
{ 999999, 45000, 10503, 198388 }
1
Returns: 599194
1000
{ 233, 877 }
2
Returns: 555
1000
{ 232, 877 }
2
Returns: 554
477
{ 11, 33, 57, 400, 239 }
2
Returns: 86
3
{ }
2
Returns: 1
1000
{ 501 }
1
Returns: 498