Bunty and Dolly are playing a game, described as follows:There are two boxes having A and B number of chocolates respectively. Both can eat L (L ≥ 1) chocolates from any one box or L chocolates from both boxes in one move. They play the game alternatively and the last one to eat the chocolate […]
The post Game of Chocolates  Wythoff’s Game appeared first on Techno Blender.
]]>
Bunty and Dolly are playing a game, described as follows:
There are two boxes having A and B number of chocolates respectively. Both can eat L (L ≥ 1) chocolates from any one box or L chocolates from both boxes in one move. They play the game alternatively and the last one to eat the chocolate will be the winner.
You have to help Bunty in deciding who should play first such that Dolly is always the winner. Assume that both players play optimally.
Note: This game is also known as Wythoff’s Game.
Examples:
Input: A = 1 and B = 2
Output: Bunty
Explanation: If Bunty starts first, all the possible states after Bunty eats chocolates are (0, 2), (1, 1), (1, 0). These are all wining states for DollyInput: A = 1 and B = 3
Output: Dolly
Approach: The problem can be solved based on the following idea:
Any state can be uniquely identified using two integers (n, m) which are the number of chocolates in the boxes. Now each state can be classified into two categories: cold state (the player whose turn it is will lose) and hot state (the player whose turn it is will win). The classification can be done as follows:
 (0, 0) is a cold position
 Any state from which a cold state can be reached in a single move is a hot state.
 The state from which any possible move leads to a hot state is a cold state.
The task is to check if the given state (A, B) is a hot state or cold state and based on that the one to start will be Dolly or Bunty respectively.
Follow the below steps to solve the problem:
Below is the implementation of the above approach:

Time Complexity: O(1)
Auxiliary Space: O(1)
Bunty and Dolly are playing a game, described as follows:
There are two boxes having A and B number of chocolates respectively. Both can eat L (L ≥ 1) chocolates from any one box or L chocolates from both boxes in one move. They play the game alternatively and the last one to eat the chocolate will be the winner.
You have to help Bunty in deciding who should play first such that Dolly is always the winner. Assume that both players play optimally.
Note: This game is also known as Wythoff’s Game.
Examples:
Input: A = 1 and B = 2
Output: Bunty
Explanation: If Bunty starts first, all the possible states after Bunty eats chocolates are (0, 2), (1, 1), (1, 0). These are all wining states for DollyInput: A = 1 and B = 3
Output: Dolly
Approach: The problem can be solved based on the following idea:
Any state can be uniquely identified using two integers (n, m) which are the number of chocolates in the boxes. Now each state can be classified into two categories: cold state (the player whose turn it is will lose) and hot state (the player whose turn it is will win). The classification can be done as follows:
 (0, 0) is a cold position
 Any state from which a cold state can be reached in a single move is a hot state.
 The state from which any possible move leads to a hot state is a cold state.
The task is to check if the given state (A, B) is a hot state or cold state and based on that the one to start will be Dolly or Bunty respectively.
Follow the below steps to solve the problem:
Below is the implementation of the above approach:

Time Complexity: O(1)
Auxiliary Space: O(1)
The post Game of Chocolates  Wythoff’s Game appeared first on Techno Blender.
]]>