Problem Statement:
Given an array of integers, find the subset of non-adjacent elements with the maximum sum. Calculate the sum of that subset.
https://www.hackerrank.com/challenges/max-array-sum/problem

Sample Code:

def maxSubsetSum(arr):
    n=len(arr)
    x=[0]*n
    x[0]=arr[0]
    x[1]=max(arr[1],x[0])
    for i in range(2,n):
        x[i]=max(arr[i],x[i-1],arr[i]+x[i-2])
    return x[-1]    

Explanation:

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top