- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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