Data Structures & AlgorithmsHardGATE CS · Codeforces Div 2 B

Two unmarked jugs hold 4 litres and 5 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 3 litres?

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

Correct answer: C. 4

A BFS over the states (litres in the first jug, litres in the second jug) gives 4 operations. Restricting the goal to the first jug alone would give 4, and to the second jug alone 6.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic