XOR of all subarray XORs | Set 1
Last Updated :
19 Sep, 2023
Given an array of integers, we need to get the 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}
Output : 2
Input : arr[] = {1, 2, 3, 4}
Output : 0
A simple solution is to generate all subarrays and compute XOR of all of them. Below is the implementation of the above idea :
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int getTotalXorOfSubarrayXors( int arr[], int N)
{
int res = 0;
for ( int i=0; i<N; i++)
for ( int j=i; j<N; j++)
for ( int k=i; k<=j; k++)
res = res ^ arr[k];
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
public class GFG {
static int getTotalXorOfSubarrayXors(
int arr[], int N)
{
int res = 0 ;
for ( int i = 0 ; i < N; i++)
for ( int j = i; j < N; j++)
for ( int k = i; k <= j; k++)
res = res ^ arr[k];
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));
}
}
|
Python 3
def getTotalXorOfSubarrayXors(arr, N):
res = 0
for i in range ( 0 , N):
for j in range (i, N):
for k in range (i, j + 1 ):
res = res ^ arr[k]
return res
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)
{
int res = 0;
for ( int i = 0; i < N; i++)
for ( int j = i; j < N; j++)
for ( int k = i; k <= j; k++)
res = res ^ arr[k];
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 )
{
$res = 0;
for ( $i = 0; $i < $N ; $i ++)
for ( $j = $i ; $j < $N ; $j ++)
for ( $k = $i ; $k <= $j ; $k ++)
$res = $res ^ $arr [ $k ];
return $res ;
}
$arr = array (3, 5, 2, 4, 6);
$N = sizeof( $arr );
echo getTotalXorOfSubarrayXors( $arr , $N );
?>
|
Javascript
<script>
function getTotalXorOfSubarrayXors(
arr, N)
{
let res = 0;
for (let i = 0; i < N; i++)
for (let j = i; j < N; j++)
for (let k = i; k <= j; k++)
res = res ^ arr[k];
return res;
}
let arr = [3, 5, 2, 4, 6];
let N = arr.length;
document.write(getTotalXorOfSubarrayXors(arr, N));
</script>
|
Time Complexity: O(N3)
Auxiliary Space: O(1)
An efficient solution is based on the idea to enumerate all subarrays, we can count the frequency of each element that occurred totally in all subarrays, if the frequency of an element is odd then it will be included in the final result otherwise not.
As in above example,
3 occurred 5 times,
5 occurred 8 times,
2 occurred 9 times,
4 occurred 8 times,
6 occurred 5 times
So our final result will be xor of all elements which occurred odd number of times
i.e. 3^2^6 = 7
From above occurrence pattern we can observe that number at i-th index will have
(i + 1) * (N - i) frequency.
So we can iterate over all elements once and calculate their frequencies and if it is odd then we can include that in our final result by XORing it with the result.
Total time complexity of the solution will be O(N)
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int getTotalXorOfSubarrayXors( int arr[],
int N)
{
int res = 0;
for ( int i = 0; i < N; i++)
{
int freq = (i + 1) * (N - i);
if (freq % 2 == 1)
res = 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.*;
public class GFG {
static int getTotalXorOfSubarrayXors(
int arr[], int N)
{
int res = 0 ;
for ( int i = 0 ; i < N; i++)
{
int freq = (i + 1 ) * (N - i);
if (freq % 2 == 1 )
res = 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));
}
}
|
Python 3
def getTotalXorOfSubarrayXors(arr, N):
res = 0
for i in range ( 0 , N):
freq = (i + 1 ) * (N - i)
if (freq % 2 = = 1 ):
res = res ^ arr[i]
return res
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)
{
int res = 0;
for ( int i = 0; i < N; i++)
{
int freq = (i + 1) * (N - i);
if (freq % 2 == 1)
res = res ^ arr[i];
}
return res;
}
public 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 )
{
$res = 0;
for ( $i = 0; $i < $N ; $i ++)
{
$freq = ( $i + 1) * ( $N - $i );
if ( $freq % 2 == 1)
$res = $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)
{
let res = 0;
for (let i = 0; i < N; i++)
{
let freq = (i + 1) * (N - i);
if (freq % 2 == 1)
res = 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)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...