title: Max Sub Array (non-adjacent)
body: <p>I'm working on a HackerRank Max Array Sum problem. I see some simple and smart solutions but I wanna know why my code failing.</p>
  <p>Here is my code. It takes next 4 elements from array and choose index0 or index1 and shift 2 or 3 elements.
  if I can find max subset elements instead of sum of elements I can see my mistake.
  <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">HackerRank Problem Link</a></p>
  <pre><code>def Calc(arr):
  
  result = 0
  
  i = 0
  while i &lt; len(arr):
  
      tempar = [i if i &gt; 0 else 0 for i in arr[i:i+4]]
  
      if len(tempar) == 4:
          tmax = max(tempar[0] + tempar[2], tempar[0] + tempar[3], tempar[1] + tempar[3])
  
          if tempar[0] + tempar[2] == tmax or tempar[0] + tempar[3] == tmax:
              result += tempar[0]
              i += 2
          elif tempar[1] + tempar[3] == tmax:
              result += tempar[1]
              i+=3
  
      if len(tempar) == 3:
          if tempar[0] + tempar[2] &gt; tempar[1]:
              result += tempar[0] + tempar[2]
          else:
              result += tempar[1]
          i+=3
  
      if len(tempar) == 2:
          result += max(tempar)
          i+=2
      
      if len(tempar) == 1:
          result += tempar[0]
          i+=1
  return result
  
  
  input()
  
  ar = list(map(int, input().split()))
      
      
  print(Calc(ar))
  </code></pre>