Please help getting wrong answer exchanging coins

Hey! you checking for n if it is divisible by 2, 3 or 4 is wrong. Its nowhere mentioned in the problem statement that the number n must be divisible by 2, 3 or 4. For example you have 1 coin with N = 5. You’ll recieve 3 coins with numbers 2, 1 and 1 respectively. Remove the check of number N and see if this resolves your problem.

    ll a, b, c;
    a = ec(n / 2, dp);
    b = ec(n / 3, dp);
    c = ec(n / 4, dp);
    dp[n] = max(n, a + b + c);

Also you must pass the dp (unordered_map) by reference or declare it globally otherwise you’ll get TLE as the unordered_map will pass by copy.

ll ec(ll n, unordered_map<ll, ll> &dp) {

or declare it globally like this :point_down:

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.