+ 2
How to take every possibilities of non-adjacent elements from list
Find largest sum of non-adjacent elements in the list
2 Antworten
+ 10
def maxSum(a, n, k):
if (n <= 0):
return 0
option = maxSum(a, n - 1, k)
if (k >= a[n - 1]):
option = max(option, a[n - 1] +
maxSum(a, n - 2, k - a[n - 1]))
return option
if __name__ == '__main__':
arr = [ 50, 10, 20, 30, 40 ]
N = len(arr)
K = 100
print(maxSum(arr, N, K))
i think it should bhe solve your problem
+ 1
Thanks a lot but I can't understand how