-
Notifications
You must be signed in to change notification settings - Fork 80
/
MaximumXor.py
50 lines (39 loc) · 1.1 KB
/
MaximumXor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the maxXor function below.
def maxXor(arr, queries):
ans = []
trie = {}
k = len(bin(max(arr+queries))) - 2
for number in ['{:b}'.format(x).zfill(k) for x in arr]:
node = trie
for char in number:
node = node.setdefault(char, {})
for n in queries:
node = trie
s = ''
for char in'{:b}'.format(n).zfill(k) :
tmp = str(int(char) ^ 1)
tmp = tmp if tmp in node else char
s += tmp
node = node[tmp]
ans.append(int(s, 2) ^ n)
return ans
# solve here
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
n = int(input())
arr = list(map(int, input().rstrip().split()))
m = int(input())
queries = []
for _ in range(m):
queries_item = int(input())
queries.append(queries_item)
result = maxXor(arr, queries)
fptr.write('\n'.join(map(str, result)))
fptr.write('\n')
fptr.close()