# Check if a number is Bleak in C++

C++Server Side ProgrammingProgramming

Here we will see whether a number is Bleak or not. A number is said to be bleak if it cannot be represented as sum of a positive number x and set bit count in x. So x + set_bit_count(x) is not equal to n , for any non-negative number x.

The concept is very simple, if the set bit count + the number is not same as the number, then that is Bleak, otherwise it is not.

## Example

Live Demo

#include <iostream>
using namespace std;
int set_bit_count(int x) {
unsigned int bit_count = 0;
while (x != 0) {
x &= (x - 1);
bit_count++;
}
return bit_count;
}
bool isBleakNumber(int n) {
for (int i = 1; i < n; i++)
if (i + set_bit_count(i) == n)
return false;
return true;
}
int main() {
isBleakNumber(3) ? cout << "Yes\n" : cout << "No\n";
isBleakNumber(4) ? cout << "Yes\n" : cout << "No\n";
}

## Output

No
Yes
Published on 22-Oct-2019 11:41:28