VOOZH about

URL: https://www.geeksforgeeks.org/dsa/two-water-jug-puzzle/

⇱ The Water Jug Problem - Count Min Steps - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

The Water Jug Problem - Count Min Steps

Last Updated : 13 Apr, 2026

Given a m litre jug and a n litre jug where 0 < m < n, both initially empty and without any markings to measure intermediate quantities. You need to measure d litres of water where d < n. The operations you can perform are: 

  1. Empty a Jug
  2. Fill a Jug (You may assume that you have unlimited supply of water)
  3. Pour water from one jug to the other until one of the jugs is either empty or full

Find the minimum number of operations required to obtain exactly d litres of water in either of the two jugs. If it is not possible, return -1.

Examples:

Input: m = 2, n = 3, d = 1
Output: 2
Explanation:
1. Fill up the 3 litre jug
2. Pour 2 litre of water from the 3 litre jug to 2 litre Jug, Now the 3 litre jug has 1 litre water.

Input: m = 3, n = 5, d = 4
Output: 6
Explanation: Explained in the above images.

Input: m = 2, n = 3, d = 5
Output: -1
Explanation: As d > n here, it is not possible to measure exactly 5 litres using the given jugs.

Input: m = 2, n = 4, d = 3
Output: -1
Explanation: Not possible since gcd(2, 4) = 2 does not divide 3.

[Naive] Using BFS

To solve this problem, we can think like it as a state exploration problem where each state represents the amount of water in both jugs at a particular point in time. From any given state, a set of possible operations can be performed to move to a new state and this continues until we either reach the desired amount d in one of the jugs or determining that it's not possible.

For this problem, the state is represented as a pair (x, y), where x is the amount of water in the first jug and y is the amount in the second jug. The initial state is (0, 0) because both jugs start empty. Since we're looking for the minimum number of operations, Breadth-First Search (BFS) is a good choice as it explores all possible states level by level and this make sure that the first time we find the solution, it's with the minimum number of steps.

There are six possible operations that can be applied at any given state:

  1. Fill Jug 1: Fill the first jug to its maximum capacity (m liters).
  2. Fill Jug 2: Fill the second jug to its maximum capacity (n liters).
  3. Empty Jug 1: Completely empty the first jug.
  4. Empty Jug 2: Completely empty the second jug.
  5. Pour Jug 1 into Jug 2: Pour as much water as possible from the first jug into the second jug until the second jug is full or the first jug is empty.
  6. Pour Jug 2 into Jug 1: Pour as much water as possible from the second jug into the first jug until the first jug is full or the second jug is empty.

For m = 2, n = 3, d = 1 the approach is:

  1. Start at (0,0) where both jugs are empty.
  2. Fill either jug and reach (2,0) (fill 2L jug) or (0,3) (fill 3L jug).
  3. From (2,0) pour into jug2 to get (0,2), or from (0,3) pour into jug1 to get (2,1).
  4. At (2,1), jug2 contains 1 litre, so the target is achieved and the total number of steps is 2.

Time Complexity: O(m * n), Each possible state (x, y) is visited at most once, and total states are bounded by m * n.
Auxiliary Space: O(m * n), Space is used by the visited array and queue, which can store up to m * n states.

[Expected] Mathematical Approach

The problem can be represented using a Diophantine equation of the form:
m × x + n × y = d

Here m litre jug is used x times and n litre jug is used y times. Also x, y can be negative because of emptying

A solution exists only if gcd(m, n) divides d. The Extended Euclidean Algorithm can be used to find values of x and y, which indicate how many times each jug is filled or emptied. However, this does not directly give the minimum number of steps.

When solving the water jug problem, we are repeatedly:

  • Adding water in chunks of m or n (filling a jug).
  • Removing water in chunks of m or n (emptying a jug).

Therefore, the final amount d is essentially formed by combining multiples of m and n.

To find the minimum number of operations, simulate two possible strategies and take the minimum:

Strategy 1: Pour from m to n

  1. Fill the m litre jug and pour it into the n litre jug.
  2. If the m litre jug becomes empty, refill it.
  3. If the n litre jug becomes full, empty it.
  4. Repeat the above steps until either jug contains d litres.

Strategy 2: Pour from n to m

  1. Fill the n litre jug and pour it into the m litre jug.
  2. If the n litre jug becomes empty, refill it.
  3. If the m litre jug becomes full, empty it.
  4. Repeat the above steps until either jug contains d litres.

The final answer is the minimum number of steps obtained from both strategies.

Suppose there is a 3 litre jug and a 5 litre jug, and we need to measure 4 litres of water. So, m = 3, n = 5, and d = 4.

The associated Diophantine equation will be:
3x + 5y = 4

We use the pair (x, y) to represent the amount of water in the 3 litre jug and 5 litre jug respectively at each step.

Using Strategy 1 (pour from 3 litre jug to 5 litre jug)

Successive states are:

(0,0)->(3,0)->(0,3)->(3,3)->(1,5)->(1,0)->(0,1)->(3,1)->(0,4)

Number of operations = 8

Using Strategy 2 (pour from 5 litre jug to 3 litre jug)

Successive states are:

(0,0)->(0,5)->(3,2)->(0,2)->(2,0)->(2,5)->(3,4)

Number of operations = 6

Therefore, the minimum number of operations required is 6, so Strategy 2 is the optimal approach.

Why mixing of the two would not lead to minimum steps?

  • The problem reduces to Bézout's identity. You can form d using: d = k·m - l·n or d = l·n - k·m. These correspond to exactly two processes. Pour m to n or pour n to m.
  • Other way to look at it is, each direction builds a strict decreasing remainder sequence.

Output
6

Time Complexity: O(m + n), because operations are bounded by jug capacities
Auxiliary Space: O(1)

Related Links:

Comment
Article Tags: