Given an array arr[] of length N and an integer X, the task is to find the number of subsets with sum equal to X using recursion.
Examples:
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}
Approach: The idea is to recursively check all the subsets. If any subset has the sum equal to N, then increment the count by 1. Else, continue.
In order to form a subset, there are two cases for every element:
- Include the element in the set.
- Exclude the element in the set.
Therefore, the following steps can be followed to compute the answer:
- Get the array for which the subsets with the sum equal to K is to be found.
- Recursively count the subsets with the sum equal to K in the following way:
- Base Case: The base case will be when the end of the array has been reached. If here the sum has been found as X, then increase the count of the subset by 1. Return the count evaluated in the base condition.
if (i == n) { if (sum == 0) count++; return count; } - Recursive Call: If the base case is not satisfied, then call the function twice. Once by including the element at index ‘i’ and once by not including the element. Find the count for both these cases and then return the final count.
count = subsetSum(arr, n, i + 1, sum - arr[i], count); count = subsetSum(arr, n, i + 1, sum, count);
- Base Case: The base case will be when the end of the array has been reached. If here the sum has been found as X, then increase the count of the subset by 1. Return the count evaluated in the base condition.
- Return Statement: At every step, the count of subsets by either including a particular element or not including a particular element is returned. Finally, when the entire recursion stack is executed, the total count is returned.
From the above approach, it can be clearly analyzed that if there are N elements in the array, then a total of 2N cases arise. Every element is checked for the above cases using recursion.
Below is the implementation of the above approach:
C++
// C++ program to print the count of // subsets with sum equal to the given value X #include <iostream> 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 = 10n = 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
Efficient Approach:
An efficient method to solve the problem using Dynamic Programming has been discussed in this article.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Divide array in two Subsets such that sum of square of sum of both subsets is maximum
- Sum of subsets of all the subsets of an array | O(3^N)
- Sum of subsets of all the subsets of an array | O(2^N)
- Sum of subsets of all the subsets of an array | O(N)
- Split an Array A[] into Subsets having equal Sum and sizes equal to elements of Array B[]
- Count of subsets with sum equal to X
- Partition of a set into K subsets with equal sum using BitMask and DP
- Count number of subsets of a set with GCD equal to a given number
- Divide first N natural numbers into 3 equal sum subsets
- Maximum sum of Bitwise XOR of all elements of two equal length subsets
- Split numbers from 1 to N into two equal sum subsets
- Split array into equal length subsets with maximum sum of Kth largest element of each subset
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count of subsets having sum of min and max element less than K
- Subsets of size K with product equal to difference of two perfect squares
- Split array into two equal length subsets such that all repetitions of a number lies in a single subset
- Partition array into minimum number of equal length subsets consisting of a single distinct value
- Perfect Sum Problem (Print all subsets with given sum)
- Sum of sum of all subsets of a set formed by first N natural numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

