📌  相关文章
📜  查询以检查给定范围内的所有元素是否出现偶数次

📅  最后修改于: 2021-05-25 10:08:21             🧑  作者: Mango

给定一个包含N个整数的数组arr [] ,并且存在Q个查询,其中每个查询包含一个范围[L,R] 。任务是查找给定索引范围内的所有元素是否具有偶数频率。

例子:

天真的方法:一种简单的方法是从每个查询的索引范围[L,R]进行迭代,并计算该范围内每个元素的出现频率,并检查每个元素是否发生偶数次。这种方法最坏的情况是时间复杂度为O(Q * N) ,其中Q是查询数, N是数组中元素的数。

高效的方法:由于两个相等数字的XOR为0,即,如果数组的所有元素出现偶数次,则整个数组的XOR将为0 。对于给定范围[L,R]中的元素可以说相同。现在,要检查给定范围内所有元素的XOR是否为0 ,可以创建一个前缀XOR数组,其中preXor [i]将存储元素arr [0…i]的XOR。元素arr [i…j]的XOR可以很容易地计算为preXor [j] ^ preXor [i – 1]

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to perform the given queries
void performQueries(vector& A,
                    vector >& q)
{
  
    int n = (int)A.size();
  
    // Making array 1-indexed
    A.insert(A.begin(), 0);
  
    // To store the cumulative xor
    vector pref_xor(n + 1, 0);
  
    // Taking cumulative Xor
    for (int i = 1; i <= n; ++i) {
        pref_xor[i]
            = pref_xor[i - 1] ^ A[i];
    }
  
    // Iterating over the queries
    for (auto i : q) {
        int L = i.first, R = i.second;
        if (L > R)
            swap(L, R);
  
        // If both indices are different and xor
        // in the range [L, R] is 0
        if (L != R and pref_xor[R] == pref_xor[L - 1])
            cout << "Yes\n";
        else
            cout << "No\n";
    }
}
  
// Driver code
int main()
{
  
    vector Arr = { 1, 1, 2, 2, 1 };
  
    vector > q = {
        { 1, 5 },
        { 1, 4 },
        { 3, 4 }
  
    };
  
    performQueries(Arr, q);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
      
class GFG
{
static class pair
{ 
    int first, second; 
    public pair(int first, int second) 
    { 
        this.first = first; 
        this.second = second; 
    } 
} 
  
// Function to perform the given queries
static void performQueries(int []A,
                           pair[] q)
{
    int n = A.length;
  
    // To store the cumulative xor
    int []pref_xor = new int[n + 1];
  
    // Taking cumulative Xor
    for (int i = 1; i <=n; ++i) 
    {
        pref_xor[i] = pref_xor[i - 1] ^ 
                             A[i - 1];
    }
  
    // Iterating over the queries
    for (pair i : q)
    {
        int L = i.first, R = i.second;
        if (L > R)
        {
            int temp = L;
            L = R;
            R = temp;
        }
          
        // If both indices are different and xor
        // in the range [L, R] is 0
        if (L != R && pref_xor[R] == pref_xor[L - 1])
            System.out.println("Yes");
    else
        System.out.println("No");
    }
}
  
// Driver code
static public void main (String []arg)
{
    int []Arr = { 1, 1, 2, 2, 1 };
    pair[] q = { new pair(1, 5 ),
                 new pair(1, 4 ),
                 new pair(3, 4 ) };
  
    performQueries(Arr, q);
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of the approach
  
# Function to perform the given queries
def performQueries(A, q):
  
    n = len(A)
      
    # To store the cumulative xor
    pref_xor = [0 for i in range(n + 1)]
  
    # Taking cumulative Xor
    for i in range(1, n + 1):
        pref_xor[i] = pref_xor[i - 1] ^ A[i - 1]
  
    # Iterating over the queries
    for i in q:
        L = i[0]
        R = i[1]
        if (L > R):
            L, R = R, L
  
        # If both indices are different and 
        # xor in the range [L, R] is 0
        if (L != R and 
            pref_xor[R] == pref_xor[L - 1]):
            print("Yes")
        else:
            print("No")
  
# Driver code
Arr = [1, 1, 2, 2, 1]
  
q = [[ 1, 5 ],
     [ 1, 4 ],
     [ 3, 4 ]]
  
performQueries(Arr, q);
  
# This code is contributed by Mohit Kumar


C#
// C# implementation of the approach
using System;
      
class GFG
{
public class pair
{ 
    public int first, second; 
    public pair(int first, int second) 
    { 
        this.first = first; 
        this.second = second; 
    } 
} 
  
// Function to perform the given queries
static void performQueries(int []A,
                           pair[] q)
{
    int n = A.Length;
  
    // To store the cumulative xor
    int []pref_xor = new int[n + 1];
  
    // Taking cumulative Xor
    for (int i = 1; i <= n; ++i) 
    {
        pref_xor[i] = pref_xor[i - 1] ^ 
                             A[i - 1];
    }
  
    // Iterating over the queries
    foreach (pair k in q)
    {
        int L = k.first, R = k.second;
        if (L > R)
        {
            int temp = L;
            L = R;
            R = temp;
        }
          
        // If both indices are different and xor
        // in the range [L, R] is 0
        if (L != R && pref_xor[R] == pref_xor[L - 1])
            Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
    }
}
  
// Driver code
static public void Main (String []arg)
{
    int []Arr = { 1, 1, 2, 2, 1 };
    pair[] q = {new pair(1, 5 ),
                new pair(1, 4 ),
                new pair(3, 4 )};
  
    performQueries(Arr, q);
}
}
      
// This code is contributed by Rajput-Ji


输出:
No
Yes
Yes

时间复杂度: O(N)