Chocolate Feast

Today’s evening is New Year’s Eve 2016. Happy New Year 2017!!! By the way, 2017 is a prime number. The next prime will be 2027. Went ahead and took a look at the HackerRank challenge Chocolate Feast. If interested take a look at the description at the following URL: https://www.hackerrank.com/challenges/chocolate-feast?utm_campaign=challenge-recommendation&utm_medium=email&utm_source=24-hour-campaign

As usual I first take a look at the Need Help? I then spend time working on my solution. After trying and passing the sample tests I read most of the discussions to improve on my solution and try additional tests that other fellow developers have obtained. Finally I go ahead and submit my solution.

Following is a screen capture of the console on the Eclipse IDE:

6

4 1 2

10 2 5

12 4 4

6 2 2

43203 60 5

12 4 4

7

6

3

5

899

3

The first case is associated with the description of the challenge. The following three are the sample tests of the challenge. The last two were test cases described in the discussion page.

Following is my Java solution:

import java.util.Scanner;

public class Solution {

/**

* compute number of chocolates per trip

*

* a % m == 0 => a = k * m

*

* (a + b) % n = (a % n + b % n) % n

* (a * b) % n = (a % n * b % n) % n

*/

static int totalChocos(int money, int chocoCost, int wrappersPerChoco) {

int chocos           = money / chocoCost;

int wrappers         = money / chocoCost;

int additionalChocos       = 0;

while (wrappers >= wrappersPerChoco) {

additionalChocos     = wrappers / wrappersPerChoco;

wrappers               = additionalChocos + (wrappers % wrappersPerChoco);

chocos                 += additionalChocos;

}

return chocos;

}

/**

* test code

*/

public static void main(String[] args) {

Scanner sc    = new Scanner(System.in);

int T         = sc.nextInt();

for (int trip = 0; trip < T; trip++) {

int money            = sc.nextInt();

int chocoCost              = sc.nextInt();

int wrappersPerChoco       = sc.nextInt();

System.out.println(totalChocos(money, chocoCost, wrappersPerChoco));

}

sc.close();

}

}

It seems like that the Need Help link provided more than what was needed for this challenge. Perhaps some solutions used it.

If you have a comment \ questions regarding this or any other post in this blog, please do not hesitate and send me a message. I will keep your name in confidence unless you explicitly allow me to include it on the post.

Happy and prosperous New Year 2017!!!

John

John.canessa@gmail.com

Follow me on Twitter: @john_canessa

Leave a Reply

Your email address will not be published. Required fields are marked *