Maximum Chocolates — Puzzle Asked In Interviews
You have 15 Rs with you. You go to a shop and shopkeeper tells you price as 1 Rs per chocolate. He also tells you that you can get a chocolate in return of 3 wrappers. How many maximum chocolates you can eat?
Buy and eat 15 chocolates
Return 15 wrappers and get 5 more chocolates.
Return 3 wrappers, get 1 chocolate and eat it (keep 2 wrappers)
Now we have 3 wrappers. Return 3 and get 1 more chocolate.So total chocolates = 15 + 5 + 1 + 1An efficient solution is to use a direct formula to find the number of chocolates.Find initial number of chocolates by
dividing the amount with per piece cost.
i.e. choc = money / price
then apply below formula
choc += (choc - 1)/(wrap - 1)