Determine if Two Coin Stacks Can Be Equalized by Doubling
Math & Bit ManipulationAuto-graded · 24 test casesLast updated
Determine if two initial quantities can reach equality through deterministic transfers that double the recipient stack.
You are given two stacks of coins containing a and b coins respectively.
You can perform a operation where coins are transferred from one stack to another. Specifically, you select one stack as the destination and transfer coins from the source stack into it. The number of coins transferred must equal the target stack's current size, effectively doubling the target stack's size. This operation is valid only if the source stack holds at least as many coins as the target stack.
Determine whether it is possible to make both stacks hold an equal number of coins after zero or more valid operations.
Constraints
1 <= a, b <= 10^9
Examples
Example 1
Input: can_balance(3, 5)
Output: True
Example 2
Input: can_balance(1, 2)
Output: False
Example 3
Input: can_balance(2, 4)
Output: False
Example 4
Input: can_balance(7, 7)
Output: True
Hints
Hint 1
Observe that the sum `a + b` remains constant throughout any sequence of valid operations.
Hint 2
Because the transfer must double the recipient, only the smaller (or equal) stack can receive coins in a valid move, making the transition deterministic.
Hint 3
Examine the value of the first stack modulo `S = a + b` after each operation: `a` transforms to `(2 * a) % S`.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Let $S = a + b$ be the constant total sum of coins in both stacks.
If $S$ is odd, the total cannot be divided equally into two integer halves, so equalizing the stacks is impossible.
When $a \neq b$, there is only one valid move: transfer coins from the larger stack to the smaller stack. If the smaller stack currently has $x$ coins, it receives $x$ coins and grows to $2x$. The larger stack, containing $S - x$ coins, drops to $S - 2x \equiv -2x \equiv 2(S - x) \pmod S$. Thus, regardless of which stack is smaller, the count of coins in the first stack after one move is congruent to $2a \pmod S$.
Because $0 < a < S$ holds at every state, the state of the first stack after $n$ moves is given by: $$a_n = (2^n \cdot a) \pmod S$$
To check if equal state ($a_n = S / 2$) can be reached, we analyze the 2-adic valuations (number of trailing zero bits):
- Let $v(S)$ be the exponent of the highest power of 2 dividing $S$.
- Let $v(a)$ be the exponent of the highest power of 2 dividing $a$.
For $(2^n \cdot a) \pmod S = S / 2$ to hold, the power of 2 in $2^n \cdot a$ must match the power of 2 in $S / 2$, which is $v(S) - 1$. This implies: $$n + v(a) = v(S) - 1 \implies n = v(S) - 1 - v(a)$$
If $v(a) \ge v(S)$, no non-negative integer $n$ satisfies this condition, so we return False. Otherwise, we directly test the candidate step count $n$ using modular exponentiation:
$$(a \cdot 2^n) \pmod S \stackrel{?}{=} S / 2$$
Complexity
- Time Complexity: $O(\log S)$ due to modular exponentiation
pow(2, moves, total). Isolating the lowest set bit takes $O(1)$ time. - Space Complexity: $O(1)$ auxiliary space.
def can_balance(a: int, b: int) -> bool:
total = a + b
if total % 2 != 0:
return False
v_total = (total & -total).bit_length() - 1
v_a = (a & -a).bit_length() - 1
if v_a >= v_total:
return False
moves = v_total - 1 - v_a
return (a * pow(2, moves, total)) % total == total // 2