Sum of All Subset XOR Totals

🏠 ⬅️ ➑️

The XOR total of an array is defined as the bitwise XOR of all its elements, or 0 if the array is empty.

  • For example, the XOR total of the array [2,5,6] is 2 XOR 5 XOR 6 = 1.

Given an array nums, return the sum of all XOR totals for every subset of nums.

Note: Subsets with the same elements should be counted multiple times.

An array a is a subset of an array b if a can be obtained from b by deleting some (possibly zero) elements of b.

Example 1:

Input: nums = [1,3] Output: 6 Explanation: The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6

Example 2:

Input: nums = [5,1,6] Output: 28 Explanation: The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28

Example 3:

Input: nums = [3,4,5,6,7,8] Output: 480 Explanation: The sum of all XOR totals for every subset is 480.

Constraints:

  • 1 <= nums.length <= 12
  • 1 <= nums[i] <= 20

Note: This problem is from LeetCode.
❌ Compiled
❌ Executed
❌ Correct
program main
    implicit none

    integer, parameter :: n = 12
    integer, dimension(n) :: nums
    integer :: i, j, k, l, m, n_subsets, xor_total
    logical :: found

    ! Examples
    nums = [1,3]
    write (*, '(A, I0)') 'Example 1: ', solve(nums)
    nums = [5,1,6]
    write (*, '(A, I0)') 'Example 2: ', solve(nums)
    nums = [3,4,5,6,7,8]
    write (*, '(A, I0)') 'Example 3: ', solve(nums)

contains

    function solve(nums) result(xor_total)
        implicit none
        integer, intent(in) :: nums(:)
        integer :: xor_total
        integer :: i, j, k, l, m

        xor_total = 0
        do i = 1, size(nums)
            do j = i+1, size(nums)
                do k = j+1, size(nums)
                    do l = k+1, size(nums)
                        do m = l+1, size(nums)
                            xor_total = xor_total + xor(nums(i), nums(j), nums(k), nums(l), nums(m))
                        end do
                    end do
                end do
            end do
        end do
    end function solve

    function xor(a, b, c, d, e) result(res)
        implicit none
        integer, value :: a, b, c, d, e
        integer :: res

        res = a XOR b XOR c XOR d XOR e
    end function xor

end program main
❌ Compiled
❌ Executed
❌ Correct
module xor_total

implicit none

contains

integer function xor_total(nums) result(total)
    integer, intent(in) :: nums(:)
    integer :: i

    total = 0
    do i = 1, size(nums)
        total = total + nums(i)
    end do
    total = total - sum(nums)
end function xor_total

integer function subset_sum(nums, total) result(sum)
    integer, intent(in) :: nums(:), total
    integer :: i, j

    sum = 0
    do i = 1, size(nums)
        do j = i, size(nums)
            sum = sum + xor_total(nums(i:j))
        end do
    end do
end function subset_sum

end module xor_total

program main
    use xor_total
    implicit none

    integer, parameter :: nums = [1, 3]
    integer :: total

    total = subset_sum(nums, 6)
    write (*,*) "The sum of all XOR totals for every subset of [1,3] is:", total

    total = subset_sum(nums, 28)
    write (*,*) "The sum of all XOR totals for every subset of [5,1,6] is:", total

    total = subset_sum(nums, 480)
    write (*,*) "The sum of all XOR totals for every subset of [3,4,5,6,7,8] is:", total
end program main
🌐 Data from online sources
def subsetXORSum(nums):
    ans = 0
    n = len(nums)
    max_val = 1 << n
    for i in range(1, max_val):
        xor_total = 0
        for j in range(n):
            if i & (1 << j):
                xor_total ^= nums[j]
        ans += xor_total
    return ans

Loop through all 2^N possible subsets of nums, which can be represented as binary numbers from 0 to 2^N - 1. A bit set to 1 at position i indicates that nums[i] is part of the subset. Calculate the XOR total for each of these subsets and accumulate it. Finally, return the total sum. Note that the starting position of the loop is 1 since we skip the empty subset.

🌐 Data from online sources
int subsetXORSum(vector<int>& nums) {
    int ans = 0, n = nums.size(), max_val = 1 << n;
    for (int i = 1; i < max_val; i++) {
        int xor_total = 0;
        for (int j = 0; j < n; ++j) {
            if (i & (1 << j)) {
                xor_total ^= nums[j];
            }
        }
        ans += xor_total;
    }
    return ans;
}

Loop through all 2^N possible subsets of nums, which can be represented as binary numbers from 0 to 2^N - 1. A bit set to 1 at position i indicates that nums[i] is part of the subset. Calculate the XOR total for each of these subsets and accumulate it. Finally, return the total sum. Note that the starting position of the loop is 1 since we skip the empty subset.