Maximum Chocolates — Puzzle Asked In Interviews

Placewit
2 min readMar 19, 2022

Question:

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?

Solution:

Answer: 22
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)

Code:

Thanks for Reading

Placewit grows the best engineers by providing an interactive classroom experience and by helping them develop their skills and get placed in amazing companies.

Learn more at Placewit. Follow us on Instagram and Facebook for daily learning.

--

--