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

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

## HackerRank Chocolate Feast Solution

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: Bobbys 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 t lines contains three space-separated integers: nc, and m. They represent money to spend, cost of a chocolate, and the number of wrappers he can turn in for a free chocolate.

Constraints

• 1 <= t <= 1000
• 2 <= n <= 105
• 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:

1. 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.
2. 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.
3. 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 tradein. 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);
for(var i = 1; i <= T;i++){
var data = lines[i].split(" ");
var N = parseInt(data);
var M = parseInt(data);
var C = parseInt(data);
var total = Math.floor(N/C);

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
for ii:=1 to tt do begin
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 BrokenProgrammers. This tutorial is only for Educational and Learning purposes.