Exchanging coins problem


i am getting run error

HI Varun ,
There are a few issues with your code.
First being that your base case is wrong. It should be if(n<12) return n;
Otherwise your program never stops at all for most cases.

Second issue being we will divide the coin in three parts even if it is not divisible by 12.
That is , say we have a coin of 15. We can still divide it into coins of 7(15/2) , 5(15/3) and 3(15/4) . In such a case we will consider the floor value after the division.

Finally the biggest issue which gave you the run-error , it is mentioned in the question that n can be as large as 1000000000 i.e. 10^9 . You have made a DP array of size 10^6. This gave you the error. Now it is not really your fault as creating an array of 10^9 is simply not allowed. You need to find a way around this problem. I’ll give you a hint - Take help of the recursion that you are already using in your Top Down Approach.

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.