Largest Sum of Non-Adjacent Elements

You are given an array of integers as an argument. Find the greatest sum obtained by adding the integers together. Once integers from the array are added together, you may not add any integers adjacent to those integers.

Requirements

  • Must return a single integer

Example #1

solve([100,1,50,10,2,18])
> 168

  1. We pick the largest two numbers 100 and 50 to add and give us 150.
  2. We cannot use 1 or 10 since they are adjacent to the integers we have already used.
  3. Our last choices are 2 and 18, so we pick the larger number 18.
  4. We now cannot use 2 since it is adjacent to the last number we picked, which leaves us with the largest sum of 168.