Description#
You are given a 0-indexed integer array nums
. A pair of integers x
and y
is called a strong pair if it satisfies the condition:
You need to select two integers from nums
such that they form a strong pair and their bitwise XOR
is the maximum among all strong pairs in the array.
Return the maximum XOR
value out of all possible strong pairs in the array nums
.
Note that you can pick the same integer twice to form a pair.
Example 1:
Input: nums = [1,2,3,4,5]
Output: 7
Explanation: There are 11 strong pairs in the array nums
: (1, 1), (1, 2), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (3, 5), (4, 4), (4, 5) and (5, 5).
The maximum XOR possible from these pairs is 3 XOR 4 = 7.
Example 2:
Input: nums = [10,100]
Output: 0
Explanation: There are 2 strong pairs in the array nums
: (10, 10) and (100, 100).
The maximum XOR possible from these pairs is 10 XOR 10 = 0 since the pair (100, 100) also gives 100 XOR 100 = 0.
Example 3:
Input: nums = [5,6,25,30]
Output: 7
Explanation: There are 6 strong pairs in the array nums
: (5, 5), (5, 6), (6, 6), (25, 25), (25, 30) and (30, 30).
The maximum XOR possible from these pairs is 25 XOR 30 = 7 since the only other non-zero XOR value is 5 XOR 6 = 3.
Constraints:
1 <= nums.length <= 50
1 <= nums[i] <= 100
Solutions#
Solution 1: Enumeration#
We can enumerate each pair of numbers $(x, y)$ in the array. If $|x - y| \leq \min(x, y)$, then this pair is a strong pair. We can calculate the XOR value of this pair and update the answer.
The time complexity is $O(n^2)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$.
1
2
3
| class Solution:
def maximumStrongPairXor(self, nums: List[int]) -> int:
return max(x ^ y for x in nums for y in nums if abs(x - y) <= min(x, y))
|
1
2
3
4
5
6
7
8
9
10
11
12
13
| class Solution {
public int maximumStrongPairXor(int[] nums) {
int ans = 0;
for (int x : nums) {
for (int y : nums) {
if (Math.abs(x - y) <= Math.min(x, y)) {
ans = Math.max(ans, x ^ y);
}
}
}
return ans;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| class Solution {
public:
int maximumStrongPairXor(vector<int>& nums) {
int ans = 0;
for (int x : nums) {
for (int y : nums) {
if (abs(x - y) <= min(x, y)) {
ans = max(ans, x ^ y);
}
}
}
return ans;
}
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| func maximumStrongPairXor(nums []int) (ans int) {
for _, x := range nums {
for _, y := range nums {
if abs(x-y) <= min(x, y) {
ans = max(ans, x^y)
}
}
}
return
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
|
1
2
3
4
5
6
7
8
9
10
11
| function maximumStrongPairXor(nums: number[]): number {
let ans = 0;
for (const x of nums) {
for (const y of nums) {
if (Math.abs(x - y) <= Math.min(x, y)) {
ans = Math.max(ans, x ^ y);
}
}
}
return ans;
}
|
Solution 2: Sorting + Binary Trie#
Observing the inequality $|x - y| \leq \min(x, y)$, which involves absolute value and minimum value, we can assume $x \leq y$, then we have $y - x \leq x$, that is, $y \leq 2x$. We can enumerate $y$ from small to large, then $x$ must satisfy the inequality $y \leq 2x$.
Therefore, we sort the array $nums$, and then enumerate $y$ from small to large. We use two pointers to maintain a window so that the elements $x$ in the window satisfy the inequality $y \leq 2x$. We can use a binary trie to maintain the elements in the window, so we can find the maximum XOR value in the window in $O(1)$ time. Each time we add $y$ to the trie, and remove the elements at the left end of the window that do not satisfy the inequality, this can ensure that the elements in the window satisfy the inequality $y \leq 2x$. Then query the maximum XOR value from the trie and update the answer.
The time complexity is $O(n \times \log M)$, and the space complexity is $O(n \times \log M)$. Here, $n$ is the length of the array $nums$, and $M$ is the maximum value in the array $nums$.
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
| class Trie:
__slots__ = ("children", "cnt")
def __init__(self):
self.children: List[Trie | None] = [None, None]
self.cnt = 0
def insert(self, x: int):
node = self
for i in range(7, -1, -1):
v = x >> i & 1
if node.children[v] is None:
node.children[v] = Trie()
node = node.children[v]
node.cnt += 1
def search(self, x: int) -> int:
node = self
ans = 0
for i in range(7, -1, -1):
v = x >> i & 1
if node.children[v ^ 1] and node.children[v ^ 1].cnt:
ans |= 1 << i
node = node.children[v ^ 1]
else:
node = node.children[v]
return ans
def remove(self, x: int):
node = self
for i in range(7, -1, -1):
v = x >> i & 1
node = node.children[v]
node.cnt -= 1
class Solution:
def maximumStrongPairXor(self, nums: List[int]) -> int:
nums.sort()
tree = Trie()
ans = i = 0
for y in nums:
tree.insert(y)
while y > nums[i] * 2:
tree.remove(nums[i])
i += 1
ans = max(ans, tree.search(y))
return ans
|
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
| class Trie {
private Trie[] children = new Trie[2];
private int cnt = 0;
public Trie() {
}
public void insert(int x) {
Trie node = this;
for (int i = 7; i >= 0; --i) {
int v = x >> i & 1;
if (node.children[v] == null) {
node.children[v] = new Trie();
}
node = node.children[v];
++node.cnt;
}
}
public int search(int x) {
Trie node = this;
int ans = 0;
for (int i = 7; i >= 0; --i) {
int v = x >> i & 1;
if (node.children[v ^ 1] != null && node.children[v ^ 1].cnt > 0) {
ans |= 1 << i;
node = node.children[v ^ 1];
} else {
node = node.children[v];
}
}
return ans;
}
public void remove(int x) {
Trie node = this;
for (int i = 7; i >= 0; --i) {
int v = x >> i & 1;
node = node.children[v];
--node.cnt;
}
}
}
class Solution {
public int maximumStrongPairXor(int[] nums) {
Arrays.sort(nums);
Trie tree = new Trie();
int ans = 0, i = 0;
for (int y : nums) {
tree.insert(y);
while (y > nums[i] * 2) {
tree.remove(nums[i++]);
}
ans = Math.max(ans, tree.search(y));
}
return ans;
}
}
|
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
| class Trie {
public:
Trie* children[2];
int cnt;
Trie()
: cnt(0) {
children[0] = nullptr;
children[1] = nullptr;
}
void insert(int x) {
Trie* node = this;
for (int i = 7; ~i; --i) {
int v = (x >> i) & 1;
if (node->children[v] == nullptr) {
node->children[v] = new Trie();
}
node = node->children[v];
++node->cnt;
}
}
int search(int x) {
Trie* node = this;
int ans = 0;
for (int i = 7; ~i; --i) {
int v = (x >> i) & 1;
if (node->children[v ^ 1] != nullptr && node->children[v ^ 1]->cnt > 0) {
ans |= 1 << i;
node = node->children[v ^ 1];
} else {
node = node->children[v];
}
}
return ans;
}
void remove(int x) {
Trie* node = this;
for (int i = 7; ~i; --i) {
int v = (x >> i) & 1;
node = node->children[v];
--node->cnt;
}
}
};
class Solution {
public:
int maximumStrongPairXor(vector<int>& nums) {
sort(nums.begin(), nums.end());
Trie* tree = new Trie();
int ans = 0, i = 0;
for (int y : nums) {
tree->insert(y);
while (y > nums[i] * 2) {
tree->remove(nums[i++]);
}
ans = max(ans, tree->search(y));
}
return ans;
}
};
|
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
| type Trie struct {
children [2]*Trie
cnt int
}
func newTrie() *Trie {
return &Trie{}
}
func (t *Trie) insert(x int) {
node := t
for i := 7; i >= 0; i-- {
v := (x >> uint(i)) & 1
if node.children[v] == nil {
node.children[v] = newTrie()
}
node = node.children[v]
node.cnt++
}
}
func (t *Trie) search(x int) int {
node := t
ans := 0
for i := 7; i >= 0; i-- {
v := (x >> uint(i)) & 1
if node.children[v^1] != nil && node.children[v^1].cnt > 0 {
ans |= 1 << uint(i)
node = node.children[v^1]
} else {
node = node.children[v]
}
}
return ans
}
func (t *Trie) remove(x int) {
node := t
for i := 7; i >= 0; i-- {
v := (x >> uint(i)) & 1
node = node.children[v]
node.cnt--
}
}
func maximumStrongPairXor(nums []int) (ans int) {
sort.Ints(nums)
tree := newTrie()
i := 0
for _, y := range nums {
tree.insert(y)
for ; y > nums[i]*2; i++ {
tree.remove(nums[i])
}
ans = max(ans, tree.search(y))
}
return ans
}
|
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
| class Trie {
children: (Trie | null)[];
cnt: number;
constructor() {
this.children = [null, null];
this.cnt = 0;
}
insert(x: number): void {
let node: Trie | null = this;
for (let i = 7; i >= 0; i--) {
const v = (x >> i) & 1;
if (node.children[v] === null) {
node.children[v] = new Trie();
}
node = node.children[v] as Trie;
node.cnt++;
}
}
search(x: number): number {
let node: Trie | null = this;
let ans = 0;
for (let i = 7; i >= 0; i--) {
const v = (x >> i) & 1;
if (node.children[v ^ 1] !== null && (node.children[v ^ 1] as Trie).cnt > 0) {
ans |= 1 << i;
node = node.children[v ^ 1] as Trie;
} else {
node = node.children[v] as Trie;
}
}
return ans;
}
remove(x: number): void {
let node: Trie | null = this;
for (let i = 7; i >= 0; i--) {
const v = (x >> i) & 1;
node = node.children[v] as Trie;
node.cnt--;
}
}
}
function maximumStrongPairXor(nums: number[]): number {
nums.sort((a, b) => a - b);
const tree = new Trie();
let ans = 0;
let i = 0;
for (const y of nums) {
tree.insert(y);
while (y > nums[i] * 2) {
tree.remove(nums[i++]);
}
ans = Math.max(ans, tree.search(y));
}
return ans;
}
|