Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

HackerRank Chocolate Feast problem solution

YASH PAL, 31 July 2024

In this HackerRank Chocolate Feast problem, you need to complete the chocolateFeast function that has three integer variables as a parameter and need to return the number of chocolate bobby can eat after taking full advantage of the promotion.

HackerRank Chocolate Feast problem solution

Problem solution in Python programming.

def chocolates(N, C, M):
    
    total = int(N/C);
    
    empty_wrapper = total
    
    while empty_wrapper >= M:
        
        temp = int(empty_wrapper/M);
        
        total = total + temp;
        
        empty_wrapper = empty_wrapper - (temp*M) + temp;
    
    return total;


T = int(input())

result = [];

for i in range(T):
    
    (N, C, M) = map(int, input().split());
    
    result = result + [chocolates(N,C,M)];


print('n'.join(map(str, result)))

Problem solution in Java Programming.

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);
        }
    }
}

Problem solution in C++ programming.

#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 */ 
    ios_base::sync_with_stdio(0);
    int T, N, C, M, w, res;
    cin >> T;
    while (T--) {
        cin >> N >> C >> M;
        w = res = N/C;
        while (w >= M) {
            res += w/M;
            w = (w%M) + w/M;
        }
        cout << res << "n";
    }
    return 0;
}

Problem solution in C programming.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
         int testcases=0;
        int N=0,M=0,C=0;
        int i=0, now=0;

        scanf("%d",&testcases);
        int temp = 0;
        int answer[testcases];

        for(i=0;i<testcases;i++)
        {

                scanf("%d",&N);
                scanf("%d",&C);
                scanf("%d",&M);
                temp = N/C;
                now=temp;
                answer[i]=temp;
                while (now>=M)
                {  
                answer[i] = answer[i] + (now/M);
                now=now - (now/M)*M + now/M;
                }

                //printf("first = %d Second = %dn",N/C,((N/C)/M));

        }

        for(i=0;i<testcases;i++)
                printf("%dn",answer[i]);
   return 0;
}

Problem solution in JavaScript programming.

function parseInts(line) {
    return line.split(' ').map(function(x) { return parseInt(x, 10) });
}

function solve(n, c, m) {
    var chocolates = Math.floor(n / c);
    return chocolates + Math.floor((chocolates - 1) / (m - 1));
}

function processData(input) {
    var lines = input.split('n');
    for (var t = parseInts(lines.shift())[0]; t > 0; t -= 1) {
        var ints = parseInts(lines.shift());
        console.log(solve.apply(null, ints));
    }
}

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});

Algorithms coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes