Game of Stones

Easy
0/40
Average time to solve is 15m
profile
Contributed by
8 upvotes
Asked in companies
SprinklrAmazonDunzo

Problem statement

Given the count of total stones in a game. Two-player β€˜Ale’ and β€˜Bob’ are playing the game. Your task is to find who will win the game if both the players are playing optimally.

Rules of the game.

1. In a single turn, a player can choose a single stone or β€˜even’ number of stones.

2. They will play alternatively, which means in the first chance β€˜Ale’ will collect the stones, in second-chance β€˜Bob’ will collect the stones. And always β€˜Ale’ will start the game.

3. If any player is not able to take any stones then another player will win the game.

Detailed explanation ( Input/output format, Notes, Images )
Input format:
The first line of input contains an integer β€˜T’ denoting the number of test cases.

The first line of each test case contains a single integer,  which denotes the total number of stones in the game.
Output Format:
For each test case, return a string β€˜Ale’ or β€˜Bob’ according to your answer.
Note:
1. Do not print anything, just return β€˜Ale’ if β€˜Ale’ will win the game else return β€˜Bob’.
Constraints:
1 <= T <= 10^5
1 <= number of stones <= 10^9

Where β€˜T’ is the total number of test cases.
Time limit: 1 second
Sample Input 1:
2
2
3
Sample Output 1:
Ale
Bob
Explanation of sample input 1:
Test Case 1:

Given the number of stones is β€˜2’.
Then first player β€˜Ale’ can choose both the stones because 2 is an even number.
So β€˜Ale’ will the game.
Return β€˜Ale’.

Test Case 2:

Given the number of stones is β€˜3’.
In the first turn β€˜Ale’ can choose β€˜1’ stone or β€˜2’ store, but not β€˜3’ stone because β€˜3’ is neither β€˜1’ or even number.
If β€˜Ale’ chooses β€˜1’ in the first turn. Then in the second turn, β€˜Bob’ will collect the remaining β€˜2’ stone, so β€˜Bob’ will win.
If β€˜Ale’ chooses β€˜2’ stones in the first turn. Then in the second turn, β€˜Bob’ will collect the remaining β€˜1’ stone, again β€˜Bob’ will win the game.
So in both cases β€˜Bob’ is winning the game. Hence Return β€˜Bob’. 
Sample Input 2:
2
4
6
Sample Output 2:
Ale
Ale
Hint

Find cases when β€˜Ale’ can lose the game.

Approaches (1)
Game Theory
  • It is clear that if the number of stones is β€˜1’ then β€˜Ale’ wins the game. If β€˜2’ then also β€˜Ale’ wins the match, and if β€˜3’ then β€˜Bob’ wins the game.
  • So we can observe that, if anyone has a chance to choose a stone from β€˜3’ number of stones then another opponent will the game. If β€˜Ale’ choosing the stones from β€˜3’ number stone then β€˜Bob’ will win and vice versa.
  • If the number of stones is β€˜4’ then β€˜Ale’ will win the game because β€˜4’ is an even number and he chooses all the β€˜4’ stones in the first turn.
  • If the number of stones is β€˜5’, and β€˜Ale’ playing optimally then he knows that if any turns you have a chance to choose stones from β€˜3’ then you will lose the game.
  • Then β€˜Ale’ choose the β€˜2’ stones, if next turn β€˜Bob’ has a chance to chose stones from remaining β€˜5-2 =3’ stones, and in the of β€˜3’ stone current player always lose the game then in β€˜Ale’ will win the game.
  • Same for all the other number, If the number of stones is even then β€˜Ale’ choose all the stones, if odd then β€˜Ale’ will choose β€˜n -3’ in the first turn, because β€˜n’ is odd then β€˜n-3’ is an even number, for the next turn of β€˜Bob’ there will be remaining only β€˜3’ number of stones. And β€˜Bob’ will lose the game.

Code -

  • If the given number of stones is β€˜3’ then return β€˜Bob’.
  • Else return β€˜Ale’.

 

Time Complexity

0(1), we are taking a constant time.

Space Complexity

O(1), we are using constant space. 

Code Solution
(100% EXP penalty)
Game of Stones
Full screen
Console