Ciel Receipt

Problem link- Contest Page | CodeChef
code gives expected output but when submitted shows wrong answer

I have compared the pp value with 2^i where i<12. ‘pr’ holds these powers of 2.
I have converted P to double and stored it in pp so that (pp-pr) will be double.

if pp==pr means we can purchase only one item at this cost and there will be no remaining money, so i have put break statement;
if pp is greater than pr then one food item will be added and new pp value will be pp-pr;

import java.util.*;
import java.lang.*;

class Codechef
	public static void main (String[] args) throws java.lang.Exception
		Scanner sc = new Scanner(;
		int T = sc.nextInt();
		double pr = 0, pp = 0;
		int p = 0, item = 0, count = 0;
		for(int i=0; i<T; i++){
		    p = sc.nextInt();
		    if(p==0){count=0; System.out.println(count); break;}
		    pp = (double)p;
		    item = 0;
		    for(int j=11; j>=0; j--){
		        pr = Math.pow(2, j);
		        if(pp==pr){item++; break;}
		        else if(pp%pr==0){item += (pp/pr); break;}
		        else if(pp>pr){
		            pp = pp-pr;
		    count = (int)item;

Please do submit the question(question link) too while posting the code and it would be much better if you can explain your logic too as it would be much easier to debug your code.

Yes I did the changes please take a look…

There are few things that I want to mention like Always try to use int instead of double I don’t know the case with java but often in cpp what happens is like double always leads to accuracy error so try to use double only when it is necessary.

Second I felt something messy in your Logic like this part.

  else if(pp>pr){
		            pp = pp-pr;

I think you should use modulo operator here as it will be better choice like for instance you want to find the remainder right??
when we keep on subtracting the number it takes more steps like if 7 divides 2 what will be remainder if you try manually 7 - 2 in each step in takes like 3 steps to arrive at the answer but you could simply use modulo operator(%) to find the same in one step.
For instance, int rem = 7 % 2 and you are got to go!!
Try to map whatever I have said with your code.

That was helpful thank you so much

Hey you can check this guys solution this guy too asked this same question few days back may be you can learn something form it.

1 Like

yeah! thank you