The hiring process of various companies has evolved over the years. They have extended their part to making the candidates solve tough puzzles using their code implementation. But why? Well, for starters the analytical skills can be tested, and secondly, the coding skills are used to get the most optimal way to solve the riddle. In today's article, we are going to understand and solve a well-known problem called the Water Jug Problem. We will understand the problem, check an example, and methods to solve it in three different languages: C++, Java, and Python.
What is the Water Jug Problem?
Here is the problem statement for Water Jug Problem:
You are given 2 jugs with the capacity 'm' and 'n' respectively. Initially, they are given empty. There is an unlimited supply of water. You can either fill the whole jug or a quantity that is less than the given capacity of jugs. Now, you are also given a third positive integer 'd'. Using the 2 given jugs, you need to come up with a solution to have 'd' amount of water in them and return the number of steps you took to reach that capacity.
Understanding the Problem
Before beginning the coding part, it is very important for you to understand what the question is asking you to do. Many students just copy the code from web servers and later, when the interviewer asks any question from it, they are stuck and even embarrassed. So, to save yourself from such questions, kindly go through this section carefully.
So, you are given 2 jugs with their capacities. It has also been clearly stated in the question that with the unlimited water supply, you can either fill the whole jug up to its capacity or somewhat less than that. Keep that in mind.
Secondly, we need to find a way to somehow have a 'd' amount of water left in the jugs. Basically, we need to transfer, fill and, empty the jugs to get the required capacity of water in the jugs.
The next most important question is which data structure should you choose for this question. You cannot use arrays as it will be a heavy task for both the compiler(in terms of time and auxiliary space complexity) and you(in terms of long codes).
You should choose a structure with which you can manage the content of both jugs simultaneously on the front. The possible and safest option is to use Sets or Pairs.
How to Approach the Solution?
Now that we have chosen what to work with, let's understand how you can actually make it work. Well, for starters you can have (a, b) which represents the amount of water currently in jug 1 and jug 2 respectively. Initially, both the components will be (0, 0) since the jugs are empty in the beginning. The final state of the jugs will be either (0, d) or (d, 0) as both add up to give a total of the required quantity.
The following operations can be performed on the jugs:
- Empty a jug (a, b) -> (0, b)
- Fill a jug (0, b) -> (a, b)
- Transfer water from one jug to another.
Input: 3, 5, 4
Explanation: The following steps are taken:
- Fill the 5-liter jug completely.
- Transfer 3 liters from a 5-liter jug to a 3-liter jug.
- Empty the 3-liter capacity jug.
- Transfer the remaining 2 liters from a 5-liter jug to a 3-liter jug.
- Now, fill the 5-liter jug fully.
- Pour 1 liter from a 5-liter jug into a 3-liter jug.
- There! We have 4 liters in the first jug, now empty the 2nd jug.
For more clarity, check the below illustration.
3 Methods to Solve the Water Jug Problem
Now, you know what the problem is and what is the approach behind the solution. Next, discuss some methods with which you can code this problem. Following are the three methods, you can use:
Method 01) Always pour from 1st jug to 2nd jug
- Fill the m liter jug and empty it into the n liter jug.
- When the m liter jug becomes empty refill it.
- When the n-liter jug becomes full, empty it.
- Repeat the above steps till any one of the jugs contains the required amount of water in them.
Method 02) Always pour from 2nd jug to 1st jug
- Fill the n-liter jug and empty it into the m-liter jug.
- When the n liter jug becomes empty refill it.
- When the m liter jug becomes full, empty it.
- Repeat the above steps till any of the jugs contain the required amount of water in them.
Method 03) Using BFS(Breadth First Search)
In the BFS method, we use a queue and a map to find the solution. In the beginning, we initialize it with zero as both the jugs are empty. We then keep applying the above operations that are permitted to us and find the possible scenarios to find the targeted amount of water in the jugs.
Also, we keep maintaining an empty matrix to keep a track of the positions or states that we have encountered. In this way, the space complexity reduces manifolds and the efficiency of the program continuously improves.
Code Implementation with C++
Here is the C++ code to solve the Water Jug Problem:
Code Implementation with Java
Here is the Java code to solve this problem:
Code Implementation with Python
Here is the Python code to solve the Water Jug Problem:
Time Complexity for the solution will be O(n*m) and the Space Complexity for it will be O(n*m).
After reading this article, it is certain that you must have understood the Water Jug problem and the approaches with which it can be solved. By exploring different approaches and solutions, we can find efficient ways to measure out specific quantities of water using limited resources. Whether you choose to pour from the smaller jug into the larger one or vice versa, the key is to find the solution that minimizes the number of steps required.