 I have opted a different approach.
The ans is correct when entering through custom input but its not accepting the solution.
#include
#include
using namespace std;
int steps(int n)
{
int ans,p;
if(n<=0)
return 0;
if(n==1)
return 1;

``````p=sqrt(n);
ans=1+ steps(n-pow(2,p));
return ans;
``````

}
int main()
{
int t,n,ans,p;
cin>>t;
for(int i=0;i<t;i++)
{
cin>>n;
ans=steps(n);
cout<<ans<<endl;
}
return 0;
}
Can anyone tell me why?

My code for this problem!
works fine.
#include
using namespace std;

int main()
{
int t;
cin>>t;
int arr[t];
for(int i=0; i<t; i++)
{
cin>>arr[i];
}
for(int i=0; i<t; i++)
{
cout<< __builtin_popcount(arr[i]) <<endl;
}

}

Try this…
#include
using namespace std;
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(0);
int T,N;
cin>>T;
while(T–){
cin>>N;
cout<<__builtin_popcount(N)<<endl;
}
return 0;
}

your code is too complex for bigger numbers. There will be a problem of TLE so try to solve using bitwise operators.

#include
using namespace std;
int main() {
int t;
cin>>t;
while(t–){
long long int n,res=0;
cin>>n;
while(n>2){
int fact=2;
while((fact2)<=n){
fact
=2;
}
n-=fact;
res++;
if(n==2){
n=1;
}

``````	}
cout<<res+n<<endl;
}
return 0;
``````

}

// why it is giving one test case failed though I check it through multiple test random cases plz help

// it is not givig TLE . It gives wrong answer in one test case can anyone tell me the problem with this code

for n = 25, your code fails

Just count the no. of set bits, and you are good to go !

#include
#include<bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t–){
long long int n;
cin>>n;
int ans = 0; //this stores the no of set bits
while(n > 0){
ans += n&1;
n = n>>1;
}

``````	cout<<ans<<endl;
}
return 0;
``````

}