Algorithm
Problem link- https://www.spoj.com/problems/MAXXOR/
MAXXOR - Find the max XOR value
You have two integers L and R, and you are required to find the max xor value of a and b where L <= a <= R and L <= b <= R
Input
Two integers in a line. L, R <= 1e9
Output
One integer, the answer
Example
Input: 1 10 Output: 15
Code Examples
#1 Code Example with Python Programming
Code -
Python Programming
l, r = map(int, input().split())
result = 0
while result < (l ^ r):
result = 2 * result + 1
print(result)
Copy The Code &
Try With Live Editor
Input
1 10
Output
15
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
unsigned int l, r;
unsigned int finalAns = 0;
int lDigits[32];
int rDigits[32];
cin >> l >> r;
for (int i = 0; i < 32; i++) {
lDigits[i] = l & 1;
l = l >> 1;
rDigits[i] = r & 1;
r = r >> 1;
}
for (int i = 31; i >= 0; i--)
if (lDigits[i] != rDigits[i]) {
finalAns = (1 << (i + 1)) - 1;
break;
}
cout << finalAns << "\n";
}
Copy The Code &
Try With Live Editor
Input
1 10
Output
15
Demonstration
SPOJ Solution-Find the max XOR value-Solution in C, C++, Java, Python