Subset with maximum sum in JavaScript

We are required to write a JavaScript function that takes in an array of integers. Our function is required find the subset of non−adjacent elements with the maximum sum.

And finally, the function should calculate and return the sum of that subset.

For example −

If the input array is −

const arr = [3, 5, 7, 8, 10];

Then the output should be 20 because the non−adjacent subset of numbers will be 3, 7 and 10.

Example

The code for this will be −

const arr = [3, 5, 7, 8, 10];
const maxSubsetSum = (arr = []) => {
   let min = −Infinity
   const helper = (arr, ind) => {
      if ( ind 

Output

And the output in the console will be −

20
Updated on: 2020-11-24T06:55:04+05:30

369 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements