Data Structures & AlgorithmsModerateGATE CS · Codeforces Div 2 B
Starting from the integer 9, one operation either adds 1, subtracts 1, or doubles the current value. Values may be any non-negative integers. What is the minimum number of operations needed to reach exactly 84?
- A. 5
- B. 75
- C. 4
- D. 6
Correct answer: A. 5
A BFS over the integers gives 5 operations. Peeling back from 84 with halving-when-even gives 5; using only +1 and doubling (no subtraction) needs 5.
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
- An undirected graph has the vertices A, O, S, T, V, Y and the edges Y–V, T–A, T–V, O–T, O–A, Y–A, S–V, O–S, V–A. What is the minimum…
- An undirected graph has the vertices M, O, U, Y and the edges U–O, U–Y, M–O, O–Y. In how many ways can each vertex be given one of 3…
- The directed acyclic graph has the vertices B, C, K, P, Q, R, V, Z and the arcs Z→V, K→R, C→B, K→C, Q→B, Z→B, Z→K, R→V, Q→Z, P→R, K→B…
- A directed graph has the vertices C, D, F, G, H, I, Q, W and the arcs I→H, I→D, I→F, I→W, Q→D, Q→F, Q→G, Q→C, H→F, H→W, H→C, F→I, F→Q…
- A directed acyclic graph has the vertices A, D, M, O, Q, U, V and the edges U→M, U→D, U→V, Q→U, Q→O, A→M, A→V, O→U. A topological order…
- An undirected connected graph on 6 vertices has the edges (weights in brackets, all distinct): X–K (3), S–D (11), X–S (24), S–K (13), D–K…