-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2935-MaximumStrongPairXORII.go
More file actions
136 lines (120 loc) · 4.31 KB
/
2935-MaximumStrongPairXORII.go
File metadata and controls
136 lines (120 loc) · 4.31 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
// 2935. Maximum Strong Pair XOR II
// 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:
// |x - y| <= min(x, y)
// 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 = [500,520,2500,3000]
// Output: 1020
// Explanation: There are 6 strong pairs in the array nums: (500, 500), (500, 520), (520, 520), (2500, 2500), (2500, 3000) and (3000, 3000).
// The maximum XOR possible from these pairs is 500 XOR 520 = 1020 since the only other non-zero XOR value is 2500 XOR 3000 = 636.
// Constraints:
// 1 <= nums.length <= 5 * 10^4
// 1 <= nums[i] <= 2^20 - 1
import "fmt"
import "sort"
import "math"
type Trie struct {
Head *Node
MaxBit int
}
type Node struct {
Child0 *Node
Child1 *Node
LastIdx int
}
func NewTrie(maxBit int) *Trie {
return &Trie{ &Node{}, maxBit, }
}
func (t *Trie) Insert(num, index int) {
temp, i := t.Head, t.MaxBit
for i >= 0 {
bit := (num & (1 << i))
if bit == 0 {
if temp.Child0 == nil {
temp.Child0 = &Node{}
}
temp = temp.Child0
} else {
if temp.Child1 == nil {
temp.Child1 = &Node{}
}
temp = temp.Child1
}
temp.LastIdx = index
i--
}
}
func (t *Trie) GetMaxXor(num, minIdx int) int {
temp, i, maxXor := t.Head, t.MaxBit, 0
for i >= 0 {
bit := (num & (1 << i))
if bit == 0 {
if temp.Child1 != nil && temp.Child1.LastIdx >= minIdx {
temp = temp.Child1
maxXor |= (1 << i)
} else {
temp = temp.Child0
}
} else {
if temp.Child0 != nil && temp.Child0.LastIdx >= minIdx {
temp = temp.Child0
maxXor |= (1 << i)
} else {
temp = temp.Child1
}
}
i--
}
return maxXor
}
func maximumStrongPairXor(nums []int) int {
sort.Ints(nums)
n, i, j, res := len(nums), 0, 0, 0
t := NewTrie(int(math.Log2(float64(nums[n-1]))))
max := func (x, y int) int { if x > y { return x; }; return y; }
for j < n {
t.Insert(nums[j], j)
half := int(math.Ceil(float64(nums[j]) / 2.0))
for nums[i] < half {
i++
}
res = max(res, t.GetMaxXor(nums[j], i))
j++
}
return res
}
func main() {
// 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.
fmt.Println(maximumStrongPairXor([]int{1,2,3,4,5})) // 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.
fmt.Println(maximumStrongPairXor([]int{10,100})) // 0
// Example 3:
// Input: nums = [500,520,2500,3000]
// Output: 1020
// Explanation: There are 6 strong pairs in the array nums: (500, 500), (500, 520), (520, 520), (2500, 2500), (2500, 3000) and (3000, 3000).
// The maximum XOR possible from these pairs is 500 XOR 520 = 1020 since the only other non-zero XOR value is 2500 XOR 3000 = 636.
fmt.Println(maximumStrongPairXor([]int{500,520,2500,3000})) // 1020
}