📌  相关文章
📜  小于 X 的最大数具有至多 K 个设置位

📅  最后修改于: 2021-10-26 05:31:13             🧑  作者: Mango

给定一个整数X > 1和一个整数K > 0 ,任务是找到最大的奇数< X使得其二进制表示中1 的数量最多为K
例子:

朴素的方法:X-1开始,检查X以下所有至多有K个设置位的数字,第一个满足条件的数字就是所需的答案。
有效的方法:是对设置的位进行计数。如果计数小于或等于 K,则返回 X。否则,当计数 – k 不为 0 时,继续删除最右边的设置位。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the greatest number <= X
// having at most K set bits.
int greatestKBits(int X, int K)
{
    int set_bit_count = __builtin_popcount(X);
    if (set_bit_count <= K)
        return X;
 
    // Remove rightmost set bits one
    // by one until we count becomes k
    int diff = set_bit_count - K;
    for (int i = 0; i < diff; i++)
        X &= (X - 1);
 
    // Return the required number
    return X;
}
 
// Driver code
int main()
{
    int X = 21, K = 2;
    cout << greatestKBits(X, K);
    return 0;
}


Java
// Java implementation of the approach
import java.io.*;
   
class GFG {
 
    // Function to return the greatest number <= X
    // having at most K set bits.
     int greatestKBits(int X, int K)
     {
       int set_bit_count = Integer.bitCount(X);
       if (set_bit_count <= K)
            return X;
 
        // Remove rightmost set bits one
        // by one until we count becomes k
        int diff = set_bit_count - K;
        for (int i = 0; i < diff; i++)
            X &= (X - 1);
 
        // Return the required number
        return X;
    }
 
// Driver code
public static void main (String[] args)
{
    int X = 21, K = 2;
    GFG g=new GFG();
      System.out.print(g.greatestKBits(X, K));
}
 
//This code is contributed by Shivi_Aggarwal
}


Python3
# Python 3 implementation of the approach
 
# Function to return the greatest
# number <= X having at most K set bits.
def greatestKBits(X, K):
    set_bit_count = bin(X).count('1')
    if (set_bit_count <= K):
        return X
 
    # Remove rightmost set bits one
    # by one until we count becomes k
    diff = set_bit_count - K
    for i in range(0, diff, 1):
        X &= (X - 1)
 
    # Return the required number
    return X
 
# Driver code
if __name__ == '__main__':
    X = 21
    K = 2
    print(greatestKBits(X, K))
     
# This code is contributed by
# Shashank_Sharma


C#
// C# implementation of the above approach
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 greatest number <= X
    // having at most K set bits.
    static int greatestKBits(int X, int K)
    {
        int set_bit_count = countSetBits(X);
        if (set_bit_count <= K)
        return X;
 
        // Remove rightmost set bits one
        // by one until we count becomes k
        int diff = set_bit_count - K;
        for (int i = 0; i < diff; i++)
            X &= (X - 1);
 
        // Return the required number
        return X;
    }
 
    // Driver code
    public static void Main()
    {
        int X = 21, K = 2;
        Console.WriteLine(greatestKBits(X, K));
         
    }
}
 
// This code is contributed by Ryuga


Javascript


输出:
20

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