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 Dolly
Input: A = 1 and B = 3 Output: Dolly Explanation: If Dolly starts first, all the possible states after Dolly eats chocolates are (0, 3), (1, 2), (1, 1), (1, 0), (0, 2). Out of these, (1, 2) is the winning state for Dolly.
[Naive Approach] Using Recursion and Memoization - O(A * B) Time and O(A * B) Space
All states of chocolates can be uniquely identified using two integers (n, m) where n and m are number of chocolates in the first and second box respectively. Now each state can be classified into two categories:
Cold State - State from which the current player will always lose the game. This is a state where all the possible moves will result in losing of the current player. Example: (1, 2), (2, 4)
Hot State - State from which the current player can win the game. This is a state where at least one of the moves will result in winning of the current player.
The task is to check if the given state (A, B) is a hot state or cold state and if it is a hot state, Dolly should start otherwise Bunty should start the game. In the recursive function, try for all the possible combinations by eating chocolates from the first box or the second box or from both the boxes simultaneously.
We can use Dynamic Programming and memoize the solution using a dp[][] table such that dp[i][j] stores the result for state (i, j).
Below is the implementation of the algorithm:
Output
Dolly
[Expected Approach] Using Golden Ratio - O(1) Time and O(1) Space
On observing carefully, if we plot all cold states in a graph then the ratio of the lines come out to be Φ and 1/Φ, where Φ is the golden ratio (which is (1 + sqrt(5))/2). It is also observed that all the coordinates of cold positions are unique and their x and y coordinates differ by k where k >= 1. So, the cold positions can be calculated by floor value of (k*Φ, k*Φ*Φ) where k>=1. For example:
If k = 1, cold state = (floor(1 * Φ), floor(1 * Φ * Φ)) = (floor(1.618), floor(2.618)) = (1, 2) and difference between coordinates = k = 1.
If k = 2, cold state = (floor(2 * Φ), floor(2 * Φ * Φ)) = (floor(3.236), floor(5.236)) = (3, 5) and difference between coordinates = k = 2.
If k = 3, cold state = (floor(3 * Φ), floor(3 * Φ * Φ)) = (floor(4.854), floor(7.854)) = (4, 7) and the difference between coordinates = k = 3. And so on...
So, we can check whether (A, B) is a cold state by finding value of k = abs(A - B) and then finding the floor value of (k*Φ, k*Φ*Φ). If A = k*Φ and B = k*Φ*Φ, then (A, B) is a cold state, so Bunty should start the game. Otherwise, (A, B) is a hot state and Dolly should start the game.
Below is the implementation of the above approach: