 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if a number is Bleak in C++
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
#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
Advertisements
 