Skip to content
← All posts

Sum of All Subset XOR Totals

5 min read
leetcodeproblemeasyarraysbit-manipulationbacktracking

Given an array nums, the XOR total of a subset is the bitwise XOR of all its elements (or 0 if the subset is empty). Return the sum of all XOR totals for every subset of nums.

This is LeetCode 1863: Sum of All Subset XOR Totals, and it is an easy-level problem. You could brute-force all 2^n subsets, but there is a beautiful O(n) solution hiding behind a single observation about how bits behave across subsets.

All subsets of [1, 3]SubsetXOR Total{}0{1}1{3}3{1, 3}2Sum = 0 + 1 + 3 + 2 = 6Add up all XOR totals for the answer
For nums = [1, 3], there are 2^2 = 4 subsets. Each subset's XOR total is shown on the right. The answer is the sum of all XOR totals.

Why this problem matters

This problem teaches you to think about bits independently. Instead of enumerating every subset and computing its XOR, you ask a simpler question: "For a single bit position, how many subsets have that bit set in their XOR total?" That per-bit reasoning is the same mental model behind problems like Single Number, Counting Bits, and many XOR-based challenges.

Once you see that each bit position contributes independently, the entire problem collapses into an OR operation and a bit shift. That is a pattern worth internalizing because it shows up whenever XOR and subsets overlap.

The approach

The brute-force solution generates all 2^n subsets, computes the XOR of each, and sums them up. That works fine when n is small (the constraint says n <= 12), but the O(n) approach is far more elegant and teaches a deeper lesson.

Here is the key insight: consider a single bit position. If none of the numbers in nums have that bit set, then no subset can have it set in its XOR total. But if at least one number has that bit set, then exactly half of all 2^n subsets will have that bit set in their XOR total. Why? Because for any subset where the bit is set, you can pair it with a "toggled" subset (add or remove one of the numbers that has that bit) where the bit is unset. This creates a perfect one-to-one pairing.

So for each bit position that appears in at least one number, its contribution to the total sum is (bit value) * 2^(n-1). The condition "at least one number has that bit set" is exactly what the bitwise OR captures. So the answer is simply (nums[0] | nums[1] | ... | nums[n-1]) * 2^(n-1), or equivalently, the OR of all elements left-shifted by n - 1.

The solution

def subsetXORSum(nums):
    or_all = 0
    for num in nums:
        or_all |= num
    return or_all << (len(nums) - 1)

Step-by-step walkthrough

Step 1: Write each number in binary

For nums = [1, 5, 6], convert to binary and look at each bit position independently.

Valuebit 2bit 1bit 0100151016110

1 = 001, 5 = 101, 6 = 110. Highlighted cells have the bit set (value 1).

Step 2: Observe that each bit position is independent

For a given bit position, if at least one number has that bit set, then exactly half of all subsets will have that bit set in their XOR total. With n = 3 numbers, there are 2^3 = 8 subsets. Half of them (4 subsets) will have each contributing bit set.

Bit 2 (value = 4):

Numbers with bit 2 set: 5, 6 (at least one exists). Half of 8 subsets = 4 subsets have this bit set in XOR. Contribution: 4 * 4 = 16.

Bit 1 (value = 2):

Numbers with bit 1 set: 6 (at least one exists). Half of 8 subsets = 4 subsets have this bit set in XOR. Contribution: 4 * 2 = 8.

Bit 0 (value = 1):

Numbers with bit 0 set: 1, 5 (at least one exists). Half of 8 subsets = 4 subsets have this bit set in XOR. Contribution: 4 * 1 = 4.

Step 3: OR all numbers together

A bit appears in the OR if and only if at least one number has that bit set. That is exactly the condition for a bit to contribute to the answer.

OR =1bit 21bit 11bit 0

1 | 5 | 6 = 7 (binary 111). All three bit positions contribute.

Step 4: Multiply by 2^(n-1)

