Hello Programmers, In this post, you will learn how to solve** HackerRank Chocolate Feast** **Solution**. This problem is a part of the ** HackerRank Algorithms** Series.

One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions. We are going to solve the HackerRank Algorithms problems using C, CPP, JAVA, PYTHON, JavaScript & SCALA Programming Languages.

You can practice and submit all HackerRank problem solutions in one place. Find a solution for other domains and Sub-domain. I.e. Hacker Rank solution for HackerRank C Programming, HackerRank C++ Programming, HackerRank Java Programming, HackerRank Python Programming, HackerRank Linux Shell, HackerRank SQL Programming, and HackerRank 10 days of Javascript.

As you already know that this site does not contain only the Hacker Rank solutions here, you can also find the solution for other problems. I.e. Web Technology, Data Structures, RDBMS Programs, Java Programs Solutions, Fiverr Skills Test answers, Google Course Answers, Linkedin Assessment, and Coursera Quiz Answers.

## HackerRank Chocolate Feast

### 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 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.

## HackerRank Chocolate Feast Solution

### Chocolate Feast Solution in C

#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { int t, n, c, m; scanf("%d", &t); while ( t-- ) { scanf("%d%d%d",&n,&c,&m); n/=c; int k = n; while (n >= m ) { k++,n-=(m-1); } printf("%d\n",k); } return 0; }

### Chocolate Feast Solution in Cpp

#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int t,n,c,m; cin>>t; while(t--){ cin>>n>>c>>m; int ans=0; ans+=n/c; int x=n/c; while(x>=m){ ans+=x/m; x=x/m+x%m; } cout<<ans<<endl; } return 0; }

### Chocolate Feast Solution in Java

import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for(int i = 0; i < t; i++){ System.out.println(f(in.nextInt(), in.nextInt(), in.nextInt())); } } private static long f(int n, int a, int b){ return n/a + g(n/a, b); } private static long g(int a, int b){ if(a < b) return 0; else return a/b + g(a-a/b*b + a/b, b); } }

### Chocolate Feast Solution in Python

T = int(raw_input()) for t in xrange(T): N, C, M = [int(e) for e in raw_input().strip().split()] chocolates = 0 wrappers = 0 while N >= C: nc = N // C N -= (nc * C) chocolates += nc wrappers += nc while wrappers >= M: wr = wrappers // M wrappers -= M * wr wrappers += wr chocolates += wr print chocolates

### Chocolate Feast Solution using JavaScript

function processData(input) { input = input.trim(); var lines = input.split("\n"); var T = parseInt(lines[0]); for(var i = 1; i <= T;i++){ var data = lines[i].split(" "); var N = parseInt(data[0]); var M = parseInt(data[2]); var C = parseInt(data[1]); var total = Math.floor(N/C); var add = Math.floor(total/M); var sum = add; add += total%M; while(add >= M){ var cho = Math.floor(add/M); add = cho + add%M; sum += cho; } console.log(total+sum); } } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); });

### Chocolate Feast Solution in Scala

import scala.io._ object Solution { val in = new In(Source.stdin) def main(args: Array[String]): Unit = { val testCount = in().toInt for (_ <- 1 to testCount) { val N, C, M = in().toInt var cho = N/C var wrap = cho while (wrap >= M) { cho += wrap/M wrap = wrap%M + wrap/M } println(cho) } } } class TokenIterator(iter: BufferedIterator[Char], delims: String) extends Iterator[String] { private val sb = new StringBuilder def hasNext: Boolean = { skipDelims() iter.hasNext } def skipDelims() { while (iter.hasNext && delims.indexOf(iter.head) != -1) { iter.next() } } def next(): String = { skipDelims() while (iter.hasNext && delims.indexOf(iter.head) == -1) { sb.append(iter.next()) } val ret = sb.toString() sb.clear() ret } } class In(source: Source) { val iter = source.buffered val tokenIterator = new TokenIterator(iter, " \r\n") val lineIterator = new TokenIterator(iter, "\r\n") def apply() = tokenIterator.next() // lineIterator.next() def apply(n: Int) = lineIterator.take(n) }

### Chocolate Feast Solution in Pascal

var tt,ii,n,m,c,w,res,tmp:longint; var settled:boolean; begin read(tt); for ii:=1 to tt do begin read(n,c,m); settled:=false; res:=0; w:=0; while not(settled) do begin settled:=true; if n>=c then begin settled:=false; tmp:=trunc(n/c); inc(res,tmp); n:=n mod c; inc(w,tmp); end; if w>=m then begin settled:=false; tmp:=trunc(w/m); inc(res,tmp); w:=w mod m; inc(w,tmp); end; end; writeln(res); end; end.

**Disclaimer:** This problem **(Chocolate Feast)** is generated by **HackerRank** but the solution is provided by ** Chase2learn**. This tutorial is only for

**Educational**and

**Learning**purposes.

## FAQ:

**1. How do you solve the first question in HackerRank?**

If you want to solve the first question of Hackerrank then you have to decide which programing language you want to practice i.e C programming, Cpp Programing, or Java programming then you have to start with the first program HELLO WORLD.

**2. How do I find my HackerRank ID?**

You will receive an email from HackerRank to confirm your access to the ID. Once you have confirmed your email, the entry will show up as verified on the settings page. You will also have an option to “Make primary”. Click on that option. Read more

**3. Does HackerRank detect cheating?**

yes, HackerRank uses a powerful tool to detect plagiarism in the candidates’ submitted code. The Test report of a candidate highlights any plagiarized portions in the submitted code and helps evaluators to verify the integrity of answers provided in the Test.

**4. Does HackerRank use camera?**

No for coding practice Hackerrank does not use camera but for companies’ interviews code submission time Hackerrank uses the camera.

**5. Should I put HackerRank certificate on resume?**

These certificates are useless, and you should not put them on your resume. The experience you gained from getting them is not useless. Use it to build a portfolio, and link to it on your resume.

**6. Can I retake HackerRank test?**

The company which sent you the HackerRank Test invite owns your Test submissions and results. It’s their discretion to permit a reattempt for a particular Test. If you wish to retake the test, we recommend that you contact the concerned recruiter who invited you to the Test and request a re-invite.

**7. What is HackerRank?**

HackerRank is a tech company that focuses on competitive programming challenges for both consumers and businesses. Developers compete by writing programs according to provided specifications. Wikipedi

Finally, we are now, in the end, I just want to conclude some important message for you

**Note:-** I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Please share our posts on social media platforms and also suggest to your friends to Join Our Groups. Don’t forget to subscribe.