
- 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 a subarray in C++
In this problem, we are given an arr[] and some queries that are range between L to R in the array. Our task is to print the XOR of the subarray between L to R.
Let’s take an example to understand the problem,
Input − array = {1, 4, 5, 7, 2, 9} L = 1 , R = 5
Output −
Explanation − 4^5^7^2^9
To solve this problem, we will create an array, based on the following observation,
We will XOR multiple bits, if there are odd number of 1s, the result will be 1 otherwise the result is 0.
Now, we will create a two-dimensional array count that will store the count of 1s. The value count[i][j] is the count of number of 1s for position i-j which is the number of 1’s that are present in subarray arr[0..j] at ith position of the bit. The number of 1s for all bits of the sub-array arr[L..R] is found using the count array. Formula to find arr[L...R] = count[i][R] - count[i][L-1]. If the number of 1s is odd, then ith bit is set in result. The final result can be obtained by summing up the power of 2 corresponding to ith bit given that it is set bit.
Program to show the implementation of our solution,
Example
#include <bits/stdc++.h> using namespace std; void preProcessArray(int arr[], int n, vector<vector<int> >& cnt) { int i, j; for (i = 0; i < 32; i++) { cnt[i][0] = 0; for (j = 0; j < n; j++) { if (j > 0) { cnt[i][j] = cnt[i][j - 1]; } if (arr[j] & (1 << i)) cnt[i][j]++; } } } int findXORofSubArray(int L, int R, const vector<vector<int> > count) { int result = 0; int noOfOnes; int i, j; for (i = 0; i < 32; i++) { noOfOnes = count[i][R] - ((L > 0) ? count[i][L - 1] : 0); if (noOfOnes & 1) { result+=(1 << i); } } return result; } int main(){ int arr[] = { 1, 4, 5, 7, 2, 9 }; int n = sizeof(arr) / sizeof(arr[0]); vector<vector<int> > count(32, vector<int>(n)); preProcessArray(arr, n, count); int L = 1; int R = 5; cout<<"The XOR of SubArray: "<<findXORofSubArray(L, R, count); return 0; }
Output
The XOR of SubArray: 13
- Related Articles
- XOR Queries of a Subarray in C++
- XOR of all subarray XORs in C++
- XOR of a submatrix queries in C++
- Find XOR of two number without using XOR operator in C++
- XOR Cipher in C++
- XOR of Prime Frequencies of Characters in a String in C++
- Sum of Subarray Minimums in C++
- Maximum XOR value of a pair from a range in C++
- Sum of XOR of all subarrays in C++
- Minimum value of “max + min” in a subarray in C++
- Maximum length of subarray such that sum of the subarray is even in C++
- Chalkboard XOR Game in C++
- Maximize number of 0s by flipping a subarray in C++
- Find the Number of Primes In A Subarray using C++
- Count number of subsets having a particular XOR value in C++
