Skip to content

Instantly share code, notes, and snippets.

@thomascrha
Last active August 29, 2015 14:14
Show Gist options
  • Save thomascrha/d16201af70917fff1f11 to your computer and use it in GitHub Desktop.
Save thomascrha/d16201af70917fff1f11 to your computer and use it in GitHub Desktop.
Maximizing XOR
/* Problem Statement
Given two integers: L and R,
find the maximal values of A xor B given, L ≤ A ≤ B ≤ R
Input Format
The input contains two lines, L is present in the first line.
R in the second line.
Constraints
1 ≤ L ≤ R ≤ 103
Output Format
The maximal value as mentioned in the problem statement.*/
function maxXor(l, r) {
var lConst = l;
var rConst = r;
var varL = l;
var varR = r;
var high = 0;
while (lConst <= r) {
for (var i = 1; varL <= varR; varL++) {
var num = lConst ^ varL;
if(num >= high){
high = num;
}
}
varL = lConst;
lConst++;
}
return high;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment