New update is available. Click here to update.

Last Updated: 11 Jan, 2021

Difficulty: Moderate

```
• Fill any of the jugs entirely with water.
• Empty any of the jugs.
• Pour water from one jug into another till the other jug is full, or the first jug itself is empty.
```

```
In order to measure 2 litres from jugs of 4 and 6 litres we can follow the following steps-
• Fill 6-litres jugs to its maximum capacity.
• Pour water from 6-litres jug to the jug with 4-litres capacity.
• Pour water from 6-litres jug to the jug with 4-litres capacity.
```

```
The first line of input contains an integer ‘T’ denoting the number of test cases to run. Then the test cases follow.
The first line of each test case contains three space-separated integers ‘X’, ‘Y’ and ‘Z’ denoting the capacities of both the jugs and the target measure, respectively.
```

```
For each test case, print True if we can measure the required value else, print False.
Output for each test case will be printed in a new line.
```

```
You do not need to print anything, it has already been taken care of. Just implement the given function.
```

```
1 <= T <= 5 * 10^4
0 <= X, Y, Z <= 5 * 10^4
Time Limit: 1 sec
```

We will run a breadth-first search(BFS), keeping states as water present in both the jugs. We will visit all the states, keeping a hashmap for visited states to not revisit the same state. If we reach a state such that the capacity of water in one of the jugs is equal to ‘Z’ liters or the sum of water in both the jugs is equal to ‘Z’ liters we return *true *else we return *false*.

Below is the detailed algorithm:

- We will use a data structure like a queue for implementing the BFS based solution.
- We will insert the initial state, i.e. (0, 0) in the queue ‘STORE’.
- While (size of 'STORE' > 0):
- We will pop out the front element ‘curr’ of the queue. Let the current water in the first jug be ‘CURR_A’ and in the second jug be ‘CURR_B’ respectively.
- If one of the values in the ‘curr’ is ‘Z’ or the sum of both the values in ‘curr’ is equal to ‘Z’ we return true.
- We now take the following cases:
- Fill one of the buckets:
- If 'CURR_A' < 'X' we can fill more water in the first jug. Hence, if ('X', 'CURR_B') is not there in the hashmap, we insert it in the queue and the hashmap.
- If 'CURR_B' < Y we can fill more water in the second jug. Hence, if ('CURR_A', Y) is not there in the hashmap, we insert it in the queue and the hashmap.

- Pour water into the other jug:
- We will now try to pour water from one jug to another. We can pour min('X' - 'CURR_A', 'CURR_B') water from the second jug to the first jug. Hence, if ('CURR_A' + min('X' - 'CURR_A', 'CURR_B'), 'CURR_B' - min('X' - 'CURR_A', 'CURR_B') ) is not there in the hashmap, we insert it in the queue and the hashmap.
- Similarly, we can pour min('CURR_A', Y - 'CURR_B') water from the first jug to the second jug. Hence, if ('CURR_A' - min('CURR_A', Y - 'CURR_B'), 'CURR_B' + min('CURR_A', Y - 'CURR_B') ) is not there in the hashmap, we insert it in the queue and the hashmap.

- Pour water to the ground:
- We can also pour water from one of the jugs to the ground and empty it. Hence, if (0, 'CURR_B') is not there in the hashmap, we insert it in the queue and the hashmap.
- Similarly, if ('CURR_A', 0) is not there in the hashmap, we insert it in the queue and the hashmap.

- Fill one of the buckets:

- If we do not find any valid combination, we return
*false*.

The above problem can be modelled by Diophantine equation ‘A’ * ‘X’ + ‘B’ * ‘Y’ = ‘Z’, where ‘A’ and ‘B’ are integers.

According to linear algebra, this equation is solvable if and only if the greatest common divisor of ‘X’ and ‘Y’ divides ‘Z’.

Hence, the conditions to measure exactly ‘Z’ litres will be-

- ‘Z’ is divisible by GCD of ('X' , 'Y').
- ‘Z’ is less than ‘X’ + ‘Y’.

SIMILAR PROBLEMS

Distance to a Cycle in Undirected Graph

Posted: 7 Nov, 2022

Difficulty: Moderate

Ninja And The Strictly Increasing Array

Posted: 27 Nov, 2022

Difficulty: Moderate

Maximum GCD

Posted: 8 Dec, 2022

Difficulty: Hard

Prime?

Posted: 9 Dec, 2022

Difficulty: Easy

Co-Prime

Posted: 14 Dec, 2022

Difficulty: Hard

Popular Interview Problems: