Passed the given test case, still WA

the question link : https://www.codechef.com/problems/CIELRCPT

WA link : https://www.codechef.com/viewsolution/35895037

I got your code accepted
Check it here

1 Like

Just use braces in the while loop, Your code is considering 2048 only once.
Moreover, the answer is simple ans = p/2048 + countSetBits(p%2048).
You can use pre-defined function for counting Set Bits.

1 Like

yes, the error was i did not used the braces and so the items did not incremented.

#include
#include <math.h>
#include <bits/stdc++.h>
using namespace std;

int main()
{
int t,p,x,c,i=0;
cin>>t;
while(t–)
{
cin>>p;
c=0;
while(1)
{
x=(int)pow(2,i);
if(p==0)
break;
if(p%x!=0||i>11)
{
x/=2;
p-=x;c++;
i=0;
}
else
i++;

    }
    cout<<c<<endl;;
}
return 0;

}