Skip to content

Commit d6edfef

Browse files
committed
ADDED Coin Change - Minimum Coins.md
This file includes detailed explanation of the Coin Change (Minimum Coins) algorithm in Markdown format.
1 parent bbfce50 commit d6edfef

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# Coin Change – Minimum Coins
2+
3+
## Problem
4+
5+
Given a total amount `N` and a set of coins `S = {S₁, S₂, …, Sₘ}`, find the **minimum number of coins** required to make the amount `N`.
6+
If the amount cannot be made with the given coins, return `-1`.
7+
8+
---
9+
10+
## Idea
11+
12+
This is a variation of the **Unbounded Knapsack Problem**, where we have an infinite supply of each coin.
13+
The goal is to determine the smallest number of coins needed to sum to `N`.
14+
15+
We define an array `minCoin[]`, where each element `minCoin[i]` stores the minimum number of coins required to make a total of `i`.
16+
The base case is `minCoin[0] = 0`.
17+
18+
For every amount from `1` to `N`, and for each coin `c` in the set `S`, the relation is given by:
19+
20+
```
21+
minCoin[i] = min( minCoin[i], 1 + minCoin[i - c] )
22+
```
23+
24+
where `minCoin[i - c]` is the minimum number of coins required for the remaining amount.
25+
26+
---
27+
28+
## Implementation
29+
30+
A straightforward C++ implementation might look as follows:
31+
32+
```cpp
33+
#include <iostream>
34+
#include <limits.h>
35+
using namespace std;
36+
37+
int myMin(int a, int b) {
38+
return (a < b) ? a : b;
39+
}
40+
41+
int coinChange(int coins[], int amount) {
42+
if (amount < 1)
43+
return 0;
44+
45+
int *minCoin = new int[amount + 1];
46+
minCoin[0] = 0;
47+
48+
for (int i = 1; i <= amount; i++) {
49+
minCoin[i] = INT_MAX;
50+
for (int j = 0; j < 3; j++) {
51+
if (coins[j] <= i && minCoin[i - coins[j]] != INT_MAX) {
52+
minCoin[i] = myMin(minCoin[i], minCoin[i - coins[j]] + 1);
53+
}
54+
}
55+
}
56+
57+
int result = (minCoin[amount] == INT_MAX) ? -1 : minCoin[amount];
58+
delete[] minCoin;
59+
return result;
60+
}
61+
62+
int main() {
63+
int coins[3] = {1, 2, 5};
64+
int amount = 11;
65+
66+
int result = coinChange(coins, amount);
67+
cout << "The minimum number of coins used: " << result << endl;
68+
69+
return 0;
70+
}
71+
```
72+
73+
Note that `INT_MAX` is used to represent an unreachable amount during computation.
74+
75+
---
76+
77+
## Analysis
78+
79+
### Space Complexity
80+
81+
`O(N)` — only one array of size `amount + 1` is required.
82+
83+
### Time Complexity
84+
85+
`O(N × M)` — for each amount up to `N`, we iterate through all `M` available coin denominations.
86+
87+
---
88+
89+
## Walkthrough
90+
91+
Finding the minimum number of coins for
92+
`coins = [1, 2, 5]` and `amount = 11`:
93+
94+
| Amount | Coin Used | Remainder | Minimum Coins | Explanation |
95+
| :----: | :-------- | :-------- | :------------ | :--------------- |
96+
| 1 | 1 | 0 | 1 | Using one 1-coin |
97+
| 2 | 2 | 0 | 1 | Using one 2-coin |
98+
| 3 | 1 | 2 | 2 | 1 + 2 |
99+
| 4 | 2 | 2 | 2 | 2 + 2 |
100+
| 5 | 5 | 0 | 1 | Using one 5-coin |
101+
| 6 | 5 | 1 | 2 | 5 + 1 |
102+
| 7 | 5 | 2 | 2 | 5 + 2 |
103+
| 8 | 5 | 3 | 3 | 5 + 3 |
104+
| 9 | 5 | 4 | 3 | 5 + 4 |
105+
| 10 | 5 | 5 | 2 | 5 + 5 |
106+
| 11 | 5 | 6 | 3 | 5 + 5 + 1 |
107+
108+
The result is:
109+
110+
```
111+
minCoin[11] = 3
112+
```
113+
114+
Thus, the minimum number of coins required to make `11` is **3**, using coins `{5, 5, 1}`.
115+
116+
---
117+
118+
## Applications
119+
120+
* Currency exchange and change-making systems
121+
* Resource optimization problems
122+
* Variants of the subset-sum and knapsack problems
123+
* Payment gateway and vending machine algorithms
124+
125+
---
126+
127+
## Resources
128+
129+
* [GeeksforGeeks – Minimum Coins to Make Change](https://www.geeksforgeeks.org/find-minimum-number-of-coins-that-make-a-change/)
130+
* [YouTube – Coin Change (Minimum Coins) by Tushar Roy](https://youtu.be/NNcN5X1wsaw?si=UHED-voB4f3viOgT)
131+
132+
---
133+

0 commit comments

Comments
 (0)