Overview
The water-jug puzzle became famous from the film Die Hard with a Vengeance, where the heroes must defuse a bomb by placing exactly four litres on a scale using only 5-litre and 3-litre containers with no markings.
With only fill, empty and pour operations, the reachable volumes are governed by simple number theory — and four litres is reachable because 5 and 3 share no common factor.
How to solve The Water Jug Problem
- Fill the 5-litre jug completely (5, 0).
- Pour it into the 3-litre jug until that's full, leaving 2 litres behind (2, 3).
- Empty the 3-litre jug (2, 0), then pour the 2 litres into it (0, 2).
- Refill the 5-litre jug (5, 2), then top up the 3-litre jug, which needs just 1 more litre — leaving exactly 4 in the big jug (4, 3).
The key insight
Every reachable amount is a whole-number combination of the two capacities: 4 = 3 + 3 − 5 + 3, or more simply 4 = 2×5 − 2×3. Because gcd(5, 3) = 1, you can measure any whole number of litres up to 5.
Variations & echoes
- There's a symmetric solution that fills the 3-litre jug repeatedly instead; it takes more steps.
- In general you can measure a target T with jugs a and b only if T is a multiple of gcd(a, b).
- Framed as a graph, each (jugA, jugB) pair is a node and breadth-first search finds the shortest pour sequence.
Frequently asked questions
Could you measure 4 litres in the 3-litre jug?
No — the 3-litre jug can never hold more than 3 litres. The 4 litres must end up in the 5-litre jug.
Which amounts are impossible?
With 5- and 3-litre jugs, every whole number from 0 to 5 is measurable because gcd(5,3)=1. Two jugs of, say, 4 and 6 could only ever measure even amounts.