Techno Blender
Digitally Yours.
0 23

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

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:

• If A > B, then swap A and B, so that, B is always greater than or equal to A.
• Initialize k = B – A and d = 1 + sqrt(5).
• Set, d = d/2, and again d = d*k.
• Make a variable c = d after typecasting it into int.
• Now, if a and c are equal then return 0 else return 1.
• If the answer is 0 then print “Dolly” else print “Bunty”.

Below is the implementation of the above approach:

## C++

 ` `  `#include ` `using` `namespace` `std;` ` `  `bool` `game(``int` `a, ``int` `b)` `{` `    ` `    ``if` `(a > b)` `        ``swap(a, b);` ` `  `    ``int` `k = b - a;` `    ``long` `double` `d = 1 + ``sqrt``(5);` ` `  `    ``d /= 2;` `    ``d *= k;` ` `  `    ``int` `c = (``int``)d;` ` `  `    ` `    ``return` `(a == c) ? 0 : 1;` `}` ` `  `int` `main()` `{` `    ``int` `A = 1, B = 2;` ` `  `    ` `    ``cout << (game(A, B) ? ``"Dolly"` `: ``"Bunty"``) << endl;` ` `  `    ``return` `0;` `}`

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 Dolly

Input: 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:

• If A > B, then swap A and B, so that, B is always greater than or equal to A.
• Initialize k = B – A and d = 1 + sqrt(5).
• Set, d = d/2, and again d = d*k.
• Make a variable c = d after typecasting it into int.
• Now, if a and c are equal then return 0 else return 1.
• If the answer is 0 then print “Dolly” else print “Bunty”.

Below is the implementation of the above approach:

## C++

 ` `  `#include ` `using` `namespace` `std;` ` `  `bool` `game(``int` `a, ``int` `b)` `{` `    ` `    ``if` `(a > b)` `        ``swap(a, b);` ` `  `    ``int` `k = b - a;` `    ``long` `double` `d = 1 + ``sqrt``(5);` ` `  `    ``d /= 2;` `    ``d *= k;` ` `  `    ``int` `c = (``int``)d;` ` `  `    ` `    ``return` `(a == c) ? 0 : 1;` `}` ` `  `int` `main()` `{` `    ``int` `A = 1, B = 2;` ` `  `    ` `    ``cout << (game(A, B) ? ``"Dolly"` `: ``"Bunty"``) << endl;` ` `  `    ``return` `0;` `}`

Time Complexity: O(1)
Auxiliary Space: O(1)