Today we are going to share a recursive solution for subset sum problem. If you are a python beginner and want to start learning the python programming, then keep your close attention in this tutorial as I am going to share **a recursive solution for subset sum problem**.

To increase your Python knowledge, practice all Python programs, here is a **collection of 100+ Python problems with solutions.**

Copy the below python program and execute it with the help of **python compiler**.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | def isSubsetSum(set,n, sum) : # Base Cases if (sum == 0) : return True if (n == 0 and sum != 0) : return False # If last element is greater than # sum, then ignore it if (set[n - 1] > sum) : return isSubsetSum(set, n - 1, sum); # else, check if sum can be obtained # by any of the following # (a) including the last element # (b) excluding the last element return isSubsetSum(set, n-1, sum) or isSubsetSum(set, n-1, sum-set[n-1]) # Driver program to test above function set = [3, 34, 4, 12, 5, 2] sum = 9 n = len(set) if (isSubsetSum(set, n, sum) == True) : print("Found a subset with given sum") else : print("No subset with given sum") |

Found a subset with given sum

- Python program for number of stopping station problem
- Create mysql database from python script
- Remove multiple elements from a list in python
- List methods in python
- Python programming exercises and solutions
- Python 3 program for pendulum arrangement of numbers
- Python program to read the contents of a file in reverse order
- Python code to reverse a string using stack
- Python program to illustrate closures
- Python program to convert time from 12 hour to 24 hour format

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok