📜  范围中一对的最大XOR值

📅  最后修改于: 2021-04-27 18:46:31             🧑  作者: Mango

给定一个范围[L,R],我们需要找到此范围内的两个整数,以使它们的XOR在两个整数的所有可能选择中最大。更正式地说,
给定[L,R],求最大值(A ^ B)其中L <= A,B
例子 :

Input  : L = 8
         R = 20
Output : 31
31 is XOR of 15 and 16.  

Input  : L = 1
         R = 3
Output : 3

一个简单的解决方案是生成所有对,找到它们的XOR值,最后返回最大XOR值。

一个有效的解决方案是考虑从L到R的二进制值的模式。我们可以看到,从L到R的第一位从0变为1或保持为1,即如果我们将任意两个数字的XOR作为最大值,则为第一个该位将被固定,与L和R本身的XOR的第一位相同。
观察获得第一位的技术后,我们可以看到,如果对L和R进行XOR,则此XOR的最高有效位将告诉我们可以实现的最大值,即让L和R的XOR为1xxx,其中x可以为0或1,那么我们可以得到的最大XOR值是1111,因为从L到R,我们具有xxx的所有可能组合,并且始终可以从两个数字中选择这些位,以使它们的XOR变为全1。下面有一些例子,

Examples 1:
L = 8    R = 20
L ^ R = (01000) ^ (10100) = (11100)
Now as L ^ R is of form (1xxxx) we
can get maximum XOR as (11111) by 
choosing A and B as 15 and 16 (01111 
and 10000)

Examples 2:
L = 16     R = 20
L ^ R = (10000) ^ (10100) = (00100)
Now as L ^ R is of form (1xx) we can 
get maximum xor as (111) by choosing  
A and B as 19 and 20 (10011 and 10100)

因此,此问题的解决方案仅取决于(L ^ R)的值。我们将首先计算L ^ R值,然后从该值的最高有效位开始,将所有1加1以获得最终结果。

C++
// C/C++ program to get maximum xor value
// of two numbers in a range
#include 
using namespace std;
  
// method to get maximum xor value in range [L, R]
int maxXORInRange(int L, int R)
{
    // get xor of limits
    int LXR = L ^ R;
  
    //  loop to get msb position of L^R
    int msbPos = 0;
    while (LXR)
    {
        msbPos++;
        LXR >>= 1;
    }
  
    // construct result by adding 1,
    // msbPos times
    int maxXOR = 0;
    int two = 1;
    while (msbPos--)
    {
        maxXOR += two;
        two <<= 1;
    }
  
    return maxXOR;
}
  
//  Driver code to test above methods
int main()
{
    int L = 8;
    int R = 20;
    cout << maxXORInRange(L, R) << endl;
    return 0;
}


Java
// Java program to get maximum xor value
// of two numbers in a range
  
class Xor
{
    // method to get maximum xor value in range [L, R]
    static int maxXORInRange(int L, int R)
    {
        // get xor of limits
        int LXR = L ^ R;
       
        //  loop to get msb position of L^R
        int msbPos = 0;
        while (LXR > 0)
        {
            msbPos++;
            LXR >>= 1;
        }
       
        // construct result by adding 1,
        // msbPos times
        int maxXOR = 0;
        int two = 1;
        while (msbPos-- >0)
        {
            maxXOR += two;
            two <<= 1;
        }
       
        return maxXOR;
    }
      
    // main function 
    public static void main (String[] args) 
    {
        int L = 8;
        int R = 20;
        System.out.println(maxXORInRange(L, R));
    }
}


Python3
# Python3 program to get maximum xor 
# value of two numbers in a range
  
# Method to get maximum xor
# value in range [L, R]
def maxXORInRange(L, R):
  
    # get xor of limits
    LXR = L ^ R
  
    # loop to get msb position of L^R
    msbPos = 0
    while(LXR):
      
        msbPos += 1
        LXR >>= 1
      
  
    # construct result by adding 1,
    # msbPos times
    maxXOR, two = 0, 1
      
    while (msbPos):
      
        maxXOR += two
        two <<= 1
        msbPos -= 1
  
    return maxXOR
  
# Driver code
L, R = 8, 20
print(maxXORInRange(L, R))
  
# This code is contributed by Anant Agarwal.


C#
// C# program to get maximum xor 
// value of two numbers in a range
using System;
  
class Xor
{
      
    // method to get maximum xor 
    // value in range [L, R]
    static int maxXORInRange(int L, int R)
    {
          
        // get xor of limits
        int LXR = L ^ R;
        
        // loop to get msb position of L^R
        int msbPos = 0;
        while (LXR > 0)
        {
            msbPos++;
            LXR >>= 1;
        }
        
        // construct result by
        // adding 1, msbPos times
        int maxXOR = 0;
        int two = 1;
        while (msbPos-- >0)
        {
            maxXOR += two;
            two <<= 1;
        }
        
        return maxXOR;
    }
       
    // Driver code 
    public static void Main() 
    {
        int L = 8;
        int R = 20;
        Console.WriteLine(maxXORInRange(L, R));
    }
}
  
// This code is contributed by Anant Agarwal.


PHP
>= 1;
    }
  
    // construct result by 
    // adding 1, msbPos times
    $maxXOR = 0;
    $two = 1;
    while ($msbPos--)
    {
        $maxXOR += $two;
        $two <<= 1;
    }
  
    return $maxXOR;
}
  
// Driver Code
$L = 8;
$R = 20;
echo maxXORInRange($L, $R), "\n";
  
// This code is contributed by aj_36
?>


输出 :

31