Chocolate Feast Problem

By | June 10, 2021

Little Bobby loves chocolate. He frequently goes to his favorite 5 & 10 store, Penny Auntie, to buy them. They are having a promotion at Penny Auntie. If Bobby saves enough wrappers, he can turn them in for a free chocolate.

Example

n = 15

c = 3

m = 2

Answer 5 + 2 + 1 + 1 = 9

Solution

[java]
public static int chocolateFeast(int total_money, int cost, int free_wrapper) {

int wrappers = total_money / cost;
int total_chocolates = wrappers;

while(wrappers >= free_wrapper){
int new_wrappers = wrappers/free_wrapper;
int remaining_wrappers = wrappers % free_wrapper;
total_chocolates += new_wrappers;
wrappers = new_wrappers + remaining_wrappers;
}

return total_chocolates;
}
[/java]

HackerRank Problem Link

Leave a Reply

Your email address will not be published. Required fields are marked *