Tank Filling: Can You Hit an Exact Volume?
Math & Bit ManipulationAuto-graded · 22 test casesLast updated
Tests whether a candidate can apply GCD-based number theory and state-space reasoning to determine if a target volume is reachable with two containers.
Tank Filling: Can You Hit an Exact Volume?
You have two tanks with fixed capacities x liters and y liters, plus an unlimited water supply. At any point you may perform exactly one of the following operations:
- Fill a tank completely from the supply.
- Drain a tank completely onto the ground.
- Transfer water from one tank into the other until the source tank is empty or the destination tank is full, whichever comes first.
You start with both tanks empty. Determine whether some sequence of these operations can leave exactly z liters in one of the two tanks.
Return True if that target is reachable, False otherwise.
Approach guidance
You are expected to reason about two complementary approaches:
State-space search (BFS/DFS)
Model each situation as a pair (a, b) representing the current contents of each tank (0 ≤ a ≤ x, 0 ≤ b ≤ y). Starting from (0, 0), apply all valid operations as edges and search for any state where a == z or b == z.
- How many distinct states exist?
- What is the time and space complexity of this search?
- When does this approach become impractical?
Number-theoretic shortcut
There is a closed-form condition using the greatest common divisor of x and y. Derive it by identifying an invariant that every reachable volume satisfies, then argue (using Bézout's identity) that every value satisfying the invariant is in fact reachable.
- What is the exact condition on
z? - What is the time and space complexity of this approach?
Edge cases to address
z == 0: both tanks start empty, so this is always achievable.z > max(x, y): a single tank can never hold more than its capacity.x == 0ory == 0: one tank is useless; only 0 and the other tank's capacity are reachable.x == 0andy == 0: nothing can ever be stored; onlyz == 0is achievable.
Constraints
0 ≤ x, y, z ≤ 10^9- All three values are non-negative integers.
- "Exactly z liters" means one tank holds precisely z, not that the two tanks together total z.
- The operations may be applied in any order, any number of times.
Examples
Example 1
Input: can_measure(3, 5, 4)
Output: True
Example 2
Input: can_measure(2, 6, 5)
Output: False
Hints
Hint 1
Start by asking: what property is shared by every volume that can ever appear in a tank? Notice that every fill sets a tank to x or y, every drain sets it to 0, and every transfer moves an amount bounded by the smaller of what's available and what fits — all multiples of gcd(x, y).
Hint 2
Once you have the invariant, argue the other direction: for any multiple of g = gcd(x, y) that fits in the larger tank, you can actually reach it. Think about repeatedly filling the small tank and pouring it into the large one, emptying the large tank whenever it overflows. How many distinct residues does this cycle through?
Hint 3
Handle the degenerate cases before touching modulo arithmetic. Check z == 0 first (always true), then z > max(x, y) (always false, and this also sidesteps division by zero when both capacities are 0).
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Invariant: every reachable volume is a multiple of g = gcd(x, y)
Both tanks start at 0, which is a multiple of g. Consider what each operation does to the amounts stored:
- Fill sets a tank to x or y — both multiples of g.
- Drain sets a tank to 0 — a multiple of g.
- Transfer moves
t = min(source, remaining_capacity)liters. Because both the source content and the capacity are already multiples of g (by induction), so is t. The contents of both tanks remain multiples of g after the move.
Therefore, no matter how many operations are applied, every amount ever held in either tank is a multiple of g. This rules out any z that is not a multiple of g, and any z that exceeds max(x, y) (a tank cannot hold more than its capacity).
Sufficiency: every qualifying z is actually reachable
Assume without loss of generality that x ≤ y. Consider this repeated procedure:
- Fill the small tank (capacity x).
- Pour it into the large tank.
- Whenever the large tank reaches capacity y, drain it and continue pouring.
After k fills of the small tank, the large tank holds (k · x) mod y liters (ignoring the times it was drained and refilled). As k ranges from 1 to y/g, the values k · x mod y cycle through every multiple of g in {0, 1, …, y − g}. This follows from the fact that x and y/g are coprime, so x generates the full cyclic group modulo y/g. Filling the large tank directly gives y itself. Together, every multiple of g in [0, y] = [0, max(x, y)] is reachable.
Edge case handling (in order)
z == 0→ returnTrueimmediately. Both tanks start empty; no operations needed.z > max(x, y)→ returnFalse. No single tank can hold that much. This guard also preventsgcd(0, 0) = 0from reaching the modulo step.z % gcd(x, y) == 0→ returnTrue; otherwiseFalse.
The guard ordering matters: checking the bound before the modulo means we never compute z % 0.
Complexity
| Approach | Time | Space |
|---|---|---|
| GCD (this solution) | O(log min(x, y)) | O(1) |
| BFS over states | O(x · y) | O(x · y) |
The GCD approach is efficient even when x and y are up to 10⁹. A BFS would be completely infeasible at that scale.
Reference solution
from math import gcd
def can_measure(x: int, y: int, z: int) -> bool:
# z == 0 is always reachable: both tanks begin empty.
if z == 0:
return True
# A single tank can hold at most max(x, y) liters.
# This check also prevents gcd(0, 0) == 0 from causing a division by zero below.
if z < 0 or z > max(x, y):
return False
# Every reachable volume is a multiple of gcd(x, y) (invariant).
# By Bézout's identity, every such multiple up to max(x, y) is reachable (sufficiency).
divisor = gcd(x, y)
return divisor != 0 and z % divisor == 0