Check if a number is Bleak | A simple Java program to check Bleak Number ; Function to get no of set bits in binary representation of passed binary no . ; Returns true if n is Bleak ; Check for all numbers ' x ' smaller than n . If x + countSetBits ( x ) becomes n , then n can 't be Bleak ; Driver code
import java . io . * ; class GFG { static int countSetBits ( int x ) { int count = 0 ; while ( x != 0 ) { x &= ( x - 1 ) ; count ++ ; } return count ; } static boolean isBleak ( int n ) { for ( int x = 1 ; x < n ; x ++ ) if ( x + countSetBits ( x ) == n ) return false ; return true ; } public static void main ( String args [ ] ) { if ( isBleak ( 3 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; if ( isBleak ( 4 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }