Skip to main content
Back to problems
#1863
Easy Algorithms

Sum of all subset xor totals

Array Math Backtracking Bit Manipulation Combinatorics Enumeration
90.1% acceptance
Feb 25, 2026
2677
354
The XOR total of an array is defined as the bitwise XOR of all its elements, or 0 if the array is empty. Given an array nums, return the sum of all XOR totals for every subset of nums.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn subset_xor_sum(nums: Vec<i32>) -> i32 {
    // Key insight: answer = (OR of all elements) * 2^(n-1)
    let n = nums.len();
    let or_all = nums.iter().fold(0, |acc, &x| acc | x);
    or_all * (1 << (n - 1))
  }
}