Coin Game

Charlie and Alan have challenged each other to a game of logic with coins.

The game consists of N piles of coins with each pile consisting of Ai coins. The game progresses as follows: in each turn, a player selects any of the piles with an even number of coins and removes exactly half the coins out of that pile. The game ends when a player can't make a move. The last move is a winning move.

Charlie makes the first move. Assuming both players play optimally, predict who wins the game.

Example:

Input:  n = 3, a = [2,4,2]
Output: Alan

Approach

C++

#include <bits/stdc++.h>
using namespace std;

void coinGame(int nint a[])
{
    int cnt = 0;
    for (int i = 0i < ni++)
    {
        int x = a[i];
        while (x % 2 == 0)
        {
            cnt++;
            x = x / 2;
        }
    }
    if (cnt & 1)
        cout << "Charlie\n";
    else
        cout << "Alan\n";
}
int main()
{

    int n = 3;
    int a[n] = {242};

    coinGame(na);

    return 0;
}


No comments:

Post a Comment