Data Structures & AlgorithmsVery hardGATE CS · ICPC regional

Two unmarked jugs hold 3 litres and 7 litres. Allowed operations: fill a jug completely, empty a jug completely, or pour one jug into the other until the source is empty or the destination is full. Both jugs start empty. What is the minimum number of operations needed so that one of the jugs contains exactly 2 litres?

  1. A. 8
  2. B. 6
  3. C. 3
  4. D. 2

Correct answer: B. 6

BFS over (first jug, second jug) states gives 6 operations: fill the 3 and pour it into the 7 three times, leaving 2 in the 3. Requiring the target in the 7-litre jug takes 8; 3 counts only the pours and 2 is the target volume, not a count.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic