HackerRank ‘Maximizing XOR’ Solution

H
Short Problem Definition:

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

  • LABR
Link

Maximizing XOR

Complexity:

time complexity is O(N^2);

space complexity is O(1)

Execution:

Based on the constraints, you can search by using brute force.

Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/py
def  maxXor( l,  r):
  max_xor = 0
  for low in xrange(l ,r+1):
    for high in xrange(low, r+1):
        max_xor = max(max_xor, low ^ high)
  return max_xor 
 
if __name__ == '__main__':
    l = int(raw_input());
    r = int(raw_input());
 
    res = maxXor(l, r);
    print(res)

About the author

Add comment

Archives

Categories