Coin Piles

You have two coin piles containing a and b coins. On each move, you can either remove one coin from the left pile and two coins from the right pile or two coins from the left pile and one coin from the right pile.

Your task is to efficiently find out if you can empty both the piles.

Example:

Input:  a = 2, b = 1
Output: YES

Approach

C++

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

void coinPiles(int aint b)
{
    if ((a + b) % 3 == 0 && (min(ab) * 2 >= max(ab)))
        cout << "YES\n";
    else
        cout << "NO\n";
}
int main()
{
    int a = 2b = 1;

    coinPiles(ab);

    return 0;
}


No comments:

Post a Comment