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 frequency of elements at odd positions are even. So we need to find only the XOR of elements at even positions.
Below is the implementation of the 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 |
Python3
# 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. ?> |
Javascript
<script> // Javascript 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) let res = 0; for (let i = 0; i < N; i += 2) { res ^= arr[i]; } return res; } // Driver Code let arr=[3, 5, 2, 4, 6]; let N = arr.length; document.write(getTotalXorOfSubarrayXors(arr, N)); // This code is contributed by avanitrachhadiya2155 </script> |
7
Time Complexity: O(n), where n is the length of the given array
Auxiliary Space: O(1)
Contact Us