
- 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
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 Articles
- 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++
- Print all subarrays with 0 sum in C++
- Sum of All Possible Odd Length Subarrays in JavaScript
- Maximize the number of subarrays with XOR as zero in C++
- XOR of all subarray XORs in C++
- Program to find sum of all odd length subarrays in Python
- Find number of subarrays with even sum in C++
- Maximum Sum of Two Non-Overlapping Subarrays in C++
- Maximum Sum of 3 Non-Overlapping Subarrays in C++
- Minimizing array sum by applying XOR operation on all elements of the array in C++
- Largest sum of subarrays in JavaScript
- Program to find XOR sum of all pairs bitwise AND in Python
