- Related Questions & Answers
- C Program for Tower of Hanoi
- C++ Program for Dijkstra’s shortest path algorithm?
- C/C++ Program for nth Catalan Number?
- C Program to Multiply two Floating Point Numbers?
- C program to find the length of a string?
- C Program to Check if all digits of a number divide it
- C Program to Check if a Given String is a Palindrome?
- Write a program to Delete a Tree in C programming
- Write a C program to print “ Tutorials Point ” without using a semicolon
- Sum of the nodes of a Singly Linked List in C Program
- Sum of first n natural numbers in C Program
- C Program for simple interest?
- Area of a n-sided regular polygon with given Radius in C Program?
- Area of a leaf inside a square in C Program?
- C Program for Area of a square inscribed in a circle which is inscribed in a hexagon?

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this article, we will learn about the solution to the problem statement given below.

**Problem statement** − We are given a set of non-negative integers in an array, and a value sum, we need to determine if there exists a subset of the given set with a sum equal to a given sum.

Now let’s observe the solution in the implementation below −

**# Naive approach**

def SubsetSum(set, n, sum) : # Base Cases if (sum == 0) : return True if (n == 0 and sum != 0) : return False # ignore if last element is > sum if (set[n - 1] > sum) : return SubsetSum(set, n - 1, sum); # else,we check the sum # (1) including the last element # (2) excluding the last element return SubsetSum(set, n-1, sum) or SubsetSum(set, n-1, sumset[n-1]) # main set = [2, 14, 6, 22, 4, 8] sum = 10 n = len(set) if (SubsetSum(set, n, sum) == True) : print("Found a subset with given sum") else : print("No subset with given sum")

Found a subset with given sum

**# dynamic approach**

# maximum number of activities that can be performed by a single person def Activities(s, f ): n = len(f) print ("The selected activities are:") # The first activity is always selected i = 0 print (i,end=" ") # For rest of the activities for j in range(n): # if start time is greater than or equal to that of previous activity if s[j] >= f[i]: print (j,end=" ") i = j # main s = [1, 2, 0, 3, 2, 4] f = [2, 5, 4, 6, 8, 8] Activities(s, f)

Found a subset with given sum

In this article, we have learned about how we can make a Python Program for Subset Sum Problem

Advertisements