Data Structures & AlgorithmsHardGATE CS · ICPC regional

From the number 1, the allowed moves are x -> x+1, x -> x-1 and x -> 2x, staying within [1, 40]. What is the minimum number of moves to reach 20 from 1?

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

Correct answer: B. 5

A breadth-first search over the values gives the minimum; greedy halving does not always work for this set of moves.

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

Exams that ask this topic