Data Structures & AlgorithmsHardGATE CS · ICPC regional

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 161?

  1. A. 152
  2. B. 5
  3. C. 7
  4. D. 6

Correct answer: D. 6

A BFS over the integers gives 6 operations. Peeling back from 161 with halving-when-even gives 6; using only +1 and doubling (no subtraction) needs 6.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic