Problem Statement
The official rules for a two-player ("singles") game of table tennis are as follows:
- The game is a sequence of exchanges. Each exchange starts by one of the two players serving the ball and ends when one of the players loses the exchange by failing to play the ball correctly. The winner of each exchange gets a point.
- A player wins the game as soon as they have at least 11 points and at the same time they lead their opponent by at least two points.
- The serve changes after every two points. (That is, one player has the serve in the first two exchanges, the other player in the next two, and so on.)
- If the score of the game reaches 10-10 ("deuce"), from that moment the serve changes after each point.
A game of table tennis just ended. After the game, we discovered that the intern who was supposed to record the score messed it up and recorded the number of serves instead. Thus, instead of the final score of the game we know two other values: the numbers A and B specifying how many times each of the players served the ball during the game. (Note that you don't know which of the two players served first, both options are possible.)
You are given the
First, determine whether these values A and B correspond to some completed game of table tennis.
If they don't, return an empty
If the values A and B do correspond to some completed game of table tennis, the final score of the game can always be uniquely determined from A and B.
Determine the numbers W and L of points scored by the winner and the loser of the game.
Return the
Definition
- Class:
- TableTennisGame
- Method:
- finalScore
- Parameters:
- int, int
- Returns:
- int[]
- Method signature:
- int[] finalScore(int A, int B)
- (be sure your method is public)
Notes
- The return value is a int[] with two elements: element at index 0 is W, element at index 1 is L.
- Note that W must always be greater than L.
Constraints
- A and B will each be between 1 and 100, inclusive.
Examples
8
6
Returns: {11, 3 }
The game proceeded as follows: Player A served twice, then player B served twice. Player A served twice, then player B served twice. Player A served twice, then player B served twice. Player A served twice. We can deduce that the final score of the game must have been 11-3. (We do not know who won the game, but we know that the winner must have had W = 11 points and the loser must have had L = 3 points.)
7
8
Returns: {11, 4 }
For this game we see that player B had to be the one to serve first. Thus, the game proceeded as follows: Player B served twice, then player A served twice. Player B served twice, then player A served twice. Player B served twice, then player A served twice. Player B served twice, then player A served once and the game was over. The final score of this game must have been 11-4, so we return {11, 4}.
13
13
Returns: {14, 12 }
Remember that once the score reaches 10-10 the serve changes after every point. Thus, it is possible that each player served exactly 13 times during a game.
1
3
Returns: { }
This is impossible for two separate reasons. First, the game cannot end after just four serves, as clearly nobody has 11 points yet. But the specific numbers of serves are also impossible: out of the first four balls each player has to serve two.
11
10
Returns: { }
12
11
Returns: { }
10
10
Returns: {11, 9 }
7
5
Returns: { }
7
6
Returns: {11, 2 }
3
5
Returns: { }
11
14
Returns: { }
14
1
Returns: { }
10
14
Returns: { }
13
10
Returns: { }
31
30
Returns: { }
10
12
Returns: { }
13
14
Returns: { }
15
5
Returns: { }
16
16
Returns: {17, 15 }
6
4
Returns: { }
13
6
Returns: { }
8
5
Returns: { }
29
29
Returns: {30, 28 }
10
11
Returns: { }
12
12
Returns: {13, 11 }
91
91
Returns: {92, 90 }
10
8
Returns: {11, 7 }
7
1
Returns: { }
9
1
Returns: { }
12
2
Returns: { }
3
3
Returns: { }
42
41
Returns: { }
12
5
Returns: { }
9
2
Returns: { }
78
78
Returns: {79, 77 }
2
8
Returns: { }
18
17
Returns: { }
37
36
Returns: { }
8
1
Returns: { }
4
12
Returns: { }
13
14
Returns: { }
44
44
Returns: {45, 43 }
47
48
Returns: { }
51
51
Returns: {52, 50 }
2
6
Returns: { }
5
1
Returns: { }
81
81
Returns: {82, 80 }
11
13
Returns: { }
15
2
Returns: { }
4
8
Returns: { }
60
59
Returns: { }
100
100
Returns: {101, 99 }
39
40
Returns: { }
2
5
Returns: { }
4
9
Returns: { }
3
6
Returns: { }
82
82
Returns: {83, 81 }
53
53
Returns: {54, 52 }
17
2
Returns: { }
14
12
Returns: { }
5
5
Returns: { }
42
42
Returns: {43, 41 }
90
89
Returns: { }
3
11
Returns: { }
39
39
Returns: {40, 38 }
13
4
Returns: { }
6
6
Returns: {11, 1 }
96
96
Returns: {97, 95 }
3
10
Returns: { }
48
49
Returns: { }
50
50
Returns: {51, 49 }
78
79
Returns: { }
27
28
Returns: { }
65
65
Returns: {66, 64 }
3
7
Returns: { }
11
13
Returns: { }
16
17
Returns: { }
4
11
Returns: { }
10
5
Returns: { }
9
9
Returns: { }
9
10
Returns: {11, 8 }
76
77
Returns: { }
10
11
Returns: { }
13
10
Returns: { }
38
38
Returns: {39, 37 }
64
64
Returns: {65, 63 }
70
70
Returns: {71, 69 }
48
48
Returns: {49, 47 }
51
50
Returns: { }
5
9
Returns: { }
1
15
Returns: { }
9
8
Returns: {11, 6 }
4
14
Returns: { }
11
5
Returns: { }
7
7
Returns: { }
5
6
Returns: {11, 0 }
2
1
Returns: { }
12
10
Returns: { }
2
3
Returns: { }
70
71
Returns: { }
38
37
Returns: { }
53
54
Returns: { }
4
5
Returns: { }
14
12
Returns: { }
79
80
Returns: { }
8
8
Returns: {11, 5 }
12
6
Returns: { }
12
8
Returns: { }
10
6
Returns: { }
1
6
Returns: { }
14
3
Returns: { }
8
11
Returns: { }
3
17
Returns: { }
1
4
Returns: { }
57
57
Returns: {58, 56 }
2
7
Returns: { }
20
21
Returns: { }
96
95
Returns: { }
10
14
Returns: { }
4
4
Returns: { }
10
4
Returns: { }
99
99
Returns: {100, 98 }
16
3
Returns: { }
40
40
Returns: {41, 39 }
4
3
Returns: { }
4
2
Returns: { }
31
31
Returns: {32, 30 }
32
33
Returns: { }
95
94
Returns: { }
63
63
Returns: {64, 62 }
88
87
Returns: { }
73
72
Returns: { }
1
19
Returns: { }
2
2
Returns: { }
5
14
Returns: { }
11
11
Returns: {12, 10 }
1
1
Returns: { }
12
11
Returns: { }
12
13
Returns: { }
3
8
Returns: { }
70
69
Returns: { }
15
15
Returns: {16, 14 }
14
14
Returns: {15, 13 }
52
52
Returns: {53, 51 }
13
1
Returns: { }
95
95
Returns: {96, 94 }
3
1
Returns: { }
53
52
Returns: { }
4
15
Returns: { }
36
36
Returns: {37, 35 }
14
11
Returns: { }
58
59
Returns: { }
16
1
Returns: { }
11
6
Returns: { }
13
12
Returns: { }
30
30
Returns: {31, 29 }
41
40
Returns: { }
77
77
Returns: {78, 76 }
19
19
Returns: {20, 18 }
7
13
Returns: { }
2
100
Returns: { }
This situation is also impossible: if one player served 100 times, the other could not have served only twice.
11
12
Returns: { }
7
9
Returns: { }
13
11
Returns: { }
16
14
Returns: { }
22
20
Returns: { }
11
9
Returns: { }
9
7
Returns: { }
15
16
Returns: { }
6
5
Returns: {11, 0 }
17
16
Returns: { }
1
2
Returns: { }
20
22
Returns: { }
5
7
Returns: { }
52
50
Returns: { }