Smallest number whose set bits are maximum in a given range
Last Updated :
07 Oct, 2022
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++
// C++ program to find number whose set
// bits are maximum among 'l' and 'r'
#include <bits/stdc++.h>
using namespace std;
// Returns smallest number whose set bits
// are maximum in given range.
int countMaxSetBits(int left, int right)
{
// Initialize the maximum count and
// final answer as 'num'
int max_count = -1, num;
for (int i = left; i <= right; ++i) {
int temp = i, cnt = 0;
// Traverse for every bit of 'i'
// number
while (temp) {
if (temp & 1)
++cnt;
temp >>= 1;
}
// If count is greater than previous
// calculated max_count, update it
if (cnt > max_count) {
max_count = cnt;
num = i;
}
}
return num;
}
// Driver code
int main()
{
int l = 1, r = 5;
cout << countMaxSetBits(l, r) << "\n";
l = 1, r = 10;
cout << countMaxSetBits(l, r);
return 0;
}
Java
// Java program to find number whose set
// bits are maximum among 'l' and 'r'
class gfg
{
// Returns smallest number whose set bits
// are maximum in given range.
static int countMaxSetBits(int left, int right)
{
// Initialize the maximum count and
// final answer as 'num'
int max_count = -1, num = 0;
for (int i = left; i <= right; ++i)
{
int temp = i, cnt = 0;
// Traverse for every bit of 'i'
// number
while (temp > 0)
{
if (temp % 2 == 1)
++cnt;
temp >>= 1;
}
// If count is greater than previous
// calculated max_count, update it
if (cnt > max_count)
{
max_count = cnt;
num = i;
}
}
return num;
}
// Driver code
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));
}
}
// This code has been contributed by 29AjayKumar
Python3
# Python code to find number whose set
# bits are maximum among 'l' and 'r'
def countMaxSetBits( left, right):
max_count = -1
for i in range(left, right+1):
temp = i
cnt = 0
# Traverse for every bit of 'i'
# number
while temp:
if temp & 1:
cnt +=1
temp = temp >> 1
# If count is greater than previous
# calculated max_count, update it
if cnt > max_count:
max_count = cnt
num=i
return num
# driver code
l = 1
r = 5
print(countMaxSetBits(l, r))
l = 1
r = 10
print(countMaxSetBits(l, r))
# This code is contributed by "Abhishek Sharma 44"
C#
// C# program to find number whose set
// bits are maximum among 'l' and 'r'
using System;
class gfg
{
// Returns smallest number whose set bits
// are maximum in given range.
static int countMaxSetBits(int left, int right)
{
// Initialize the maximum count and
// final answer as 'num'
int max_count = -1, num = 0;
for (int i = left; i <= right; ++i)
{
int temp = i, cnt = 0;
// Traverse for every bit of 'i'
// number
while (temp > 0)
{
if (temp % 2 == 1)
++cnt;
temp >>= 1;
}
// If count is greater than previous
// calculated max_count, update it
if (cnt > max_count)
{
max_count = cnt;
num = i;
}
}
return num;
}
// Driver code
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));
}
}
/* This code contributed by PrinciRaj1992 */
PHP
<?php
// PHP program to find number
// whose set bits are maximum
// among 'l' and 'r'
// Returns smallest number
// whose set bits are maximum
// in given range.
function countMaxSetBits($left, $right)
{
// Initialize the maximum
// count and final answer
// as 'num'
$max_count = -1; $num;
for ($i = $left; $i <= $right; ++$i)
{
$temp = $i; $cnt = 0;
// Traverse for every
// bit of 'i' number
while ($temp)
{
if ($temp & 1)
++$cnt;
$temp >>= 1;
}
// If count is greater than
// previous calculated
// max_count, update it
if ($cnt > $max_count)
{
$max_count = $cnt;
$num = $i;
}
}
return $num;
}
// Driver code
$l = 1; $r = 5;
echo countMaxSetBits($l, $r), "\n";
$l = 1; $r = 10;
echo countMaxSetBits($l, $r);
// This code is contributed by m_kit
?>
JavaScript
<script>
// JavaScript program to find number whose set
// bits are maximum among 'l' and 'r'
// Returns smallest number whose set bits
// are maximum in given range.
function countMaxSetBits(left, right)
{
// Initialize the maximum count and
// final answer as 'num'
let max_count = -1, num = 0;
for(let i = left; i <= right; ++i)
{
let temp = i, cnt = 0;
// Traverse for every bit of 'i'
// number
while (temp > 0)
{
if (temp % 2 == 1)
++cnt;
temp >>= 1;
}
// If count is greater than previous
// calculated max_count, update it
if (cnt > max_count)
{
max_count = cnt;
num = i;
}
}
return num;
}
// Driver Code
let l = 1, r = 5;
document.write(countMaxSetBits(l, r) + "<br/>");
l = 1; r = 10;
document.write(countMaxSetBits(l, r));
// This code is contributed by code_hunt
</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++
// C++ program to find number whose set
// bits are maximum among 'l' and 'r'
#include <bits/stdc++.h>
using namespace std;
// Returns smallest number whose set bits
// are maximum in given range.
int countMaxSetBits(int left, int right)
{
while ((left | (left + 1)) <= right)
left |= left + 1;
return left;
}
// Driver code
int main()
{
int l = 1, r = 5;
cout << countMaxSetBits(l, r) << "\n";
l = 1, r = 10;
cout << countMaxSetBits(l, r) ;
return 0;
}
Java
// Java program to find number
// whose set bits are maximum
// among 'l' and 'r'
import java.io.*;
class GFG
{
// Returns smallest number
// whose set bits are
// maximum in given range.
static int countMaxSetBits(int left,
int right)
{
while ((left | (left + 1)) <= right)
left |= left + 1;
return left;
}
// Driver code
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));
}
}
// This code is contributed by @ajit
Python3
# Python code to find number whose set
# bits are maximum among 'l' and 'r'
def countMaxSetBits( left, right):
while(left | (left+1)) <= right:
left |= left+1
return left
# driver code
l = 1
r = 5
print(countMaxSetBits(l, r))
l = 1
r = 10
print(countMaxSetBits(l, r))
# This code is contributed by "Abhishek Sharma 44"
C#
// C# program to find number
// whose set bits are maximum
// among 'l' and 'r'
using System;
class GFG
{
// Returns smallest number
// whose set bits are
// maximum in given range.
static int countMaxSetBits(int left,
int right)
{
while ((left | (left + 1)) <= right)
left |= left + 1;
return left;
}
// Driver code
static public void Main ()
{
int l = 1;
int r = 5;
Console.WriteLine(countMaxSetBits(l, r));
l = 1;
r = 10;
Console.WriteLine(countMaxSetBits(l, r));
}
}
// This code is contributed by @ajit
PHP
<?php
// PHP program to find number
// whose set bits are maximum
// among 'l' and 'r'
// Returns smallest number
// whose set bits are
// maximum in given range.
function countMaxSetBits($left,
$right)
{
while (($left | ($left + 1)) <= $right)
$left |= $left + 1;
return $left;
}
// Driver code
$l = 1 ; $r = 5;
echo countMaxSetBits($l, $r) , "\n";
$l = 1; $r = 10;
echo countMaxSetBits($l, $r) ;
// This code is contributed by aj_36
?>
JavaScript
<script>
// Javascript program to find number whose set
// bits are maximum among 'l' and 'r'
// Returns smallest number whose set bits
// are maximum in given range.
function countMaxSetBits( left, right)
{
while ((left | (left + 1)) <= right)
left |= left + 1;
return left;
}
// driver code
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)
Similar Reads
Set all the bits in given range of a number Given a non-negative number n and two values l and r. The problem is to set the bits in the range l to r in the binary representation of n, i.e, to unset bits from the rightmost lth bit to the rightmost r-th bit. Constraint: 1 <= l <= r <= number of bits in the binary representation of n.Ex
5 min read
Minimum number using set bits of a given number Given an unsigned number, find the minimum number that could be formed by using the bits of the given unsigned number. Examples : Input : 6 Output : 3 Binary representation of 6 is 0000....0110. Smallest number with same number of set bits 0000....0011. Input : 11 Output : 7 Simple Approach: 1. Find
4 min read
M-th smallest number having k number of set bits. Given two non-negative integers m and k. The problem is to find the m-th smallest number having k number of set bits.Constraints: 1 <= m, k.Examples: Input : m = 4, k = 2 Output : 9 (9)10 = (1001)2, it is the 4th smallest number having 2 set bits. Input : m = 6, k = 4 Output : 39 Approach: Follow
6 min read
Value in a given range with maximum XOR Given positive integers N, L, and R, we have to find the maximum value of N ? X, where X ? [L, R].Examples: Input : N = 7 L = 2 R = 23 Output : 23 Explanation : When X = 16, we get 7 ? 16 = 23 which is the maximum value for all X ? [2, 23].Input : N = 10 L = 5 R = 12 Output : 15 Explanation : When X
10 min read
Find a value whose XOR with given number is maximum Given a value X, the task is to find the number Y which will give maximum value possible when XOR with X. (Assume X to be 8 bits) Maximum possible value of X and Y is 255.Examples: Input: X = 2 Output: 253 Binary Representation of X = 00000010 Binary Representation of Y = 11111101 Maximum XOR value:
4 min read