Sum of All Subset XOR Totals (LeetCode 1863): Solution and Explanation
The XOR total of a subset is the XOR of its elements, or 0 when it is empty. Given nums, add up the XOR totals of all 2^n subsets and return the sum.
Intuition
The direct approach enumerates every subset with backtracking, XORs its members and adds the result. With n at most 12 that is only 4,096 subsets, so it passes. But there is a much shorter argument that removes the enumeration entirely.
A sum of numbers can be split by bit position: the total equals the sum over bits b of 2^b times the number of subsets whose XOR has bit b set. So the question becomes, for each bit, how many subsets have it on?
If no number has bit b, no XOR can have it, so the count is 0. If some number x has bit b, pair each subset without x with the same subset plus x. Adding x flips bit b of the XOR, so in every pair exactly one subset has the bit on. The pairs cover all 2^n subsets, so exactly 2^(n-1) of them have bit b set.
Every bit that appears in at least one number therefore contributes 2^b * 2^(n-1), and the bits that appear in at least one number are exactly the bits of nums[0] | nums[1] | .... The whole sum collapses to that OR multiplied by 2^(n-1), which is a left shift by n - 1.
Approach: OR of all numbers, shifted
- Set
bits = 0. - For each number
x, dobits |= x. Nowbitshas a 1 in every position that appears in some number. - Each of those positions is on in exactly half of the
2^nsubsets. - Return
bits << (n - 1), which equalsbits * 2^(n-1).
Step-by-step example
nums = [1,3]
- bits starts at 0.
- OR in 1 (binary 01): bits = 01.
- OR in 3 (binary 11): bits = 11 = 3.
- There are 2^2 = 4 subsets. Bit 0 is on in 2 of them ([1] and [3]), contributing 1 x 2 = 2. Bit 1 is on in 2 of them ([3] and [1,3]), contributing 2 x 2 = 4.
- Total = 3 << 1 = 6, which matches 0 + 1 + 3 + 2.
Code: Python, Java, C++
1class Solution:
2 def subsetXORSum(self, nums: List[int]) -> int:
3 bits = 0
4 for x in nums:
5 bits |= x
6 return bits << (len(nums) - 1)1class Solution {
2 public int subsetXORSum(int[] nums) {
3 int bits = 0;
4 for (int x : nums) {
5 bits |= x;
6 }
7 return bits << (nums.length - 1);
8 }
9}1class Solution {
2public:
3 int subsetXORSum(vector<int>& nums) {
4 int bits = 0;
5 for (int x : nums) {
6 bits |= x;
7 }
8 return bits << (nums.size() - 1);
9 }
10};Time and space complexity
O(n)
One pass ORs the numbers together, then a single shift produces the answer.
O(1)
Only the running OR is stored.
Other approaches
Backtracking over subsets (time O(2^n), space O(n))
Recurse with an index and the XOR so far. At each element, either include it (XOR it in) or skip it, and add the XOR at each leaf. It is the natural first solution and easy to verify, and fast enough for n up to 12.
Bitmask enumeration (time O(n * 2^n), space O(1))
Loop over masks from 0 to 2^n - 1 and XOR the elements whose bits are set in the mask. Same work as backtracking without recursion.
Common mistakes
- Using XOR instead of OR when combining the numbers. Two equal numbers would cancel, even though each bit is still present in half of the subsets.
- Shifting by n instead of n - 1. Only half of the 2^n subsets have any given bit on.
- Forgetting that the empty subset counts. It contributes 0, which the formula already handles.
- Deduplicating equal values. Subsets taken from different positions are distinct and must each be counted.
Frequently asked questions
Why exactly half of the subsets?
Pick any number x that has the bit. Including or excluding x flips that bit of the XOR, so subsets split into pairs where exactly one member has the bit on. There are 2^(n-1) such pairs.
What if no number has a certain bit?
Then no XOR can produce it, and the OR has a 0 there, so the formula correctly adds nothing for that bit.
Can the result overflow a 32-bit integer?
No. The OR is at most 31 because nums[i] is at most 20, and shifting by at most 11 gives at most 63,488.
Is the backtracking solution still worth knowing?
Yes. It is the standard include or skip pattern used by Subsets and many other problems, and it is what an interviewer often expects first before the bit argument.