给定长度为N的数组arr [] ,任务是找到该数组所有子集的子集的总和。
例子:
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
方法:在本文中,将讨论O(3 N )时间复杂度来解决给定问题的方法。
首先,生成数组的所有可能子集。总共将有2 N个子集。然后,对于每个子集,找到其所有子集的总和。
现在,让我们了解此解决方案的时间复杂性。
存在长度为K的N C k个子集,找到长度为K的数组的时间为2 K。
总时间=( N C 1 * 2 1 )+( N C 2 * 2 2 )+…+( N C k * K )= 3 K
下面是上述方法的实现:
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 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& 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 c;
subsetGen(arr, 0, n, ans, c);
cout << ans;
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
static Vector 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 c = new List();
// 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
6