Welcome to our comprehensive guide on solving the **2 Water Jug Problem** using **Python**! In this article, we will delve into the intricacies of this classic problem and provide you with a detailed explanation and code implementation that will help you tackle it with ease. Whether you are a beginner or an experienced programmer, this article will equip you with the knowledge to solve the Jug Water Problem efficiently.

Table of Contents

Looking for comprehensive study materials on Python, Data Structures and Algorithms (DSA), Object-Oriented Programming (OOPs), Java, Software Testing, and more?

**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. **

**Measure 2 liters of water with two empty jugs of 4 liters and 3-liter capacity and a tank full of water. Pour as many times as you want.**

How many ways can you do it by repeating no move?

The two jugs problem is an interesting little riddle that has a number of approaches to solve. Go on. Explore.

**List of Operations you can PERFORM.**

- 1. Fill Jug A Completely
- 2. Fill Jug B Completely
- 3. Empty Jug A Completely
- 4. Empty Jug B Completely
- 5. Pour From Jug A till Jug B is filled Completely or A becomes empty
- 6. Pour From Jug B till Jug A filled Completely or B becomes empty
- 7. Pour all From Jug B to Jug A
- 8. Pour all From Jug A to Jug B

## PYTHON Code of 2 Water Jug Problem

Now, let’s move on to the code implementation. Below is a Python function that solves the **2 Jug Water Problem** based on the given inputs:

```
a=int(input("Enter Jug A Capacity: "));
b=int(input("Enter Jug B Capacity: "));
ai=int(input("Initially Water in Jug A: "));
bi=int(input("Initially Water in Jug B: "));
af=int(input("Final State of Jug A: "));
bf=int(input("Final State of Jug B: "));
print("List Of Operations You can Do:\n");
print("1.Fill Jug A Completely\n");
print("2.Fill Jug B Completely\n");
print("3.Empty Jug A Completely\n");
print("4.Empty Jug B Completely\n");
print("5.Pour From Jug A till Jug B filled Completely or A becomes empty\n");
print("6.Pour From Jug B till Jug A filled Completely or B becomes empty\n");
print("7.Pour all From Jug B to Jug A\n");
print("8.Pour all From Jug A to Jug B\n");
while ((ai!=af or bi!=bf)):
op=int(input("Enter the Operation: "));
if(op==1):
ai=a;
elif(op==2):
bi=b;
elif(op==3):
ai=0;
elif(op==4):
bi=0;
elif(op==5):
if(b-bi>ai):
bi=ai+bi;
ai=0;
else:
ai=ai-(b-bi);
bi=b;
elif(op==6):
if(a-ai>bi):
ai=ai+bi;
bi=0;
else:
bi=bi-(a-ai);
ai=a;
elif(op==7):
ai=ai+bi;
bi=0;
elif(op==8):
bi=bi+ai;
ai=0;
print(ai,bi);
```

**Checkout: 3 Jug Water Puzzle in Python**

## Conclusion

In this article, we have explored the **2 Water**

**Jug Problem**and provided you with a detailed explanation and Python code implementation to solve it. By understanding the problem statement, approaching the solution, and implementing the code, you now have the tools to tackle this puzzle effectively.

Remember, computational thinking and problem-solving skills are essential in the world of programming. **The Jug Water Problem** is just one example that can help you enhance your logical reasoning abilities. Keep practicing and exploring new challenges to sharpen your skills further.

Leave in the comments if you have anything in mind.