+ 2

How to take every possibilities of non-adjacent elements from list

Find largest sum of non-adjacent elements in the list

14th Jan 2022, 5:58 PM
Anna Hari
Anna Hari - avatar
2 ответов
+ 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
14th Jan 2022, 6:34 PM
Vaibhav
Vaibhav - avatar
+ 1
Thanks a lot but I can't understand how
14th Jan 2022, 7:00 PM
Anna Hari
Anna Hari - avatar