📜  数组所有子集的子集总和| O(2 ^ N)

📅  最后修改于: 2021-05-24 18:58:57             🧑  作者: Mango

给定长度为N的数组arr [] ,任务是找到该数组所有子集的子集的总和。
例子:

方法:在本文中,将讨论O(N * 2 N )时间复杂度用于解决给定问题的方法。
首先,生成数组的所有可能子集。总共将有2 N个子集。然后,对于每个子集,找到其所有子集的总和。
因为,可以观察到,在长度为L的数组中,每个元素在子集的总和中将恰好是2 (L – 1)倍。因此,每个元素的贡献将为其值的2 (L – 1)倍。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to sum of all subsets of a
// given array
void subsetSum(vector& 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;
}
 
// Function to generate the subsets
void subsetGen(int* arr, int i, int n,
               int& ans, vector& c)
{
    // Base-case
    if (i == n) {
 
        // Finding the sum of all the subsets
        // of the generated subset
        subsetSum(c, ans);
        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 c;
 
    subsetGen(arr, 0, n, ans, c);
    cout << ans;
 
    return 0;
}


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


Python3
# Python3 implementation of the approach
 
# store the answer
c = []
ans = 0
 
# Function to sum of all subsets of a
# given array
def subsetSum():
    global ans
    L = len(c)
    mul = pow(2, L - 1)
    i = 0
    while ( i < len(c)):
        ans += c[i] * mul
        i += 1
         
# Function to generate the subsets
def subsetGen(arr, i, n):
 
    # Base-case
    if (i == n) :
 
        # Finding the sum of all the subsets
        # of the generated subset
        subsetSum()
        return
     
    # Recursively accepting and rejecting
    # the current number
    subsetGen(arr, i + 1, n)
    c.append(arr[i])
    subsetGen(arr, i + 1, n)
    c.pop()
 
# Driver code
if __name__ == "__main__" :
 
    arr = [ 1, 1 ]
    n = len(arr)
 
    subsetGen(arr, 0, n)
    print (ans)
     
# This code is contributed by Arnab Kundu


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


Javascript


输出:
6