HackerRank ‘Maximizing XOR’ Solution

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:
#!/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)

If you enjoyed this post, then make sure you subscribe to my Newsletter and/or Feed.

Facebooktwittergoogle_plusredditpinterestlinkedin