# Hackerrank Solution: Maximizing XOR

Original Problem

Given two integers, $$L$$ and $$R$$, find the maximal value of $$A\oplus B$$, where $$A$$ and $$B$$ satisfy the following condition:

$$L\leq A\leq B \leq R$$

Input Format

The input contains two lines; $$L$$ is present in the first line and $$R$$ in the second line.

Constraints
$$1\leq L\leq R\leq 10^3$$

Output Format

The maximal value as mentioned in the problem statement.

Sample Input

10
15


Sample Output

7


Explanation

The input tells us that $$L=10$$ and $$R=15$$. All the pairs which comply to above condition are the following:
$$10\oplus 10= 0$$
$$10\oplus 11= 1$$
$$10\oplus 12= 6$$
$$10\oplus 13= 7$$
$$10\oplus 14= 4$$
$$10\oplus 15= 5$$
$$11\oplus 11= 0$$
$$11\oplus 12= 7$$
$$11\oplus 13= 6$$
$$11\oplus 14= 5$$
$$11\oplus 15= 4$$
$$12\oplus 12= 0$$
$$12\oplus 13= 1$$
$$12\oplus 14= 2$$
$$12\oplus 15= 3$$
$$13\oplus 13= 0$$
$$13\oplus 14= 3$$
$$13\oplus 15= 2$$
$$14\oplus 14= 0$$
$$14\oplus 15= 1$$
$$15\oplus 15= 0$$

Here two pairs (10, 13) and (11, 12) have maximum xor value 7, and this is the answer.

## Solution

This problem can be solved quite trivial by implementing the solution the way it is stated in the description.

def maxXor(l, r)
m = 0
for a in l..r
for b in a..r
m = (a ^ b) if (a ^ b) > m
end
end
return m
end

However, that's not optimal, since the solution is bound to $$O(lr)$$. They say the maximum for the example is (10, 13) and (11, 12), so let's dig into these numbers:

10: 001010
13: 001101
^^: 000111

11: 001011
12: 001100
^^: 000111

XOR is always true if the bits are different. When we look into the original numbers $$l$$ and $$r$$, we see that

10: 001010
15: 001111

If we would XOR these two numbers, the result would be

^^: 000101

It's not the maximum value we're looking for, but the most significant bit (MSB) is already correct, which means we need to flood the rest with 1 in order to maximize the value in the interval. This means that the maximum depends exclusively on the highest bit set. The MSB is equal to the integer log base 2. In order to maximize the value, we shift the MSB one to the left and subtract 1:

$f(l, r) = 2 \lfloor\log_2(l\oplus r)\rfloor - 1$

The only remaining question is, how can we find an efficient implementation for that. We can loop through the bits, which is bound to $$O(r)$$, since $$l\leq r$$:

def maxXor(l, r)
s = l ^ r
p = 1
while s > 0 do
p<<= 1
s>>= 1
end
return p - 1
end

Another idea is to use ruby to make a string representation of the XOR-value and use the length of that as MSB:

def maxXor(l, r)
return (1 << (l ^ r).to_s(2).size) - 1
end

« Back to problem overview
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 1, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.7608550786972046, "perplexity": 801.3851627085062}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.3, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2019-47/segments/1573496668644.10/warc/CC-MAIN-20191115120854-20191115144854-00081.warc.gz"}