# Program for Chocolate and Wrapper Puzzle

Given following three values, the task is to find the total number of maximum chocolates you can eat.

1. money : Money you have to buy chocolates
2. price : Price of a chocolate
3. wrap : Number of wrappers to be returned for getting one extra chocolate.

It may be assumed that all given values are positive integers and greater than 1.

Examples:

```Input :   money = 16, price = 2, wrap = 2
Output :   15
Price of a chocolate is 2. You can buy 8 chocolates from
amount 16. You can return 8 wrappers back and get 4 more
chocolates. Then you can return 4 wrappers and get 2 more
chocolates. Finally you can return 2 wrappers to get 1
more chocolate.

Input :   money = 15, price = 1, wrap = 3
Output :   22
We buy and eat 15 chocolates
We return 15 wrappers and get 5 more chocolates.
We 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 + 1

Input :  money = 20, price = 3, wrap = 5
Output :   7
```

A naive method is to continuously count number of chocolates by returning wrappers until wrappers left didn’t become less than required to get a chocolate. Below is the implementation of this method.

## C++

 `// Recursive C++ program to find maximum ` `// number of chocolates ` `#include ` `using` `namespace` `std; ` ` `  `// Returns number of chocolates we can ` `// have from given number of chocolates ` `// and number of wrappers required to ` `// get a chocolate. ` `int` `countRec(``int` `choc, ``int` `wrap) ` `{ ` `    ``// If number of chocolates is less than ` `    ``// number of wrappers required. ` `    ``if` `(choc < wrap) ` `        ``return` `0; ` ` `  `    ``// We can immediatly get newChoc using ` `    ``// wrappers of choc. ` `    ``int` `newChoc = choc/wrap; ` ` `  `    ``// Now we have "newChoc + choc%wrap" wrappers. ` `    ``return` `newChoc + countRec(newChoc + choc%wrap, ` `                              ``wrap); ` `} ` ` `  `// Returns maximum number of chocolates we can eat ` `// with given money, price of chocolate and number ` `// of wrappers required to get a chocolate. ` `int` `countMaxChoco(``int` `money, ``int` `price, ``int` `wrap) ` `{ ` `    ``// We can directly buy below number of chocolates ` `    ``int` `choc = money/price; ` ` `  `    ``// countRec returns number of chocolates we can ` `    ``// have from given number of chocolates ` `    ``return` `choc + countRec(choc, wrap); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `money = 15 ; ``// total money ` `    ``int` `price = 1; ``// cost of each candy ` `    ``int` `wrap = 3 ; ``// no of wrappers needs to be ` `    ``// exchanged for one chocolate. ` ` `  `    ``cout << countMaxChoco(money, price, wrap); ` `    ``return` `0; ` `} `

