The XOR total of an array is defined as the bitwise XOR
of all its elements, or 0
if the array is empty.
[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
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
temp.f95:44:9: 44 | res = a XOR b XOR c XOR d XOR e | 1 Error: Unclassifiable statement at (1) temp.f95:10:4: 10 | nums = [1,3] | 1 Error: Different shape for array assignment at (1) on dimension 1 (12 and 2) temp.f95:12:4: 12 | nums = [5,1,6] | 1 Error: Different shape for array assignment at (1) on dimension 1 (12 and 3) temp.f95:14:4: 14 | nums = [3,4,5,6,7,8] | 1 Error: Different shape for array assignment at (1) on dimension 1 (12 and 6)
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
temp.f95:7:26: 7 | integer function xor_total(nums) result(total) | 1 Error: MODULE attribute of βxor_totalβ conflicts with PROCEDURE attribute at (1) temp.f95:8:34: 8 | integer, intent(in) :: nums(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:9:16: 9 | integer :: i | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:11:13: 11 | total = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:12:24: 12 | do i = 1, size(nums) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:13:31: 13 | total = total + nums(i) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:14:7: 14 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:15:29: 15 | total = total - sum(nums) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:16:3: 16 | end function xor_total | 1 Error: Expecting END MODULE statement at (1) temp.f95:25:34: 25 | sum = sum + xor_total(nums(i:j)) | 1 Error: Symbol at (1) is not appropriate for an expression temp.f95:33:9: 33 | use xor_total | 1 Fatal Error: Cannot open module file βxor_total.modβ for reading at (1): No such file or directory compilation terminated.
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.
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.