問題
Each move removes two coins from one pile and one coin from the other. Either orientation is allowed. You may never remove more coins than remain. Determine whether both piles can end at zero.
Implement coin_piles(left, right).
Input
left: The number of coins in the left pile.
right: The number of coins in the right pile.
Output
Return true if the piles can both be emptied; otherwise return false.
制約
0 ≤ left, right ≤ 1,000,000,000. Two empty piles are already a successful result.
例
入力:
left = 4
right = 2
期待される出力:
expected = True
入力:
long long left = 4;
long long right = 2;
期待される出力:
bool expected = true;
入力:
long left = 4;
long right = 2;
期待される出力:
bool expected = true;
解説
Remove (2, 1) twice: (4, 2) → (2, 1) → (0, 0).
入力:
left = 4
right = 1
期待される出力:
expected = False
入力:
long long left = 4;
long long right = 1;
期待される出力:
bool expected = false;
入力:
long left = 4;
long right = 1;
期待される出力:
bool expected = false;
解説
The only possible first move leaves (2, 0), after which no move can empty the left pile.
解き方のヒント
まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。
ヒント 1
Each move removes exactly three coins total, so the overall coin count must fit that pattern.
ヒント 2
Neither pile can be reduced below zero, so one pile cannot exceed what the other can support.
ヒント 3
Check both the total amount and the difference between piles.
目安の計算量
O(1) time, O(1) extra space
参考資料
この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。