Chocolate Feast HackerRank Solution in C, C++, Java, Python

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 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 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: n, c, and m. They represent money 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

Chocolate Feast HackerRank 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 HackerRank Solution in C++

#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 HackerRank 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 HackerRank 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 HackerRank Solution in C#

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace ChocolateFeast
{
    class Program
    {
        static void Main(string[] args)
        {
            int cases = int.Parse(Console.ReadLine());
            for (int i = 0; i < cases; i++)
            {
                int res = 0;
                string[] temp = Console.ReadLine().Split(' ');
                int candies = int.Parse(temp[0]) / int.Parse(temp[1]);
                res += candies;
                int wrapper = res;
                while (wrapper >= int.Parse(temp[2]))
                {
                    int extra = wrapper / int.Parse(temp[2]);
                    wrapper %= int.Parse(temp[2]);
                    res += extra;
                    wrapper += extra;
                }
                Console.WriteLine(res);
            }
            Console.ReadLine();
        }
    }
}

 

Attempt Chocolate Feast HackerRank Challenge

Link – https://www.hackerrank.com/challenges/chocolate-feast/

Next HackerRank Challenge Solution 

Link – https://exploringbits.com/service-lane-hackerrank-solution/

Leave a Comment