In this post, we will solve **Chocolate Feast HackerRank Solution**. This problem **(Chocolate Feast)** is a part of **HackerRank Problem Solving** series.

**Task**

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

He hasÂ **15**Â to spend, bars costÂ **3**, and he can turn inÂ **2**Â wrappers to receive another bar. Initially, he buysÂ **5**Â bars and hasÂ **5**Â wrappers after eating them. He turns inÂ **4**Â of them, leaving him withÂ **1**, forÂ **2**Â more bars. After eating those two, he hasÂ **3**Â wrappers, turns inÂ **2**Â leaving him withÂ **1**Â wrapper and his new bar. Once he eats that one, he hasÂ **2**Â wrappers and turns them in for another bar. After eating that one, he only hasÂ Â wrapper, and his feast ends. Overall, he has eatenÂ **5 + 2 + 1 + 1 = 9**Â bars.

**Function Description**

Complete the *chocolateFeast* function in the editor below.

chocolateFeast has the following parameter(s):

*int n:*Bobby’s initial amount of money*int c:*the cost of a chocolate bar*int m:*the number of wrappers he can turn in for a free bar

**Returns**

*int:*the number of chocolates Bobby can eat after taking full advantage of the promotion

**Note**: Little Bobby will always turn in his wrappers if he has enough to get a free chocolate.

**Input Format**

The first line contains an integer,Â * t*, the number of test cases to analyze.

Each of the nextÂ

*Â lines contains three space-separated integers:Â*

**t***,Â*

**n***, andÂ*

**c***. They represent money to spend, cost of a chocolate, and the number of wrappers he can turn in for a free chocolate.*

**m****Constraints**

**1 <=***t*<= 1000**2 <=***n*<= 10^{5}**1 <=***c*<=*n***2 <=***m*<=*n*

**Sample Input**

```
STDIN Function
----- --------
3 t = 3 (test cases)
10 2 5 n = 10, c = 2, m = 5 (first test case)
12 4 4 n = 12, c = 4, m = 4 (second test case)
6 2 2 n = 6, c = 2, m = 2 (third test case)
```

**Sample Output**

```
6
3
5
```

**Explanation**

Bobby makes the followingÂ **3**Â trips to the store:

- He spendsÂ
**10**Â onÂ**5**Â chocolates atÂ**2**Â apiece. He then eats them and exchanges allÂ**5**Â wrappers to getÂ**1**Â more. He eatsÂ**6**Â chocolates. - He spends hisÂ
**12**Â onÂ**3**Â chocolates atÂ**4**Â apiece. He has**Â 3**Â wrappers, but needsÂ**4**Â to trade for his next chocolate. He eatsÂ**3**Â chocolates. - He spendsÂ
**6**Â onÂ**3**Â chocolates atÂ**2**Â apiece. He then exchangesÂ**2**Â of theÂ**3**Â wrappers forÂ**1**Â additional piece. Next, he uses his third leftover chocolate wrapper from his initial purchase with the wrapper from his trade-in to do a second trade-in forÂ**1**Â more piece. At this point he hasÂ**1**Â wrapper left, which is not enough to perform another trade-in. He eatsÂ**5**Â chocolates.

**Solution – Chocolate Feast – HackerRank Solution**

**C++**

#include <iostream> using namespace std; int main() { int t, n, c, m; cin >> t; while (t--) { cin >> n >> c >> m; int answer = n / c; int wrappers = answer; // Spend wrappers for additional chocolates. while (wrappers >= m) { wrappers -= m; // Eat chocolate, produce new wrapper. answer++; wrappers++; } cout << answer << endl; } return 0; }

**Python**

import sys def wrappers(wr, m): res = 0 if wr//m > 0: res += wr//m + wrappers(wr//m + wr%m, m) return res def chocolateFeast(n, c, m): return n//c + wrappers(n//c, m) if __name__ == "__main__": t = int(input().strip()) for a0 in range(t): n, c, m = input().strip().split(' ') n, c, m = [int(n), int(c), int(m)] result = chocolateFeast(n, c, m) print(result)

**Java**

import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); scanner.nextLine(); while (scanner.hasNext()) { int money = scanner.nextInt(); int price = scanner.nextInt(); int bonus = scanner.nextInt(); int count = money / price; int wrappers = count; while (wrappers >= bonus) { int freebies = wrappers / bonus; count += freebies; wrappers = freebies + wrappers % bonus; } System.out.println(count); } } }

**Note:** This problem **(Chocolate Feast)** is generated by **HackerRank** but the solution is provided by **CodingBroz**. This tutorial is only for **Educational** and **Learning** purpose.