Given an integer N, the task is to find the largest number smaller than N having the maximum number of set bits.
Examples:
Input : N = 345
Output : 255
Explanation:
345 in binary representation is 101011001 with 5 set bits, and 255 is 11111111 with maximum number of set bits less than the integer N.
Input : N = 2
Output : 1
Explanation:
2 in binary representation is 10 with 1 set bit, and 1 has maximum number of set bits less than the integer N.
Naive Approach:
The naive approach to solve the problem is to iterate till the integer N – 1 and find the number of set bits for each number and store the number having the largest set bits, and max set bits of the number.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int largestNum( int n)
{
int num = 0;
int max_setBits = 0;
for ( int i = 0; i < n; i++) {
int setBits = __builtin_popcount(i);
if (setBits >= max_setBits) {
num = i;
max_setBits = setBits;
}
}
return num;
}
int main()
{
int N = 345;
cout << largestNum(N);
return 0;
}
|
Java
import java.io.*;
public class GFG
{
static int countSetBits( int n)
{
int count = 0 ;
while (n > 0 )
{
count += n & 1 ;
n >>= 1 ;
}
return count;
}
static int largestNum( int n)
{
int num = 0 ;
int max_setBits = 0 ;
for ( int i = 0 ; i < n; i++)
{
int setBits = countSetBits(i);
if (setBits >= max_setBits)
{
num = i;
max_setBits = setBits;
}
}
return num;
}
public static void main (String[] args)
{
int N = 345 ;
System.out.println(largestNum(N));
}
}
|
Python3
def largestNum(n):
num = 0 ;
max_setBits = 0 ;
for i in range (n):
setBits = bin (i).count( '1' );
if (setBits > = max_setBits):
num = i;
max_setBits = setBits;
return num;
if __name__ = = "__main__" :
N = 345 ;
print (largestNum(N));
|
C#
using System;
class GFG{
static int countSetBits( int n)
{
int count = 0;
while (n > 0)
{
count += n & 1;
n >>= 1;
}
return count;
}
static int largestNum( int n)
{
int num = 0;
int max_setBits = 0;
for ( int i = 0; i < n; i++)
{
int setBits = countSetBits(i);
if (setBits >= max_setBits)
{
num = i;
max_setBits = setBits;
}
}
return num;
}
public static void Main(String[] args)
{
int N = 345;
Console.Write(largestNum(N));
}
}
|
Javascript
<script>
function countSetBits(n)
{
let count = 0;
while (n > 0)
{
count += n & 1;
n >>= 1;
}
return count;
}
function largestNum(n)
{
let num = 0;
let max_setBits = 0;
for (let i = 0; i < n; i++)
{
let setBits = countSetBits(i);
if (setBits >= max_setBits)
{
num = i;
max_setBits = setBits;
}
}
return num;
}
let N = 345;
document.write(largestNum(N));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above solution we need to observe that the number with the highest set bits will surely be of form 2k – 1. Numbers of the form 2k will have only one bit set, and numbers of the form 2k – 1 will have all the bits set before the kth position. So, we only need to iterate over the possible values of k and find the highest value just less than the integer N. Since we are iterating over the exponent variable therefore at most log(N) steps will be required.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int largestNum( int n)
{
int num = 0;
for ( int i = 0; i <= 32; i++)
{
int x = (1 << i);
if ((x - 1) <= n)
num = (1 << i) - 1;
else
break ;
}
return num;
}
int main()
{
int N = 345;
cout << largestNum(N);
return 0;
}
|
Java
import java.io.*;
class GFG{
static int largestNum( int n)
{
int num = 0 ;
for ( int i = 0 ; i <= 32 ; i++)
{
int x = ( 1 << i);
if ((x - 1 ) <= n)
num = ( 1 << i) - 1 ;
else
break ;
}
return num;
}
public static void main(String args[])
{
int N = 345 ;
System.out.print(largestNum(N));
}
}
|
Python3
def largestNum(n):
num = 0 ;
for i in range ( 32 ):
x = ( 1 << i);
if ((x - 1 ) < = n):
num = ( 1 << i) - 1 ;
else :
break ;
return num;
if __name__ = = "__main__" :
N = 345 ;
print (largestNum(N));
|
C#
using System;
class GFG{
static int largestNum( int n)
{
int num = 0;
for ( int i = 0; i <= 32; i++)
{
int x = (1 << i);
if ((x - 1) <= n)
num = (1 << i) - 1;
else
break ;
}
return num;
}
public static void Main()
{
int N = 345;
Console.Write(largestNum(N));
}
}
|
Javascript
<script>
function largestNum(n)
{
let num = 0;
for (let i = 0; i <= 32; i++)
{
let x = (1 << i);
if ((x - 1) <= n)
num = (1 << i) - 1;
else
break ;
}
return num;
}
let N = 345;
document.write(largestNum(N));
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)