Sum of subsets of all the subsets of an array | O(2^N)
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: 6
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(N * 2N) 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 subsets.
For, that it can be observed that in an array of length L, every element will come exactly 2(L – 1) times in the sum of subsets. So, the contribution of each element will be 2(L – 1) times its values.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void subsetSum(vector< int >& c, int & ans)
{
int L = c.size();
int mul = ( int ) pow (2, L - 1);
for ( int i = 0; i < c.size(); i++)
ans += c[i] * mul;
}
void subsetGen( int * arr, int i, int n,
int & ans, vector< int >& c)
{
if (i == n) {
subsetSum(c, ans);
return ;
}
subsetGen(arr, i + 1, n, ans, c);
c.push_back(arr[i]);
subsetGen(arr, i + 1, n, ans, c);
c.pop_back();
}
int main()
{
int arr[] = { 1, 1 };
int n = sizeof (arr) / sizeof ( int );
int ans = 0;
vector< int > c;
subsetGen(arr, 0, n, ans, c);
cout << ans;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int ans;
static void subsetSum(Vector<Integer> c)
{
int L = c.size();
int mul = ( int )Math.pow( 2 , L - 1 );
for ( int i = 0 ; i < c.size(); i++)
ans += c.get(i) * mul;
}
static void subsetGen( int []arr, int i,
int n, Vector<Integer> c)
{
if (i == n)
{
subsetSum(c);
return ;
}
subsetGen(arr, i + 1 , n, c);
c.add(arr[i]);
subsetGen(arr, i + 1 , n, c);
c.remove( 0 );
}
public static void main(String []args)
{
int arr[] = { 1 , 1 };
int n = arr.length;
Vector<Integer> c = new Vector<Integer>();
subsetGen(arr, 0 , n, c);
System.out.println(ans);
}
}
|
Python3
c = []
ans = 0
def subsetSum():
global ans
L = len (c)
mul = pow ( 2 , L - 1 )
i = 0
while ( i < len (c)):
ans + = c[i] * mul
i + = 1
def subsetGen(arr, i, n):
if (i = = n) :
subsetSum()
return
subsetGen(arr, i + 1 , n)
c.append(arr[i])
subsetGen(arr, i + 1 , n)
c.pop()
if __name__ = = "__main__" :
arr = [ 1 , 1 ]
n = len (arr)
subsetGen(arr, 0 , n)
print (ans)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int ans;
static void subsetSum(List< int > c)
{
int L = c.Count;
int mul = ( int )Math.Pow(2, L - 1);
for ( int i = 0; i < c.Count; i++)
ans += c[i] * mul;
}
static void subsetGen( int []arr, int i,
int n, List< int > c)
{
if (i == n)
{
subsetSum(c);
return ;
}
subsetGen(arr, i + 1, n, c);
c.Add(arr[i]);
subsetGen(arr, i + 1, n, c);
c.RemoveAt(0);
}
public static void Main(String []args)
{
int []arr = { 1, 1 };
int n = arr.Length;
List< int > c = new List< int >();
subsetGen(arr, 0, n, c);
Console.WriteLine(ans);
}
}
|
Javascript
<script>
var ans = 0;
function subsetSum( c) {
var L = c.length;
var mul = parseInt( Math.pow(2, L - 1));
for (i = 0; i < c.length; i++)
ans += c[i] * mul;
}
function subsetGen(arr , i , n, c) {
if (i == n) {
subsetSum(c);
return ;
}
subsetGen(arr, i + 1, n, c);
c.push(arr[i]);
subsetGen(arr, i + 1, n, c);
c.pop(0);
}
var arr = [ 1, 1 ];
var n = arr.length;
var c = [];
subsetGen(arr, 0, n, c);
document.write(ans);
</script>
|
Time Complexity: O(2^n), where n is the size of the given array.
Subset generation takes O(2^n) time as there are 2^n subsets of a given set.
Space Complexity: O(n).
Recursion stack will be used which will take O(n) space.
Last Updated :
27 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...