
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
XOR of all subarray XORs in C++
In this problem, we are given an array of n elements. Our task is to print XOR of XORs all possible subarrays (taken in order) created from elements of the array.
Let’s take an example to understand the problem,
Input − array = {1, 3, 6, 8}
Output − 0
Explanation −
(1) ^ (3) ^ (6) ^ (8) ^ (1^3) ^ (3^6)^ (6^8) ^ (1^3^6) ^ (3^6^8) ^ (1^3^6^8)
To solve this problem, a simple solution could be iterating over all the subarray and find xors. But this is an inefficient approach. A better approach could be counting the frequency of each element of the array that occurred in all subarrays and using the property of xor − xor of the element even number of times is 0. Using this we will ignore all values that occur even number of times in the sub-array list, now elements with odd occurrence frequency are to be considered i.e. xor of elements that have odd occurrence frequency will give the final result.
To find the occurrence of each element of the array in its sub-arrays, we will use this formula (i+1)*(n-i).
Using this formula, we will find the frequency of occurrence of each element and then consider those elements that have an odd frequency, xor then to get the final result.
Example
Program to show the implementation of our solution,
#include <iostream> using namespace std; int xorSubarrayXors(int arr[], int N){ int result = 0; for (int i = 0; i < N; i++){ int freqency = (i + 1) * (N - i); if (freqency % 2 == 1) result ^= arr[i]; } return result; } int main() { int arr[] = {1, 3, 6, 8}; int N = sizeof(arr) / sizeof(arr[0]); cout<<"The xor of all subarray xors is : "<<xorSubarrayXors(arr, N); return 0; }
Output
The xor of all subarray xors is : 0
- Related Articles
- C++ Queries on XOR of XORs of All Subarrays
- XOR of a subarray in C++
- XOR Queries of a Subarray in C++
- Sum of XOR of all subarrays in C++
- Sum of XOR of all possible subsets in C++
- XOR of all Prime numbers in an Array in C++
- Sum of XOR of all pairs in an array in C++
- Count all pairs with given XOR in C++
- Maximize the subarray sum after multiplying all elements of any subarray with X in C++
- Sum of XOR of sum of all pairs in an array in C++
- Maximum value of XOR among all triplets of an array in C++
- XOR of all the elements in the given range [L, R] in C++
- XOR of all elements of array with set bits equal to K in C++
- XOR of all the nodes in the sub-tree of the given node in C++
- Find Sum of all unique subarray sum for a given array in C++
