
- 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 the elements in the given range [L, R] in C++
In this problem, we are given two integer L and R denoting a range. Our task is to find xor of all elements within the range [L, R].
Let’s take an example to understand the problem,
Input − L=3, R = 6
Explanation − 3^4^5^6 =
To solve this problem, we will find the MSB of R. the MSB of the answer will not be greater than R. Now, we will find the parity of count of the number of bits from 0 to MSB.
Now, to find the parity count for an ith bit, we can see that the state of an ith bit will change on every 2ith number. The same is for all ith bit set in the range L to R. On doing this, two cases arise −
Case 1(i != 0) − Check ith bit of L. if it is set, check parity count of the number between L and L+2i. And if an ith bit of L is set, then L is odd, then the count is odd otherwise it is even. Now, we will move to R, and determine the parity of count of a number of elements between R-2i and R and follow the same method.
Rest all integers are not taken under consideration as they will generate even the number of an integer with ith bit set.
Case 2(i = 0) − here, we will have to consider the following case −
Case 2.1 − L and R both odd, count the number of integers with the 0th-bit set will be (R-L)/2+1.
Case 2.2 − Else, the count will be round down a number of (R-L+1)/2.
Example
Program to show the implementation of our solution,
#include <iostream> using namespace std; int findMSB(int x) { int ret = 0; while ((x >> (ret + 1)) != 0) ret++; return ret; } int XOREleInRange(int L, int R) { int max_bit = findMSB(R); int mul = 2; int ans = 0; for (int i = 1; i <= max_bit; i++) { if ((L / mul) * mul == (R / mul) * mul) { if (((L & (1 << i)) != 0) && (R - L + 1) % 2 == 1) ans += mul; mul *= 2; continue; } bool oddCount = 0; if (((L & (1 << i)) != 0) && L % 2 == 1) oddCount = (oddCount ^ 1); if (((R & (1 << i)) != 0) && R % 2 == 0) oddCount = (oddCount ^ 1); if (oddCount) ans += mul; mul *= 2; } int zero_bit_cnt = zero_bit_cnt = (R - L + 1) / 2; if (L % 2 == 1 && R % 2 == 1) zero_bit_cnt++; if (zero_bit_cnt % 2 == 1) ans++; return ans; } int main(){ int L = 1, R = 4; cout<<"The XOR of all element within the range ("<<L<<", "<<R<<") is : "<<XOREleInRange(L, R); return 0; }
Output
The XOR of all element within the range (1, 4) is : 4
- Related Articles
- Python – Test for all Even elements in the List for the given Range
- Find a range that covers all the elements of given N ranges in C++
- XOR of all the nodes in the sub-tree of the given node in C++
- Check if an array contains all elements of a given range in Python
- XOR of numbers that appeared even number of times in given Range in C++
- Count all pairs with given XOR in C++
- Minimizing array sum by applying XOR operation on all elements of the array in C++
- Find the Number Whose Sum of XOR with Given Array Range is Maximum using C++
- Count number of smallest elements in given range in C++
- Python Program for Number of elements with odd factors in the given range
- Number of indexes with equal elements in given range in C++
- XOR of all elements of array with set bits equal to K in C++
- Print all Good numbers in given range in C++
- C++ Queries on XOR of Greatest Odd Divisor of the Range
- Construct sum-array with sum of elements in given range in C++
