Data Structures & AlgorithmsHardGATE CS · ICPC regional
Two unmarked jugs hold 6 litres and 8 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 4 litres?
- A. 5
- B. 4
- C. 3
- D. 6
Correct answer: B. 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.
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 four-wheel combination lock shows four digits 0–9 on wheels that wrap around (9 goes to 0 and 0 goes to 9). One move turns one wheel by…
- A tree has the vertices B, E, H, I, Q, R, U, V and the edges V–I, I–H, H–R, H–U, R–B, I–E, U–Q. A centroid is a vertex whose removal…
- A tree on the vertices B, C, D, H, J, T, Z has the arcs T→B, Z→T, B→C, B→D, J→D, J→H (each undirected edge is directed as shown). You may…
- A grid has 4 rows and 4 columns with these values (top to bottom): 9446 8699 5398 3165 A path moves between side-adjacent cells (up, down…
- Floyd–Warshall runs on vertices 1 to 5 with these arcs u→v (weight): 1→2 (4), 1→3 (2), 2→1 (11), 3→1 (8), 3→2 (4), 3→4 (7), 3→5 (4), 4→1…
- Bellman–Ford is run from source U on the directed graph whose arcs (weights in brackets) are listed in the order they are relaxed: K→U…