I was solving Sum of All Subset XOR Totals on Leetcode. It was obviously a Backtracking problem so I solved it in both n*(2^n) and 2^n. But it surprisingly has a linear solution. Like this
class Solution {
public:
int subsetXORSum(vector<int>& nums) {
int sum=0;
for(auto i:nums)sum|=i;
return sum*(1<<(nums.size()-1));
}
};
Why this works ? Does anyone have the proof??



