📜  查找小于整数N且设置位数最大的最大数

📅  最后修改于: 2021-04-22 02:14:24             🧑  作者: Mango

给定整数N ,任务是找到设置位数最大的N小的最大数。
例子:

天真的方法:
解决上述问题的简单方法是迭代直到整数N并找到每个数字的设置位数,并在每个步骤中存储具有最大设置位数的位数。
下面是上述方法的实现:

C++
// C++ implementation to Find the
// largest number smaller than integer
// N with maximum number of set bits
#include 
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


Javascript


C++
// C++ implementation to Find the
// largest number smaller than integer
// N with maximum number of set bits
#include 
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


Javascript


输出:
255

高效方法:要优化上述解决方案,我们必须观察到设置位最高的数肯定是2 k – 1的形式。因此,我们只需要遍历k的可能值并找到最高值刚好小于整数N。由于我们要遍历指数变量,因此最多将需要log(N)个步骤。
下面是上述方法的实现:

C++

// C++ implementation to Find the
// largest number smaller than integer
// N with maximum number of set bits
#include 
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

Java脚本


输出:
255

时间复杂度: O(log N)