So, I want to create a function that takes int s and array A, and then returns array of elements A that add up to s. if no subset, should return value closest to s.
For example:
A = [12, 79, 99, 91, 81, 47]
s = 150
Will return:
[12, 91, 47]
as 12 + 91 + 47
is 150
.
The below is what I have so far. What am I doing wrong?
def closest(s, A):
if s == 0:
return 0
for i in range(len(A)):
if A[i] <= s:
return 1 + closest(s - A[i], A)