You are on page 1of 4

Assignment No : 04

Name:-AMAAN KHAN PATHAN


Roll No:-167 Div:-B(B3)
Sub:-DAA

Problem statement: Suppose we have a set of coins of denominations Cn-1,


Cn-2,……C0 for some C>1. Coins of each denominations are available
unlimitedquantity. The problem is to make up an exact amount A using a
minimum totalnumber of coins. Device a solution for this problem and analyze
the time complexity of the algorithm.

Theory:- The coin changing problem is a well-known algorithmic problem in


computer science and mathematics. The problem can be stated as follows:
Given a set of coins with different denominations and a target value, find the
minimum number of coins required to make up the target value.
For example, suppose we have the following set of coins: {1, 5, 10, 25} and we
want to make change for a target value of 47 cents. One solution would be to use
one 25 cent coin, one 10 cent coin, and two 1 cent coins, for a total of four coins.

This problem can be solved using dynamic programming. We can define a table
where the rows represent the different denominations of coins, and the columns
represent the target values. We can then fill in the table row by row, computing the
minimum number of coins required for each value, using the values in the previous
row.

The base case is when the target value is 0, in which case we require 0 coins for any
denomination. For each subsequent value, we consider all possible coin
denominations and choose the one that results in the minimum number of coins.
We can express this recursively as follows:

Let C be the set of coin denominations


Let T be the target value
Let D be the minimum number of coins required to make up T using the coins in C
D(C,T) = 0, if T = 0
D(C,T) = infinity, if T < 0
D(C,T) = min(D(C,T-c) + 1), for all c in C

Once we have computed the table, we can read off the minimum number of coins
required for the target value by looking up the value in the table for the highest
denomination coin.
The time complexity :-

The time complexity of the coin changing problem using dynamic programming is
O(nm), where n is the number of coin denominations and m is the target value.

This is because we need to fill in a table of size n x m, and for each cell in the
table, we consider all possible coin denominations, which takes O(n) time.
Therefore, the total time complexity is O(nm).

However, we can optimize the algorithm to have a space complexity of O(m) by


using a 1D array instead of a 2D table. In this case, we would update the array
values as we iterate over the coin denominations and target values. This reduces
the space complexity, but the time complexity remains O(nm).

Code:
// A Naive recursive C++ program to find minimum of coins
// to make a given change V
#include <algorithm>
#include <chrono>
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
using namespace std::chrono;

// m is size of coins array (number of different coins)


int minCoins(int coins[], int m, int V)
{
// base case
if (V == 0) return 0;

// Initialize result
int res = INT_MAX;

// Try every coin that has smaller value than V


for (int i=0; i<m; i++)
{
if (coins[i] <= V)
{
int sub_res = minCoins(coins, m, V-coins[i]);

// Check for INT_MAX to avoid overflow and see if


// result can minimized
if (sub_res != INT_MAX && sub_res + 1 < res)
res = sub_res + 1;
}
}
return res;
}

// Driver program to test above function


int main()
{
//int coins[] = {1, 5, 6, 9};
int n;
cout<<"Enter no of coins: ";
cin>>n;
int coins[n];
cout<<"Enter Coin values: ";
for(int i=0;i<n;i++)
{
cin>>coins[i];
}
cout<<endl;

int m = sizeof(coins)/sizeof(coins[0]);
int V ;
cout<<endl<<"Enter Total sum: ";
cin>>V;
auto start = high_resolution_clock::now();
cout << "Minimum coins required is: "<< minCoins(coins, m, V);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);

cout << "\n\n\n\nTime taken by function: "<< duration.count() << "


microseconds" << endl;

return 0;
}
Output:-

You might also like