1863. Sum of All Subset XOR Totals

Sum of All Subset XOR Totals (LeetCode 1863) is a Easy Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Backtracking Patterns pattern, in the Subsets (Include/Exclude) subpattern.

How to solve Sum of All Subset XOR Totals

Sum of All Subset XOR Totals is a Generate Subsets problem, part of Backtracking Patterns. Generate all subsets using include/exclude decision pattern. Handle duplicates in subsets.

Work towards the solution in these steps:

  1. Process the elements one at a time and decide whether each one belongs to the current subset.
  2. At each decision point, maintain the XOR accumulated by the elements selected so far.
  3. The two branches for an element are the branch that leaves the current XOR unchanged and the branch that XORs the element into it.

The reference solution runs in O(n) time and O(1) space.

The Sum of All Subset XOR Totals solution and editorial walks through the full approach with reference code.

Problem statement

nums contains integers, and a subset is formed by choosing any collection of its elements, while preserving no ordering requirement. Each element may be chosen at most once, and the empty subset is allowed.

The XOR total of a subset is the bitwise XOR of all values in that subset. The XOR total of the empty subset is 0.

Implement Solution.subsetXORSum(nums) and return the sum of the XOR totals of every possible subset of nums, including the empty subset.

Example 1:

Input: nums = [1,2,4] Output: 28 Explanation: The eight subset XOR totals sum to 28; the empty subset contributes 0.

Example 2:

Input: nums = [0,5,0] Output: 20 Explanation: Exactly four subsets have XOR total 5, while the others have total 0, giving 4 5 = 20.

Example 3:

Input: nums = [6] Output: 6 Explanation: The empty subset contributes 0 and the subset containing 6 contributes 6.

Constraints:

- 1 <= nums.length <= 12 - 0 <= nums[i] <= 10^4 - nums contains only integers - Every position in nums represents a distinct selectable occurrence, even when values are equal

Topics and companies

Topics: Array, Backtracking, Bit Manipulation, Recursion.

How to practise Sum of All Subset XOR Totals on Thita.ai

Starter code is provided in C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(1) space.

Open Sum of All Subset XOR Totals in the code editor, or read the Sum of All Subset XOR Totals editorial for a worked solution with its approach and complexity analysis.

Where Sum of All Subset XOR Totals sits in the DSA pattern sheet

Problems related to Sum of All Subset XOR Totals

Other problems that use the same Subsets (Include/Exclude) and Backtracking Patterns techniques:

Sum of All Subset XOR Totals FAQ

What is the LeetCode number of Sum of All Subset XOR Totals?

Sum of All Subset XOR Totals is LeetCode problem 1863, rated Easy.

What pattern is used to solve Sum of All Subset XOR Totals?

Sum of All Subset XOR Totals is a Generate Subsets problem. It belongs to the Backtracking Patterns pattern, in the Subsets (Include/Exclude) subpattern.

What is the time complexity of Sum of All Subset XOR Totals?

The reference solution runs in O(n) time and O(1) space.

Preparing this workspace.