2.1k views
5 votes
2. You have a 3 litre jug and a 5 litre jug.

You need to fill the 5 litre jug with exactly 4 litres of water.
Write an algorithm to solve this problem

1 Answer

6 votes
Sure, here's a simple algorithm to achieve that:

1. Fill the 5-litre jug.
2. Pour water from the 5-litre jug into the 3-litre jug until it's full.
3. Now, the 5-litre jug has 2 litres of water (5 - 3).
4. Empty the 3-litre jug.
5. Pour the remaining 2 litres from the 5-litre jug into the empty 3-litre jug.
6. Fill the 5-litre jug again.
7. Carefully pour water from the 5-litre jug into the 3-litre jug until it's full. This will leave exactly 4 litres in the 5-litre jug.

This algorithm ensures you measure exactly 4 litres in the 5-litre jug using only the 3-litre and 5-litre jugs.
User Atish Shimpi
by
7.8k points