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?- A. 3
- B. 4
- C. 5
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- A binary search finds the largest x with xx <= 50 using the bounds 0 and 50. python lo, hi = 0, 50 while lo < hi: mid = (lo + hi + 1) // 2…
- A routine computes the number of subarrays whose sum is divisible by 5, using the remainder of each prefix sum. python a = [3, -1, 4, -6…
- A problem asks for the minimum number of refuelling stops on a road, where the car has a fuel capacity and each station offers some fuel…
- For the problem 'choose the largest set of non-overlapping intervals', which greedy rule is correct?
- A DP over prefixes of an array computes the number of ways to split it into segments where each segment's sum is at most L. Which…
- Which problem is the standard application of 'binary search on the answer' where the feasibility test itself is a greedy scan?