## Java

 `// Recursive java program to find maximum ` `// number of chocolates ` ` `  `import` `java.io.*; ` ` `  `class` `GFG { ` ` `  `    ``// Returns number of chocolates we can ` `    ``// have from given number of chocolates ` `    ``// and number of wrappers required to ` `    ``// get a chocolate. ` `    ``static` `int` `countRec(``int` `choc, ``int` `wrap) ` `    ``{ ` `         `  `        ``// If number of chocolates is less than ` `        ``// number of wrappers required. ` `        ``if` `(choc < wrap) ` `            ``return` `0``; ` `     `  `        ``// We can immediatly get newChoc using ` `        ``// wrappers of choc. ` `        ``int` `newChoc = choc / wrap; ` `     `  `        ``// Now we have "newChoc + choc%wrap"  ` `        ``// wrappers. ` `        ``return` `newChoc + countRec(newChoc +  ` `                          ``choc % wrap, wrap); ` `    ``} ` `     `  `    ``// Returns maximum number of chocolates ` `    ``// we can eat with given money, price of ` `    ``// chocolate and number of wrappers  ` `    ``// required to get a chocolate. ` `    ``static` `int` `countMaxChoco(``int` `money,  ` `                          ``int` `price, ``int` `wrap) ` `    ``{ ` `         `  `        ``// We can directly buy below number of ` `        ``// chocolates ` `        ``int` `choc = money/price; ` `     `  `        ``// countRec returns number of chocolates ` `        ``// we can have from given number of  ` `        ``// chocolates ` `        ``return` `choc + countRec(choc, wrap); ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `main (String[] args) ` `    ``{ ` `        ``int` `money = ``15` `; ``// total money ` `        ``int` `price = ``1``; ``// cost of each candy ` `         `  `        ``// no of wrappers needs to be ` `        ``// exchanged for one chocolate. ` `        ``int` `wrap = ``3` `;  ` `        ``System.out.println(  ` `            ``countMaxChoco(money, price, wrap)); ` `    ``} ` `} ` ` `  `// This code is contributed by anuj_67. `

## Python3

 `# Recursive Python3 program to find ` `# maximum number of chocolates ` `import` `math ` ` `  `# Returns number of chocolates we can ` `# have from given number of chocolates ` `# and number of wrappers required to ` `# get a chocolate. ` `def` `countRec(choc, wrap): ` `     `  `    ``# If number of chocolates is less  ` `    ``# than number of wrappers required. ` `    ``if` `(choc < wrap): ` `        ``return` `0``; ` ` `  `    ``# We can immediatly get newChoc ` `    ``# using wrappers of choc. ` `    ``newChoc ``=` `choc ``/` `wrap; ` ` `  `    ``# Now we have "newChoc + choc%wrap" wrappers. ` `    ``return` `newChoc ``+` `countRec(newChoc ``+` `choc ``%`  `                                  ``wrap, wrap); ` ` `  `# Returns maximum number of chocolates  ` `# we can eat with given money, price  ` `# of chocolate and number of wrappers  ` `# required to get a chocolate. ` `def` `countMaxChoco(money, price, wrap): ` `     `  `    ``# We can directly buy below  ` `    ``# number of chocolates ` `    ``choc ``=` `money ``/` `price; ` ` `  `    ``# countRec returns number ` `    ``# of chocolates we can ` `    ``# have from given number  ` `    ``# of chocolates ` `    ``return` `math.floor(choc ``+` `countRec(choc, wrap)); ` ` `  `# Driver code ` ` `  `# total money ` `money ``=` `15``; ` `     `  `# cost of each candy ` `price ``=` `1``;  ` `     `  `# no of wrappers needs to be ` `wrap ``=` `3` `;  ` `     `  `# exchanged for one chocolate. ` `print``(countMaxChoco(money, price, wrap)); ` ` `  `# This code is contributed by mits `

## C#

 `// Recursive C# program to find maximum ` `// number of chocolates ` ` `  `using` `System; ` ` `  `class` `GFG { ` ` `  `    ``// Returns number of chocolates we can ` `    ``// have from given number of chocolates ` `    ``// and number of wrappers required to ` `    ``// get a chocolate. ` `    ``static` `int` `countRec(``int` `choc, ``int` `wrap) ` `    ``{ ` `         `  `        ``// If number of chocolates is less than ` `        ``// number of wrappers required. ` `        ``if` `(choc < wrap) ` `            ``return` `0; ` `     `  `        ``// We can immediatly get newChoc using ` `        ``// wrappers of choc. ` `        ``int` `newChoc = choc / wrap; ` `     `  `        ``// Now we have "newChoc + choc%wrap"  ` `        ``// wrappers. ` `        ``return` `newChoc + countRec(newChoc +  ` `                        ``choc % wrap, wrap); ` `    ``} ` `     `  `    ``// Returns maximum number of chocolates ` `    ``// we can eat with given money, price of ` `    ``// chocolate and number of wrappers  ` `    ``// required to get a chocolate. ` `    ``static` `int` `countMaxChoco(``int` `money,  ` `                        ``int` `price, ``int` `wrap) ` `    ``{ ` `         `  `        ``// We can directly buy below number of ` `        ``// chocolates ` `        ``int` `choc = money/price; ` `     `  `        ``// countRec returns number of chocolates ` `        ``// we can have from given number of  ` `        ``// chocolates ` `        ``return` `choc + countRec(choc, wrap); ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main () ` `    ``{ ` `        ``int` `money = 15 ; ``// total money ` `        ``int` `price = 1; ``// cost of each candy ` `         `  `        ``// no of wrappers needs to be ` `        ``// exchanged for one chocolate. ` `        ``int` `wrap = 3 ;  ` `        ``Console.WriteLine(  ` `            ``countMaxChoco(money, price, wrap)); ` `    ``} ` `} ` ` `  `// This code is contributed by anuj_67. `

## PHP

 ` `

Output :

`22`

An 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 / wrap

then apply below formula
choc += (choc - 1)/(wrap - 1)
```

In above naive implementation, we noticed that after finding initial number of chocolates, we recursively divide the number of chocolates with the number of wrappers required. until we left with 1 chocolate or wrapper.
We are recomputing the values i.e. ((choc/wrap + choc%wrap)/wrap until we get 1.
It is observed that, we can get the result by just reducing the values of chocolates and wrappers by 1 and then divide them to get the result (choc-1)/(wrap-1)

## C++

 `// Efficient C++ program to find maximum ` `// number of chocolates ` `#include ` `using` `namespace` `std; ` ` `  `// Returns maximum number of chocolates we can eat ` `// with given money, price of chocolate and number ` `// of wrapprices required to get a chocolate. ` `int` `countMaxChoco(``int` `money, ``int` `price, ``int` `wrap) ` `{ ` `    ``// Corner case ` `    ``if` `(money < price) ` `       ``return` `0; ` ` `  `    ``// First find number of chocolates that ` `    ``// can be purchased with the given amount ` `    ``int` `choc = money / price; ` ` `  `    ``// Now just add number of chocolates with the ` `    ``// chocolates gained by wrapprices ` `    ``choc = choc + (choc - 1) / (wrap - 1); ` `    ``return` `choc; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `money = 15 ; ``// total money ` `    ``int` `price = 1; ``// cost of each candy ` `    ``int` `wrap = 3 ; ``// no of wrappers needs to be ` `    ``// exchanged for one chocolate. ` ` `  `    ``cout << countMaxChoco(money, price, wrap); ` `    ``return` `0; ` `} `

## Java

 `// Efficient Java program to find maximum ` `// number of chocolates ` `import` `java.io.*; ` ` `  `class` `GFG { ` ` `  `    ``// Returns maximum number of chocolates  ` `    ``// we can eat with given money, price  ` `    ``// of chocolate and number of wrapprices ` `    ``// required to get a chocolate. ` `    ``static` `int` `countMaxChoco(``int` `money,  ` `                        ``int` `price, ``int` `wrap) ` `    ``{ ` `         `  `        ``// Corner case ` `        ``if` `(money < price) ` `            ``return` `0``; ` `     `  `        ``// First find number of chocolates  ` `        ``// that can be purchased with the ` `        ``// given amount ` `        ``int` `choc = money / price; ` `     `  `        ``// Now just add number of chocolates ` `        ``// with the chocolates gained by ` `        ``// wrapprices ` `        ``choc = choc + (choc - ``1``) / (wrap - ``1``); ` `        ``return` `choc; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `main (String[] args)  ` `    ``{ ` `         `  `        ``// total money ` `        ``int` `money = ``15``; ` `         `  `        ``// cost of each candy ` `        ``int` `price = ``1``;  ` `         `  `        ``// no of wrappers needs to be ` `        ``int` `wrap = ``3` `;  ` `         `  `        ``// exchanged for one chocolate. ` `        ``System.out.println( ` `           ``countMaxChoco(money, price, wrap)); ` `    ``} ` `} ` ` `  `// This code is contributed by anuj_67. `

## Python3

 `# Efficient Python3 program to find  ` `# maximum number of chocolates  ` ` `  `# Returns maximum number of  ` `# chocolates we can eat with  ` `# given money, price of chocolate ` `# and number of wrapprices ` `# required to get a chocolate.  ` `def` `countMaxChoco(money, price, wrap) : ` ` `  `    ``# Corner case  ` `    ``if` `(money < price) : ` `        ``return` `0` ` `  `    ``# First find number of chocolates  ` `    ``# that can be purchased with the ` `    ``# given amount  ` `    ``choc ``=` `int``(money ``/` `price)  ` ` `  `    ``# Now just add number of ` `    ``# chocolates with the chocolates  ` `    ``# gained by wrapprices  ` `    ``choc ``=` `choc ``+` `(choc ``-` `1``) ``/` `(wrap ``-` `1``)  ` `    ``return` `int``(choc ) ` ` `  `# Driver code  ` `money ``=` `15` `# total money  ` `price ``=` `1` `# cost of each candy  ` `wrap ``=` `3` `# no of wrappers needs to be  ` `         ``# exchanged for one chocolate.  ` ` `  `print``(countMaxChoco(money, price, wrap)) ` ` `  `# This code is contributed by Smitha `

## C#

 `// Efficient C# program to find maximum ` `// number of chocolates ` `using` `System; ` ` `  `class` `GFG { ` ` `  `    ``// Returns maximum number of chocolates  ` `    ``// we can eat with given money, price  ` `    ``// of chocolate and number of wrapprices ` `    ``// required to get a chocolate. ` `    ``static` `int` `countMaxChoco(``int` `money,  ` `                        ``int` `price, ``int` `wrap) ` `    ``{ ` `         `  `        ``// Corner case ` `        ``if` `(money < price) ` `            ``return` `0; ` `     `  `        ``// First find number of chocolates  ` `        ``// that can be purchased with the ` `        ``// given amount ` `        ``int` `choc = money / price; ` `     `  `        ``// Now just add number of chocolates ` `        ``// with the chocolates gained by ` `        ``// wrapprices ` `        ``choc = choc + (choc - 1) / (wrap - 1); ` `        ``return` `choc; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main ()  ` `    ``{ ` `         `  `        ``// total money ` `        ``int` `money = 15; ` `         `  `        ``// cost of each candy ` `        ``int` `price = 1;  ` `         `  `        ``// no of wrappers needs to be ` `        ``int` `wrap = 3 ;  ` `         `  `        ``// exchanged for one chocolate. ` `        ``Console.WriteLine( ` `        ``countMaxChoco(money, price, wrap)); ` `    ``} ` `} ` ` `  `// This code is contributed by anuj_67. `

## PHP

 ` `

Output :

`22`

This article is contributed by Sahil Chhabra (akku). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

4

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.