#1863

Sum of All Subset XOR Totals

Easy Pick / not pick recursion

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

For every subset of the array, take the XOR of its elements. Return the sum of all those XOR values.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: XOR each subset, add all those results.

Why this works: every element is either in or out — two branches. At the end of the array you have one finished subset.

Example: [1, 3]

{} → 0, {1} → 1, {3} → 3, {1,3} → 2total 6

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Forgetting the empty subset is included — it contributes 0, so it's harmless, but the base case must still return.

Solve on LeetCode ↗
Complexity
  • Time: O(2^n).
  • Space: O(n) recursion depth.
Topic: Recursion & Backtracking
🎬 Episodes 29–36: video coming soon