Skip to content

Commit

Permalink
Added sub set sum in python
Browse files Browse the repository at this point in the history
  • Loading branch information
rpjayasekara committed Oct 22, 2018
1 parent 445f8b2 commit 0887629
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Dynamic Programming/sub set sum/python/subSetSum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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])


# Test case
set = [3, 34, 4, 12, 5, 2]
sum = 100
n = len(set)
if (isSubsetSum(set, n, sum) == True) :
print("Found a subset with given sum")
else :
print("No subset with given sum")

0 comments on commit 0887629

Please sign in to comment.