📜  总和等于 X 的子集计数 |第 2 组

📅  最后修改于: 2022-05-13 01:56:04.481000             🧑  作者: Mango

总和等于 X 的子集计数 |第 2 组

给定一个长度为N的数组arr[]和一个整数X ,任务是找到总和等于X的子集的数量。

例子:

节省空间的方法:这个问题已经在这里的文章中讨论过。本文重点介绍仅使用O(X)空间的类似动态规划方法。上篇文章讨论的解决这个问题的标准DP关系为:

其中dp[i][C]存储子数组arr[0…i]的子集的数量,使得它们的和等于C 。可以注意到,第dp[i]状态只需要第dp[i-1]状态的数组值。因此,上述关系可以简化为:

这里需要注意的一点是,在计算dp[C]期间,变量C必须按降序迭代,以避免arr[i]在子集和计数中的重复性。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
 
// Function to find the count of subsets
// having the given sum
int subsetSum(int arr[], int n, int sum)
{
    // Initializing the dp-table
    int dp[sum + 1] = {};
 
    // Case for sum of elements in empty set
    dp[0] = 1;
 
    // Loop to iterate over array elements
    for (int i = 0; i < n; i++) {
        for (int j = sum; j >= 0; j--) {
 
            // If j-arr[i] is a valid index
            if (j - arr[i] >= 0) {
                dp[j] = dp[j - arr[i]] + dp[j];
            }
        }
    }
 
    // Return answer
    return dp[sum];
}
 
// Driven Code
int main()
{
    int arr[] = { 1, 1, 1, 1 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int sum = 1;
 
    cout << subsetSum(arr, N, sum) << endl;
 
    return 0;
}


Java
// Java implementation of the above approach
import java.util.*;
public class GFG
{
   
// Function to find the count of subsets
// having the given sum
static int subsetSum(int arr[], int n, int sum)
{
    // Initializing the dp-table
    int dp[] = new int[sum + 1];
 
    // Case for sum of elements in empty set
    dp[0] = 1;
 
    // Loop to iterate over array elements
    for (int i = 0; i < n; i++) {
        for (int j = sum; j >= 0; j--) {
 
            // If j-arr[i] is a valid index
            if (j - arr[i] >= 0) {
                dp[j] = dp[j - arr[i]] + dp[j];
            }
        }
    }
 
    // Return answer
    return dp[sum];
}
 
// Driver code
public static void main(String args[])
{
    int arr[] = { 1, 1, 1, 1 };
    int N = arr.length;
    int sum = 1;
     
    System.out.println(subsetSum(arr, N, sum));
}
}
 
// This code is contributed by Samim Hossain Mondal.


Python3
# Python implementation of the above approach
 
# Function to find the count of subsets
# having the given sum
def subsetSum(arr, n, sum):
 
    # Initializing the dp-table
    dp = [0] * (sum + 1)
 
    # Case for sum of elements in empty set
    dp[0] = 1;
 
    # Loop to iterate over array elements
    for i in range(n):
        for j in range(sum, 0, -1):
 
            # If j-arr[i] is a valid index
            if (j - arr[i] >= 0):
                dp[j] = dp[j - arr[i]] + dp[j];
    # Return answer
    return dp[sum];
 
# Driven Code
arr = [1, 1, 1, 1];
N = len(arr)
sum = 1;
 
print(subsetSum(arr, N, sum))
 
# This code is contributed by gfgking.


C#
// C# implementation of the above approach
using System;
 
public class GFG
{
   
// Function to find the count of subsets
// having the given sum
static int subsetSum(int []arr, int n, int sum)
{
   
    // Initializing the dp-table
    int []dp = new int[sum + 1];
 
    // Case for sum of elements in empty set
    dp[0] = 1;
 
    // Loop to iterate over array elements
    for (int i = 0; i < n; i++) {
        for (int j = sum; j >= 0; j--) {
 
            // If j-arr[i] is a valid index
            if (j - arr[i] >= 0) {
                dp[j] = dp[j - arr[i]] + dp[j];
            }
        }
    }
 
    // Return answer
    return dp[sum];
}
 
// Driver code
public static void Main(String []args)
{
    int []arr = { 1, 1, 1, 1 };
    int N = arr.Length;
    int sum = 1;
     
    Console.WriteLine(subsetSum(arr, N, sum));
}
}
 
// This code is contributed by shikhasingrajput


Javascript


输出
4

时间复杂度: O(N * X)
辅助空间: O(X)