📌  相关文章
📜  子数组的异或(元素范围)| 2套

📅  最后修改于: 2021-09-06 05:39:37             🧑  作者: Mango

给定一个大小为NQ的数组整数arr[]查询。每个查询的形式为(L, R) ,其中LR是数组的索引。任务是找到子数组 arr[L…R]的 XOR 值。

例子:

O(N) 辅助空间方法:O(N) 辅助空间方法请参考这篇文章。

方法:这里我们将讨论一个常数空间解决方案,但我们将修改输入数组。

1. 将输入数组从索引1更新为N – 1 ,以便arr[i]存储从arr[0]arr[i]的 XOR。

2. 要处理从LR的查询,只需返回arr[L-1] ^ arr[R]

例如:

下面是上述方法的实现:

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


Javascript


输出:
2
3

时间复杂度: O (N + Q)
辅助空间: O (1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live