hackerrank-solutions/algorithms/bit-manipulation/maximizing-xor
2016-09-12 16:22:59 +03:00
..
maxXor.cpp maximizing-xor: c++ implementation 2016-09-12 16:22:59 +03:00
maxXor.py maximizing-xor: c++ implementation 2016-09-12 16:22:59 +03:00
README.md maximizing-xor: initial commit 2016-09-12 14:58:05 +03:00

https://www.hackerrank.com/challenges/maximizing-xor

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

L <= A <= B <= R

#Input Format

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

#Constraints

1 <= L <= L <= 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 xor 10 = 0
10 xor 11 = 1
10 xor 12 = 6
10 xor 13 = 7
10 xor 14 = 4
10 xor 15 = 5
11 xor 11 = 0
11 xor 12 = 7
11 xor 13 = 6
11 xor 14 = 5
11 xor 15 = 4
12 xor 12 = 0
12 xor 13 = 1
12 xor 14 = 2
12 xor 15 = 3
13 xor 13 = 0
13 xor 14 = 3
13 xor 15 = 2
14 xor 14 = 0
14 xor 15 = 1
15 xor 15 = 0

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