# c ++ – Increase the efficiency of the recursive currency exchange algorithm

Problem: find the amount of money a stack of coins can make.

The vector, coins, contains the values ​​of the coins, and the corresponding vector, quantity, contains the number of coins that there are (let's say coins = {1} and quantity = {5}, this means that I have 5 cents).

Although I have solved this problem, I would like to make it more efficient. Could someone give me some advice?

Miscellaneous information: the variable ctq means coinsToQuantity. I solved this by systematically iterating all possible combinations of coins and putting their sum in a set. There has to be a better way to solve this problem, since this is supposed to be a dynamic programming problem and I solved this problem without any knowledge of what it is supposed to be.

``````#include
#include
#include
#include

int money (std :: map X)
{
int sum = 0;
for (auto it = x.begin (); it! = x.end (); ++ it)
{
sum + = it-> first * it-> second;
}
sum of return
}

int rec
(
std :: set and sums
std :: map ctq
std :: vector coins
std :: vector quantity,
int m
)
{
for (int k = quantity[m]; k> = 0; k--)
{
ctq[coins[coins[monedas[coins[m]]= k;
sums.insert (money (ctq));
yes (m! = 0)
{
rec (sums, ctq, coins, quantity, m-1);
}
}
return sums.size () - 1;
}

int possibleSums (std :: vector coins, std :: vector quantity)
{
std :: map ctq;
for (std :: size_t i = 0; i <coins.size (); i ++)
{
yes (ctq.find (coins[i]) == ctq.end ())
{
ctq.insert (std :: make_pair (coins[i], quantity[i]));
}
plus
{
ctq[coins[coins[monedas[coins[i]]+ = quantity[i];
}
}

std :: vector n_coins, n_quantity;
for (auto it = ctq.begin (); it! = ctq.end (); ++ it)
{
n_coins.push_back (it-> first);
n_quantity.push_back (it-> second);
}

std :: set sums
returns rec (sums, ctq, n_coins, n_quantity, n_coins.size () - 1);
}

int main ()
{
std :: vector coins = {10, 50, 100, 200};
std :: vector amount = {511, 22, 30, 50};
std :: cout << possibleSums (coins, quantity) << " n";
returns 0;
}
``````