Sum of XOR

MediumArrayBacktrackingBit Manipulation

Description

Given an array nums, return the sum of all XOR totals for every subset. XOR total is the XOR of all elements in the subset.

Examples

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

Subsets: []=0, [1]=1, [3]=3, [1,3]=2. Sum=6.

Input:nums = [2]
Output:2
Explanation:

With only one element, there are subsets: [] (XOR = 0) and [2] (XOR = 2). Sum = 0 + 2 = 2.

Input:nums = [4,2,7]
Output:28
Explanation:

Subsets and their XORs: [] = 0, [4] = 4, [2] = 2, [7] = 7, [4,2] = 6, [4,7] = 3, [2,7] = 5, [4,2,7] = 1. Sum = 0 + 4 + 2 + 7 + 6 + 3 + 5 + 1 = 28.

Constraints

  • 1 ≤ nums.length ≤ 12

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!