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- A. 10
- B. 11
- C. 7
- 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.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- A number is given as the digit string 1432219. Exactly 3 digits must be deleted (the digits keep their order) so that the remaining number…
- The same stack method is applied to the string 5317 with exactly 2 digits deleted, but now the aim is the LARGEST number. The procedure…
- Children have greed values 7, 8, 9, 10 and cookies have sizes 5, 6, 7, 8, 8. A child is content if the cookie given is at least his greed…
- Closed intervals (start, end): (1,4), (2,6), (8,10), (9,12), (11,13), (3,5). The greedy below places points so that every interval…
- Job durations (burst times) are 6, 8, 7, 3, 4 with all jobs arriving at time 0. Shortest-job-first runs them one after another in…
- A row of positions 1 to 20 is scanned in order. A position i is picked if no position has been picked yet, or if i minus the last picked…