You are viewing a single comment's thread. Return to all comments →
@ansonete: This is indeed the cleanest solution, and the logic is simple: for every m wrappers you get a candy, candy = wrapper, i.e. for every m wrappers you get 1 wrapper back. Thus, the actual "wrapper price" of one candy is (m - 1) rather than m. Now, dividing your original number of wrappers by the "reduced price" is unfair - you still have to pay "full price" (i.e. full m) for the very first "exchange candy". Thus, let us first put m aside to make sure we get our first free candy. So the amount of wrappers to divide by reduced price is (boughtCandy - m). Now, the total number of candies is:
boughtCandy = money / cost
total = boughtCandy + 1 + (boughtCandy - m) / (m - 1)
where 1 is the candy we bought for the m we put aside in the first place. This can be simplified as follows:
total = boughtCandy + (m - 1)/(m - 1) + (boughtCandy - m) / (m - 1)
= boughtCandy + (m - 1 + boughtCandy - m) / ( m - 1 )
= boughtCandy + (boughtCandy - 1)/(m - 1)
That was brilliant Anna, thanks ;)
thats wrong bud...
check for 2nd case of 1st test case
Awesome explanation. Thanks a lot.
My idea was same as you but i am getting runtime error for test case 5.could u help me?
for(int i =0;i
wrappers = n/c;
count = wrappers;
while (wrappers >= m)
wrappers = wrappers - m;
count = count + 1;
wrappers = wrappers + 1;
/*The main logic is above u can compare it easily */