One of the test cases says timelimit-exachanging coin problem

#include
using namespace std;

int main() {

long int n;
cin>>n;
long int dp[n+1];
dp[0]=0;
dp[1]=1;
for(int i=2;i<=n;i++){
    dp[i]=max((int)i,(int)dp[i/2]+(int)dp[i/3]+(int)dp[i/4]);
}
cout<<dp[n];

return 0;

}

Hello @Marr_iss,
I have modified your code:

#include<bits/stdc++.h>
using namespace std;

int main() {

long int n;
cin>>n;
long int dp[n+1];
dp[0]=0;
dp[1]=1;
for(long int i=2;i<=n;i++){ // 1.
dp[i]=max(i,dp[(long)(i/2)]+dp[(long)(i/3)]+dp[(long)(i/4)]); //2.
}
cout<<dp[n];

return 0;
}

There are two problems with your code:

  1. i has int datatype, but the max function compares the values of same type.
  2. You were accessing the index n/2,n/3 and n/4, though these can also be float values.

Hope, this would help.
Give a like, if you are satisfied.

1 Like