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 way to solve the above problem is to iterate till the integer N and find the number of set bits of each number and store the number having the largest set bits at each step.
Below is the implementation of the above approach:
C++
// C++ implementation to Find the // largest number smaller than integer // N with maximum number of set bits #include <bits/stdc++.h> using namespace std; // Function to return the largest // number less than N int largestNum(int n) { int num = 0; int max_setBits = 0; // Iterate through all the numbers for (int i = 0; i <= n; i++) { // Find the number of set bits // for the current number int setBits = __builtin_popcount(i); // Check if this number has the // highest set bits if (setBits >= max_setBits) { num = i; max_setBits = setBits; } } // Return the result return num; } // Driver code int main() { int N = 345; cout << largestNum(N); return 0; } |
Java
// Java implementation to Find the // largest number smaller than integer // N with maximum number of set bits class GFG { /* Function to get no of set bits in binary representation of positive integer n */ static int countSetBits(int n) { int count = 0; while (n > 0) { count += n & 1; n >>= 1; } return count; } // Function to return the largest // number less than N static int largestNum(int n) { int num = 0; int max_setBits = 0; // Iterate through all the numbers for (int i = 0; i <= n; i++) { // Find the number of set bits // for the current number int setBits = countSetBits(i); // Check if this number has the // highest set bits if (setBits >= max_setBits) { num = i; max_setBits = setBits; } } // Return the result return num; } // Driver code public static void main (String[] args) { int N = 345; System.out.println(largestNum(N)); } } // This code is contributed by AnkitRai01 |
Python3
# Python3 implementation to find the # largest number smaller than integer # N with maximum number of set bits # Function to return the largest # number less than N def largestNum(n): num = 0; max_setBits = 0; # Iterate through all the numbers for i in range(n + 1): # Find the number of set bits # for the current number setBits = bin(i).count('1'); # Check if this number has the # highest set bits if (setBits >= max_setBits): num = i; max_setBits = setBits; # Return the result return num; # Driver code if __name__ == "__main__" : N = 345; print(largestNum(N)); # This code is contributed by AnkitRai01 |
C#
// C# implementation to Find the // largest number smaller than integer // N with a maximum number of set bits using System; class GFG{ // Function to get no of set // bits in binary representation // of positive integer n static int countSetBits(int n) { int count = 0; while (n > 0) { count += n & 1; n >>= 1; } return count; } // Function to return the largest // number less than N static int largestNum(int n) { int num = 0; int max_setBits = 0; // Iterate through all the numbers for(int i = 0; i <= n; i++) { // Find the number of set bits // for the current number int setBits = countSetBits(i); // Check if this number has // the highest set bits if (setBits >= max_setBits) { num = i; max_setBits = setBits; } } // Return the result return num; } // Driver code public static void Main(String[] args) { int N = 345; Console.Write(largestNum(N)); } } // This code is contributed by shivanisinghss2110 |
255
Efficient Approach: To optimize the above solution we have to observe that the number with the highest set bits will surely be of form 2k – 1. 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++
// C++ implementation to Find the // largest number smaller than integer // N with maximum number of set bits #include <bits/stdc++.h> using namespace std; // Function to return the largest // number less than N int largestNum(int n) { int num = 0; // Iterate through all possible values for (int i = 0; i <= 32; i++) { // Multiply the number by 2 i times int x = (1 << i); if ((x - 1) <= n) num = (1 << i) - 1; else break; } // Return the final result return num; } // Driver code int main() { int N = 345; cout << largestNum(N); return 0; } |
Java
// Java implementation to Find the // largest number smaller than integer // N with maximum number of set bits import java.util.*; class GFG{ // Function to return the largest // number less than N static int largestNum(int n) { int num = 0; // Iterate through all possible values for (int i = 0; i <= 32; i++) { // Multiply the number by 2 i times int x = (1 << i); if ((x - 1) <= n) num = (1 << i) - 1; else break; } // Return the final result return num; } // Driver code public static void main(String args[]) { int N = 345; System.out.print(largestNum(N)); } } // This code is contributed by Akanksha_Rai |
Python3
# Python3 implementation to find the # largest number smaller than integer # N with the maximum number of set bits # Function to return the largest # number less than N def largestNum(n): num = 0; # Iterate through all possible # values for i in range(32): # Multiply the number by # 2 i times x = (1 << i); if ((x - 1) <= n): num = (1 << i) - 1; else: break; # Return the final result return num; # Driver code if __name__ == "__main__": N = 345; print(largestNum(N)); # This code is contributed by AnkitRai01 |
C#
// C# implementation to Find the // largest number smaller than integer // N with maximum number of set bits using System; class GFG{ // Function to return the largest // number less than N static int largestNum(int n) { int num = 0; // Iterate through all possible values for (int i = 0; i <= 32; i++) { // Multiply the number by 2 i times int x = (1 << i); if ((x - 1) <= n) num = (1 << i) - 1; else break; } // Return the final result return num; } // Driver code public static void Main() { int N = 345; Console.Write(largestNum(N)); } } // This code is contributed by Nidhi_Biet |
255
Time Complexity: O(log N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Previous smaller integer having one less number of set bits
- Find largest number smaller than N with same set of digits
- Largest number smaller than or equal to n and digits in non-decreasing order
- Largest number smaller than or equal to N divisible by K
- Maximize number of groups formed with size not smaller than its largest element
- Largest number less than X having at most K set bits
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Closest (or Next) smaller and greater numbers with same number of set bits
- Minimum integer with at most K bits set such that their bitwise AND with N is maximum
- Find the largest number with n set and m unset bits
- Largest number less than N with digit sum greater than the digit sum of N
- Check if bits of a number has count of consecutive set bits in increasing order
- Previous perfect square and cube number smaller than number N
- Next greater integer having one more number of set bits
- Largest smaller number possible using only one swap operation
- Print numbers having first and last bits as the only set bits
- Maximize a number considering permutations with values smaller than limit
- Count set bits in an integer
- Count set bits in an integer using Lookup Table
- Count of Binary Digit numbers smaller than N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

