> For the complete documentation index, see [llms.txt](https://maksimdan.gitbook.io/interview-practice-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/recursion-and-dynamic-programming/coin-change-problem.md).

# Coin Change Problem

The change-making problem, also known as minimum coin change problem, addresses the question of finding the minimum number of coins (of certain denominations) that add up to a given amount of money.

**The Idea:** The central idea is to build a tree BFS style. The root is initially the amount of change, and the child of every node is defined as `parent - denomination`. Since our goal is not only to return the minimum amount of coins possible, but which coins these are, our structure is also going to carry parent nodes from so we backtrack from earlier nodes. I also sorted the denominations in acending order. That way, when the moment a denomination fails to fit the current amount of the change, I know I can ignore the remaining denominations.

![](/files/-LoJIiEgC9RiixMxrF5N)

**Complexity:** ?

```cpp
struct Coin_Node {
    Coin_Node(const int r)
        : remainder(r), parent(nullptr) {}

    Coin_Node(const int r, Coin_Node *p) 
        : remainder(r), parent(p) {}

    const int remainder;
    Coin_Node *parent;
    vector<Coin_Node*> children;
};

vector<int> reach_root_get_coins(Coin_Node* bottom) {
    Coin_Node *iter = bottom;
    if (!iter) return {};

    vector<int> change;
    int prev = iter->remainder;
    iter = iter->parent;

    while (iter) {
        int cur = iter->remainder;
        change.push_back(cur - prev);
        prev = cur;
        iter = iter->parent;
    }
    return change;
}

// this algorithm assumes that it is possible
// to return the requested change with the denominatins
// e.g. change can be completely factored by denominations
vector<int> min_coins_dp(vector<int> &denom, int change) {
    if (denom.empty() || change == 0) return {};

    sort(denom.begin(), denom.end());
    queue<Coin_Node*> q;
    Coin_Node *root = new Coin_Node(change);
    q.push(root);

    while (!q.empty()) {
        Coin_Node *parent = q.front();
        q.pop();

        if (parent->remainder == 0) 
            return reach_root_get_coins(parent);

        for (int type : denom) {
            int remainder = parent->remainder - type;
            if (remainder >= 0) {
                Coin_Node *next = new Coin_Node(remainder, parent);
                parent->children.push_back(next);
                q.push(next);
            }

            else break;
        }
    }
}
```

**Testing:**

```cpp
template<typename T>
inline void printnl(vector<T> &stuffs) {
    for (auto i : stuffs) cout << i << " "; cout << endl;
}

int main()
{
    /* DP */
    printnl(min_coins_dp(vector<int>({ 1,5,10,25 }), 45));                      // 25 10 10
    printnl(min_coins_dp(vector<int>({ 1,10,25 }), 30));                      // 10 10 10
    printnl(min_coins_dp(vector<int>({ 10,25 }), 60));                          // 25 25 10
    printnl(min_coins_dp(vector<int>({ 1,5,10,25,31,212,312,3,21,32 }), 45)); // 32 10 3
    printnl(min_coins_dp(vector<int>({ 1,5,10,25 }), 31));                      // 25 5 1
    printnl(min_coins_dp(vector<int>({ 1,5,10,25,31,212,312,3,21,32 }), 99)); // 32 32 32 3
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/recursion-and-dynamic-programming/coin-change-problem.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
