Getting a wrong answer in Bytelandian gold coins problem

test_case = 10
arr = []
for i in range(0,test_case):
coin = int(input())
dollars = int(coin/2) + int(coin/3) + int(coin/4)
if(dollars<coin):
dollars = coin
arr.append(dollars)
for i in range(0,test_case):
print(arr[i])

this is my code for the Bytelandian gold coins question with reference code COINS. Would you please solve my problem that on submission of this code to the problem i’m getting Wrong answer as feedback.

You need to consider converting the coins that result from the first conversion, then possibly converting those, etc.


Hint: It never hurts to convert a coin of value 12 or more. It never benefits to convert a coin less than 12.

Second hint: You’ll need to retain intermediate conversion results, to avoid calculating them over & over again, if you want to complete the process within the time limit.

You are dividing only given input into three parts and then checking it to be greater than given input.
What you have to do is check for divided parts too if they can be divided into more coins resulting more value than it is now.
A recursive solution is CodeChef: Practical coding for everyone

3 Likes

I’m getting wrong answer for my code.
Can anyone please tell me whats wrong in my code??
#include
#include<unordered_map>
#include
#include
using namespace std;
#define ll long long
unordered_map<ll,ll> mp;
ll solve(ll n){
if(n <= 2) return n;
if(mp.find(n) == mp.end()){
mp[n] = max(n,solve(n/2) + solve(n/3) + solve(n/4));
}
return mp[n];
}
int main() {
// your code goes here
int t;
ll n;
cin>>t;
while(t–){
cin>>n;
mp.clear();
cout<<solve(n)<<endl;
}
return 0;
}

your input format is incorrect see input format carefully