Algorithm to solve minimal number of coins problem.

The problem is: What is the minimal number of coins needed to make a change?

For given coin nominals

500, 200, 100, 50, 20, 10, 5, 2, 1, 0.50, 0.25, 0.1, 0.05, 0.01

What coins (or banknotes) will be given as a change for 254.78?

To solve this we need to check how many coins or banknotes of each nominal beginning from highest to lowest can be “fitted” for each part of the value.

The answer is:

200.0 50.0 2.0 2.0 0.5 0.25 0.01 0.01 0.01

Pseudo Code:

1. Store initial money amount in variable M.
2. Place money nominals from highest to lowest in an array A. Create index to this array and name it I - it should be zero in the beginning.
3. Check if M < A[I]. If it is true increase I by one. If not subtract A[I] from M.
4. Check if M >= 0.01 <- or if it is bigger than smallest coin in array A <- last element of that array. If so go to step 3. 

 

Minimum Number Of Coins Algorithm
Tagged on:

Leave a Reply

Your email address will not be published. Required fields are marked *

This website stores some user agent data. These data are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to opt-out of any future tracking, a cookie will be set up in your browser to remember this choice for one year. I Agree, Deny
504