Problem Statement: Given two numbers (int) L (low) and H (high) , The problem is to find the maximum value of A xor B such that L ≤ A ≤ B ≤ U.
C++ Code:
- #include <iostream>
- using namespace std;
- int maxXor(int l, int r)
- {
- int max = 0;
- for (int i = l; i <= r; ++i) {
- for (int j = l; j <= r; ++j) {
- if((i ^ j) >= max) {
- max = i ^ j;
- }
- }
- }
- return max;
- }
- int main()
- {
- int res, l, r;
- cin >> l;
- cin >> r;
- res = maxXor(l, r);
- cout << res;
- return 0;
- }