You are viewing a single comment's thread. Return to all comments →
Here is my Python solution!
def binary(num): remainders = [] while num != 0 : remainders.insert(0, str(num % 2)) num //= 2 return "".join(remainders) def xor(num1, num2): newnum = [] if len(num1) > len(num2): num2 = (len(num1) - len(num2)) * "0" + num2 elif len(num2) > len(num1): num1 = (len(num2) - len(num1)) * "0" + num1 for i in range(len(num1)): if num1[i] == num2[i]: newnum.append("0") else: newnum.append("1") return "".join(newnum) def decimal(num): newnum = 0 for i in range(len(num)): newnum += int(num[-i - 1]) * 2 ** i return newnum def maximizingXor(l, r): values = [] for num1 in range(l, r + 1): for num2 in range(num1 + 1, r + 1): values.append(decimal(xor(binary(num1), binary(num2)))) return max(values)
Seems like cookies are disabled on this browser, please enable them to open this website
I agree to HackerRank's Terms of Service and Privacy Policy.
Maximizing XOR
You are viewing a single comment's thread. Return to all comments →
Here is my Python solution!