Yes – there is a very small “litmus test’’ you can run in O(1) time.
Denote the two piles by (a , b) with a ≥ 0, b ≥ 0.
Let big = max(a,b) , small = min(a,b).
-
Check the total number of items
– Each allowed move removes exactly 3 items, so a + b must be a multiple of 3.
if (a + b) mod 3 ≠ 0 → IMPOSSIBLE.
-
Check that neither pile is “too large’’
– During the game every move consumes at least one item from each pile.
Hence no pile may ever contain more than twice the other, otherwise the
smaller pile would empty first.
Formally:
big ≤ 2 · small
If this fails → IMPOSSIBLE.
If both tests pass → POSSIBLE, otherwise not.
Pseudocode
──────────
function can_eliminate(a,b):
if a==0 and b==0: return True
big = max(a,b)
small = min(a,b)
if (a + b) % 3 != 0: return False
if big > 2*small: return False
return True
Why these two conditions are both necessary and sufficient
───────────────────────────────────────────────────────────
Write x = #times we do (2,1) and y = #times we do (1,2).
We must solve
2x + y = a
x + 2y = b with x,y ≥ 0 integers.
Solving:
x = (2a − b)/3
y = (2b − a)/3
So
• a + b must be divisible by 3 (both numerators share the same divisor 3),
• x ≥ 0 ⇔ 2a ≥ b (i.e. a not less than half of b),
• y ≥ 0 ⇔ 2b ≥ a (i.e. b not less than half of a).
These two inequalities are exactly “big ≤ 2·small’’.
If they hold, x and y are non-negative integers, giving an explicit sequence
of moves that empties both piles; if any fails, no such x,y exist, so it is
impossible.
Examples
────────
| a b | (a+b)%3 | big ≤ 2·small? | Result |
|---|
| 2 1 | 0 | 2 ≤ 2·1 ✓ | POSSIBLE (one (2,1) move) |
| 4 10 | 14≡2 | 10 ≤ 2·4 ✗ | IMPOSSIBLE (fails both tests) |
| 6 3 | 9≡0 | 6 ≤ 2·3 ✓ | POSSIBLE (x=3,y=0) |
| 0 3 | 0 | 3 ≤ 0 ✗ | IMPOSSIBLE (single pile non-zero) |
So the two-line test above is a complete, constant-time decision procedure.