You are at the side of a river. You are given a m litre jug and a n litre jug where 0 < m < n. Both the jugs are initially empty. The jugs don’t have markings to allow measuring smaller quantities. You have to use the jugs to measure d litres of water where d < n. Determine the minimum no of operations to be performed to obtain d litres of water in one of jug.
The operations you can perform are:
- Empty a Jug
- Fill a Jug
- Pour water from one jug to the other until one of the jugs is either empty or full.
There are several ways of solving this problem including BFS and DP. In this article an arithmetic approach to solve the problem is discussed. The problem can be modeled by means of Diophantine equation of the form mx + ny = d which is solvable if and only if gcd(m, n) divides d. Also, the solution x,y for which equation is satisfied can be given using the Extended Euclid algorithm for GCD.
For example, if we have a jug J1 of 5 litre (n = 5) and another jug J2 of 3 litre (m = 3) and we have to measure 1 litre of water using them. The associated equation will be 5n + 3m = 1. First of all this problem can be solved since gcd(3,5) = 1 which divides 1 (See this for detailed explanation). Using the Extended Euclid algorithm, we get values of n and m for which the equation is satisfied which are n = 2 and m = -3. These values of n, m also have some meaning like here n = 2 and m = -3 means that we have to fill J1 twice and empty J2 thrice.
Now to find the minimum no of operations to be performed we have to decide which jug should be filled first. Depending upon which jug is chosen to be filled and which to be emptied we have two different solutions and the minimum among them would be our answer.
Solution 1 (Always pour from m litre jug into n litre jug)
- Fill the m litre jug and empty it into n litre jug.
- Whenever the m litre jug becomes empty fill it.
- Whenever the n litre jug becomes full empty it.
- Repeat steps 1,2,3 till either n litre jug or the m litre jug contains d litres of water.
Each of steps 1, 2 and 3 are counted as one operation that we perform. Let us say algorithm 1 achieves the task in C1 no of operations.
Solution 2 (Always pour from n litre jug into m litre jug)
- Fill the n litre jug and empty it into m litre jug.
- Whenever the n litre jug becomes empty fill it.
- Whenever the m litre jug becomes full empty it.
- Repeat steps 1, 2 and 3 till either n litre jug or the m litre jug contains d litres of water.
Let us say solution 2 achieves the task in C2 no of operations.
Now our final solution will be minimum of C1 and C2.
Now we illustrate how both of the solutions work. Suppose there are a 3 litre jug and a 5 litre jug to measure 4 litres water so m = 3,n = 5 and d = 4. The associated Diophantine equation will be 3m + 5n = 4. We us pair (x, y) to represent amounts of water inside 3 litre jug and 5 litre jug respectively in each pouring step.
Using Solution 1, successive pouring steps are:
Hence the no of operations you need to perform are 8.
Using Solution 2, successive pouring steps are:
Hence the no of operations you need to perform are 6.
Therefore we would use solution 2 to measure 4 liters of water in 6 operations or moves.
Based on the explanation here is an implementation in C++.
Minimum number of steps required is 6
This article is contributed by Madhur Modi. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Water Jug Problem using Memoization
- Trapping Rain Water
- Program to find amount of water in a given glass
- Puzzle 39 | (100 coins puzzle)
- Puzzle 29 | (Car Wheel Puzzle)
- Egg Dropping Puzzle | DP-11
- Puzzle 7 | (3 Bulbs and 3 Switches)
- Puzzle 8 | (Find the Jar with contaminated pills)
- Puzzle 9 | (Find the fastest 3 horses)
- How to check if an instance of 15 puzzle is solvable?
- Puzzle 1 | (How to Measure 45 minutes using two identical wires?)
- Puzzle | Program to find number of squares in a chessboard
- Minimum Cost Graph
- Count of different groups using Graph