给定长度为N的数组arr []和整数X ,任务是使用递归找到总和等于X的子集数。
例子:
Input: arr[] = {2, 3, 5, 6, 8, 10}, X = 10
Output: 3
Explanation:
All possible subsets with sum 10 are {2, 3, 5}, {2, 8}, {10}
Input: arr[] = {1, 2, 3, 4, 5}, X = 7
Output: 3
Explanation:
All possible subsets with sum 7 are {2, 5}, {3, 4}, {1, 2, 4}
方法:想法是递归检查所有子集。如果任何子集的总和等于N,则将计数增加1。否则,继续。
为了形成子集,每个元素有两种情况:
- 将元素包括在集合中。
- 排除集合中的元素。
因此,可以按照以下步骤计算答案:
- 获取要找到总和等于K的子集的数组。
- 通过以下方式递归计算总和等于K的子集:
- 基本情况:基本情况将是到达数组末尾的时间。如果在此求和为X,则将子集的计数增加1。返回在基本条件下评估的计数。
if (i == n) { if (sum == 0) count++; return count; }
- 递归调用:如果不满足基本要求,则调用该函数两次。一次通过将元素包括在索引“ i”处,一次通过不包括该元素。找到这两种情况的计数,然后返回最终计数。
count = subsetSum(arr, n, i + 1, sum - arr[i], count); count = subsetSum(arr, n, i + 1, sum, count);
- 返回语句:在每个步骤中,返回通过包含特定元素或不包含特定元素的子集计数。最后,当执行整个递归堆栈时,将返回总计数。
- 基本情况:基本情况将是到达数组末尾的时间。如果在此求和为X,则将子集的计数增加1。返回在基本条件下评估的计数。
从以上方法可以清楚地分析出,如果数组中有N个元素,则总共会出现2 N个情况。在上述情况下,使用递归检查每个元素。
下面是上述方法的实现:
C++
// C++ program to print the count of
// subsets with sum equal to the given value X
#include
using namespace std;
// Recursive function to return the count
// of subsets with sum equal to the given value
int subsetSum(int arr[], int n, int i,
int sum, int count)
{
// The recursion is stopped at N-th level
// where all the subsets of the given array
// have been checked
if (i == n) {
// Incrementing the count if sum is
// equal to 0 and returning the count
if (sum == 0) {
count++;
}
return count;
}
// Recursively calling the function for two cases
// Either the element can be counted in the subset
// If the element is counted, then the remaining sum
// to be checked is sum - the selected element
// If the element is not included, then the remaining sum
// to be checked is the total sum
count = subsetSum(arr, n, i + 1, sum - arr[i], count);
count = subsetSum(arr, n, i + 1, sum, count);
return count;
}
// Driver code
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int sum = 10;
int n = sizeof(arr) / sizeof(arr[0]);
cout << subsetSum(arr, n, 0, sum, 0);
}
Java
// Java program to print the count of
// subsets with sum equal to the given value X
import java.util.*;
class GFG
{
// Recursive function to return the count
// of subsets with sum equal to the given value
static int subsetSum(int arr[], int n, int i,
int sum, int count)
{
// The recursion is stopped at N-th level
// where all the subsets of the given array
// have been checked
if (i == n)
{
// Incrementing the count if sum is
// equal to 0 and returning the count
if (sum == 0)
{
count++;
}
return count;
}
// Recursively calling the function for two cases
// Either the element can be counted in the subset
// If the element is counted, then the remaining sum
// to be checked is sum - the selected element
// If the element is not included, then the remaining sum
// to be checked is the total sum
count = subsetSum(arr, n, i + 1, sum - arr[i], count);
count = subsetSum(arr, n, i + 1, sum, count);
return count;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 2, 3, 4, 5 };
int sum = 10;
int n = arr.length;
System.out.print(subsetSum(arr, n, 0, sum, 0));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to print the count of
# subsets with sum equal to the given value X
# Recursive function to return the count
# of subsets with sum equal to the given value
def subsetSum(arr, n, i,sum, count):
# The recursion is stopped at N-th level
# where all the subsets of the given array
# have been checked
if (i == n):
# Incrementing the count if sum is
# equal to 0 and returning the count
if (sum == 0):
count += 1
return count
# Recursively calling the function for two cases
# Either the element can be counted in the subset
# If the element is counted, then the remaining sum
# to be checked is sum - the selected element
# If the element is not included, then the remaining sum
# to be checked is the total sum
count = subsetSum(arr, n, i + 1, sum - arr[i], count)
count = subsetSum(arr, n, i + 1, sum, count)
return count
# Driver code
arr = [1, 2, 3, 4, 5]
sum = 10
n = len(arr)
print(subsetSum(arr, n, 0, sum, 0))
# This code is contributed by mohit kumar 29
C#
// C# program to print the count of
// subsets with sum equal to the given value X
using System;
class GFG
{
// Recursive function to return the count
// of subsets with sum equal to the given value
static int subsetSum(int []arr, int n, int i,
int sum, int count)
{
// The recursion is stopped at N-th level
// where all the subsets of the given array
// have been checked
if (i == n)
{
// Incrementing the count if sum is
// equal to 0 and returning the count
if (sum == 0)
{
count++;
}
return count;
}
// Recursively calling the function for two cases
// Either the element can be counted in the subset
// If the element is counted, then the remaining sum
// to be checked is sum - the selected element
// If the element is not included, then the remaining sum
// to be checked is the total sum
count = subsetSum(arr, n, i + 1, sum - arr[i], count);
count = subsetSum(arr, n, i + 1, sum, count);
return count;
}
// Driver code
public static void Main(String[] args)
{
int []arr = { 1, 2, 3, 4, 5 };
int sum = 10;
int n = arr.Length;
Console.Write(subsetSum(arr, n, 0, sum, 0));
}
}
// This code is contributed by PrinciRaj1992
输出:
3
高效方法:
本文讨论了一种使用动态编程解决问题的有效方法。