给定两个整数L和R ,任务是查找范围[L,R]的元素的XOR。
例子:
Input: L = 4, R = 8
Output: 8
4 ^ 5 ^ 6 ^ 7 ^ 8 = 8
Input: L = 3, R = 7
Output: 3
天真的方法:将答案归纳为零,从L到R遍历所有数字,并对答案与数字一一进行异或。这将花费O(N)时间。
高效方法:通过遵循此处讨论的方法,我们可以在O(1)时间中从[1,N]范围内找到元素的XOR。
使用这种方法,我们必须找到[1,L – 1]范围和[1,R]范围内元素的异或,然后再次对相应的答案进行异或,以得出[L, R] 。这是因为范围[1,L – 1]中的每个元素都将在结果中进行两次“异或”运算,结果为0 ,当将其与范围[L,R]中的元素进行异或时,将得出结果。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the XOR of elements
// from the range [1, n]
int findXOR(int n)
{
int mod = n % 4;
// If n is a multiple of 4
if (mod == 0)
return n;
// If n % 4 gives remainder 1
else if (mod == 1)
return 1;
// If n % 4 gives remainder 2
else if (mod == 2)
return n + 1;
// If n % 4 gives remainder 3
else if (mod == 3)
return 0;
}
// Function to return the XOR of elements
// from the range [l, r]
int findXOR(int l, int r)
{
return (findXOR(l - 1) ^ findXOR(r));
}
// Driver code
int main()
{
int l = 4, r = 8;
cout << findXOR(l, r);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the XOR of elements
// from the range [1, n]
static int findXOR(int n)
{
int mod = n % 4;
// If n is a multiple of 4
if (mod == 0)
return n;
// If n % 4 gives remainder 1
else if (mod == 1)
return 1;
// If n % 4 gives remainder 2
else if (mod == 2)
return n + 1;
// If n % 4 gives remainder 3
else if (mod == 3)
return 0;
return 0;
}
// Function to return the XOR of elements
// from the range [l, r]
static int findXOR(int l, int r)
{
return (findXOR(l - 1) ^ findXOR(r));
}
// Driver code
public static void main(String[] args)
{
int l = 4, r = 8;
System.out.println(findXOR(l, r));
}
}
// This code contributed by Rajput-Ji
Python3
# Python3 implementation of the approach
from operator import xor
# Function to return the XOR of elements
# from the range [1, n]
def findXOR(n):
mod = n % 4;
# If n is a multiple of 4
if (mod == 0):
return n;
# If n % 4 gives remainder 1
elif (mod == 1):
return 1;
# If n % 4 gives remainder 2
elif (mod == 2):
return n + 1;
# If n % 4 gives remainder 3
elif (mod == 3):
return 0;
# Function to return the XOR of elements
# from the range [l, r]
def findXORFun(l, r):
return (xor(findXOR(l - 1) , findXOR(r)));
# Driver code
l = 4; r = 8;
print(findXORFun(l, r));
# This code is contributed by PrinciRaj1992
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the XOR of elements
// from the range [1, n]
static int findXOR(int n)
{
int mod = n % 4;
// If n is a multiple of 4
if (mod == 0)
return n;
// If n % 4 gives remainder 1
else if (mod == 1)
return 1;
// If n % 4 gives remainder 2
else if (mod == 2)
return n + 1;
// If n % 4 gives remainder 3
else if (mod == 3)
return 0;
return 0;
}
// Function to return the XOR of elements
// from the range [l, r]
static int findXOR(int l, int r)
{
return (findXOR(l - 1) ^ findXOR(r));
}
// Driver code
public static void Main()
{
int l = 4, r = 8;
Console.WriteLine(findXOR(l, r));
}
}
// This code is contributed by AnkitRai01
Javascript
输出:
8