给定一个大小为N和Q个查询的整数arr [] 。每个查询的格式为(L,R) ,其中L和R是数组的索引。任务是找到子数组arr [L…R]的XOR值。
例子:
Input: arr[] = {2, 5, 1, 6, 1, 2, 5} query[] = {{1, 4}}
Output: 3
The XOR value of arr[1…4] is 3.
Input: arr[] = {2, 5, 1, 6, 1, 2, 5} query[] = {{0, 6}}
Output: 6
The XOR value of arr[0…6] is 6.
O(N)辅助空间方法:请参考本文以了解O(N)辅助空间方法。
方法:在这里我们将讨论一个恒定的空间解决方案,但是我们将修改输入数组。
- 将输入数组从索引1更新为N – 1 ,以便arr [i]存储从arr [0]到arr [i]的XOR。
arr[i] = XOR(arr[0], arr[1], .., arr[i])
- 要处理从L到R的查询,只需返回arr [L-1] ^ arr [R]即可。
arr[L-1] = arr[0] ^ arr[1] ^ arr[2]…. ^ arr[L-1]
arr[R] = arr[0] ^ arr[1]……arr[L-1] ^ arr[L] ^ arr[L+1]…… ^ arr[R]
arr[L-1] ^ arr[R] = arr[L] ^ arr[L+1]…. ^ arr[R]where ^ denotes XOR
例如:
Consider the example: arr[] = { 3, 2, 4, 5, 1, 1, 5, 3 }, query[] = {{1, 4 }, { 3, 7}}
After taking XOR of consecutive elements, arr[] = {3, 1, 5, 0, 1, 0, 5, 6}
For first query {1, 4} ans = arr[0] ^ arr[4] = 3 ^ 1 = 2
For second query {3, 7} ans = arr[2] ^ arr[7] = 5 ^ 6 = 3
下面是上述方法的实现:
C++
// C++ program to find XOR
// in a range from L to R
#include
using namespace std;
// Function to find XOR
// in a range from L to R
void find_Xor(int arr[],
pair querry[],
int N, int Q)
{
// Compute xor from arr[0] to arr[i]
for (int i = 1; i < N; i++) {
arr[i] = arr[i] ^ arr[i - 1];
}
int ans = 0;
// process every query
// in constant time
for (int i = 0; i < Q; i++) {
// if L==0
if (querry[i].first == 0)
ans = arr[querry[i].second];
else
ans = arr[querry[i].first - 1]
^ arr[querry[i].second];
cout << ans << endl;
}
}
// Driver Code
int main()
{
int arr[] = { 3, 2, 4, 5,
1, 1, 5, 3 };
int N = 8;
int Q = 2;
pair query[Q]
= { { 1, 4 },
{ 3, 7 } };
// query[]
find_Xor(arr, query, N, Q);
return 0;
}
Java
// Java program to find XOR
// in a range from L to R
class GFG{
static class pair
{
int first, second;
public pair(int first, int second)
{
this.first = first;
this.second = second;
}
}
// Function to find XOR
// in a range from L to R
static void find_Xor(int arr[],
pair querry[],
int N, int Q)
{
// Compute xor from arr[0] to arr[i]
for(int i = 1; i < N; i++)
{
arr[i] = arr[i] ^ arr[i - 1];
}
int ans = 0;
// Process every query
// in constant time
for(int i = 0; i < Q; i++)
{
// If L==0
if (querry[i].first == 0)
ans = arr[querry[i].second];
else
ans = arr[querry[i].first - 1] ^
arr[querry[i].second];
System.out.print(ans + "\n");
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 3, 2, 4, 5,
1, 1, 5, 3 };
int N = 8;
int Q = 2;
pair query[] = { new pair(1, 4),
new pair(3, 7) };
// query[]
find_Xor(arr, query, N, Q);
}
}
// This code is contributed by gauravrajput1
Python3
# Python3 program to find XOR
# in a range from L to R
# Function to find XOR
# in a range from L to R
def find_Xor(arr, query, N, Q):
# Compute xor from arr[0] to arr[i]
for i in range(1, N):
arr[i] = arr[i] ^ arr[i - 1]
ans = 0
# Process every query
# in constant time
for i in range(Q):
# If L == 0
if query[i][0] == 0:
ans = arr[query[i][1]]
else:
ans = (arr[query[i][0] - 1] ^
arr[query[i][1]])
print(ans)
# Driver code
def main():
arr = [ 3, 2, 4, 5, 1, 1, 5, 3 ]
N = 8
Q = 2
# query[]
query = [ [ 1, 4 ],
[ 3, 7 ] ]
find_Xor(arr, query, N, Q)
main()
# This code is contributed by Stuti Pathak
C#
// C# program to find XOR
// in a range from L to R
using System;
class GFG{
class pair
{
public int first, second;
public pair(int first, int second)
{
this.first = first;
this.second = second;
}
}
// Function to find XOR
// in a range from L to R
static void find_Xor(int []arr,
pair []querry,
int N, int Q)
{
// Compute xor from arr[0] to arr[i]
for(int i = 1; i < N; i++)
{
arr[i] = arr[i] ^ arr[i - 1];
}
int ans = 0;
// Process every query
// in constant time
for(int i = 0; i < Q; i++)
{
// If L==0
if (querry[i].first == 0)
ans = arr[querry[i].second];
else
ans = arr[querry[i].first - 1] ^
arr[querry[i].second];
Console.Write(ans + "\n");
}
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 3, 2, 4, 5,
1, 1, 5, 3 };
int N = 8;
int Q = 2;
pair []query = { new pair(1, 4),
new pair(3, 7) };
// query[]
find_Xor(arr, query, N, Q);
}
}
// This code is contributed by 29AjayKumar
2
3
时间复杂度: O(N + Q)
辅助空间: O(1)