The XOR total of an array is defined as the bitwise XOR of all its elements, or 0 if the array is empty.
[2,5,6] is 2 XOR 5 XOR 6 = 1.You are given an array nums, return the sum of all XOR totals for every subset of nums.
Note: Subsets with the same elements should be counted multiple times.
An array a is a subset of an array b if a can be obtained from b by deleting some (possibly zero) elements of b.
Example 1:
Input: nums = [2,4]
Output: 12Explanation: The four subsets of [2,4] are
Example 2:
Input: [3,1,1]
Output: 12Explanation: The eight subsets of [3,1,1] are
Constraints:
1 <= nums.length <= 121 <= nums[i] <= 20We need to generate all possible subsets and compute the XOR total of each. The standard backtracking approach builds subsets by deciding for each element whether to include it or not. At each step, we compute the XOR of the current subset and add it to our running total.
res = 0 to accumulate the sum of XOR totals.index and the current subset:subset and add it to res.index:subset.backtrack with the next index.subset.index 0 with an empty subset.res.class Solution:
def subsetXORSum(self, nums: List[int]) -> int:
res = 0
def backtrack(i, subset):
nonlocal res
xorr = 0
for num in subset:
xorr ^= num
res += xorr
for j in range(i, len(nums)):
subset.append(nums[j])
backtrack(j + 1, subset)
subset.pop()
backtrack(0, [])
return resA cleaner recursive approach avoids explicitly building subsets. For each element, we make a binary choice: include it in the XOR or skip it. We pass the running XOR total down the recursion tree. When we reach the end of the array, we return the accumulated XOR value. The sum of all returned values gives us the total.
dfs(i, total) where i is the current index and total is the running XOR:i == len(nums), return total.dfs(i + 1, total ^ nums[i]) + dfs(i + 1, total).nums[i] in the XOR.nums[i].dfs(0, 0) and return the result.Every subset can be represented by a bitmask where bit i indicates whether element i is included. With n elements, there are 2^n subsets corresponding to masks from 0 to 2^n - 1. We iterate through all masks, compute the XOR of selected elements for each mask, and sum them up.
res = 0.mask from 0 to 2^n - 1:xorr = 0.i from 0 to n - 1:i is set in the mask, XOR nums[i] into xorr.xorr to res.res.Each bit position in any number contributes independently to the XOR totals. For any bit that is set in at least one number, exactly half of all subsets will have that bit set in their XOR result (because each element either flips the bit or not, and the combinations balance out). The OR of all numbers gives us all bits that appear in at least one element. Each such bit contributes its value multiplied by 2^(n-1) subsets.
nums. This gives a number where each bit is set if and only if that bit is set in at least one element.result by n - 1 positions (multiply by 2^(n-1)).result.The problem includes all subsets, including the empty subset which has an XOR value of 0. While this does not affect the sum, a common conceptual error is starting the subset generation from size 1 instead of size 0, or incorrectly counting the total number of subsets as 2^n - 1 instead of 2^n. This can lead to off-by-one issues in verification.
A frequent mistake is confusing XOR with addition or other operations. XOR has the property that a ^ a = 0 and a ^ 0 = a. When computing subset XOR totals, some assume that duplicate elements cancel out across all subsets, but each subset is computed independently. Also, the optimal solution relies on understanding that each bit appears in exactly half of all subsets, which requires grasping XOR's bit-level behavior.