ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains

Currency notes are paid out greedily from the largest denomination down, using as many notes of each denomination as possible. For amount 1789 with denominations 500, 200, 100, 50, 20, 10, 5, 2, 1, what is the number of notes printed?

Currency notes are paid out greedily from the largest denomination down, using as many notes of each denomination as possible. For amount 1789 with denominations 500, 200, 100, 50, 20, 10, 5, 2, 1, what is the number of notes printed?
amount = 1789; notes = 0
for each denomination d, largest first
    k = amount div d
    notes = notes + k; amount = amount - k * d
end for
Print notes
  1. A. 10
  2. B. 11
  3. C. 7
  4. D. 12

Correct answer: A. 10

The greedy takes 500 × 3, 200, 50, 20, 10, 5 and 2 × 2: 10 notes. Skipping the 200 note (two 100s instead) gives 11, counting one note per denomination used gives 7, and paying the last 4 with four ₹1 notes instead of two ₹2 notes gives 12.

More Pseudocode and Code Tracing questions

Exams that ask this topic