Exchanging coins


doesn’t gives correct output

hello @ajayrajsingh_817
wait i m checking

u havent intialised ur dp with -1.
pls initialise it.


still not getting correct output

check this->https://ide.codingblocks.com/s/277685

first intialsie ur dp array and then after call ur function.

but the constraints of n is very large how to handle such large constraints with dp array

run time error occurs

it not possible.
use map .in place of dp array.

run time error in this code

,… … … …

how sir ???/
plz make changes in the code n comment down ur logic

logic will remain same,
just change long long dp[n];
with
map<long long,long long> dp;

and replace if(dp[n]!=-1) with if(dp.count(n)!=0)

rest evrthing is same

plzz explain the logic behind it in large n

did nt get ur question.
i havent changed the logic.
have just replaced array with map.
thats it.

that the my question why it not working with dp array in case of dp array it shows run time error and in map it works

value of n can be upto 10^9.

and we cannot declare array of size 10^9 becuase it will take too much memory.

map will not take much memory because it creates key at run time .
so it will consume only required amount of space (numbe of distinct dp state).