ProgrammingHardTCS NQT · Accenture pattern

What will be the output of the following pseudocode (number of ways to make change for an amount using given coin denominations, counting combinations, not permutations)?

What will be the output of the following pseudocode (number of ways to make change for an amount using given coin denominations, counting combinations, not permutations)?
Integer coins[3] = {1, 2, 5}
Integer amount = 5
Integer dp[6] = all zeros
dp[0] = 1
for (each c in coins)
    for (each a from c to amount)
        dp[a] = dp[a] + dp[a - c]
    end for
end for
Print dp[amount]
  1. A. 3
  2. B. 4
  3. C. 5
  4. D. 6

Correct answer: B. 4

The 4 ways to make 5 using {1,2,5} as combinations are: 1+1+1+1+1, 1+1+1+2, 1+2+2, and 5 itself.

More Pseudocode and Code Tracing questions

Exams that ask this topic