can someone tell why the following code for coin combinations 1 question from cses problemset is getting time limit exceeded
#include <bits/stdc++.h>
#define int long long
using namespace std;
int MOD=1e9+7;
signed main()
{
int n,x;
cin>>n>>x;
int nums[n];
for(int i=0;i<n;i++){
cin>>nums[i];
}
int dp[x+1]={0};
dp[0]=1;
for(int i=1;i<=x;i++){
for(int q:nums){
if(i-q>=0){
dp[i]+=dp[i-q];
dp[i]%=MOD;
}
}
}
cout<<dp[x]<<endl;
}
I tried changing the variable MOD from int MOD to const int MOD and it worked. i don’t know what really changed in the code with it as i’m not changing the value of MOD anywhere.
New contributor
Aryan phad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.