Last active
August 29, 2015 14:14
-
-
Save thomascrha/d16201af70917fff1f11 to your computer and use it in GitHub Desktop.
Maximizing XOR
This file contains hidden or 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
/* 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