Partition Array for Maximum XOR and AND Problem
Description
LeetCode Problem 3630.
You are given an integer array nums.
Partition the array into three (possibly empty) subsequences A, B, and C such that every element of nums belongs to exactly one subsequence.
Your goal is to maximize the value of: XOR(A) + AND(B) + XOR(C)
where:
XOR(arr)denotes the bitwise XOR of all elements inarr. Ifarris empty, its value is defined as 0.AND(arr)denotes the bitwise AND of all elements inarr. Ifarris empty, its value is defined as 0.
Return the maximum value achievable.
Note: If multiple partitions result in the same maximum sum, you can consider any one of them.
Example 1:
Input: nums = [2,3]
Output: 5
Explanation:
One optimal partition is:
- A = [3], XOR(A) = 3
- B = [2], AND(B) = 2
- C = [], XOR(C) = 0
The maximum value of: XOR(A) + AND(B) + XOR(C) = 3 + 2 + 0 = 5. Thus, the answer is 5.
Example 2:
Input: nums = [1,3,2]
Output: 6
Explanation:
One optimal partition is:
- A = [1], XOR(A) = 1
- B = [2], AND(B) = 2
- C = [3], XOR(C) = 3
The maximum value of: XOR(A) + AND(B) + XOR(C) = 1 + 2 + 3 = 6. Thus, the answer is 6.
Example 3:
Input: nums = [2,3,6,7]
Output: 15
Explanation:
One optimal partition is:
- A = [7], XOR(A) = 7
- B = [2,3], AND(B) = 2
- C = [6], XOR(C) = 6
The maximum value of: XOR(A) + AND(B) + XOR(C) = 7 + 2 + 6 = 15. Thus, the answer is 15.
Constraints:
1 <= nums.length <= 191 <= nums[i] <= 10^9
Solution
Approach: Enumerate the AND Subset + Linear Basis
Enumerate which elements go into B; their AND is easy to precompute for every subset. The remaining elements, with total XOR T, must be split between A and C.
Since XOR(A) ^ XOR(C) = T, we have XOR(A) + XOR(C) = T + 2 * (XOR(A) & XOR(C)). On the bits where T is 1 the two values differ, so their AND is 0 there. On the bits where T is 0 they are equal, so the AND is just the bits of XOR(A) outside T. Hence we only need the largest value of XOR(A) & ~T over all subsets A of the remaining elements.
That is a maximum subset XOR: mask every remaining element with ~T, insert it into a linear basis, and greedily combine the basis vectors to get the maximum m. The value for this choice of B is AND(B) + T + 2 * m, and the answer is the maximum over all subsets.
Complexity: O(2^n * n * log M) time and O(2^n) space, where n is the length of the array and M the maximum value.
class Solution:
def maximizeXorAndXor(self, nums: List[int]) -> int:
n = len(nums)
full = (1 << n) - 1
xr = [0] * (1 << n) # xor of the chosen elements
an = [0] * (1 << n) # and of the chosen elements (0 for the empty set)
for mask in range(1, 1 << n):
low = mask & -mask
i = low.bit_length() - 1
rest = mask ^ low
xr[mask] = xr[rest] ^ nums[i]
an[mask] = nums[i] if rest == 0 else an[rest] & nums[i]
ones = (1 << 30) - 1
ans = 0
for b in range(1 << n):
rest = full ^ b
t = xr[rest]
keep = ones ^ t # bits where XOR(A) and XOR(C) must agree
basis = []
r = rest
while r:
low = r & -r
y = nums[low.bit_length() - 1] & keep
r ^= low
for v in basis:
y = min(y, y ^ v)
if y:
basis.append(y)
m = 0
for v in sorted(basis, reverse=True):
m = max(m, m ^ v)
ans = max(ans, an[b] + t + 2 * m)
return ans