Water and Jug Problem - Problem

You're standing at a river with two empty jugs and need to measure exactly target liters of water. The first jug holds x liters, the second holds y liters, and you have an unlimited water supply from the river.

Your available operations are:

  • Fill either jug completely with water
  • Empty either jug completely
  • Pour water from one jug to another until either the source jug is empty or the target jug is full

The question is: Can you measure exactly target liters using these operations?

For example, with a 3-liter jug and 5-liter jug, you can measure exactly 4 liters by filling the 5L jug, pouring into 3L jug (leaving 2L in the 5L jug), emptying the 3L jug, pouring the 2L into it, then filling the 5L jug again and pouring 1L into the 3L jug, leaving exactly 4L!

Input & Output

example_1.py — Standard Case
$ Input: x = 3, y = 5, target = 4
Output: true
💡 Note: Fill 5L jug → Pour into 3L jug (2L remains) → Empty 3L jug → Pour 2L into 3L jug → Fill 5L jug → Pour 1L into 3L jug, leaving exactly 4L in the 5L jug
example_2.py — Impossible Case
$ Input: x = 2, y = 6, target = 5
Output: false
💡 Note: GCD(2,6) = 2, and 5 is not divisible by 2, so it's impossible to measure exactly 5L using 2L and 6L jugs
example_3.py — Edge Case
$ Input: x = 1, y = 2, target = 3
Output: true
💡 Note: Fill both jugs completely: 1L + 2L = 3L exactly

Visualization

Tap to expand
3L Jug5L Jug2L~4LMathematical Analysis:GCD(3, 5) = 1Target: 4 liters✓ 4 % 1 = 0 (divisible)✓ 4 ≤ 8 (within capacity)Result: POSSIBLEMeasurable amounts:1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L(all multiples of GCD within range)pourKey insight: Operations create linear combinations ax + byWater Jug Problem: 3L + 5L → 4L
Understanding the Visualization
1
Identify Operations
We have 6 possible operations: fill jug A, fill jug B, empty jug A, empty jug B, pour A→B, pour B→A
2
State Space
Each state is represented as (amount_in_A, amount_in_B). BFS explores all reachable states
3
Mathematical Insight
The problem reduces to Bézout's identity: can we express target as ax + by for integers a,b?
4
GCD Connection
The answer is YES iff target is divisible by GCD(x,y) and target ≤ x+y
Key Takeaway
🎯 Key Insight: This classic puzzle is actually a number theory problem! The GCD determines the 'granularity' of measurements possible, and any target that's a multiple of this GCD (within capacity limits) can be achieved through the pouring operations.

Time & Space Complexity

Time Complexity
⏱️
O(log(min(x,y)))

Time complexity of GCD calculation using Euclidean algorithm

n
2n
Linearithmic
Space Complexity
O(1)

Only using a few variables

n
2n
Linear Space

Constraints

  • 1 ≤ x, y ≤ 103
  • 0 ≤ target ≤ x + y
  • Note: Jugs cannot be partially filled
Asked in
Google 25 Microsoft 18 Amazon 15 Apple 12
21.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen