title: Ruby - Max Array Sum from non-adjacent integers in array
body: <p>This code walks through the array and returns the largest sum of non-adjacent integers. This is from HackerRank - Can anyone explain why this works? Its a solution I found online but I don't understand it and didn't figure this out myself.</p>
  
  <p>Thanks!</p>
  
  <p><a href="https://www.hackerrank.com/challenges/max-array-sum/problem?h_l=interview&amp;playlist_slugs%5B%5D=interview-preparation-kit&amp;playlist_slugs%5B%5D=dynamic-programming" rel="nofollow noreferrer" title="hackerrank challenge">https://www.hackerrank.com/challenges/max-array-sum/problem?h_l=interview&amp;playlist_slugs%5B%5D=interview-preparation-kit&amp;playlist_slugs%5B%5D=dynamic-programming</a></p>
  
  <pre><code>def maxSubsetSum(arr)
      incl = 0
      excl = 0
      temp = 0
      for i in 0...arr.size
          temp = incl
          incl = [arr[i]+excl, temp].max            
          excl = temp
      end
  return [incl, excl].max
  end
  
  maxSubsetSum([1,3,5,2,4,6,8])
  </code></pre>