# Leetcode problem 1863 - simple - find the XOR and sum of all subsets

Bu Xiaochan 2021-09-15 09:16:58 # ️ Preface ️

Algorithm as an extremely important point , It is the core competitiveness of college students to find a job after graduation , So in order not to fall behind with others , Start the brush force deduction algorithm problem ！

# Author's brief introduction

Hello everyone , I'm buxiaochan , An effort to make ruthless code lively and interesting IT The small white , Nice to meet you , Pay attention to me , Keep learning something every day , We'll be big guys in the future ！

Author's column ：
Python
Java

Force deduction question

This is the first time I brush 55/100 Daoli deduction simple question

# One 、 Title Description

Of an array XOR sum Defines all elements in an array by bit XOR Result ; If the array is empty , Then the sum of XORs is 0 .
for example , Array [2,5,6] Of XOR sum by 2 XOR 5 XOR 6 = 1 .
Give you an array nums , Please find out nums Each of them A subset of Of XOR sum , Calculate and return the sum of these values and .
Be careful ： In the subject , Elements identical Different subsets of should many times Count .
Array a It's an array b One of the A subset of The precondition is ： from b Delete a few （ Or maybe not ） Elements can get a

source ： Power button （LeetCode）

Example 1：

 Input ：nums = [1,3]
Output ：6
explain ：[1,3] share 4 A subset of ：
- The XOR sum of empty subsets is 0 .
-  The sum of XORs of is 1 .
-  The sum of XORs of is 3 .
- [1,3] The sum of XORs of is 1 XOR 3 = 2 .
0 + 1 + 3 + 2 = 6


Example 2：

 Input ：nums = [5,1,6]
Output ：28
explain ：[5,1,6] share 8 A subset of ：
- The XOR sum of empty subsets is 0 .
-  The sum of XORs of is 5 .
-  The sum of XORs of is 1 .
-  The sum of XORs of is 6 .
- [5,1] The sum of XORs of is 5 XOR 1 = 4 .
- [5,6] The sum of XORs of is 5 XOR 6 = 3 .
- [1,6] The sum of XORs of is 1 XOR 6 = 7 .
- [5,1,6] The sum of XORs of is 5 XOR 1 XOR 6 = 2 .
0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28


Tips ：1 <= nums.length <= 12
1 <= nums[i] <= 20

# Two 、 title

thinking road 1 ： \color{green}{ Ideas 1：}

Use bitwise operation

# 3、 ... and 、 Code

## ️ 1️⃣. python ️

class Solution:
def subsetXORSum(self, nums: List[int]) -> int:
ans = 0
n = len(nums)
for i in nums:
ans |= i
return ans<<(n-1)


## ️ 2️⃣. C# ️

public class Solution {

public int SubsetXORSum(int[] nums) {

int ans = 0;
int n = nums.Length-1;
foreach (int i in nums)
{

ans |= i;
}
return ans<<n;
}
}


# Conclusion

Persistence is the most important , A daily question is essential ！

Look forward to your attention and supervision ！ 