time limit issue

Can anyone tell me why this code snippet is raising time limit issue?


using namespace std;

int main()

{
int t,p,n=2048,res=0;

std::cin >> t;

while(t–)

{

std::cin >> p;
  • while(p!=0)
  • {
  • if(p>=n)
  • {
    
  •       p-=n;
    
  •    n/=2;
    
  •    res+=1;
    
  • }
    
  • else
    
  • {
    
  •   n/=2;
    
  • }
    
  •   }
    
  • std::cout << res << ‘\n’;
  • }
  • return 0;

}


Properly format your code, or use websites like pastebin.com or gist.github.com to share your code.
You should have also mentioned the problem link.

Hi, @mmaroliya

Agreed with @muktadirkhan – we need to be able to read your code, and we need the problem description so we know the constraints on input p.

For example, this code results in an infinite loop if p is initialized to a number greater than or equal to 4096 (or starts as a negative number).

    while(p!=0)
    {  
        if(p>=n) 
        { 
            p-=n;
            n/=2;
            res+=1;
        }
        else
        {
            n/=2;
        }
    }