HackerRank Chocolate Feast Problem Solution Yashwant Parihar, April 18, 2023April 19, 2023 In this post, we will solve HackerRank Chocolate Feast Problem 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.Examplen = 15c = 3m = 2He 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 1 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 FormatThe first line contains an integer, t, the number of test cases to analyze.Each of the next lines contains three space-separated integers: n. c, and m. They representmoney to spend, cost of a chocolate, and the number of wrappers he can turn in for a free chocolate. 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 ExplanationBobby 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. HackerRank Chocolate Feast Problem Solution Chocolate Feast C Solution #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { int T; scanf("%d\n",&T); for(int test_case = 0 ; test_case < T; test_case++) { int N, C, M; scanf("%d %d %d\n",&N, &C, &M); int total = N / C; int unused = total; while(unused >= M) { int new = unused / M; unused = new + (unused - new * M); total += new; } printf("%d\n",total); } return 0; } Chocolate Feast C++ Solution #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> #include <cassert> using namespace std; int main() { int t; cin >> t; assert(t>0); for (int i=0; i<t; i++) { int n, c, m; cin >> n >> c >> m; int numEaten = n/c; int numWrappers = numEaten; while (numWrappers >= m) { int exchanged = numWrappers/m; int remain = numWrappers%m; numEaten += exchanged; numWrappers = remain + exchanged; } cout << numEaten << endl; } return 0; } Chocolate Feast C Sharp Solution using System; using System.Collections.Generic; using System.IO; using System.Text; namespace ChocolateFeast { //https://www.hackerrank.com/challenges/chocolate-feast class Solution { static void Main(string[] args) { //StringBuilder sb = new StringBuilder(); Input lInputData = ReadInputsFromConsole(); //Input lInputData = ReadInputsFromFile(); int iTotalChoclolates = 0, iTotalChocWithCash = 0, iFreeChocsWithWraps = 0; int iRemainingChocAfterRedem = 0, iTotalRemainingWrapsInHand = 0; int tempUsedWraps = 0, tempRemainingWraps = 0, tempFreeChocs = 0; foreach (InputData data in lInputData.Data) { iTotalChocWithCash = (int)Math.Truncate(data.AvailableCash / data.AmountPerChocolate); iFreeChocsWithWraps = iTotalChocWithCash / data.TotalWrapsPerFreeChocolate; iRemainingChocAfterRedem = iTotalChocWithCash - (data.TotalWrapsPerFreeChocolate * iFreeChocsWithWraps); iTotalRemainingWrapsInHand = iRemainingChocAfterRedem + iFreeChocsWithWraps; while (iTotalRemainingWrapsInHand >= data.TotalWrapsPerFreeChocolate) { tempFreeChocs = iTotalRemainingWrapsInHand / data.TotalWrapsPerFreeChocolate; iFreeChocsWithWraps = iFreeChocsWithWraps + tempFreeChocs; tempUsedWraps = tempFreeChocs * data.TotalWrapsPerFreeChocolate; tempRemainingWraps = iTotalRemainingWrapsInHand % data.TotalWrapsPerFreeChocolate; iTotalRemainingWrapsInHand = tempFreeChocs + tempRemainingWraps; } iTotalChoclolates = iTotalChocWithCash + iFreeChocsWithWraps; Console.WriteLine(iTotalChoclolates); //sb.Append(iTotalChoclolates.ToString()); //sb.Append(Environment.NewLine); } //WriteOutputToFile(sb); } private static void WriteOutputToFile(StringBuilder sb) { File.WriteAllText(@"C:\Output.txt", sb.ToString()); Console.WriteLine("Done..."); Console.ReadLine(); } private static Input ReadInputsFromFile() { Input lInputs = new Input(); try { List<InputData> lData = new List<InputData>(lInputs.TotalInputs); Console.WriteLine("Please enter the input file path: "); string fPath = Console.ReadLine(); using (StreamReader sr = new StreamReader(fPath)) { string line = string.Empty; while ((line = sr.ReadLine()) != null) { line.Trim(); if (line.Split(new char[] { ' ' }).Length > 1) { string[] data = line.Split(new char[] { ' ' }); InputData ip = new InputData() { AvailableCash = Convert.ToInt32(data[0]), AmountPerChocolate = Convert.ToDouble(data[1]), TotalWrapsPerFreeChocolate = Convert.ToInt32(data[2]) }; lData.Add(ip); } else { lInputs.TotalInputs = Convert.ToInt32(line); } } } lInputs.Data = lData; } catch (Exception) { throw; } return lInputs; } private static Input ReadInputsFromConsole() { Input lInputs = new Input(); lInputs.TotalInputs = Convert.ToInt32(Console.ReadLine()); List<InputData> lData = new List<InputData>(lInputs.TotalInputs); for (int i = 0; i < lInputs.TotalInputs; i++) { string[] data = Console.ReadLine().Split(new char[] { ' ' }); InputData ip = new InputData() { AvailableCash = Convert.ToInt32(data[0]), AmountPerChocolate = Convert.ToDouble(data[1]), TotalWrapsPerFreeChocolate = Convert.ToInt32(data[2]) }; lData.Add(ip); } lInputs.Data = lData; return lInputs; } } class Input { public int TotalInputs { get; set; } public List<InputData> Data { get; set; } } class InputData { public int AvailableCash { get; set; } public double AmountPerChocolate { get; set; } public int TotalWrapsPerFreeChocolate { get; set; } } } Chocolate Feast Java Solution import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; class Result { /* * Complete the 'chocolateFeast' function below. * * The function is expected to return an INTEGER. * The function accepts following parameters: * 1. INTEGER n * 2. INTEGER c * 3. INTEGER m */ public static int chocolateFeast(int cash, int cost, int wrapper_cost) { int bars = cash / cost; int wrappers = bars; while (wrappers >= wrapper_cost) { wrappers -= wrapper_cost; ++bars; ++wrappers; } return bars; } } public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); int t = Integer.parseInt(bufferedReader.readLine().trim()); IntStream.range(0, t).forEach(tItr -> { try { String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" "); int n = Integer.parseInt(firstMultipleInput[0]); int c = Integer.parseInt(firstMultipleInput[1]); int m = Integer.parseInt(firstMultipleInput[2]); int result = Result.chocolateFeast(n, c, m); bufferedWriter.write(String.valueOf(result)); bufferedWriter.newLine(); } catch (IOException ex) { throw new RuntimeException(ex); } }); bufferedReader.close(); bufferedWriter.close(); } } Chocolate Feast JavaScript Solution function processData(input) { var re = /\r?\n/, testCaseCount, testCase, n, c, m, i, out, wrappers; input = input.split(re); testCaseCount = input[0]; for (i = 0; i < testCaseCount; i++) { testCase = input[i+1].split(' '); n = parseInt(testCase[0]); c = parseInt(testCase[1]); m = parseInt(testCase[2]); out = Math.floor(n/c); out += cashInWrappers(out, m); console.log(out.toString()); } } function cashInWrappers(wrapperCount, wrapperToChoc) { var chocs; if (wrapperCount < wrapperToChoc) { return 0; } else { chocs = Math.floor(wrapperCount / wrapperToChoc); return chocs + cashInWrappers(chocs + (wrapperCount % wrapperToChoc), wrapperToChoc); } } 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 Python Solution def solve(n,c,w): wc = n//c result = wc r = 0 while( wc >= w ): result += wc//w r = wc%w wc = wc//w+r return result t = int(input()) for i in range(0,t): n,c,m = map(int,input().split()) print(solve(n,c,m)) Other Solutions HackerRank Service Lane Problem Solution HackerRank Lisa’s Workbook Problem Solution c C# C++ HackerRank Solutions java javascript python CcppCSharpHackerrank Solutionsjavajavascriptpython