Ctrl K

XOR of Every Subset

Easy
You are given an integer array values. The XOR of a collection of numbers is the bitwise exclusive OR of all of them; for an empty collection it is 0. Go through every subset of the array, including the empty one and the whole array, compute its XOR, and return the sum of all these results. Subsets are told apart by which positions they pick, so subsets with equal values taken from different places are counted separately.
Example 1
values=[2, 3] → 6

The subsets are the empty one (0), [2] (2), [3] (3) and [2,3] (2 XOR 3 = 1). The sum is 0 + 2 + 3 + 1 = 6.

Example 2
values=[4, 4] → 8

The empty subset gives 0, each of the two singles gives 4, and the pair gives 4 XOR 4 = 0. Total 8: equal elements still count as different picks.

Example 3
values=[1, 2, 4] → 28

The eight subsets give XORs 0 through 7, which sum to 28.

Constraints
  • 1 ≤ values.length ≤ 12
  • 1 ≤ values[i] ≤ 10⁶
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSquare-Sum Chains HardA Fair Split of Colored Beads HardMoves Between Offices HardPicks With the Full OR MediumHow Many Witnesses Can Be Honest Hard
loading Python…
Input
values=[2, 3]
Expected
6

Run checks the first 3 tests in your browser. Submit checks all 99 tests on the server, hidden ones included.