Saturday, 16 Oct 2021

# Find the winner of a game of donating i candies in every i-th move

Find the winner of a game of donating i candies in every i-th moveGiven two integers X and Y representing the number of candies allocated to players A and B respectively, where both the players indulge in a game of donating i candies to the opponent in every ith move. Starting with player A, the game continues with alternate turns until a player is unable to donate the required amount of candies and loses the game, the task is to find the winner of the game.Examples:Input: X = 2, Y = 3Output: AExplanation: The game turns out in the following manner:StepXY023114232305441Since A fails to donate 5 candies in the 5th step, B wins the game. Input: X = 2, Y = 1Output: BExplanation: The game turns out in the following manner:StepXY021112230303Since B fails to give 4 candies in the 4th step, A wins the game.Approach: The idea is to solve the problem based on the following observation:StepNumber of candiesPossessed by ANumber of candiesPossessed by B0XY1X – 1Y + 12X – 1 + 2 = X + 1Y + 1 – 2 = Y – 13X – 2Y + 24X + 2Y – 2The player whose candies reduce to 0 first, will not be having enough candies to give in the next move.Count of candies of player A decreases by 1 in odd moves.Count of candies of player B decreases by 1 in even moves.Follow the steps below to solve the problem:Initialize two variables, say chanceA and chanceB, representing the number of moves in which the number of candies possessed by a player reduces to 0.Since count of candies of player A decreases by 1 in odd moves, chanceA = 2 * (X – 1) + 1Since count of candies of player B decreases by 1 in even moves, chanceB = 2 * YIf chanceA < chanceB, then B will be the winning player.Otherwise, A will be the winning player.Print the winning player.Below is the implementation of the above approach:C++  #include using namespace std;  int stepscount(int a, int b){            int chanceA = 2 * a - 1;              int chanceB = 2 * b;          if (chanceA < chanceB) {        cout