15 November 2014

Quiz 47: Find maximum value of XOR

Problem:
Given 2 number, find maximum XOR between them

Input Format: 
num 1
num 2

Output Format: 
Max XOR

Constraints: 
none

Sample Input
5
6

Sample Output:
3

Explanations:
5 XOR 5=0
5 XOR 6=3
6 XOR 6=0, so max is 3.


Solution:

chomp($a=<STDIN>);
chomp($b=<STDIN>);
$c=0;
for($i=$a;$i<=$b;$i++)
{
for($j=$i;$j<=$b;$j++)
{
$tmp=$i ^ $j;
if($tmp>$c)
{
$c=$tmp;
}
}
}
print $c;


Tips:
Find XOR for all possible combinations and print highest among them

No comments:

Post a Comment