所有可能子集的总和
给定一个大小为N的数组a 。任务是找到所有可能子集的总和。
例子:
Input: a[] = {3, 7}
Output: 20
The subsets are: {3} {7} {3, 7}
{3, 7} = 10
{3} = 3
{7} = 7
10 + 3 + 7 = 20
Input: a[] = {10, 16, 14, 9}
Output: 392
朴素方法:一种朴素的方法是使用幂集找到所有子集,然后对所有可能的子集求和以获得答案。
C++
// C++ program to check if there is a subset
// with sum divisible by m.
#include
using namespace std;
int helper(int N, int nums[], int sum, int idx)
{
// if we reach last index
if (idx == N) {
// and if the sum mod m is zero
return sum;
}
// 2 choices - to pick or to not pick
int picked = helper(N, nums, sum + nums[idx], idx + 1);
int notPicked = helper(N, nums, sum, idx + 1);
return picked + notPicked;
}
int sumOfSubset(int arr[], int n)
{
return helper(n, arr, 0, 0);
}
// Driver code
int main()
{
int arr[] = { 3, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << sumOfSubset(arr, n);
return 0;
}
C++
// C++ program to find the sum of
// the addition of all possible subsets.
#include
using namespace std;
// Function to find the sum
// of sum of all the subset
int sumOfSubset(int a[], int n)
{
int times = pow(2, n - 1);
int sum = 0;
for (int i = 0; i < n; i++) {
sum = sum + (a[i] * times);
}
return sum;
}
// Driver Code
int main()
{
int a[] = { 3, 7 };
int n = sizeof(a) / sizeof(a[0]);
cout << sumOfSubset(a, n);
}
Java
// Java program to find the sum of
// the addition of all possible subsets.
class GFG
{
// Function to find the sum
// of sum of all the subset
static int sumOfSubset(int []a, int n)
{
int times = (int)Math.pow(2, n - 1);
int sum = 0;
for (int i = 0; i < n; i++)
{
sum = sum + (a[i] * times);
}
return sum;
}
// Driver Code
public static void main(String[] args)
{
int []a = { 3, 7 };
int n = a.length;
System.out.println(sumOfSubset(a, n));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to find the Sum of
# the addition of all possible subsets.
# Function to find the sum
# of sum of all the subset
def SumOfSubset(a, n):
times = pow(2, n - 1)
Sum = 0
for i in range(n):
Sum = Sum + (a[i] * times)
return Sum
# Driver Code
a = [3, 7]
n = len(a)
print(SumOfSubset(a, n))
# This code is contributed by Mohit Kumar
C#
// C# program to find the sum of
// the addition of all possible subsets.
using System;
class GFG
{
// Function to find the sum
// of sum of all the subset
static int sumOfSubset(int []a, int n)
{
int times = (int)Math.Pow(2, n - 1);
int sum = 0;
for (int i = 0; i < n; i++)
{
sum = sum + (a[i] * times);
}
return sum;
}
// Driver Code
public static void Main()
{
int []a = { 3, 7 };
int n = a.Length;
Console.Write(sumOfSubset(a, n));
}
}
// This code is contributed by Nidhi
Javascript
输出
20
时间复杂度: O(2 N )
空间复杂度: O(N),因为递归堆栈空间
有效方法:一种有效的方法是使用观察来解决问题。如果我们写出所有的子序列,一个共同的观察点是每个数字在一个子集中出现2 (N-1)次,因此将导致2 (N-1)作为对总和的贡献。遍历数组并将(arr[i] * 2 N-1 )添加到答案中。
下面是上述方法的实现:
C++
// C++ program to find the sum of
// the addition of all possible subsets.
#include
using namespace std;
// Function to find the sum
// of sum of all the subset
int sumOfSubset(int a[], int n)
{
int times = pow(2, n - 1);
int sum = 0;
for (int i = 0; i < n; i++) {
sum = sum + (a[i] * times);
}
return sum;
}
// Driver Code
int main()
{
int a[] = { 3, 7 };
int n = sizeof(a) / sizeof(a[0]);
cout << sumOfSubset(a, n);
}
Java
// Java program to find the sum of
// the addition of all possible subsets.
class GFG
{
// Function to find the sum
// of sum of all the subset
static int sumOfSubset(int []a, int n)
{
int times = (int)Math.pow(2, n - 1);
int sum = 0;
for (int i = 0; i < n; i++)
{
sum = sum + (a[i] * times);
}
return sum;
}
// Driver Code
public static void main(String[] args)
{
int []a = { 3, 7 };
int n = a.length;
System.out.println(sumOfSubset(a, n));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to find the Sum of
# the addition of all possible subsets.
# Function to find the sum
# of sum of all the subset
def SumOfSubset(a, n):
times = pow(2, n - 1)
Sum = 0
for i in range(n):
Sum = Sum + (a[i] * times)
return Sum
# Driver Code
a = [3, 7]
n = len(a)
print(SumOfSubset(a, n))
# This code is contributed by Mohit Kumar
C#
// C# program to find the sum of
// the addition of all possible subsets.
using System;
class GFG
{
// Function to find the sum
// of sum of all the subset
static int sumOfSubset(int []a, int n)
{
int times = (int)Math.Pow(2, n - 1);
int sum = 0;
for (int i = 0; i < n; i++)
{
sum = sum + (a[i] * times);
}
return sum;
}
// Driver Code
public static void Main()
{
int []a = { 3, 7 };
int n = a.Length;
Console.Write(sumOfSubset(a, n));
}
}
// This code is contributed by Nidhi
Javascript
输出:
20
时间复杂度:O(N)
空间复杂度:O(1)
注意:如果 N 很大,答案可能会溢出,从而使用更大的数据类型。