Minimum Operations to Make XOR of All Elements Zero Problem
Description
LeetCode Problem 4047.
You are given an integer array nums consisting of positive integers.
You may perform the following operation any number of times:
- Choose two distinct indices
iandjsuch thatnums[i] != nums[j], and replace eithernums[i]ornums[j]withnums[i] ^ nums[j], where^denotes the bitwise XOR.
Return the minimum number of operations required to make the bitwise XOR of all elements in nums equal to 0. If it is impossible, return -1.
Example 1:
Input: nums = [8,1,4,8,2]
Output: 3
Explanation:
One optimal sequence of operations is:
- Choose indices 0 and 1, and replace nums[0] with 8 ^ 1 = 9. The array becomes [9, 1, 4, 8, 2].
- Choose indices 2 and 3, and replace nums[3] with 4 ^ 8 = 12. The array becomes [9, 1, 4, 12, 2].
- Choose indices 0 and 4, and replace nums[0] with 9 ^ 2 = 11. The array becomes [11, 1, 4, 12, 2].
The XOR of all elements of nums is 11 ^ 1 ^ 4 ^ 12 ^ 2 = 0, so the answer is 3.
Example 2:
Input: nums = [1,2,3]
Output: 0
Explanation:
The XOR of all elements of nums is 1 ^ 2 ^ 3 = 0, so no operations are required.
Example 3:
Input: nums = [1,2,4]
Output: -1
Explanation:
It is impossible to make the XOR of all elements of nums equal to 0, so the answer is -1.
Constraints:
2 <= nums.length <= 10^51 <= nums[i] <= 2000
Solution
Approach: Shortest Subset with the Total XOR
Let X be the XOR of the whole array. Replacing a by a ^ b while b stays changes the total XOR from X to X ^ b. So every operation XORs the total with the value of the element that stays, and we need those staying values to XOR to X.
Using an element that was already changed never beats using the original values: creating a ^ b costs one operation that already contributes b, so the pair of operations contributes the same as using a and b directly. Hence the answer is the size m of the smallest set of elements whose XOR is X. Two special cases:
X == 0: no operation is needed.- All elements are equal: no operation is possible, so the answer is
-1. m == n(every element is needed): there is no spare element to take thea ^ bresult, so the answer is-1(this was confirmed by brute force on small arrays).
Using a value twice cancels out, so each distinct value is used at most once. Since all values are below 2048, run a BFS over the XOR values 0..2047: from x you can move to x ^ v for each distinct value v. The BFS distance to X is m; if X is unreachable the answer is -1.
Complexity: O(2048 * d) time and O(2048) space, where d <= 2000 is the number of distinct values.
class Solution:
def minOperations(self, nums: List[int]) -> int:
target = 0
for x in nums:
target ^= x
if target == 0:
return 0
values = set(nums)
if len(values) == 1: # no operation is possible
return -1
# fewest values (each used once) whose XOR is target: BFS over the XOR values below 2048
dist = [-1] * 2048
dist[0] = 0
frontier = [0]
while frontier and dist[target] < 0:
nxt = []
for x in frontier:
for v in values:
y = x ^ v
if dist[y] < 0:
dist[y] = dist[x] + 1
nxt.append(y)
frontier = nxt
if dist[target] < 0 or dist[target] == len(nums):
return -1
return dist[target]