1309. XOR of All Subsets

EasyBit ManipulationBit ManipulationXORSubsets

Given an array of non-negative integers, consider every subset (including the empty subset) and compute the XOR of that subset's elements. Return the sum of these subset-XOR values. For an empty array the answer is 0.

Input: A JSON object {"nums": [<non-negative integers>]}.

Output: Return the sum over all subsets of the XOR of the subset.

Examples

Example 1
Input: {"nums":[1,3]}
Output: 6
Explanation: Subset XORs 0,1,3,2 sum to 6; OR=3, 3<<1=6.
Example 2
Input: {"nums":[5]}
Output: 5
Explanation: Subsets {} and {5} give 0 and 5 -> sum 5.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →