2 test case is giving wrong answer.
Basically i have used the logic of counting set bits in n.
here is my code
#include<bits/stdc++.h>
using namespace std;
int count_set_bits(int n){
int ans = 0;
int power = 1;
while(n>0){
int last_bit = (n&1);
ans += (powerlast_bit);
power = power10;
n = n>>1;
}
string s = to_string(ans);
int count = 0;
for(int i=0;i<s.size();i++){
if(s[i]==‘1’){
count++;
}
}
return count;
}
int main() {
int t;
cin>>t;
while(t–){
int n;
cin>>n;
cout<<count_set_bits(n)<<endl;
}
return 0;
}
Incredible hunk
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.