Each contributing bit appears in exactly half of all 2^n subsets. That means each set bit in the OR contributes its value multiplied by 2^(n-1). Summing over all bits is the same as multiplying the entire OR value by 2^(n-1).

n = 3, so 2^(n-1) = 2^2 = 4

OR = 7

Answer = 7 * 4 = 28

Equivalently: (1 | 5 | 6) << (3 - 1) = 7 << 2 = 28.

Sum of all subset XOR totals = 28

You can verify by enumerating all 8 subsets: 0 + 1 + 5 + 6 + 4 + 7 + 3 + 2 = 28. The bit-contribution formula gives the same result in O(n) time.

The walkthrough uses nums = [1, 5, 6] to show the full reasoning. First you write each number in binary and check which bit positions are active. Then you observe that each active bit contributes to exactly half the subsets. The OR of all numbers identifies which bits are active, and multiplying by 2^(n-1) accounts for the half-of-subsets factor. The final answer, 28, matches what you get from manually summing XOR totals of all 8 subsets.

Complexity analysis

ApproachTimeSpace
Brute force (enumerate all subsets)O(2^n * n)O(n)
Backtracking (recursive subset generation)O(2^n)O(n)
Bit-contribution formula (OR + shift)O(n)O(1)

The brute force is acceptable here since n <= 12 means at most 4096 subsets, but the O(n) solution is what you should aim for in an interview. It demonstrates deeper understanding of bitwise operations.

The building blocks

Bit-contribution analysis (per-bit reasoning)

The core technique is analyzing each bit position independently. Instead of thinking about numbers as whole values, you decompose the problem into individual bits and ask "how does this single bit behave across all subsets?" This decomposition is powerful because XOR, AND, and OR all operate bit by bit. Any problem involving these operators is a candidate for per-bit analysis.

or_all = 0
for num in collection:
    or_all |= num

The OR accumulates which bit positions are "active" across the entire collection. It is the bitwise counterpart to checking "does any element have this property?"

Whenever a problem combines XOR with counting or summing over subsets, try reasoning about one bit at a time. XOR at a single bit position is just parity (even vs. odd count of 1s), which is much easier to analyze than full XOR across all bits simultaneously.

Edge cases

Single element. When nums has one element, there are two subsets: the empty set (XOR = 0) and the full set (XOR = nums[0]). The formula gives nums[0] << 0 = nums[0], which equals 0 + nums[0]. Correct.

All zeros. If every element is 0, the OR is 0, and 0 shifted by anything is still 0. Every subset has XOR total 0, so the sum is 0. Correct.

All elements identical. If all n elements are the same value v, the OR is just v. The answer is v << (n - 1). You can verify: subsets with an odd number of elements have XOR = v, and subsets with an even number (excluding the empty set) have XOR = 0. The count of odd-size subsets is 2^(n-1), so the sum is v * 2^(n-1). Matches.

Maximum constraint. With n = 12 and all values up to 20, the OR is at most 31 (five bits). The answer is at most 31 << 11 = 63488, well within 32-bit integer range. The guarantee in the problem statement holds.

From understanding to recall

The formula (OR of all elements) << (n - 1) is short enough to memorize, but the real value is understanding why it works. The per-bit argument (if a bit is present, it contributes to exactly half the subsets) is the insight that makes the formula obvious rather than magical.

In an interview, you might start with the brute-force backtracking approach to show you can generate subsets. Then you pivot to the optimized formula by explaining the bit-contribution reasoning. That progression, from working solution to elegant solution, demonstrates both coding ability and analytical thinking.

Related posts

  • Single Number - XOR to find the unique element, using the same cancellation property that underpins per-bit reasoning
  • Subsets - Generate all subsets with backtracking, the brute-force approach for this problem
  • Counting Bits - Bit manipulation patterns that build fluency with binary representations

CodeBricks uses spaced repetition to help you internalize patterns like per-bit analysis and XOR reasoning. Instead of passively re-reading solutions, you reconstruct them from scratch at increasing intervals. After a few review cycles, the connection between "sum over subsets" and "bit-contribution formula" becomes automatic.