📌  相关文章
📜  找到小于整数 N 且设置位数最大的最大数

📅  最后修改于: 2021-10-26 05:54:46             🧑  作者: 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

蟒蛇3

# 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

时间复杂度: O(log N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程