Data Structures & AlgorithmsVery hardCodeforces Div2 C · GATE CS

Starting from n = 191, in each step you may replace n by n/2 when n is even, or by n−1 or n+1 when n is odd. What is the minimum number of steps needed to reach 1?

  1. A. 9
  2. B. 10
  3. C. 13
  4. D. 11

Correct answer: A. 9

For odd n the choice between n−1 and n+1 decides the answer: n+1 is better when n ends in binary 11, which is not always true of a naive rule. A BFS over reachable values gives the exact minimum.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic