// Find all possible sums made from subsets of items from array A
function subsetSums(arr, l, r, sum = 0) {
// Print current subset
if (l > r) {
console.log(sum);
return;
}
// Subset including arr[l]
subsetSums(arr, l + 1, r, sum + arr[l]);
// Subset excluding arr[l]
subsetSums(arr, l + 1, r, sum);
}
let arr = [5, 4, 3, 1];
subsetSums(arr, 0, arr.length - 1); // 2^n results