class Solution:
def subsetXORSum(self, nums):
def dfs(i, curr_xor):
if i == len(nums):
return curr_xor
# Include nums[i]
with_curr = dfs(i + 1, curr_xor ^ nums[i])
# Exclude nums[i]
without_curr = dfs(i + 1, curr_xor)
return with_curr + without_curr
return dfs(0, 0)
class Solution {
public:
int subsetXORSum(vector<int>& nums) {
return dfs(nums, 0, 0);
}
private:
int dfs(const vector<int>& nums, int i, int curr_xor) {
if (i == nums.size()) {
return curr_xor;
}
int with_curr = dfs(nums, i + 1, curr_xor ^ nums[i]);
int without_curr = dfs(nums, i + 1, curr_xor);
return with_curr + without_curr;
}
};
class Solution {
public int subsetXORSum(int[] nums) {
return dfs(nums, 0, 0);
}
private int dfs(int[] nums, int i, int currXor) {
if (i == nums.length) {
return currXor;
}
int withCurr = dfs(nums, i + 1, currXor ^ nums[i]);
int withoutCurr = dfs(nums, i + 1, currXor);
return withCurr + withoutCurr;
}
}
var subsetXORSum = function(nums) {
function dfs(i, currXor) {
if (i === nums.length) {
return currXor;
}
let withCurr = dfs(i + 1, currXor ^ nums[i]);
let withoutCurr = dfs(i + 1, currXor);
return withCurr + withoutCurr;
}
return dfs(0, 0);
};
Given an array of positive integers nums
, your task is to find the sum of the XOR totals of every subset of nums
. The XOR total of a subset is the result of applying the bitwise XOR operation to all its elements (if the subset is empty, its XOR total is 0). Return the sum of all these XOR totals.
nums
is a list of integers (length up to 12, values up to 20).
The problem asks us to consider all possible subsets of the array nums
and compute the XOR of each subset, then sum all these results. At first glance, this seems like a brute-force problem: for each subset, calculate its XOR total, then sum them up.
Since the number of subsets for an array of length n
is 2^n
, a brute-force approach could work for small arrays. However, for larger arrays, this approach becomes inefficient. We need to consider whether there's a more clever way to avoid recalculating the same XORs or to leverage properties of XOR and subsets to optimize our solution.
The key insight is that for each element, we have a choice: either include it in the current subset or not. This naturally leads to a recursive or backtracking approach, where at each step we branch into two possibilities.
We can solve this problem efficiently using recursion (depth-first search) to generate all subsets and calculate their XOR totals. Here's how:
i
in nums
, and the current XOR value curr_xor
of the chosen subset so far.
i == len(nums)
), return curr_xor
because we've formed a subset.
nums[i]
in the subset: call the function with i+1
and curr_xor ^ nums[i]
.nums[i]
from the subset: call the function with i+1
and curr_xor
unchanged.This approach ensures we consider all possible subsets and accumulate their XOR totals. Since the input size is small (up to 12 elements), this method is efficient enough.
Let's walk through an example with nums = [1, 3]
.
[]
, [1]
, [3]
, [1, 3]
[]
: 0[1]
: 1[3]
: 3[1,3]
: 1 ^ 3 = 2Our recursive function explores both choices at each element:
2^n
subsets for an array of length n
.O(n)
time, so total time is O(n * 2^n)
.2^n
recursive calls, but each call does only O(1)
work.O(2^n)
.O(n)
for the recursion stack.
The problem asks us to sum the XOR totals of all subsets of an array. By using a recursive approach that explores all subset combinations (including or excluding each element), we efficiently compute the answer in O(2^n)
time. This is feasible given the input constraints. The solution leverages the natural structure of subsets and the properties of XOR, making it both elegant and efficient for the problem size.