Data Structures & AlgorithmsVery hardGATE CS · Codeforces Div2 C

A DP counts the minimum number of coins to pay exactly 11 with coins [2, 5], returning −1 if impossible.

A DP counts the minimum number of coins to pay exactly 11 with coins [2, 5], returning −1 if impossible.
INF = float('inf')
coins = [2, 5]; amt = 11
d = [0] + [INF] * amt
for v in range(1, amt + 1):
    for c in coins:
        if c <= v and d[v - c] + 1 < d[v]: d[v] = d[v - c] + 1
print(d[amt] if d[amt] != INF else -1)
What is printed?
  1. A. 3
  2. B. 4
  3. C. 5
  4. D. 6

Correct answer: B. 4

The minimum is 4 coins (5 + 2 + 2 + 2). The DP over amounts with an infinity sentinel finds the exact minimum, and it marks unreachable amounts so they are never chosen. The formula-style variant assumes a greedy split and is not exact in general.

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

Exams that ask this topic