From 2f223b7c98be144506cd9a7903fab36c4cfe9fe5 Mon Sep 17 00:00:00 2001 From: kiki-here <117074799+kiki-here@users.noreply.github.com> Date: Mon, 31 Oct 2022 15:04:13 +0530 Subject: [PATCH] Create Leetcode Sum of All Subset XOR Totals.py --- Leetcode Sum of All Subset XOR Totals.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Leetcode Sum of All Subset XOR Totals.py diff --git a/Leetcode Sum of All Subset XOR Totals.py b/Leetcode Sum of All Subset XOR Totals.py new file mode 100644 index 0000000..8c0751f --- /dev/null +++ b/Leetcode Sum of All Subset XOR Totals.py @@ -0,0 +1,14 @@ +class Solution: + def subsetXORSum(self, nums: List[int]) -> int: + ans=[] #result array , stores eveery subset's xor + a=0 #stores subset's xor + make(nums,a,0,ans) #function call for recursion or backtracking + return sum(ans) #final answer after updation of result array + +def make(nums,a,i,ans): + if i==len(nums): #if the index reaches to end of nums, + ans.append(a) #time to store the xor formed of subset + return #return back to the called function + make(nums,a,i+1,ans) #when the element need not to include in subset(no xor) and i+1 is for next element + #will backtrack + make(nums,a^nums[i],i+1,ans) #when the element is included