-
Notifications
You must be signed in to change notification settings - Fork 277
/
Copy pathMaximumXorOfTwoNumbersInAnArray.java
70 lines (62 loc) · 1.44 KB
/
MaximumXorOfTwoNumbersInAnArray.java
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// class Solution {
// public int findMaximumXOR(int[] nums) {
// int ans = 0;
// for(int el1: nums){
// for(int el2: nums){
// ans = Math.max(ans, el1 ^el2);
// }
// }
// return ans;
// }
// }
class Solution {
class Trie {
Trie[] children;
public Trie() {
children = new Trie[2];
}
}
private Trie root;
private void buildTrie(int[] nums){
for(int num: nums) {
Trie curNode = root;
for(int i = 31; i >= 0; i --) {
int curBit = (num >>> i) & 1;
if(curNode.children[curBit] == null) {
curNode.children[curBit] = new Trie();
}
curNode = curNode.children[curBit];
}
}
}
private int findMaxXorForCurrentEl(int num){
Trie curNode = root;
int targetNum = 0;
for(int i = 31; i >= 0; i --) {
int curBit = (num >>> i) & 1;
int targetBit = curBit == 0 ? 1:0;
if(curNode.children[targetBit] != null) {
targetNum = targetNum*2 +targetBit;
curNode = curNode.children[targetBit];
}else {
targetNum = targetNum*2 +curBit;
curNode = curNode.children[curBit];
}
}
return targetNum;
}
public int findMaximumXOR(int[] nums) {
if(nums == null || nums.length == 0) {
return 0;
}
// Init Trie.
root = new Trie();
buildTrie(nums);
int max = Integer.MIN_VALUE;
for(int num: nums) {
int maxXORForNum = findMaxXorForCurrentEl(num);
max = Math.max(maxXORForNum ^ num, max);
}
return max;
}
}