Data Structures & AlgorithmsModerateGATE CS · ICPC regional

A 3-litre jug and a 5-litre jug start empty and you can fill either, empty either, or pour one into the other until one is full or empty. What is the minimum number of operations needed to have exactly 4 litres in one of the jugs?

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

Correct answer: B. 6

BFS over (A, B) states: fill 5, pour into 3, empty 3, pour 2 into 3, fill 5, pour into 3 leaves 4 in B after 6 operations. Starting with the 3-litre jug takes 8; 2 and 3 count only the fills or only the pours of the optimal route.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic