For given set, and sum, and length of elements,
I want to get the boolean value whether the set satisfy the condition
For example...
Input : set = [18,0,2,20], sum = 20, length = 2 <br>
Output : True (subset [18,2] satisfy the sum=20 for given length 2)
Input : set = [18,0,2,20], sum = 22, length = 1 <br>
Output : False
How can I solve the problem if there is a given length constraint?
(I can solve it easily if there is no length condition:
subset-sum-problem)
def isSubsetSum(set, n, sum):
if sum == 0:
return True
if (sum != 0) and (n == 0):
return False
if (set[n-1] > sum):
return isSubsetSum(set,n-1,sum)
# (a) including the last element
# (b) excluding the last element
# Not "AND", But "OR" !!!!!
return isSubsetSum(set,n-1,sum) or isSubsetSum(set,n-1,sum-set[n-1])