Open In App

Sum of all subsets in array using JavaScript

Last Updated : 01 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

We are given an array, we have to print the sum of the subset generated. If the number of elements in the array is n, then 2n subset will be generated. One empty subset will also be generated. So the total subset generated is (2n + 1).

Example:

Input: 1 , 2, 3 

Output: 0 , 1 , 2 , 3 , 3 , 4 , 5 , 6

Explanation:
[] = 0
[1] = 1
[2] = 2
[3] = 3
[1, 2] = 1 + 2 = 3
[1 , 3] = 1 + 3 = 4
[3 , 2] = 3 + 2 = 5
[1 , 2 , 3] = 1 + 2 + 3 = 6

There are several methods in JavaScript to find the sum of all subsets in an array which are as follows:

Recursive Approach

In this approach we will create a recursive function. We will iterate the array and at each index we have two option either pick the element and add it to the sum or we do not pick the element , and move to the next element recursively. We stop after reaching to the end of array, which is base condition of function.

Example: To demonstrate finding the sum of all subsets in array using recursive approach.

JavaScript
class Solution {
    subsetSumsFunc(ind, arr, n, ans, sum) {
        if (ind === n) {
            ans.push(sum);
            return;
        }
        this.subsetSumsFunc(ind + 1, arr, n,
            ans, sum + arr[ind]);
        this.subsetSumsFunc(ind + 1, arr,
            n, ans, sum);
    }

    subsetSums(arr) {
        let ans = [];
        this.subsetSumsFunc(0, arr,
            arr.length, ans, 0);
        ans.sort((a, b) => a - b);
        return ans;
    }
}

const ob = new Solution();
const arr = [1, 2, 3];
const ans = ob.subsetSums(arr);
console.log("The sum of each subset formed is  ");
ans.forEach(sum => {
    console.log(sum);
});  

Output
The sum of each subset formed is  
0
1
2
3
3
4
5
6

Time Complexity: O(n*2n)

Space Complexity: O(2n)

Dynamic Programming

In dynamic approach we will initialize a set dp with 0, representing the empty subset. Now we iterate each element in array and for each element we will create a new set next and add the current element to each sum in dp, then merge the current dp set with next. After iterating through all elements, we convert the set dp to array and sort it and return the result.

Example: This code computes the sum of each subset of a given array using dynamic programming, utilizing sets for efficient storage and sorting the results.

JavaScript
class Solution {
    subsetSums(arr) {
        const n = arr.length;
        let dp = new Set([0]);

        for (let num of arr) {
            let next = new Set(dp);
            for (let sum of dp) {
                next.add(sum + num);
            }
            dp = new Set([...dp, ...next]);
        }

        const ans = [...dp];
        ans.sort((a, b) => a - b);
        return ans;
    }
}

const ob = new Solution();
const arr = [3, 1, 2];
const ans = ob.subsetSums(arr);
console.log("The sum of each subset is ");
ans.forEach(sum => {
    console.log(sum);
});  

Output
The sum of each subset is 
0
1
2
3
4
5
6

Time Complexity: O(n*2n)

Space Complexity: O(2n)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads