Splitting Tokens: Will a Bucket Ever Run Dry?
Math & Bit ManipulationAuto-graded · 26 test casesLast updated
Tests whether a candidate can identify a modular-arithmetic invariant to avoid simulating an exponentially long process.
Problem
You have two buckets holding tokens. Bucket A starts with a tokens and Bucket B starts with b tokens. Neither bucket is empty at the start.
Each turn, you are forced to make exactly one move:
- Count the tokens in the smaller bucket — call that number
s. Transfer exactlystokens from the larger bucket into the smaller one. The smaller bucket now holds2stokens. - If both buckets hold the same number of tokens, pour all tokens from one bucket into the other (it doesn't matter which direction — the resulting pair of sizes is the same either way).
The total number of tokens across both buckets is fixed throughout — tokens are only moved, never created or destroyed. The move is always forced, so the sequence of states is entirely determined by the starting values.
Your task: decide whether this process will ever produce a bucket containing zero tokens.
Return True if some bucket empties after a finite number of moves. Return False if the process continues indefinitely, cycling through states without ever emptying a bucket.
Important note on efficiency
A direct step-by-step simulation is not an acceptable solution. The number of moves before a cycle is detected can be as large as the total number of tokens, which may be up to 2 × 10^9. Your solution must run in O(log(a + b)) time.
Constraints
1 <= a, b <= 10^9- Both
aandbare positive integers.
Examples
Example 1
Input: bucket_empties(3, 5)
Output: True
Example 2
Input: bucket_empties(2, 6)
Output: True
Example 3
Input: bucket_empties(1, 24)
Output: False
Example 4
Input: bucket_empties(1, 23)
Output: False
Hints
Hint 1
The total T = a + b never changes. Track just one bucket's size x; the other is always T - x. Write out what happens to x in one move and see if you can express it as a modular operation.
Hint 2
After each move, x becomes 2x mod T. So after k moves the first bucket holds (2^k * a) mod T. A bucket empties exactly when this residue hits 0 — that is, when T divides 2^k * a for some k >= 1.
Hint 3
Factor T as 2^q * m where m is odd. Doubling can supply any power of two, but it can never introduce a new odd factor. So T | 2^k * a requires m | a. Check whether the odd part of T divides a — that's the whole test.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Reducing the problem to modular arithmetic
The total T = a + b is an invariant — it never changes. That means the full state of the system at any point is captured by a single number: the size of one bucket. Call it x; the other bucket always holds T - x.
Trace what one move does to x:
- If
x < T - x(bucket A is smaller): A receivesxtokens from B, sox → 2x. - If
x > T - x(bucket A is larger): A gives awayT - xtokens, sox → x - (T - x) = 2x - T. - If
x = T - x: one bucket is poured into the other, givingx → 0orx → T.
In every case, the new value is 2x mod T. After k moves the first bucket holds (2^k * a) mod T. A bucket empties when this residue first equals 0 (or equivalently T, but T means the other bucket is empty). So the question becomes:
Does there exist an integer
k >= 1such thatT | 2^k * a?
Reducing to a divisibility check
Factor T = 2^q * m with m odd.
For T | 2^k * a we need:
2^q | 2^k * a: satisfied for anyk >= qregardless ofa.m | 2^k * a: becausegcd(2^k, m) = 1(m is odd), this simplifies tom | a.
Doubling can contribute powers of two freely, but it can never introduce an odd factor that wasn't already there. So the answer is True if and only if m | a, where m is the odd part of T.
What the code checks
The implementation phrases the same condition slightly more explicitly to also identify the exact move:
- If
Tis odd, returnFalseimmediately (the odd part ofTisTitself, anda < T). - Strip all factors of 2 from
Tto getqandm. - Strip all factors of 2 from
ato getrandvalue(the odd part ofa). - If
r >= q, returnFalse. (This is a safe guard: ifm | aanda < T = 2^q * m, thena = m * twitht < 2^q, sov₂(a) = v₂(t) < q. The guard never rejects a valid case.) - Return
value % (2 * m) == m.
Step 5 captures m | value in a form that's easy to verify: since both value and m are odd, m | value forces the quotient to be odd, which means value ≡ m (mod 2m). The two conditions are equivalent.
Why it's correct
- If
m | a: after exactlyq - r - 1doublings, the first bucket holds2^(q-1) * value ≡ T/2 (mod T). Both buckets are equal. The very next move empties one of them. - If
m ∤ a: the residue2^k * a mod Tis never zero for anyk, so the process cycles forever. The state space is finite, so a non-terminating run must eventually repeat a state — matching the cycling behavior in the examples. - The condition is symmetric:
m | aiffm | b(sincem | T = a + b), so it doesn't matter which bucket you call "first."
Complexity
- Time: O(log T). Stripping factors of two from
Tand fromaeach take at most ~31 iterations forT <= 2 * 10^9. - Space: O(1).
Direct simulation is a trap: the process can cycle through O(T) distinct states before repeating, which is up to ~2 × 10^9 steps.
Reference solution
def bucket_empties(a: int, b: int) -> bool:
total = a + b
# An odd total means 2^k * a is never divisible by total (odd factor
# can't be supplied by doubling), so the process cycles forever.
if total & 1:
return False
# Write total = 2^q * m with m odd.
q = 0
reduced = total
while (reduced & 1) == 0:
q += 1
reduced >>= 1
m = reduced
# Write a = 2^r * value with value odd.
r = 0
value = a
while (value & 1) == 0:
r += 1
value >>= 1
# If a already carries at least as many factors of 2 as total,
# the residue 2^k * a (mod total) can never reach total/2, so no emptying.
if r >= q:
return False
# After q - 1 - r moves, the residue equals 2^(q-1) * value.
# That equals total / 2 (buckets become equal, next move empties one)
# if and only if value ≡ m (mod 2m), which is equivalent to m | a.
return value % (2 * m) == m