DSA sheet / Introductory

Coin Piles

medium about 20 min mathinvariantsgames
Open on CSES

The problem in brief

There are t test cases (up to 100 000). Each gives two pile sizes a and b (up to 10^9). A move removes exactly 2 coins from one pile and exactly 1 coin from the other, your choice of which. For each test, print YES if some sequence of moves empties both piles exactly, otherwise NO.

Try it

Tab indents; press Esc, then Tab, to leave the box. Ctrl+Enter runs.

Hints

Stuck? Reveal one hint at a time. Each nudges without giving away the next.

The walkthrough

Spoilers ahead: open a section only after you have given the hints a fair try.

How to think

For “can this be reduced to zero” problems, the fastest route is to look for invariants and then check whether the necessary conditions are also sufficient. Every move removes 3 coins, so a + b must be divisible by 3. That is necessary, not sufficient.

Next, model the moves algebraically instead of simulating. Let x be the number of moves that take 2 from A (and 1 from B) and y the number that take 2 from B (and 1 from A). Then a = 2x + y and b = x + 2y. Solving gives x = (2a - b)/3 and y = (2b - a)/3. You need both to be non-negative integers, which is exactly: a + b divisible by 3, and 2a >= b and 2b >= a, i.e. the larger pile is at most twice the smaller.

The habit: when you can write the process as a linear system, existence questions become inequalities you can check in O(1). Confirm the closed form against a brute-force search on small piles.

Intuition

Each move drains the piles in a 2:1 ratio, one way or the other. Mixing the two directions gives you overall drain ratios anywhere between 2:1 and 1:2. So the piles can only be emptied together if their sizes sit between those two ratios, and the total must be a multiple of 3 so that the coin count matches.

Approach

For each test, answer YES if and only if (a + b) % 3 == 0 and max(a, b) <= 2 * min(a, b). Otherwise print NO.

Edge cases: (0, 0) is YES (nothing to do). (0, k) with k > 0 is NO because the larger pile cannot be at most twice zero. Values are up to 10^9, so 2 * min fits in 32 bits but use 64-bit anyway to be safe. Read the input with fast I/O; there can be 100 000 lines.

Complexity

O(1) per test, O(t) overall.

Solutions

Written from scratch and checked by compiling and running each one against a brute force on random inputs. Fast input/output, the way you would submit it.

Show solutions (C++17, Python 3, Java 17, Node.js)
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--) {
        long long a, b;
        cin >> a >> b;
        bool ok = (a + b) % 3 == 0 && max(a, b) <= 2 * min(a, b);
        cout << (ok ? "YES" : "NO") << "\n";
    }
    return 0;
}

Problem: CSES Problem Set (Antti Laaksonen, University of Helsinki), CC BY-NC-SA 4.0. The explanations and code above are original (© Anupam Kumar).

Related problems

Back to the sheet