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?
- A. 8
- B. 6
- C. 3
- 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.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- A tree on the vertices E, H, N, U, W, Z has the arcs H→N, N→Z, U→N, U→E, W→Z (each undirected edge is directed as shown). You may reverse…
- A directed graph with no directed cycles has arcs u→v (weight) : V→K (4), V→X (3), K→F (-1), K→X (-2), F→X (8), X→W (8). Weights may be…
- A counter starts at 1. Each step, you may add 1, add 2, or double the counter. Each step must stay at or below 19. Two sequences of steps…
- An undirected graph has the vertices D, M, R, V and the edges M–D, M–V, M–R, D–V, D–R, V–R. A walk may repeat vertices and edges. How many…
- The directed acyclic graph has the vertices A, B, C, G, J, O and the arcs J→C, G→C, J→O, J→A, G→A, J→B, B→C, O→B. How many distinct…
- A tree has vertices with weights in brackets: T[6], C[15], K[8], Z[3], U[2], V[2], N[14], G[14], F[1], H[7]. The edges are T–C, T–K, K–Z…