We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

43203 / 60 = 720 chocolates ( 3 dollars left and you cannot buy any more chocolate for 3 dollar )
720 / 5 = 144 chocolates ( we can buy 144 chocolates from 720 wrappers )
144 / 5 = 28 chocolates ( we can buy 28 chocolates from 144 wrappers, 4 wrappers left )
(28 + 4 ) / 5 = 6 ( 28 wrappers + 4 wrappers left from 144, we can buy 6 chocolates )
(6 + 2) / 5 = 1 ( 6 wrappers + 2 wrappers left from 32 wrappers, we can buy 1 chocolate )
This sums to 720 + 144 + 28 + 6 + 1 = 899.

this kinda worked
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 a0 = 0; a0 < t; a0++){
int n = in.nextInt();
int c = in.nextInt();
int m = in.nextInt();
int totalChoc = n / c;
int totalWrappers = totalChoc;
int freeChocs = 0;
while (m <= totalWrappers) {
int wrapperTogive=totalWrappers-(totalWrappers%m) ;
freeChocs = totalWrappers / m;
totalWrappers = (totalWrappers - wrapperTogive) + freeChocs;
totalChoc=totalChoc + freeChocs;
}
System.out.println(totalChoc);
}
}

thanks. I had a terrible time with this problem. maybe it was the noise in my environment, maybe it was something to do with the computer i'm using... I couldn't get it exactly right untill I read this. Thanks for the code.

## Chocolate Feast

You are viewing a single comment's thread. Return to all comments →

Same problem with my code also.

thaks I miss that case

check out this code .. https://www.hackerrank.com/challenges/chocolate-feast/submissions/code/20066920

this kinda worked import java.io.

; import java.util.; import java.text.; import java.math.; import java.util.regex.*;public class Solution {

}

I guess you are doing way to much unnecessary work. Heree is my while loop

This approach is clean and easy to follow, which is great.

However, it results in O(N) time (input of 10000 1 2 results in the loop running 9999 times.)

If you switch the loop to the following you end up with O( lg(N)) (or 14 in the worst case, since you are using integer math).

Hi Jason I kind of did the same thing what you did but mine didn't pass the test cases how whats the difference

import time

start_time = time.time()

def chocolateFeast1(he_has,cost,wrapper):

start_time = time.time() def chocolateFeast(cash, cost, wrapper_cost):

wrappers = wrappers % wrapper_cost instead of wrappers -= new_chocolate * wrapper_cost

was better understandable for me.

wrappers %= wrapper_cost would be even shorter

your code will result in performance issues when the count is too high. Thats the reason suggested to do with / and % operators

there is problem with ur code. 899 is correct.

any idea if 3 dollars left are carry to next trip?

thanks. I had a terrible time with this problem. maybe it was the noise in my environment, maybe it was something to do with the computer i'm using... I couldn't get it exactly right untill I read this. Thanks for the code.