Anyone can please explain this code how it works?

// Ciel and Receipt Problem Code: CIELRCPT

   import java.util.*;
   import java.lang.*;
   import java.io.*;
   class Codechef
   {
        public static void main (String[] args) throws java.lang.Exception
        {
	            Scanner sc = new Scanner(System.in);
	            int testCases = sc.nextInt();
	            while(testCases-->0){
	                   int p = sc.nextInt();
	                   int count = 0;
	                   for(int i = 2048;i!=0;i = i/2){
	                          count += p/i;
	                          p %= i;
	                    }
                               System.out.println(count);
                 }
        }
    }

format your code ``` before and after code.

formatted!!

The Logic of the code is that we have to find the minimum number of meals that the pretty girl can eat with a price exactly equals p now in the code first we are taking the test cases and thereafter we run the loop till test cases becomes zero and after that we start the loop from 2048 and goes uptil 0(not considering 0).

What is the logic behind doing so??
This is inorder to make sure that she eats minimum number of dishes now if we start from the minimum prices then she would end up eating huge number of dishes .
finally inside the loop we are counting the number of dishes the logic is that
think of it if i > p then count would remain zero finally when p > i we start incrementing the count and subsequently p would become the remainder p = p % i. Finally we print the answer.
TIme complexity:O(Log(N)) per test cases
i think so
Hope this answers your query :smiley:
do ping me for any doubts.

2 Likes

yeah!! thankyou for this explanation

welcome!!