Sum of All Subset XOR Totals

class Solution:
def subsetXORSum(self, nums: List[int]) -> int:
length = len(nums)
result=0
for i in range(2**length):
binary = bin(i)[2:]
if(len(binary)<length):
binary = ((length-len(binary))*"0")+binary
xor_=0
for i in range(len(binary)):
if binary[i]=="1":
xor_^=nums[i]
result+=xor_
return(result)
class Solution:
def subsetXORSum(self, nums: List[int]) -> int:
length = len(nums)
result=0
for i in range(1<<length):
xor_=0
for j in range(length):
if( i & 1<<j !=0 ):
xor_^=nums[j]
result+=xor_
return(result)
class Solution:
def subsetXORSum(self, nums: List[int]) -> int:
total,length=0,len(nums)
for i in nums:
total|=i
return(2**(length-1)*total)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dhanarajappu

Dhanarajappu

Tech-Enthusiast, Coder,Explorer,Geeky,Software Engineer |A piece of code delivers everything that you need. The world is all about codes.