-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathpartition-array-for-maximum-xor-and-and.cpp
More file actions
171 lines (163 loc) · 5.71 KB
/
partition-array-for-maximum-xor-and-and.cpp
File metadata and controls
171 lines (163 loc) · 5.71 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Time: O(nlogr * 2^n)
// Space: O(2^n)
// bitmasks, greedy
class Solution {
public:
long long maximizeXorAndXor(vector<int>& nums) {
const auto& bit_length = [](int x) {
return (x ? std::__lg(x) : -1) + 1;
};
const int l = bit_length(ranges::max(nums));
const int n = size(nums);
const int full_mask = (1 << n) - 1;
vector<int> and_arr(1 << n);
vector<int> xor_arr(1 << n);
for (int mask = 1; mask < (1 << n); ++mask) {
const auto& lb = mask & -mask;
const auto& i = __builtin_ctz(lb);
and_arr[mask] = (mask ^ lb) ? (and_arr[mask ^ lb] & nums[i]) : nums[i];
xor_arr[mask] = xor_arr[mask ^ lb] ^ nums[i];
}
int64_t result = 0;
vector<int> base(l); // to improve performance
for (int mask = 1; mask < (1 << n); ++mask) {
const auto& total_and = and_arr[mask];
const auto& total_xor = xor_arr[full_mask ^ mask];
base.assign(l, 0);
for (int remain = full_mask ^ mask; remain; remain &= remain - 1) {
const int i = __builtin_ctz(remain);
int x = nums[i] & ~total_xor;
for (int i = l - 1; i >= 0; --i) {
if (!(x & (1 << i))) {
continue;
}
if (base[i] == 0) {
base[i] = x;
break;
}
x ^= base[i];
}
}
int max_xor = 0;
for (int i = l - 1; i >= 0; --i) {
if ((max_xor ^ base[i]) > max_xor) {
max_xor ^= base[i];
}
}
result = max(result, total_and + total_xor + static_cast<int64_t>(2) * max_xor);
}
return result;
}
};
// Time: O(nlogr * 2^n)
// Space: O(2^n)
// bitmasks, greedy
class Solution2 {
public:
long long maximizeXorAndXor(vector<int>& nums) {
const auto& bit_length = [](int x) {
return (x ? std::__lg(x) : -1) + 1;
};
const int l = bit_length(ranges::max(nums));
const auto& max_xor_subset = [&](const vector<int>& nums) { // Time: O(nlogr)
vector<int> base(l);
for (auto x : nums) { // gaussian elimination over GF(2)
for (int i = l - 1; i >= 0; --i) {
if (!(x & (1 << i))) {
continue;
}
if (base[i] == 0) {
base[i] = x;
break;
}
x ^= base[i];
}
}
int max_xor = 0;
for (int i = l - 1; i >= 0; --i) { // greedy
if ((max_xor ^ base[i]) > max_xor) {
max_xor ^= base[i];
}
}
return max_xor;
};
const int n = size(nums);
const int full_mask = (1 << n) - 1;
vector<int> and_arr(1 << n);
vector<int> xor_arr(1 << n);
for (int mask = 1; mask < (1 << n); ++mask) {
const auto& lb = mask & -mask;
const auto& i = __builtin_ctz(lb);
and_arr[mask] = (mask ^ lb) ? (and_arr[mask ^ lb] & nums[i]) : nums[i];
xor_arr[mask] = xor_arr[mask ^ lb] ^ nums[i];
}
int64_t result = 0;
for (int mask = 1; mask < (1 << n); ++mask) {
const auto& total_and = and_arr[mask];
const auto& total_xor = xor_arr[full_mask ^ mask];
vector<int> needs;
for (int i = 0; i < n; ++i) {
if (!(mask & (1 << i))) {
needs.emplace_back(nums[i] & ~total_xor);
}
}
result = max(result, total_and + total_xor + static_cast<int64_t>(2) * max_xor_subset(needs));
}
return result;
}
};
// Time: O(nlogr * 2^n)
// Space: O(1)
// bitmasks, greedy
class Solution3 {
public:
long long maximizeXorAndXor(vector<int>& nums) {
const auto& bit_length = [](int x) {
return (x ? std::__lg(x) : -1) + 1;
};
const int l = bit_length(ranges::max(nums));
const auto& max_xor_subset = [&](const vector<int>& nums) { // Time: O(nlogr)
vector<int> base(l);
for (auto x : nums) { // gaussian elimination over GF(2)
for (int i = l - 1; i >= 0; --i) {
if (!(x & (1 << i))) {
continue;
}
if (base[i] == 0) {
base[i] = x;
break;
}
x ^= base[i];
}
}
int max_xor = 0;
for (int i = l - 1; i >= 0; --i) { // greedy
if ((max_xor ^ base[i]) > max_xor) {
max_xor ^= base[i];
}
}
return max_xor;
};
const int n = size(nums);
int64_t result = 0;
for (int mask = 1; mask < (1 << n); ++mask) {
int and_val = -1;
int xor_val = 0;
for (int i = 0; i < n; ++i) {
if (mask & (1 << i)) {
and_val = (and_val != -1) ? (and_val & nums[i]) : nums[i];
} else {
xor_val ^= nums[i];
}
}
vector<int> needs;
for (int i = 0; i < n; ++i) {
if (!(mask & (1 << i))) {
needs.emplace_back(nums[i] & ~xor_val);
}
}
result = max(result, and_val + xor_val + static_cast<int64_t>(2) * max_xor_subset(needs));
}
return result;
}
};