XOR of all subarray XORs | Set 2
Last Updated :
12 Jul, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int getTotalXorOfSubarrayXors( int arr[], int N)
{
if (N % 2 == 0)
return 0;
int res = 0;
for ( int i = 0; i<N; i+=2)
res ^= arr[i];
return res;
}
int main()
{
int arr[] = {3, 5, 2, 4, 6};
int N = sizeof (arr) / sizeof (arr[0]);
cout << getTotalXorOfSubarrayXors(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int getTotalXorOfSubarrayXors( int arr[],
int N)
{
if (N % 2 == 0 )
return 0 ;
int res = 0 ;
for ( int i = 0 ; i < N; i += 2 )
res ^= arr[i];
return res;
}
public static void main (String[] args)
{
int arr[] = { 3 , 5 , 2 , 4 , 6 };
int N = arr.length;
System.out.println(
getTotalXorOfSubarrayXors(arr, N));
}
}
|
Python3
def getTotalXorOfSubarrayXors(arr, N):
if (N % 2 = = 0 ):
return 0
res = 0
for i in range ( 0 , N, 2 ):
res ^ = arr[i]
return res
if __name__ = = "__main__" :
arr = [ 3 , 5 , 2 , 4 , 6 ]
N = len (arr)
print (getTotalXorOfSubarrayXors(arr, N))
|
C#
using System;
class GFG
{
static int getTotalXorOfSubarrayXors( int []arr,
int N)
{
if (N % 2 == 0)
return 0;
int res = 0;
for ( int i = 0; i < N; i += 2)
res ^= arr[i];
return res;
}
static void Main()
{
int []arr = {3, 5, 2, 4, 6};
int N = arr.Length;
Console.Write(getTotalXorOfSubarrayXors(arr, N));
}
}
|
PHP
<?php
function getTotalXorOfSubarrayXors( $arr , $N )
{
if ( $N % 2 == 0)
return 0;
$res = 0;
for ( $i = 0; $i < $N ; $i += 2)
$res ^= $arr [ $i ];
return $res ;
}
$arr = array (3, 5, 2, 4, 6);
$N = count ( $arr );
echo getTotalXorOfSubarrayXors( $arr , $N );
?>
|
Javascript
<script>
function getTotalXorOfSubarrayXors(arr,N)
{
if (N % 2 == 0)
return 0;
let res = 0;
for (let i = 0; i < N; i += 2)
{
res ^= arr[i];
}
return res;
}
let arr=[3, 5, 2, 4, 6];
let N = arr.length;
document.write(getTotalXorOfSubarrayXors(arr, N));
</script>
|
Time Complexity: O(n), where n is the length of the given array
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...