

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sum of XOR of all subarrays in C++
In this problem, we are given an array arr[] of n numbers. Our task is to create a program to find the sum of XOR of all subarrays of the array.
Here, we need to find all sub-arrays of the given array, and then for each subarray, we will find the xor of element and add the XOR value to the sum variable.
Let’s take an example to understand the problem,
Input: arr[] = {5, 1, 4} Output: Explanation: XOR of all subarrays for the array : XOR {5} = 5 XOR {1} = 1 XOR {4} = 4 XOR {5,1} = 5^1 = 4 XOR {1, 4} = 1^4 = 5 XOR {5, 1, 4} = 5^1^4 = 0 Sum = 5 + 1 + 4 + 4 + 5 + 0 = 19
A simple method to solve this problem is using the next for loop to find all the subarrays. Then finding XOR of elements of the subarray and adding to the sum variable.
This solution is not efficient as it using nesting of loops and will have exponential time complexity.
An efficient approach to solve this problem is using the prefix array. This prefix array will store the xor of all elements of the array till i. i.e,
prefixarr[i] = arr[0]^arr[1]^ … ^arr[i].
After this, we can apply a simple formula to find XOR of elements from index i to j.
XOR(i-j) = prefixarr[j] - prefixarr[i]for i >= 0. If i = 0, XOR(i-j) = prefixarr[j]
Using this formula we will find the XOR of all subarrays.
Example
Program to illustrate the working of our solution,
#include <iostream> using namespace std; int calcSubArrayXORSum(int arr[], int n) { int sum = 0; int multiplier = 1; for (int i = 0; i < 30; i++) { int oddCount = 0; bool isOdd = 0; for (int j = 0; j < n; j++) { if ((arr[j] & (1 << i)) > 0) isOdd = (!isOdd); if (isOdd) oddCount++; } for (int j = 0; j < n; j++) { sum += (multiplier * oddCount); if ((arr[j] & (1 << i)) > 0) oddCount = (n - j - oddCount); } multiplier *= 2; } return sum; } int main() { int arr[] = { 3, 8, 13 }; int n = sizeof(arr) / sizeof(arr[0]); cout<<"Sum of XOR of all subarrays is "<<calcSubArrayXORSum(arr, n); return 0; }
Output
Sum of XOR of all subarrays is 46
- Related Questions & Answers
- C++ Queries on XOR of XORs of All Subarrays
- Sum of XOR of all possible subsets in C++
- Sum of XOR of sum of all pairs in an array in C++
- Sum of XOR of all pairs in an array in C++
- Sum of All Possible Odd Length Subarrays in JavaScript
- Print all subarrays with 0 sum in C++
- Maximize the number of subarrays with XOR as zero in C++
- XOR of all subarray XORs in C++
- Largest sum of subarrays in JavaScript
- Program to find sum of all odd length subarrays in Python
- Maximum Sum of 3 Non-Overlapping Subarrays in C++
- Maximum Sum of Two Non-Overlapping Subarrays in C++
- Find number of subarrays with even sum in C++
- XOR of Sum of every possible pair of an array in C++
- Program to find XOR sum of all pairs bitwise AND in Python