0

Possible Duplicate:
Algorithm to find which numbers from a list of size n sum to another number

What is a good algorithm for deciding whether a passed in amount can be built additively from a set of numbers? In my case, I am determining whether a certain currency amount (such as $40) can be met by adding up some combination of a set of bills (such as $5, $10 and $20 bills). That is a simple example, but the algorithm needs to work for the generic case where the bill set can differ over time (due to running out of a bill) or due to bill denominations differing by currency. The problem would apply to a foreign exchange teller at an airport.

So $50 can be met with a set of ($20 and $30), but cannot be met with a set of ($20 and $40).

In addition. If the amount cannot be met with the bill denominations available, how do you determine the closest amounts above and below which can be met?

OmG
  • 18,337
  • 10
  • 57
  • 90
Stimy
  • 1,491
  • 3
  • 15
  • 36

5 Answers5

1

This seems closely related to the Subset Sum Problem, which is NP-Complete in general.

0

Start with the largest bills and work down. With each denomination, start with the largest number of those bills and work down. You might need fewer of a large denomination because you need multiple smaller ones to hit a value on the head.

Jason Cohen
  • 81,399
  • 26
  • 107
  • 114
0

Sum = 100
Bills = (40,30,20,10)

Number of 40's = 100 / 40 = 2 Remainder = 100 % 40 = 20

Number of 30's = 20 / 30 = 0 Remainder = 20 % 30 = 20

Number of 20's = 20 / 20 = 1 Remainder = 20 % 20 = 0

As soon as remainder = 0 you can stop.

If you run out of bills then you can't make it up and need to go to the second part which is how close can you get. This is a minimization problem that can be solved with Linear algebra methods (I'm a little rusty on that)

Restore the Data Dumps
  • 38,967
  • 12
  • 96
  • 122
0

You know - You asked this exact same question twice now.

What is a good non-recursive algorithm for deciding whether a passed in amount can be built additively from a set of numbers?

Community
  • 1
  • 1
seanyboy
  • 5,623
  • 7
  • 43
  • 56
  • It's not the exact same question. The previous question asked for a non-recursive algorithm for which no good answer was provided. This question removed the non-recursive constraint in order to, hopefully, get some better answers. – Stimy Sep 16 '08 at 18:03