Problems178
Search for a command to run...
Total of every subset, via include / exclude
Initialization
Branch include / exclude at every index, adding the element to a running sum on the include side. When the index runs out, that sum is one subset's total — there are exactly 2ⁿ of them, duplicates included.
Include / Exclude
1function subsetSums(nums) {2 function func(ind, sum, nums, ans) {3 // Base case: every element decided4 if (ind === nums.length) {5 ans.push(sum);6 return;7 }8 9 // Include nums[ind] in the running sum10 func(ind + 1, sum + nums[ind], nums, ans);11 12 // Exclude it13 func(ind + 1, sum, nums, ans);14 }15 16 const ans = [];17 func(0, 0, nums, ans);//n: 318 return ans;19}