Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

XOR of all subarray XORs | Set 2

  • Difficulty Level : Hard
  • Last Updated : 06 Jul, 2021

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 : 

Become a success story instead of just reading about them. Prepare for coding interviews at Amazon and other top product-based companies with our Amazon Test Series. Includes topic-wise practice questions on all important DSA topics along with 10 practice contests of 2 hours each. Designed by industry experts that will surely help you practice and sharpen your programming skills. Wait no more, start your preparation today!

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 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

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>

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 write.geeksforgeeks.org or mail your article to review-team@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.
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!