Imagine you're a digital engineer working with binary circuits, and you need to adjust the electrical signals to achieve a specific output pattern. You have an array of integers representing different circuit states, and your goal is to make their combined XOR equal to a target value k.
You can perform one type of operation: flip any bit in any number's binary representation. This means changing a 0 to 1 or a 1 to 0 at any position, including leading zeros.
Your mission: Find the minimum number of bit flips needed to make the XOR of all array elements equal to k.
Example: If you have nums = [2, 1, 3, 4] and k = 1, the current XOR is 2 ⊕ 1 ⊕ 3 ⊕ 4 = 4. You need to transform it to get XOR = 1.
Input & Output
Constraints
-
1 ≤ nums.length ≤ 105 -
0 ≤ nums[i] ≤ 106 -
0 ≤ k ≤ 106 - All array elements and k fit in 32-bit integers