Https://dunjudge.me/analysis/problems/142/

https://dunjudge.me/analysis/problems/142/

So basically given a total change value and n coin values, you have to find the minimum number of coins required to make change. I think my solution fails when it is not possible to make change using those denominations (failing only 2 test cases). Can someone please take a look.

``````#include <iostream>
#include <algorithm>

#define FAST_IO ios_base::sync_with_stdio(false), cin.tie(nullptr)
//#define int long long
using namespace std;

signed main() {
FAST_IO;

int n, v; // coins, value needed
cin >> n >> v;
int coins[n + 1];
for (int i = 1; i <= n; i++) cin >> coins[i];
int dp[n + 1][v + 1];
for (int i = 0; i <= n; i++) dp[i][0] = 0;
for (int i = 0; i <= v; i++) dp[0][i] = i;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= v; j++) {
if (j < coins[i]) dp[i][j] = dp[i - 1][j];
else dp[i][j] = min(1 + dp[i][j - coins[i]], dp[i - 1][j]);
}
}
/*
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= v; j++) {
cout << dp[i][j] << " ";
}
cout << "\n";
}
*/
cout << dp[n][v];
return 0;
}
``````