Smallest number whose set bits are maximum in a given range | Java program to find number whose set bits are maximum among ' l ' and ' r ' ; Returns smallest number whose set bits are maximum in given range . ; Driver code
import java . io . * ; class GFG { static int countMaxSetBits ( int left , int right ) { while ( ( left | ( left + 1 ) ) <= right ) left |= left + 1 ; return left ; } public static void main ( String [ ] args ) { int l = 1 ; int r = 5 ; System . out . println ( countMaxSetBits ( l , r ) ) ; l = 1 ; r = 10 ; System . out . println ( countMaxSetBits ( l , r ) ) ; } }