给定正整数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进行XOR,同时记录到目前为止所遇到的最大结果。该算法的复杂度为O(R – L),并且当输入变量接近高值(例如10 9 )时,这是不可行的。
高效的方法:由于仅当两个位互补时,两个位的XOR才为1,因此我们需要X具有与N互补的位,以具有最大值。我们将会从最大的迭代位(日志2(R)个位)到最低(第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