HackerRank 'Maximizing XOR' Solution

Martin Kysel · February 26, 2015

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:

  • L≤A≤B≤R

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)

Twitter, Facebook

To learn more about solving Coding Challenges in Python, I recommend these courses: Educative.io Python Algorithms, Educative.io Python Coding Interview.