I am unable to understand what is wrong with my code or logic. I am getting wrong answer after submitting.
My Code:
#include <stdio.h>
int main(void)
{
int t;
scanf("%d",&t);
for(int i=0;i<t;i++)
{
int p,cnt=0,a[11]={2,4,8,16,32,64,128,256,512,1024,2048};
scanf("%d",&p);
while(p>0)
{
if(p%2!=0)
{
cnt++;
p--;
}
for(int j=10;j>-1;j--)
{
if(p%a[j]==0)
{
cnt=cnt+(p/a[j]);
p=p%a[j];
}
else if(p>a[j])
{
p=p-a[j];
cnt++;
}
if(p==0)
{
break;
}
}
}
printf("%d\n",cnt);
}
return 0;
}