# Check if a number is Bleak in C++

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

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