Bytelandian Gold Coins Problem – Dynamic Programming Solutions

This is a C++ Program that Solves the Bytelandian Gold Coins Problem using Dynamic Programming technique.

Problem Description

Each Bytelandian gold coin has an integer number written on it. A coin n can be exchanged in a bank into three coins: n/2, n/3 and n/4. But these numbers are all rounded down (the banks have to make a profit).

You can also sell Bytelandian coins for American dollars. The exchange rate is 1:1. But you can not buy Bytelandian coins.

You have one gold coin of value n, 0 <= n <= 1000000000. What is the maximum amount of American dollars you can get for it?

Problem Solution

This is a Dynamic programming problem. We can observe that for amount upto 8, we can’t get more money by dividing (into n/2, n/3 and n/4). We will create an array to memoize the values. For any value, min. amount we can get out of it=n. We will compare this value with the value we get after dividing n and select the bigger value.

Expected Input and Output
advertisement
advertisement

Case-1:

 
Amount of Bytelandian coin=8
Maximum amount of american dollars which can be made out of it=8

Case-2:

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
 
Amount of Bytelandian coin=25
Maximum amount of american dollars which can be made out of it=27

Case-3:

 
Amount of Bytelandian coin=520
Maximum amount of american dollars which can be made out of it=576
Program/Source Code

Here is source code of the C++ Program to Solve the Bytelandian gold coins problem. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

advertisement
  1.  
  2. //bytelandian gold coins
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5.  
  6. long long int calculate(vector<long long int> &dp, long long int n)
  7. {
  8.     if(n<10)
  9.         return n;
  10.  
  11.     if(n<1000000)
  12.     {
  13.         if(dp[n])
  14.             return dp[n];
  15.  
  16.         else
  17.         {
  18.             //now, calculate
  19.             long long int x=calculate(dp, n/2)+calculate(dp, n/3)+calculate(dp, n/4);
  20.             dp[n]=max(n,x);
  21.  
  22.             return dp[n];
  23.         }
  24.     }
  25.  
  26.     long long int x=calculate(dp, n/2)+calculate(dp, n/3)+calculate(dp, n/4);
  27.  
  28.     return max(n,x);
  29. }
  30.  
  31. int main()
  32. {
  33.     long long int i,j,k,n;
  34.     long long int x,y,z;
  35.     int t;
  36.  
  37.     vector<long long int> dp(1000000,0);
  38.  
  39.     cout<<"Enter the amount of bytelandian coin you have ?"<<endl;
  40.     cin>>n;
  41.     cout<<"Maximum amount of american dollars you can make from this is "<<endl;
  42.     cout<<calculate(dp,n)<<endl;
  43.  
  44.     return 0;
  45. }
Program Explanation

In the main function, we take the amount of the bytelandian coin we have. Then, we invoke the calculate function. the calculate function will compute the maximum amount of american dollars we can make out of this and return this value. The output will be displayed on the standard output.

Runtime Test Cases
advertisement
 
Case-1:
$ g++ bytelandian_gold_coins.cpp
$ ./a.out
Enter the amount of bytelandian coin you have ?
8 
Maximum amount of american dollars you can make from this is 
8
 
Case-2:
$ ./a.out
Enter the amount of bytelandian coin you have ?
25
Maximum amount of american dollars you can make from this is 
27
 
Case-3:
$ ./a.out
Enter the amount of bytelandian coin you have ?
520
Maximum amount of american dollars you can make from this is 
689

Sanfoundry Global Education & Learning Series – Dynamic Programming Problems.
To practice all Dynamic Programming Problems, here is complete set of 100+ Problems and Solutions.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.