Header Ad

HackerRank Chocolate Feast problem solution

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 = %d\n",N/C,((N/C)/M));

        }

        for(i=0;i<testcases;i++)
                printf("%d\n",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);
});


Post a Comment

0 Comments