Queries to check whether all the elements in the given range occurs even number of times
Given an array arr[] containing N integers and there are Q queries where each query consists of a range [L, R]. The task is to find whether all the elements from the given index range have even frequency or not.
Examples:
Input: arr[] = {1, 1, 2, 2, 1}, Q[][] = {{1, 5}, {1, 4}, {3, 4}}
Output:
No
Yes
Yes
Input: arr[] = {100, 100, 200, 100}, Q[][] = {{1, 4}, {1, 2}}
Output:
No
Yes
Naive approach: A simple approach will be to iterate from the index range [L, R] for each of the query and count the frequency of each element in that range and check that every element is occurring even number of times or not. The worst case time complexity of this approach will be O(Q * N) where Q is the number of queries and N is the number of elements in the array.
Efficient approach: Since XOR of two equal number is 0 i.e. if all the elements of the array appears even number of times then the XOR of the complete array will be 0. The same can be said about the elements in the given range [L, R]. Now, to check if the XOR of all the elements in the given range is 0 or not, a prefix XOR array can be created where preXor[i] will store the XOR of the elements arr[0…i]. And the XOR of the elements arr[i…j] can be easily calculated as preXor[j] ^ preXor[i – 1].
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to perform the given queries void performQueries(vector<int>& A, vector<pair<int, int> >& q) { int n = (int)A.size(); // Making array 1-indexed A.insert(A.begin(), 0); // To store the cumulative xor vector<int> 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<int> Arr = { 1, 1, 2, 2, 1 }; vector<pair<int, int> > 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
Time Complexity: O(N)
Recommended Posts:
- Number of times the given string occurs in the array in the range [l, r]
- Queries to check whether all the elements can be made positive by flipping signs exactly K times
- Count pairs (p, q) such that p occurs in array at least q times and q occurs at least p times
- Queries for number of array elements in a range with Kth Bit Set
- Number of elements greater than K in the range L to R using Fenwick Tree (Offline queries)
- Range Queries for Frequencies of array elements
- Queries for GCD of all numbers of an array except elements in a given range
- Array range queries for elements with frequency same as value
- Queries to check whether a given digit is present in the given Range
- Queries for counts of array elements with values in given range
- Element which occurs consecutively in a given subarray more than or equal to K times
- Count number of indices such that s[i] = s[i+1] : Range queries
- Range Queries to Find number of sub-arrays with a given xor
- XOR of numbers that appeared even number of times in given Range
- Array range queries over range queries
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



