Created
May 11, 2019 15:20
-
-
Save dsc712/32c5fd4ac70e6aec3f69dc2edb6adada to your computer and use it in GitHub Desktop.
max xor b/w a range
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
int getMax(int L, int R) { | |
int LxR = L ^ R; | |
int p = 0; | |
// msb of L ^ R | |
while(LxR) { | |
p++; | |
LxR >>= 1; | |
} | |
int max = 0; | |
int two = 1; | |
while(p--) { | |
max += two; | |
two <<= 1; | |
} | |
return max; | |
} | |
int main() { | |
#ifndef ONLINE_JUDGE | |
freopen("input.txt", "r", stdin); | |
freopen("output.txt", "w", stdout); | |
#endif | |
int tc,L,R; | |
cin>>tc; | |
while(tc--) { | |
cin>>L>>R; | |
cout<<getMax(L,R)<<"\n"; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment