Data Structures & AlgorithmsHardCodeforces Div2 C · TCS NQT

A frog at position 0 wants to land exactly on position 17. Each jump has length 3, 5 or 7. What is the minimum number of jumps needed, or −1 if it is impossible?

  1. A. 3
  2. B. 4
  3. C. 5
  4. D. 2

Correct answer: A. 3

This is the coin-change minimum with coins {3, 5, 7}: d[v] = 1 + min d[v − s]. Greedy by the largest jump that fits (7 + 7 + 3) gives 3 jumps for 17, which happens to be optimal only by luck, so the DP is the safe method.

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

Exams that ask this topic