![]() |
VOOZH | about |
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:
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.
Table of Content
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:
For m = 2, n = 3, d = 1 the approach is:
- Start at (0,0) where both jugs are empty.
- Fill either jug and reach (2,0) (fill 2L jug) or (0,3) (fill 3L jug).
- From (2,0) pour into jug2 to get (0,2), or from (0,3) pour into jug1 to get (2,1).
- 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.
The problem can be represented using a Diophantine equation of the form:
m × x + n × y = dHere m litre jug is used x times and n litre jug is used y times. Also x
, ycan be negative because of emptyingA 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
Strategy 2: Pour from n to m
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 = 4We 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?
6
Time Complexity: O(m + n), because operations are bounded by jug capacities
Auxiliary Space: O(1)