Open In App

Sum of subsets of all the subsets of an array | O(3^N)

Improve
Improve
Like Article
Like
Save
Share
Report

Given an array arr[] of length N, the task is to find the overall sum of subsets of all the subsets of the array.
Examples: 
 

Input: arr[] = {1, 1} 
Output:
All possible subsets: 
a) {} : 0 
All the possible subsets of this subset 
will be {}, Sum = 0 
b) {1} : 1 
All the possible subsets of this subset 
will be {} and {1}, Sum = 0 + 1 = 1 
c) {1} : 1 
All the possible subsets of this subset 
will be {} and {1}, Sum = 0 + 1 = 1 
d) {1, 1} : 4 
All the possible subsets of this subset 
will be {}, {1}, {1} and {1, 1}, Sum = 0 + 1 + 1 + 2 = 4 
Thus, ans = 0 + 1 + 1 + 4 = 6
Input: arr[] = {1, 4, 2, 12} 
Output: 513 
 

 

Approach: In this article, an approach with O(3N) time complexity to solve the given problem will be discussed. 
First, generate all the possible subsets of the array. There will be 2N subsets in total. Then for each subset, find the sum of all of its subset. 
Now, let’s understand the time-complexity of this solution. 
There are NCk subsets of length K and time to find the subsets of an array of length K is 2K
Total time = (NC1 * 21) + (NC2 * 22) + … + (NCk * K) = 3K
Below is the implementation of the above approach: 
 

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to sum of all subsets of a
// given array
void subsetSum(vector<int>& c, int i,
               int& ans, int curr)
{
    // Base case
    if (i == c.size()) {
        ans += curr;
        return;
    }
 
    // Recursively calling subsetSum
    subsetSum(c, i + 1, ans, curr + c[i]);
    subsetSum(c, i + 1, ans, curr);
}
 
// Function to generate the subsets
void subsetGen(int* arr, int i, int n,
               int& ans, vector<int>& c)
{
    // Base-case
    if (i == n) {
 
        // Finding the sum of all the subsets
        // of the generated subset
        subsetSum(c, 0, ans, 0);
        return;
    }
 
    // Recursively accepting and rejecting
    // the current number
    subsetGen(arr, i + 1, n, ans, c);
    c.push_back(arr[i]);
    subsetGen(arr, i + 1, n, ans, c);
    c.pop_back();
}
 
// Driver code
int main()
{
    int arr[] = { 1, 1 };
    int n = sizeof(arr) / sizeof(int);
 
    // To store the final ans
    int ans = 0;
    vector<int> c;
 
    subsetGen(arr, 0, n, ans, c);
    cout << ans;
 
    return 0;
}


Java




// Java implementation of the approach
import java.util.*;
 
class GFG
{
    static Vector<Integer> c = new Vector<>();
 
    // To store the final ans
    static int ans = 0;
 
    // Function to sum of all subsets of a
    // given array
    static void subsetSum(int i, int curr)
    {
 
        // Base case
        if (i == c.size())
        {
            ans += curr;
            return;
        }
 
        // Recursively calling subsetSum
        subsetSum(i + 1, curr + c.elementAt(i));
        subsetSum(i + 1, curr);
    }
 
    // Function to generate the subsets
    static void subsetGen(int[] arr, int i, int n)
    {
 
        // Base-case
        if (i == n)
        {
 
            // Finding the sum of all the subsets
            // of the generated subset
            subsetSum(0, 0);
            return;
        }
 
        // Recursively accepting and rejecting
        // the current number
        subsetGen(arr, i + 1, n);
        c.add(arr[i]);
        subsetGen(arr, i + 1, n);
        c.remove(c.size() - 1);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[] arr = { 1, 1 };
        int n = arr.length;
 
        subsetGen(arr, 0, n);
        System.out.println(ans);
    }
}
 
// This code is contributed by
// sanjeev2552


Python3




# Python3 implementation of the approach
 
# Function to sum of all subsets
# of a given array
c = []
ans = 0
 
def subsetSum(i, curr):
    global ans, c
     
    # Base case
    if (i == len(c)):
        ans += curr
        return
 
    # Recursively calling subsetSum
    subsetSum(i + 1, curr + c[i])
    subsetSum(i + 1, curr)
 
# Function to generate the subsets
def subsetGen(arr, i, n):
    global ans, c
     
    # Base-case
    if (i == n):
 
        # Finding the sum of all the subsets
        # of the generated subset
        subsetSum(0, 0)
        return
 
    # Recursively accepting and rejecting
    # the current number
    subsetGen(arr, i + 1, n)
    c.append(arr[i])
    subsetGen(arr, i + 1, n)
    del c[-1]
 
# Driver code
arr = [1, 1]
n = len(arr)
 
subsetGen(arr, 0, n)
 
print(ans)
 
# This code is contributed by Mohit Kumar


C#




// C# implementation of the approach
using System;
using System.Collections.Generic;
 
class GFG
{
    static List<int> c = new List<int>();
 
    // To store the readonly ans
    static int ans = 0;
 
    // Function to sum of all subsets of a
    // given array
    static void subsetSum(int i, int curr)
    {
 
        // Base case
        if (i == c.Count)
        {
            ans += curr;
            return;
        }
 
        // Recursively calling subsetSum
        subsetSum(i + 1, curr + c[i]);
        subsetSum(i + 1, curr);
    }
 
    // Function to generate the subsets
    static void subsetGen(int[] arr, int i, int n)
    {
 
        // Base-case
        if (i == n)
        {
 
            // Finding the sum of all the subsets
            // of the generated subset
            subsetSum(0, 0);
            return;
        }
 
        // Recursively accepting and rejecting
        // the current number
        subsetGen(arr, i + 1, n);
        c.Add(arr[i]);
        subsetGen(arr, i + 1, n);
        c.RemoveAt(c.Count - 1);
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        int[] arr = { 1, 1 };
        int n = arr.Length;
 
        subsetGen(arr, 0, n);
        Console.WriteLine(ans);
    }
}
 
// This code is contributed by Rajput-Ji


Javascript




<script>
 
// Javascript implementation of the approach
 
var ans = 0;
 
var c = [];
 
// Function to sum of all subsets of a
// given array
function subsetSum(i, curr)
{
    // Base case
    if (i == c.length) {
        ans += curr;
        return;
    }
 
    // Recursively calling subsetSum
    subsetSum( i + 1, curr + c[i]);
    subsetSum(i + 1, curr);
}
 
// Function to generate the subsets
function subsetGen(arr, i, n, ans)
{
    // Base-case
    if (i == n) {
 
        // Finding the sum of all the subsets
        // of the generated subset
        subsetSum(0, ans, 0);
        return;
    }
 
    // Recursively accepting and rejecting
    // the current number
    subsetGen(arr, i + 1, n, ans);
    c.push(arr[i]);
    subsetGen(arr, i + 1, n, ans);
    c.pop();
}
 
// Driver code
var arr = [1, 1 ];
var n = arr.length;
// To store the final ans
var ans = 0;
var c = [];
subsetGen(arr, 0, n, ans);
document.write( ans);
 
</script>


Output: 

6

 

Time Complexity: O(n * 2n ), to generate all the subsets where n is the size of the given array
Auxiliary Space: O(n), to store the final answer



Last Updated : 10 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads