📌  相关文章
📜  在给定范围内设置位数最大的最小数字

📅  最后修改于: 2021-05-04 18:58:17             🧑  作者: Mango

给定正整数“ l ”和“ r ”。找出最小的数字“ n ”,使l <= n <= r,并且设置位数(二进制表示中的“ 1”的数量)最大。
例子 :

Input: 1 4
Output: 3
Explanation:
Binary representation from '1' to '4':
110 = 0012
210 = 0102
310 = 0112
110 = 1002
Thus number '3' has maximum set bits = 2

Input: 1 10
Output: 7

一种简单的方法是从’l’遍历到’r’并为每个’x’计数设置的位(l <= n <= r)并打印其中计数最大的数字。此方法的时间复杂度为O(n * log(r))。

C++
// C++ program to find number whose set
// bits are maximum among 'l' and 'r'
#include 
using namespace std;
 
// Returns smallest number whose set bits
// are maximum in given range.
int countMaxSetBits(int left, int right)
{
    // Initialize the maximum count and
    // final answer as 'num'
    int max_count = -1, num;
    for (int i = left; i <= right; ++i) {
        int temp = i, cnt = 0;
 
        // Traverse for every bit of 'i'
        // number
        while (temp) {
            if (temp & 1)
                ++cnt;
            temp >>= 1;
        }
 
        // If count is greater than previous
        // calculated max_count, update it
        if (cnt > max_count) {
            max_count = cnt;
            num = i;
        }
    }
    return num;
}
 
// Driver code
int main()
{
    int l = 1, r = 5;
    cout << countMaxSetBits(l, r) << "\n";
 
    l = 1, r = 10;
    cout << countMaxSetBits(l, r);
    return 0;
}


Java
// Java program to find number whose set
// bits are maximum among 'l' and 'r'
class gfg
{
     
 
// Returns smallest number whose set bits
// are maximum in given range.
static int countMaxSetBits(int left, int right)
{
    // Initialize the maximum count and
    // final answer as 'num'
    int max_count = -1, num = 0;
    for (int i = left; i <= right; ++i)
    {
        int temp = i, cnt = 0;
 
        // Traverse for every bit of 'i'
        // number
        while (temp > 0)
        {
            if (temp % 2 == 1)
                ++cnt;
            temp >>= 1;
        }
 
        // If count is greater than previous
        // calculated max_count, update it
        if (cnt > max_count)
        {
            max_count = cnt;
            num = i;
        }
    }
    return num;
}
 
// Driver code
public static void main(String[] args)
{
    int l = 1, r = 5;
    System.out.println(countMaxSetBits(l, r));
 
    l = 1; r = 10;
    System.out.print(countMaxSetBits(l, r));
}
}
 
// This code has been contributed by 29AjayKumar


Python 3
# Python code to find number whose set
# bits are maximum among 'l' and 'r'
 
def countMaxSetBits( left, right):
    max_count = -1
    for i in range(left, right+1):
        temp = i
        cnt = 0
 
        # Traverse for every bit of 'i'
        # number
        while temp:
            if temp & 1:
                cnt +=1
            temp = temp >> 1
         
        # If count is greater than previous
        # calculated max_count, update it
        if cnt > max_count:
            max_count = cnt
            num=i
    return num
 
# driver code
l = 1
r = 5
print(countMaxSetBits(l, r))
l = 1
r = 10
print(countMaxSetBits(l, r))
 
# This code is contributed by "Abhishek Sharma 44"


C#
// C# program to find number whose set
// bits are maximum among 'l' and 'r'
using System;
     
class gfg
{
     
// Returns smallest number whose set bits
// are maximum in given range.
static int countMaxSetBits(int left, int right)
{
    // Initialize the maximum count and
    // final answer as 'num'
    int max_count = -1, num = 0;
    for (int i = left; i <= right; ++i)
    {
        int temp = i, cnt = 0;
 
        // Traverse for every bit of 'i'
        // number
        while (temp > 0)
        {
            if (temp % 2 == 1)
                ++cnt;
            temp >>= 1;
        }
 
        // If count is greater than previous
        // calculated max_count, update it
        if (cnt > max_count)
        {
            max_count = cnt;
            num = i;
        }
    }
    return num;
}
 
// Driver code
public static void Main(String[] args)
{
    int l = 1, r = 5;
    Console.WriteLine(countMaxSetBits(l, r));
 
    l = 1; r = 10;
    Console.Write(countMaxSetBits(l, r));
}
}
 
/* This code contributed by PrinciRaj1992 */


PHP
>= 1;
        }
 
        // If count is greater than
        // previous calculated
        // max_count, update it
        if ($cnt > $max_count)
        {
            $max_count = $cnt;
            $num = $i;
        }
    }
    return $num;
}
 
// Driver code
$l = 1; $r = 5;
echo countMaxSetBits($l, $r), "\n";
 
$l = 1; $r = 10;
echo countMaxSetBits($l, $r);
     
// This code is contributed by m_kit
?>


C++
// C++ program to find number whose set
// bits are maximum among 'l' and 'r'
#include 
using namespace std;
 
// Returns smallest number whose set bits
// are maximum in given range.
int countMaxSetBits(int left, int right)
{
    while ((left | (left + 1)) <= right)
        left |= left + 1;
 
    return left;
}
 
// Driver code
int main()
{
    int l = 1, r = 5;
    cout << countMaxSetBits(l, r) << "\n";
 
    l = 1, r = 10;
    cout << countMaxSetBits(l, r) ;
    return 0;
}


Java
// Java program to find number
// whose set bits are maximum
// among 'l' and 'r'
import java.io.*;
 
class GFG
{
     
    // Returns smallest number
    // whose set bits are
    // maximum in given range.
    static int countMaxSetBits(int left,
                               int right)
    {
    while ((left | (left + 1)) <= right)
        left |= left + 1;
 
    return left;
    }
 
// Driver code
public static void main (String[] args)
{
    int l = 1;
    int r = 5;
    System.out.println(countMaxSetBits(l, r));
 
    l = 1;
    r = 10;
    System.out.println(countMaxSetBits(l, r));
}
}
 
// This code is contributed by @ajit


Python3
# Python code to find number whose set
# bits are maximum among 'l' and 'r'
 
def countMaxSetBits( left, right):
     
    while(left | (left+1)) <= right:
        left |= left+1
    return left
     
# driver code
l = 1
r = 5
print(countMaxSetBits(l, r))
l = 1
r = 10
print(countMaxSetBits(l, r))
 
# This code is contributed by "Abhishek Sharma 44"


C#
// C# program to find number
// whose set bits are maximum
// among 'l' and 'r'
using System;
 
class GFG
{
     
    // Returns smallest number
    // whose set bits are
    // maximum in given range.
    static int countMaxSetBits(int left,
                               int right)
    {
    while ((left | (left + 1)) <= right)
        left |= left + 1;
 
    return left;
    }
 
// Driver code
static public void Main ()
{
    int l = 1;
    int r = 5;
    Console.WriteLine(countMaxSetBits(l, r));
     
    l = 1;
    r = 10;
    Console.WriteLine(countMaxSetBits(l, r));
}
}
 
// This code is contributed by @ajit


PHP


Javascript


输出 :

3
7

高效的方法是使用位操作。代替对从“ l”到“ r”的每个数字进行迭代,仅在更新所需的数字(“ num”)之后进行迭代,即,将数字与连续数字进行按位“或”运算。例如,

Let l = 2, and r = 10
1. num = 2
2. x = num OR (num + 1)
       = 2 | 3 = 010 | 011 = 011
   num = 3(011)
3. x = 3 | 4 = 011 | 100 = 111
   num = 7(111)
4. x = 7 | 8 = 0111 | 1000 = 1111
   Since 15(11112) is greater than
   10, thus stop traversing for next number.
5. Final answer = 7 

C++

// C++ program to find number whose set
// bits are maximum among 'l' and 'r'
#include 
using namespace std;
 
// Returns smallest number whose set bits
// are maximum in given range.
int countMaxSetBits(int left, int right)
{
    while ((left | (left + 1)) <= right)
        left |= left + 1;
 
    return left;
}
 
// Driver code
int main()
{
    int l = 1, r = 5;
    cout << countMaxSetBits(l, r) << "\n";
 
    l = 1, r = 10;
    cout << countMaxSetBits(l, r) ;
    return 0;
}

Java

// Java program to find number
// whose set bits are maximum
// among 'l' and 'r'
import java.io.*;
 
class GFG
{
     
    // Returns smallest number
    // whose set bits are
    // maximum in given range.
    static int countMaxSetBits(int left,
                               int right)
    {
    while ((left | (left + 1)) <= right)
        left |= left + 1;
 
    return left;
    }
 
// Driver code
public static void main (String[] args)
{
    int l = 1;
    int r = 5;
    System.out.println(countMaxSetBits(l, r));
 
    l = 1;
    r = 10;
    System.out.println(countMaxSetBits(l, r));
}
}
 
// This code is contributed by @ajit

Python3

# Python code to find number whose set
# bits are maximum among 'l' and 'r'
 
def countMaxSetBits( left, right):
     
    while(left | (left+1)) <= right:
        left |= left+1
    return left
     
# driver code
l = 1
r = 5
print(countMaxSetBits(l, r))
l = 1
r = 10
print(countMaxSetBits(l, r))
 
# This code is contributed by "Abhishek Sharma 44"

C#

// C# program to find number
// whose set bits are maximum
// among 'l' and 'r'
using System;
 
class GFG
{
     
    // Returns smallest number
    // whose set bits are
    // maximum in given range.
    static int countMaxSetBits(int left,
                               int right)
    {
    while ((left | (left + 1)) <= right)
        left |= left + 1;
 
    return left;
    }
 
// Driver code
static public void Main ()
{
    int l = 1;
    int r = 5;
    Console.WriteLine(countMaxSetBits(l, r));
     
    l = 1;
    r = 10;
    Console.WriteLine(countMaxSetBits(l, r));
}
}
 
// This code is contributed by @ajit

的PHP


Java脚本


输出 :

3
7

时间复杂度: O(log(n))
辅助空间: O(1)