Given an integer, check whether it is Bleak or not. - Codeprg

Breaking

programing News Travel Computer Engineering Science Blogging Earning

Wednesday 10 July 2019

Given an integer, check whether it is Bleak or not.

Given an integer, check whether it is Bleak or not.
A number ‘n’ is called Bleak if it cannot be represented as sum of a positive number x and set bit count in x, i.e., x +countSetBit(x) is not equal to n for any non-negative number x.

3 is not Bleak as it can be represented
as 2 + countSetBits(2).

4 is  Bleak as it cannot be represented 
as sum of a number x and countSetBits(x)
for any number x.

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

int main() {
//code
int t,n,x;
cin>>t;
while(t--){
    cin>>n;
   int f=1;
    for(int i=n;i>=n/2;i--){
     string  s=bitset<16>(i).to_string();
       int cnt=count(s.begin(),s.end(),'1');
       if(cnt+i==n){
           f=0;
           break;
       }
    }
        if(f)
        cout<<"1"<<endl;
        else
        cout<<"0"<<endl;
    
        
}
return 0;
}