XOR of all subarray XORs | Set 2
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.
Recommended Posts:
- XOR of all subarray XORs | Set 1
- XOR of XORs of all sub-matrices
- Queries on XOR of XORs of all subarrays
- Find array using different XORs of elements in groups of size 4
- Maximize the subarray sum after multiplying all elements of any subarray with X
- Maximum length of subarray such that sum of the subarray is even
- Subarray with XOR less than k
- Subarray Inversions
- Subarray of size k with given sum
- Largest subarray with GCD one
- Smallest Subarray with given GCD
- Subarray whose sum is closest to K
- Minimum value of "max + min" in a subarray
- Shortest Un-ordered Subarray
- Maximum subarray sum in O(n) using prefix sum



