Skip to content

Commit bddc131

Browse files
authored
Add files via upload
1 parent a9d22d9 commit bddc131

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

Coin change.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
3+
#include <bits/stdc++.h>
4+
#define ll long long
5+
#define ull unsigned long long
6+
#define endl '\n'
7+
#define pb push_back
8+
#define mp make_pair
9+
#define sp(x) fixed<<setprecision(x)
10+
#define p_q priority_queue
11+
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
12+
using namespace std;
13+
14+
int main()
15+
{
16+
fast_io;
17+
18+
ll n,x;
19+
20+
cin>>x>>n;
21+
22+
vector <ll> coin(n);
23+
for(ll&i : coin)
24+
cin>>i;
25+
26+
vector <ll> dp(x+1,0);
27+
dp[0] = 1; // ways to achieve sum of 0,using the array = 1, i.e = taking a null subset.
28+
29+
for(ll i=0;i<n;i++)//we take each coin specifically, and do the needed calculations
30+
{
31+
for(ll j=0;j<=x;j++)//we build numbers from 0 to sum, using the taken coin i.e- coin[i]
32+
{
33+
if(j-coin[i] >= 0)
34+
{
35+
dp[j] += dp[j-coin[i]];
36+
37+
}
38+
}
39+
}
40+
cout<<dp[x]<<endl;
41+
42+
return 0;
43+
}

0 commit comments

Comments
 (0)