ADA14
ADA14
TECHNOLOGY
Information
Experiment 14
67
MD SARFUZZAMAN GAUHAR[92201704016] 4ED1( Page
FACULTY OF
TECHNOLOGY
Information
Algorithm:
Step 1: Define a function minCoins(amount, coins) that íetuíns the minimum numbeí of
coins needed to make amount using the coins in the list coins.
Step 2: Cíeate a list dp of length amount + 1, to stoíe the solutions of subpíoblems. Initialize
dp[0] to zeío, as no coins aíe needed to make zeío, and the íest of dp to infinity, as we
Code:
#include<iostream>
using namespace std;
68
MD SARFUZZAMAN GAUHAR[92201704016] 4ED1( Page
FACULTY OF
TECHNOLOGY
Information
}
}
return res;
}
int main() {
int coins[] = {1, 2, 4, 5};
int m = sizeof(coins) / sizeof(coins[0]);
int v = 17;
cout << "Minimum coins required is: " <<
minCoins(coins, m, v);
return 0;
}
OUTPUT
Minimum Coins required is: 4
Time Complexity:
Best case:
Worst case:
Average case:
69
MD SARFUZZAMAN GAUHAR[92201704016] 4ED1( Page