Data Structures & AlgorithmsHardGATE CS · Codeforces Div 2 B
Two unmarked jugs hold 7 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 3 litres?
- A. 10
- B. 12
- C. 5
- D. 3
Correct answer: A. 10
BFS over (first jug, second jug) states gives 10 operations: fill the 8, pour into the 7, empty the 7, and repeat until 3 is left in the 8. Requiring the target in the 7-litre jug takes 12; 5 counts only the pours and 3 only the fills of the optimal route.
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
- Cities A, B, N, T, U are joined by roads with these travel costs: A–N 15, A–U 2, A–B 4, A–T 14, N–U 17, N–B 5, N–T 13, U–B 8, U–T 6, B–T…
- An undirected connected graph on the vertices D, U, W, X has the edges U–X, D–W, D–X, W–X, D–U. How many spanning trees does it have? (A…
- A connected undirected graph has the vertices B, M, R, T, U and the edges U–T, U–M, U–R, T–R, T–B, M–B, R–B. Parallel edges are allowed…
- An undirected graph has the vertices A, D, F, P, U, Y and the edges P–A, Y–A, D–A, D–U, Y–F, D–F, P–F, U–A, Y–D, F–U. What is the minimum…
- A directed acyclic graph has the vertices D, L, P, Q, R, U, V and the edges V→R, Q→R, U→R, U→Q, U→D, U→P, L→Q. A topological order lists…
- Consider this recursive function: python def f(n): if n <= 0: return f(n - 2) if n % 3 == 0: f(n - 2) How many times is f called in total…