Given a positive integer ‘l‘ and ‘r‘. Find the smallest number ‘n‘ such that l <= n <= r and count of the number of set bits(number of ‘1’s in binary representation) is as maximum as possible.
Examples :
Input: 1 4
Output: 3
Explanation:
Binary representation from ‘1’ to ‘4’:
110 = 0012
210 = 0102
310 = 0112
110 = 1002
Thus number ‘3’ has maximum set bits = 2
Input: 1 10
Output: 7
Simple approach is to traverse from ‘l’ to ‘r’ and count the set bits for each ‘x'(l <= n <= r) and print the number whose count is maximum among them. Time complexity of this approach is O(n*log(r)).
C++
#include <bits/stdc++.h>
using namespace std;
int countMaxSetBits( int left, int right)
{
int max_count = -1, num;
for ( int i = left; i <= right; ++i) {
int temp = i, cnt = 0;
while (temp) {
if (temp & 1)
++cnt;
temp >>= 1;
}
if (cnt > max_count) {
max_count = cnt;
num = i;
}
}
return num;
}
int main()
{
int l = 1, r = 5;
cout << countMaxSetBits(l, r) << "\n" ;
l = 1, r = 10;
cout << countMaxSetBits(l, r);
return 0;
}
|
Java
class gfg
{
static int countMaxSetBits( int left, int right)
{
int max_count = - 1 , num = 0 ;
for ( int i = left; i <= right; ++i)
{
int temp = i, cnt = 0 ;
while (temp > 0 )
{
if (temp % 2 == 1 )
++cnt;
temp >>= 1 ;
}
if (cnt > max_count)
{
max_count = cnt;
num = i;
}
}
return num;
}
public static void main(String[] args)
{
int l = 1 , r = 5 ;
System.out.println(countMaxSetBits(l, r));
l = 1 ; r = 10 ;
System.out.print(countMaxSetBits(l, r));
}
}
|
Python3
def countMaxSetBits( left, right):
max_count = - 1
for i in range (left, right + 1 ):
temp = i
cnt = 0
while temp:
if temp & 1 :
cnt + = 1
temp = temp >> 1
if cnt > max_count:
max_count = cnt
num = i
return num
l = 1
r = 5
print (countMaxSetBits(l, r))
l = 1
r = 10
print (countMaxSetBits(l, r))
|
C#
using System;
class gfg
{
static int countMaxSetBits( int left, int right)
{
int max_count = -1, num = 0;
for ( int i = left; i <= right; ++i)
{
int temp = i, cnt = 0;
while (temp > 0)
{
if (temp % 2 == 1)
++cnt;
temp >>= 1;
}
if (cnt > max_count)
{
max_count = cnt;
num = i;
}
}
return num;
}
public static void Main(String[] args)
{
int l = 1, r = 5;
Console.WriteLine(countMaxSetBits(l, r));
l = 1; r = 10;
Console.Write(countMaxSetBits(l, r));
}
}
|
PHP
<?php
function countMaxSetBits( $left , $right )
{
$max_count = -1; $num ;
for ( $i = $left ; $i <= $right ; ++ $i )
{
$temp = $i ; $cnt = 0;
while ( $temp )
{
if ( $temp & 1)
++ $cnt ;
$temp >>= 1;
}
if ( $cnt > $max_count )
{
$max_count = $cnt ;
$num = $i ;
}
}
return $num ;
}
$l = 1; $r = 5;
echo countMaxSetBits( $l , $r ), "\n" ;
$l = 1; $r = 10;
echo countMaxSetBits( $l , $r );
?>
|
Javascript
<script>
function countMaxSetBits(left, right)
{
let max_count = -1, num = 0;
for (let i = left; i <= right; ++i)
{
let temp = i, cnt = 0;
while (temp > 0)
{
if (temp % 2 == 1)
++cnt;
temp >>= 1;
}
if (cnt > max_count)
{
max_count = cnt;
num = i;
}
}
return num;
}
let l = 1, r = 5;
document.write(countMaxSetBits(l, r) + "<br/>" );
l = 1; r = 10;
document.write(countMaxSetBits(l, r));
</script>
|
Output :
3
7
Time Complexity: O(n*log(r))
Auxiliary Space: O(1)
Efficient approach is to use bit-manipulation. Instead of iterating for every number from ‘l’ to ‘r’, iterate only after updating the desired number(‘num’) i.e., take the bitwise ‘OR’ of number with the consecutive number. For instance,
Let l = 2, and r = 10
1. num = 2
2. x = num OR (num + 1)
= 2 | 3 = 010 | 011 = 011
num = 3(011)
3. x = 3 | 4 = 011 | 100 = 111
num = 7(111)
4. x = 7 | 8 = 0111 | 1000 = 1111
Since 15(11112) is greater than
10, thus stop traversing for next number.
5. Final answer = 7
C++
#include <bits/stdc++.h>
using namespace std;
int countMaxSetBits( int left, int right)
{
while ((left | (left + 1)) <= right)
left |= left + 1;
return left;
}
int main()
{
int l = 1, r = 5;
cout << countMaxSetBits(l, r) << "\n" ;
l = 1, r = 10;
cout << countMaxSetBits(l, r) ;
return 0;
}
|
Java
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));
}
}
|
Python3
def countMaxSetBits( left, right):
while (left | (left + 1 )) < = right:
left | = left + 1
return left
l = 1
r = 5
print (countMaxSetBits(l, r))
l = 1
r = 10
print (countMaxSetBits(l, r))
|
C#
using System;
class GFG
{
static int countMaxSetBits( int left,
int right)
{
while ((left | (left + 1)) <= right)
left |= left + 1;
return left;
}
static public void Main ()
{
int l = 1;
int r = 5;
Console.WriteLine(countMaxSetBits(l, r));
l = 1;
r = 10;
Console.WriteLine(countMaxSetBits(l, r));
}
}
|
PHP
<?php
function countMaxSetBits( $left ,
$right )
{
while (( $left | ( $left + 1)) <= $right )
$left |= $left + 1;
return $left ;
}
$l = 1 ; $r = 5;
echo countMaxSetBits( $l , $r ) , "\n" ;
$l = 1; $r = 10;
echo countMaxSetBits( $l , $r ) ;
?>
|
Javascript
<script>
function countMaxSetBits( left, right)
{
while ((left | (left + 1)) <= right)
left |= left + 1;
return left;
}
let l = 1, r = 5;
document.write(countMaxSetBits(l, r) + "</br>" );
l = 1, r = 10;
document.write(countMaxSetBits(l, r)) ;
</script>
|
Output :
3
7
Time complexity: O(log(n))
Auxiliary space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!