We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I'm confused... The sample states that we get chocolates per trip, and that means we get wrappers per trip. However, this also means that we get an extra chocolates per trip, right?
Given 10 dollars with chocolates for 2 dollars each, you can get 5 chocolates. That's 5 wrappers. To get a free chocolate, you need 5 wrappers, which we have, so now we have 6 chocolates. This means we now have 1 extra wrapper (total: 6, extra: 1)
Next, we have 12 dollars with chocolates for 4 dollars each. That means we can purchase 3 chocolates, meaning we now have 3 wrappers, plus the extra 1 from the last purchase. We need 4 wrappers to get a free chocolate, which we have, so we get a free chocolate and consequently have 1 extra wrapper (total: 4, extra: 1)
Finally, we have 6 dollars to spend on chocolates for 2 dollars each. This means we can buy 3 chocolates, giving us 3 new wrappers, plus the extra 1 from the last purchase. We need 2 wrappers to obtain a free chocolate, and we actually have 4 wrappers, meaning we get 2 free chocolates and 2 more wrappers as a consequence. We can obtain 1 chocolate more for free as a result, leading to 1 final chocolate and 1 extra wrapper (total: 6, extra: 1)
So how is the sample output 6, 3, 5 when we can clearly obtain 6, 4, 6?
What am I missing?
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Chocolate Feast
You are viewing a single comment's thread. Return to all comments →
I'm confused... The sample states that we get chocolates per trip, and that means we get wrappers per trip. However, this also means that we get an extra chocolates per trip, right?
Given 10 dollars with chocolates for 2 dollars each, you can get 5 chocolates. That's 5 wrappers. To get a free chocolate, you need 5 wrappers, which we have, so now we have 6 chocolates. This means we now have 1 extra wrapper (total: 6, extra: 1)
Next, we have 12 dollars with chocolates for 4 dollars each. That means we can purchase 3 chocolates, meaning we now have 3 wrappers, plus the extra 1 from the last purchase. We need 4 wrappers to get a free chocolate, which we have, so we get a free chocolate and consequently have 1 extra wrapper (total: 4, extra: 1)
Finally, we have 6 dollars to spend on chocolates for 2 dollars each. This means we can buy 3 chocolates, giving us 3 new wrappers, plus the extra 1 from the last purchase. We need 2 wrappers to obtain a free chocolate, and we actually have 4 wrappers, meaning we get 2 free chocolates and 2 more wrappers as a consequence. We can obtain 1 chocolate more for free as a result, leading to 1 final chocolate and 1 extra wrapper (total: 6, extra: 1)
So how is the sample output 6, 3, 5 when we can clearly obtain 6, 4, 6?
What am I missing?