📌  相关文章
📜  最小数 N 使得从 1 到 N 的所有数的总集合位至少为 X

📅  最后修改于: 2021-09-16 11:06:04             🧑  作者: Mango

给定一个数 X,任务是找到最小数 N,使得从 1 到 n 的所有数的总集合位至少为 X。

例子:

Input: x = 5 
Output: 4 
Set bits in 1-> 1
Set bits in 2-> 1
Set bits in 3-> 2 
Set bits in 4-> 1 
Hence first four numbers add upto 5 

Input: x = 20 
Output: 11 

方法:使用二分查找得到最小的最大数,直到 N 的位总和至少为 X。开始时,low 为 0,high 根据约束进行初始化。检查设置位的计数是否至少为X,每次是,将高更改为mid-1,否则将其更改为mid+1。每次我们做 high = mid-1 时,存储答案的最小值。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
#define INF 99999
#define size 10
 
// Function to count sum of set bits
// of all numbers till N
int getSetBitsFromOneToN(int N)
{
    int two = 2, ans = 0;
    int n = N;
 
    while (n) {
        ans += (N / two) * (two >> 1);
 
        if ((N & (two - 1)) > (two >> 1) - 1)
            ans += (N & (two - 1)) - (two >> 1) + 1;
 
        two <<= 1;
        n >>= 1;
    }
    return ans;
}
 
// Function to find the minimum number
int findMinimum(int x)
{
    int low = 0, high = 100000;
 
    int ans = high;
 
    // Binary search for the lowest number
    while (low <= high) {
 
        // Find mid number
        int mid = (low + high) >> 1;
 
        // Check if it is atleast x
        if (getSetBitsFromOneToN(mid) >= x) {
            ans = min(ans, mid);
            high = mid - 1;
        }
        else
            low = mid + 1;
    }
 
    return ans;
}
 
// Driver Code
int main()
{
    int x = 20;
    cout << findMinimum(x);
 
return 0;
}


Java
// Java implementation of the above approach
import java.util.*;
 
class solution
{
static int INF = 99999;
static int size = 10;
 
// Function to count sum of set bits
// of all numbers till N
static int getSetBitsFromOneToN(int N)
{
    int two = 2, ans = 0;
    int n = N;
 
    while (n!=0) {
        ans += (N / two) * (two >> 1);
 
        if ((N & (two - 1)) > (two >> 1) - 1)
            ans += (N & (two - 1)) - (two >> 1) + 1;
 
        two <<= 1;
        n >>= 1;
    }
    return ans;
}
 
// Function to find the minimum number
static int findMinimum(int x)
{
    int low = 0, high = 100000;
 
    int ans = high;
 
    // Binary search for the lowest number
    while (low <= high) {
 
        // Find mid number
        int mid = (low + high) >> 1;
 
        // Check if it is atleast x
        if (getSetBitsFromOneToN(mid) >= x) {
            ans = Math.min(ans, mid);
            high = mid - 1;
        }
        else
            low = mid + 1;
    }
 
    return ans;
}
 
// Driver Code
public static void main(String args[])
{
    int x = 20;
    System.out.println(findMinimum(x));
 
}
 
}
 
//This code is contributed by
// Shashank_Sharma


Python3
# Python3 implementation of the
# above approach
INF = 99999
size = 10
 
# Function to count sum of set bits
# of all numbers till N
def getSetBitsFromOneToN(N):
 
    two, ans = 2, 0
    n = N
 
    while (n > 0):
        ans += (N // two) * (two >> 1)
 
        if ((N & (two - 1)) > (two >> 1) - 1):
            ans += (N & (two - 1)) - (two >> 1) + 1
 
        two <<= 1
        n >>= 1
    return ans
 
# Function to find the minimum number
def findMinimum(x):
 
    low = 0
    high = 100000
 
    ans = high
 
    # Binary search for the lowest number
    while (low <= high):
 
        # Find mid number
        mid = (low + high) >> 1
 
        # Check if it is atleast x
        if (getSetBitsFromOneToN(mid) >= x):
 
            ans = min(ans, mid)
            high = mid - 1
        else:
            low = mid + 1
     
    return ans
 
# Driver Code
x = 20
print(findMinimum(x))
 
# This code is contributed by
# Mohit kumar 29


C#
// C# implementation of the above approach
using System ;
 
class solution
{
static int INF = 99999;
static int size = 10;
 
// Function to count sum of set bits
// of all numbers till N
static int getSetBitsFromOneToN(int N)
{
    int two = 2, ans = 0;
    int n = N;
 
    while (n!=0) {
        ans += (N / two) * (two >> 1);
 
        if ((N & (two - 1)) > (two >> 1) - 1)
            ans += (N & (two - 1)) - (two >> 1) + 1;
 
        two <<= 1;
        n >>= 1;
    }
    return ans;
}
 
// Function to find the minimum number
static int findMinimum(int x)
{
    int low = 0, high = 100000;
 
    int ans = high;
 
    // Binary search for the lowest number
    while (low <= high) {
 
        // Find mid number
        int mid = (low + high) >> 1;
 
        // Check if it is atleast x
        if (getSetBitsFromOneToN(mid) >= x) {
            ans = Math.Min(ans, mid);
            high = mid - 1;
        }
        else
            low = mid + 1;
    }
 
    return ans;
}
 
    // Driver Code
    public static void Main()
    {
        int x = 20;
        Console.WriteLine(findMinimum(x));
     
    }
    // This code is contributed by Ryuga
}


PHP
> 1);
 
        if (($N & ($two - 1)) > ($two >> 1) - 1)
            $ans += ($N & ($two - 1)) -
                          ($two >> 1) + 1;
 
        $two <<= 1;
        $n >>= 1;
    }
    return $ans;
}
 
// Function to find the minimum number
function findMinimum($x)
{
    $low = 0;
    $high = 100000;
 
    $ans = $high;
 
    // Binary search for the lowest number
    while ($low <= $high)
    {
 
        // Find mid number
        $mid = ($low + $high) >> 1;
 
        // Check if it is atleast x
        if (getSetBitsFromOneToN($mid) >= $x)
        {
            $ans = min($ans, $mid);
            $high = $mid - 1;
        }
        else
            $low = $mid + 1;
    }
 
    return $ans;
}
 
// Driver Code
$x = 20;
echo findMinimum($x);
 
// This code is contributed
// by Sach_Code
?>


Javascript


输出:

11

时间复杂度: O(log N * log N)

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