Professional Documents
Culture Documents
61 Practical 04
61 Practical 04
Theory:
We can solve this problem using dynamic programming by iteratively computing the
minimum number of coins required to make up each amount from 0 to A. At each
step, we consider all possible coin denominations and choose the one that
minimizes the total number of coins required.
Approach:
Dynamic programming offers an efficient solution to the coin change problem. The
key idea is to build up the solution incrementally, considering subproblems of
smaller amounts and gradually computing the minimum number of coins required
for larger amounts.
Example :
Suppose we have coin denominations [1, 3, 4] and we want to make up the amount
6. We can represent the solution as follows:
Amount: 0 1 2 3 4 5 6
Coins: 0 1 2 1 1 2 2
static final int[] deno = new int[] {1, 2, 5, 10, 20, 50, 100, 200, 500, 2000};
while(n > 0) {
int d = getMaxDeno(n);
n -= d;
res.add(d);
return res;
throw new Exception("Invalid amount " + n + " is lesser than " + deno[0] );
if (index < 0) {
index = - index - 2;
return deno[index];
}
CODE:
#include <iostream>
#include <vector>
using namespace std;
double moneyValues[] = {0.1, 0.5, 1, 2, 5, 10, 20, 50, 100, 200, 500};
int numberOfCoinsinCirculation = sizeof(moneyValues) / sizeof(moneyValues[0]);
int main() {
double numberToChange = 236.9;
changeMoney(numberToChange);
return 0;
}
OUTPUT:
TIME COMPLEXITY:
Considering the nested loop as the dominant factor:
• The outer loop runs A times.
• The inner loop runs n times.
So, the overall time complexity of the dynamic programming solution is O(A⋅n)