Find the Number Whose Sum of XOR with Given Array Range is Maximum using C++

To solve a problem in which we are given an array and some queries. Now in each query, we are given a range. Now we need to find a number such that the sum of their xor with x is maximized, for example

Input : A = {20, 11, 18, 2, 13}
Three queries as (L, R) pairs
1 3
3 5
2 4
Output : 2147483629
2147483645
2147483645

In this problem, we are going to take a prefix count of 1’s present in the numbers at each position now as we have precalculated our number of ones, so for finding the number of ones in the given range from L to R, we need to subtract presumed till R with presumed till L.

Approach to Find the Solution

In this approach, as we are required to find the maximum sum, so we need to make the majority of bits of xor equal to 1; hence we check if for any bit if one is more than the number of 0’s so we reset that bit of x as now majority of number has that bit equal to 1 so when we pair that majority 1’s with 0’s so eventually we have the majority of that bit equal to 1 thus this is how we maximize our answer.

Example

C++ Code for the Above Approach

#include 
using namespace std;
#define MAX 2147483647 // 2^31 - 1
int prefix[100001][32]; // the prefix array
void prefix_bit(int A[], int n){ // taking prefix count of 1's present
    for (int j = 0; j = numberofbits - x){ // is number of 1's is more than number of 0's
            int currentbit = 1 

Output

2147483629
2147483647
2147483629

Explanation of the Above Code

In this approach, we first calculate the prefix count of 1’s present for each bit. Now when we get this count, we have solved our biggest problem that was traversing the queries. As of now, we are not going to traverse through each range. So we can calculate that through our prefix array now. Our main logic is that we calculate the number of resets and set bits in the range when we encounter a position where the set bit number is more than the number of reset bits. Hence, we reset that bit in our x now as we have initialized x with 2^31 - 1 so all of its bits will be set, and we find our answer by toggling the bits in x.

Conclusion

In this tutorial, we solve a problem to find the number whose sum of XOR with a given array range is maximum. We also learned the C++ program for this problem and the complete approach (Normal) by which we solved this problem. We can write the same program in other languages such as C, java, python, and other languages. We hope you find this tutorial helpful.

Updated on: 2021-11-25T09:31:43+05:30

356 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements