Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

PRACTICAL - 04

NAME : Vedant Gawade


PRN : 202101040090 ROLL NO : 61
BATCH : A-3
SUBJECT : DAA[LAB]
PROBLEM STATEMENT :
Suppose we have a set of coins of denominations Cn-1, Cn-2,. . . . . . C0 for some C>1. Coins of
each denomination are available in unlimited quantity. The problem is to make up an exact amount A using a
minimum total number of coins. Device a solution for this problem and analyze the time complexity of the
algorithm.

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

So, to make up the amount 6, we need a minimum of 2 coins.


Algorithm:

1. Initialize a 1D array dp[] of size (A + 1) to store the minimum


number of coins required to make up each amount from 0 to A.
2. Initialize dp[0] to 0, as it takes 0 coins to make up the amount 0.
3. Iterate through each coin denomination:
For each denomination coin, iterate through amounts from
coin to A:
Update dp[amount] by taking the minimum of its
current value and dp[amount - coin] + 1.
DAA PRACTICAL NO 4
Here, dp[amount - coin] + 1 represents the minimum
number of coins required to make up the remaining
amount after taking one coin of denomination coin.
4. After iterating through all denominations and amounts, dp[A] will
contain the minimum number of coins required to make up the
amount A.
5. Return dp[A] as the solution.

Time Complexity Analysis:

The time complexity of the dynamic programming solution for the


Coin Change Problem is O(A * n), where:
A is the target amount.
n is the number of coin denominations.
This is because we iterate through each amount from 0 to A for
each coin denomination.
As a result, the time complexity is proportional to the product of
the target amount and the number of coin denominations.
Pseudocode:
class Solution{

static final int[] deno = new int[] {1, 2, 5, 10, 20, 50, 100, 200, 500, 2000};

static List<Integer> minPartition(int n) {

List<Integer> res = new ArrayList<>();

while(n > 0) {

int d = getMaxDeno(n);

n -= d;

res.add(d);

return res;

static int getMaxDeno(int n) {

if (n < deno[0]) // validity check

throw new Exception("Invalid amount " + n + " is lesser than " + deno[0] );

int index = Arrays.binarySearch(deno, n);

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]);

void changeMoney(double sum) {


double initialSum = sum;
vector<double> coins;
for (int i = numberOfCoinsinCirculation - 1; i >= 0; i--) {
while (sum >= moneyValues[i]) {
sum -= moneyValues[i];
coins.push_back(moneyValues[i]);
}
}

// Print the result


cout << "The coins/banknotes needed to sum up " << initialSum << " is:\n\t";
int counter = 0;
int coinsCounter = 0;
int banknotesCounter = 0;
for (int i = 0; i < coins.size(); i++) {
double currentAmount = coins[i];
cout << currentAmount << "\t";
counter++;
if (currentAmount >= 1.0) {
banknotesCounter++;
} else {
coinsCounter++;
}
}
cout << endl;
cout << "The number of coins/banknotes is: " << counter << endl;
if (coinsCounter == 1) {
cout << coinsCounter << " Coin" << endl;
} else {
cout << coinsCounter << " Coins" << endl;
}
if (banknotesCounter == 1) {
cout << banknotesCounter << " Banknote" << endl;
} else {
cout << banknotesCounter << " Banknotes" << endl;
}
}

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)

You might also like