> For the complete documentation index, see [llms.txt](https://algorithm.prettylog.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://algorithm.prettylog.com/algorithm-problems/algoexpert/medium/min-number-of-coins-for-change.md).

# Min Number Of Coins For Change

![](/files/Yx0tqx83Yf0wc9DAcAsj)

* n, n

```jsx
function minNumberOfCoinsForChange(n, denoms) {
  denoms.sort((a, b) => a - b);
  const coins = new Array(n + 1).fill(Infinity);
  coins[0]= 0;

  for (const denom of denoms) {
    for (let i = 1; i < n + 1; i++) {
      if (denom > i) continue;
      coins[i] = Math.min(coins[i], coins[i - denom] + 1);
    }
  }

  return coins[n] === Infinity ? -1 : coins[n];
}

// Do not edit the line below.
exports.minNumberOfCoinsForChange = minNumberOfCoinsForChange;
```
