给定正整数 N、L 和 R,我们必须找到 N ⊕ X 的最大值,其中 X ∈ [L, R]。
例子:
Input : N = 7
L = 2
R = 23
Output : 23
Explanation : When X = 16, we get 7 ⊕ 16 = 23 which is the maximum value for all X ∈ [2, 23].
Input : N = 10
L = 5
R = 12
Output : 15
Explanation : When X = 5, we get 10 ⊕ 5 = 15 which is the maximum value for all X ∈ [5, 12].
蛮力方法:我们可以使用蛮力方法解决这个问题,方法是遍历范围 [L, R] 上的所有整数,并将它们与 N 进行异或,同时记录迄今为止遇到的最大结果。该算法的复杂度为O(R – L),当输入变量接近10 9等高值时是不可行的。
有效的方法:由于两个位的 XOR 为 1 当且仅当它们彼此互补时,我们需要 X 具有与 N 的互补位才能具有最大值。我们将从最大位(log 2 (R) th Bit)迭代到最低位(第0位)。每个位可能出现以下两种情况:
- 如果该位未设置,即 0,我们将尝试在 X 中设置它。如果将此位设置为 1 导致 X 超过 R,则我们不会设置它。
- 如果该位已设置,即 1,那么我们将尝试在 X 中取消设置它。如果 X 的当前值已经大于或等于 L,那么我们可以安全地取消设置该位。在另一种情况下,我们将检查设置所有接下来的位是否足以保持 X >= L。如果不是,那么我们需要设置当前位。请注意,设置所有接下来的位等效于添加 (1 << b ) – 1,其中b是当前位。
这种方法的时间复杂度是 O(log 2 (R))。
C++
// CPP program to find the x in range [l, r]
// such that x ^ n is maximum.
#include
#include
using namespace std;
// Function to calculate the maximum value of
// N ^ X, where X is in the range [L, R]
int maximumXOR(int n, int l, int r)
{
int x = 0;
for (int i = log2(r); i >= 0; --i)
{
if (n & (1 << i)) // Set bit
{
if (x + (1 << i) - 1 < l)
x ^= (1 << i);
}
else // Unset bit
{
if ((x ^ (1 << i)) <= r)
x ^= (1 << i);
}
}
return n ^ x;
}
// Driver Code
int main()
{
int n = 7, l = 2, r = 23;
cout << "The output is " << maximumXOR(n, l, r);
return 0;
}
Java
// Java program to find the x in range [l, r]
// such that x ^ n is maximum.
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
// Function to calculate the maximum value of
// N ^ X, where X is in the range [L, R]
static int maximumXOR(int n, int l, int r)
{
int x = 0;
for (int i = (int)(Math.log(r)/Math.log(2)); i >= 0; --i)
{
if ((n & (1 << i))>0) // Set bit
{
if (x + (1 << i) - 1 < l)
x ^= (1 << i);
}
else // Unset bit
{
if ((x ^ (1 << i)) <= r)
x ^= (1 << i);
}
}
return n ^ x;
}
// Driver function
public static void main(String args[])
{
int n = 7, l = 2, r = 23;
System.out.println( "The output is " + maximumXOR(n, l, r));
}
}
// This code is Contributed by tufan_gupta2000
Python3
# Python program to find the
# x in range [l, r] such that
# x ^ n is maximum.
import math
# Function to calculate the
# maximum value of N ^ X,
# where X is in the range [L, R]
def maximumXOR(n, l, r):
x = 0
for i in range(int(math.log2(r)), -1, -1):
if (n & (1 << i)): # Set bit
if (x + (1 << i) - 1 < l):
x ^= (1 << i)
else: # Unset bit
if (x ^ (1 << i)) <= r:
x ^= (1 << i)
return n ^ x
# Driver code
n = 7
l = 2
r = 23
print("The output is",
maximumXOR(n, l, r))
# This code was contributed
# by VishalBachchas
C#
// C# program to find the x in range
// [l, r] such that x ^ n is maximum.
using System;
class GFG
{
// Function to calculate the
// maximum value of N ^ X,
// where X is in the range [L, R]
public static int maximumXOR(int n,
int l, int r)
{
int x = 0;
for (int i = (int)(Math.Log(r) /
Math.Log(2)); i >= 0; --i)
{
if ((n & (1 << i)) > 0) // Set bit
{
if (x + (1 << i) - 1 < l)
{
x ^= (1 << i);
}
}
else // Unset bit
{
if ((x ^ (1 << i)) <= r)
{
x ^= (1 << i);
}
}
}
return n ^ x;
}
// Driver Code
public static void Main(string[] args)
{
int n = 7, l = 2, r = 23;
Console.WriteLine("The output is " +
maximumXOR(n, l, r));
}
}
// This code is contributed
// by Shrikant13
PHP
= 0; --$i)
{
if ($n & (1 << $i))
{
// Set bit
if ($x + (1 << $i) - 1 < $l)
$x ^= (1 << $i);
}
else
{
// Unset bit
if (($x ^ (1 << $i)) <= $r)
$x ^= (1 << $i);
}
}
return $n ^ $x;
}
// Driver Code
$n = 7;
$l = 2;
$r = 23;
echo "The output is " ,
maximumXOR($n, $l, $r);
// This code is contributed by ajit
?>
Javascript
输出
The output is 23
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。