Given an array of integers, we need to get total XOR of all subarray XORs where subarray XOR can be obtained by XORing all elements of it.
Examples :
Input : arr[] = [3, 5, 2, 4, 6]
Output : 7
Total XOR of all subarray XORs is,
(3) ^ (5) ^ (2) ^ (4) ^ (6)
(3^5) ^ (5^2) ^ (2^4) ^ (4^6)
(3^5^2) ^ (5^2^4) ^ (2^4^6)
(3^5^2^4) ^ (5^2^4^6) ^
(3^5^2^4^6) = 7
Input : arr[] = {1, 2, 3, 4}
Output : 0
Total XOR of all subarray XORs is,
(1) ^ (2) ^ (3) ^ (4) ^
(1^2) ^ (2^3) ^ (3^4) ^
(1^2^3) ^ (2^3^4) ^
(1^2^3^4) = 0
We have discussed a O(n) solution in below post.
XOR of all subarray XORs | Set 1
As discussed in above post, frequency of element at i-th index is given by (i+1)*(N-i), where N is the size of the array
There are 4 cases possible:
Case 1: i is odd, N is odd
Let i = 2k+1, N = 2m+1
freq[i] = ((2k+1)+1)*((2m+1)-(2k+1)) = 4(m-k)(k+1) = even
Case 2: i is odd, N is even
Let i = 2k+1, N = 2m
freq[i] = ((2k+1)+1)*((2m)-(2k+1)) = 2(k+1)(2m-2k-1) = even
Case 3: i is even, N is odd
Let i = 2k, N = 2m+1
freq[i] = ((2k)+1)*((2m+1)-(2k)) = 2k(2m-2k+1)+(2m-2k)+1 = odd
Case 4: i is even, N is even
Let i = 2k, N = 2m
freq[i] = ((2k)+1)*((2m)-(2k)) = 2(m-k)(2k+1) = even
From this, we can conclude that if total no.of elements in the array is even, then frequency of element at any position is even. So total XOR will be 0. And if total no. of elements are odd, then frequency of elements at even positions are odd and add positions are even. So we need to find only the XOR of elements at even positions.
Below is implementation of above idea :
C++
// C++ program to get total xor of all subarray xors #include <bits/stdc++.h> using namespace std; // Returns XOR of all subarray xors int getTotalXorOfSubarrayXors(int arr[], int N) { // if even number of terms are there, all // numbers will appear even number of times. // So result is 0. if (N % 2 == 0) return 0; // else initialize result by 0 as (a xor 0 = a) int res = 0; for (int i = 0; i<N; i+=2) res ^= arr[i]; return res; } // Driver code to test above methods int main() { int arr[] = {3, 5, 2, 4, 6}; int N = sizeof(arr) / sizeof(arr[0]); cout << getTotalXorOfSubarrayXors(arr, N); return 0; } |
Java
// Java program to get total // xor of all subarray xors import java.io.*; class GFG { // Returns XOR of all // subarray xors static int getTotalXorOfSubarrayXors(int arr[], int N) { // if even number of terms are // there, all numbers will appear // even number of times. So result is 0. if (N % 2 == 0) return 0; // else initialize result // by 0 as (a xor 0 = a) int res = 0; for (int i = 0; i < N; i += 2) res ^= arr[i]; return res; } // Driver Code public static void main (String[] args) { int arr[] = {3, 5, 2, 4, 6}; int N = arr.length; System.out.println( getTotalXorOfSubarrayXors(arr, N)); } } // This code is contributed by ajit |
Python 3
# Python 3 program to get total xor # of all subarray xors # Returns XOR of all subarray xors def getTotalXorOfSubarrayXors(arr, N): # if even number of terms are there, # all numbers will appear even number # of times. So result is 0. if (N % 2 == 0): return 0 # else initialize result by 0 # as (a xor 0 = a) res = 0 for i in range(0, N, 2): res ^= arr[i] return res # Driver code if __name__ == "__main__": arr = [3, 5, 2, 4, 6] N = len(arr) print(getTotalXorOfSubarrayXors(arr, N)) # This code is contributed by ita_c |
C#
// C# program to get total // xor of all subarray xors using System; class GFG { // Returns XOR of all // subarray xors static int getTotalXorOfSubarrayXors(int []arr, int N) { // if even number of terms // are there, all numbers // will appear even number // of times. So result is 0. if (N % 2 == 0) return 0; // else initialize result // by 0 as (a xor 0 = a) int res = 0; for (int i = 0; i < N; i += 2) res ^= arr[i]; return res; } // Driver Code static void Main() { int []arr = {3, 5, 2, 4, 6}; int N = arr.Length; Console.Write(getTotalXorOfSubarrayXors(arr, N)); } } // This code is contributed by aj_36 |
PHP
<?php // PHP program to get total // xor of all subarray xors // Returns XOR of all subarray xors function getTotalXorOfSubarrayXors($arr, $N) { // if even number of terms // are there, all numbers // will appear even number // of times. So result is 0. if ($N % 2 == 0) return 0; // else initialize result // by 0 as (a xor 0 = a) $res = 0; for ($i = 0; $i < $N; $i += 2) $res ^= $arr[$i]; return $res; } // Driver Code $arr = array(3, 5, 2, 4, 6); $N = count($arr); echo getTotalXorOfSubarrayXors($arr, $N); // This code is contributed by anuj_67. ?> |
Output:
7
This article is contributed by Rishabh Raj Jha. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- XOR of all subarray XORs | Set 1
- Queries on XOR of XORs of all subarrays
- XOR of XORs of all sub-matrices
- Find array using different XORs of elements in groups of size 4
- Count of distinct XORs formed by rearranging two Binary strings
- Given a set, find XOR of the XOR's of all subsets.
- Maximize the subarray sum after multiplying all elements of any subarray with X
- Maximum length of subarray such that all elements are equal in the subarray
- XOR of a subarray (range of elements) | Set 2
- Find XOR of two number without using XOR operator
- Choose X such that (A xor X) + (B xor X) is minimized
- Find the maximum subarray XOR in a given array
- Subarray with XOR less than k
- XOR of a subarray (range of elements)
- Longest Subarray having strictly positive XOR
- Find array such that no subarray has xor zero or Y
- Find the subarray of size K with minimum XOR
- Query to count odd and even parity elements in subarray after XOR with K
- Queries to update each element in subarray to Bitwise XOR with a given value
- Number of triplets in array having subarray xor equal

