Sum of All Subset XOR Totals (LeetCode 1863): Interview Notes
The XOR total of an array is the bitwise XOR of all its elements (0 if empty). Return the sum of XOR totals over every subset of nums.
Approach
Insight: each bit that appears in any element contributes to exactly half of the subsets. Equivalent closed form: OR of all nums, shifted left by (n-1).
def subsetXORSum(nums):
bits = 0
for x in nums:
bits |= x
return bits << (len(nums) - 1)
Complexity
Time O(n), space O(1).
Practice tip
Keep the same narration under time pressure with Linkjob.ai / coding interview copilot.
Disclosure: I work on Linkjob AI.