Compute the sum of XOR values over every subset of an array.
Problem
Given an integer array nums, consider every possible subset of nums, including the empty subset.
For each subset, compute the bitwise XOR of all its elements. Return the sum of these XOR values across all subsets.
A subset is any selection of elements from the array, preserving no particular order. Different positions in the array are treated as distinct elements.
Notes
- The XOR of an empty subset is
0. - You must count every subset exactly once.
- The array may contain repeated values, but subsets are formed from indices, not values.
Input Format
- A single integer array
nums. - Each element is an integer value used in bitwise XOR operations.
Output Format
- Return an integer representing the sum of XOR totals over all subsets of
nums.
Constraints
0 <= nums.length <= 200 <= nums[i] <= $10^{9}$- The answer fits in a 64-bit signed integer for the intended constraints.
Example 1
Input
nums = [1,3]
Output
6
Explanation
Subsets are [], [1], [3], [1,3].
Their XOR totals are 0, 1, 3, 2.
Sum = 0 + 1 + 3 + 2 = 6.
Example 2
Input
nums = [5,1,6]
Output
28
Explanation
There are 8 subsets in total. The XOR totals are 0, 5, 1, 6, 4, 3, 7, 2, and their sum is 28.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.