The problem is to find the number of subsets that add up to a target, for example, if the target is 16 and the array is [2, 4, 6, 10], it should return 2, because 2 + 4 + 10 = 16 and 10 + 6 = 16. I tried to make the recursive solution. I need help to figure out where is the mistake in my code. This is my code:
def num_sum(target, arr, ans=0 ):
if target == 0:
return 1
elif target < 0:
return 0
for i in arr:
arr.remove(i)
num = num_sum(target - i, arr, ans)
ans += num
return ans
print(num_sum(16, [2, 4, 6, 10]))
Thanks in advance.