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?

  1. A. 5
  2. B. 75
  3. C. 4
  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.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic