Skip to content

Commit 24331a5

Browse files
committed
solutions: 1863 - Sum of All Subset XOR Totals (Easy)
1 parent c0f73fb commit 24331a5

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
---
2+
description: 'Author: @wkw | https://leetcode.com/problems/sum-of-all-subset-xor-totals/'
3+
---
4+
5+
# 1863 - Sum of All Subset XOR Totals (Easy)
6+
7+
## Problem Link
8+
9+
https://leetcode.com/problems/sum-of-all-subset-xor-totals/
10+
11+
## Problem Statement
12+
13+
The XOR total of an array is defined as the bitwise XOR of all its elements, or 0 if the array is empty.
14+
15+
For example, the XOR total of [2,5,6] is 2 XOR 5 XOR 6 = 1. Given an array nums, return the sum of all XOR totals for every subset of nums.
16+
17+
Note: Subsets with the same elements should be counted multiple times.
18+
19+
An array a is a subset of an array b if a can be obtained from b by deleting some (possibly zero or all) elements of b.
20+
21+
**Example 1:**
22+
23+
**Input:** nums = [1,3]
24+
25+
**Output:** 6
26+
27+
**Explanation:**
28+
29+
The subsets of [1,3] are:
30+
31+
- [] (the empty subset), which has an XOR total of 0.
32+
- [1], which has an XOR total of 1.
33+
- [3], which has an XOR total of 3.
34+
- [1,3], which has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6
35+
36+
**Example 2:**
37+
38+
**Input:** nums = [5,1,6]
39+
40+
**Output:** 28
41+
42+
**Explanation:**
43+
44+
The subsets of [5,1,6] are:
45+
46+
- [] (the empty subset), which has an XOR total of 0.
47+
- [5], which has an XOR total of 5.
48+
- [1], which has an XOR total of 1.
49+
- [6], which has an XOR total of 6.
50+
- [5,1], which has an XOR total of 5 XOR 1 = 4.
51+
- [5,6], which has an XOR total of 5 XOR 6 = 3.
52+
- [1,6], which has an XOR total of 1 XOR 6 = 7.
53+
- [5,1,6], which has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28
54+
55+
**Example 3:**
56+
57+
**Input:** nums = [3,4,5,6,7,8]
58+
59+
**Output:** 480
60+
61+
**Explanation:**
62+
63+
The sum of all XOR totals for every subset is 480.
64+
65+
**Constraints:**
66+
67+
- `1 <= nums.length <= 12`
68+
- `0 <= nums[i] <= 20`
69+
70+
## Approach 1: Brute Force
71+
72+
Since it is a easy question, we can think about a brute force solution first. Basically just do what it asks, for each subset, calcualte the XOR value and sum them up.
73+
74+
Time Complexity: $O(n * 2^n)$
75+
76+
Space Complexity: $O(1)$
77+
78+
<Tabs>
79+
<TabItem value="py" label="Python">
80+
<SolutionAuthor name="@wkw"/>
81+
82+
```py
83+
class Solution:
84+
def subsetXORSum(self, nums: List[int]) -> int:
85+
n = len(nums)
86+
res = 0
87+
for i in range(1 << n):
88+
x = 0
89+
for j in range(n):
90+
if (1 << j) & i:
91+
x ^= nums[j]
92+
res += x
93+
return res
94+
```
95+
96+
</TabItem>
97+
</Tabs>
98+
99+
## Approach 2: Bit Manipulation
100+
101+
This above solution will not work if $n$ gets larger, we need another way to solve it. The key observation is that for any given bit position, if that bit is set, at the end it will contribute to the overall sum because of XOR property. The total contribution of that bit equals to that bit value \* $2 ^{(n - 1)}$. Hence, we can take bitwise OR of all numbers $x$ in $nums$, then multiply it by $2 ^{(n - 1)}$. In python, we can shift it to the left by $n - 1$.
102+
103+
Time Complexity: $O(n)$
104+
105+
Space Complexity: $O(1)$
106+
107+
<Tabs>
108+
<TabItem value="py" label="Python">
109+
<SolutionAuthor name="@wkw"/>
110+
111+
```py
112+
class Solution:
113+
def subsetXORSum(self, nums: List[int]) -> int:
114+
n, res = len(nums), 0
115+
for x in nums: res |= x
116+
return res << (n - 1)
117+
118+
# or one-liner
119+
# return reduce(lambda x, y: x | y, nums) << (len(nums) - 1)
120+
121+
# or another one-liner
122+
#return reduce(ior, nums) << (len(nums) - 1)
123+
```
124+
125+
</TabItem>
126+
</Tabs>

0 commit comments

Comments
 (0)