Sum of All Subset XOR Totals

Easy

The XOR total of an array is the bitwise XOR of all its elements, or 0 if the array is empty. For example, the XOR total of [2,5,6] is 2 XOR 5 XOR 6 = 1.

Given an array nums, return the sum of the XOR totals of every subset of nums.

A subset is what you get by deleting some of the elements (possibly none, possibly all). Two subsets that pick different positions count separately, even if they hold the same numbers, so an array of length n has exactly 2^n subsets.

Examples

Example 1

Input:  nums = [1,3]
Output: 6

The four subsets are [], [1], [3] and [1,3]. Their XOR totals are 0, 1, 3 and 1 XOR 3 = 2, and 0 + 1 + 3 + 2 = 6.

Example 2

Input:  nums = [5,1,6]
Output: 28
Example 2: nums = [5,1,6][]→0[5]→5[1]→1[6]→6[5,1]→4[5,6]→3[1,6]→7[5,1,6]→2sum285 XOR 1 XOR 65101100161100102a 1 where an oddnumber of rows have one

The eight subsets have XOR totals 0, 5, 1, 6, 5 XOR 1 = 4, 5 XOR 6 = 3, 1 XOR 6 = 7 and 5 XOR 1 XOR 6 = 2. They add up to 28.

Example 3

Input:  nums = [3,4,5,6,7,8]
Output: 480

There are 2^6 = 64 subsets, and their XOR totals add up to 480.

Example 4

Input:  nums = [2,2]
Output: 4

The subsets are [], the first [2], the second [2] and [2,2], with XOR totals 0, 2, 2 and 0. Each 2 counts as its own subset.

Example 5

Input:  nums = [7]
Output: 7

[] adds 0 and [7] adds 7.

Constraints

  • 1 <= nums.length <= 12
  • 1 <= nums[i] <= 20
TypeScript
Loading editor…
Test cases36
Case 1
nums = [1,3]
Case 2
nums = [5,1,6]
Case 3
nums = [3,4,5,6,7,8]
Case 4
nums = [7]
Case 5
nums = [2,2]
Case 6
nums = [1]
Case 7
nums = [20]
Case 8
nums = [1,1]
Case 9
nums = [1,2]
Case 10
nums = [1,2,4,8,16]
Case 11
nums = [16,8,4,2,1,3]
Case 12
nums = [20,20,20,20,20,20,20,20,20,20,20,20]
Case 13
nums = [1,1,1,1,1,1,1,1,1,1,1,1]
Case 14
nums = [3,3,3]
Case 15
nums = [1,2,3]
Case 16
nums = [5,5,5,5]
Case 17
nums = [10,20]
Case 18
nums = [19,20,18,17,16,15,14,13,12,11,10,9]
Case 19
nums = [1,2,3,4,5,6,7,8,9,10,11,12]
Case 20
nums = [2,4,6,8,10,12,14,16,18,20,1,3]
Case 21
nums = [16,16,16,16,16,16,16,16,16,16,16,15]
Case 22
nums = [6,6,6,6,6,6,6,6,6,6,6]
Case 23
nums = [12,9]
Case 24
nums = [4,8,12]
Case 25
nums = [1,8,7]
Case 26
nums = [11,10,7,12]
Case 27
nums = [2,1,12,10,10,16,4,17]
Case 28
nums = [13,16,6,5,10,18,19,18,5,20,8,18]
Case 29
nums = [19,20,7]
Case 30
nums = [4,5,3,20,3,3,4,5,17]
Case 31
nums = [4,19,10]
Case 32
nums = [18,6,10,11,3,3,19,6,12,12,5,9]
Case 33
nums = [13,4,6,13,20,17,10,16]
Case 34
nums = [11,18,19,1]
Case 35
nums = [12,7,14,12,14,15,1]
Case 36
nums = [10,9,6,19]