GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

Maximum sum in an array such that no 3 consecutive elements are added

For 1 3 5 9 2, it should be 1+5+9=15

I understand the basic approach (which I think is optimal). However, I'm facing some problems in its code implementation.

 

Basically, store at each point the max answer such that: 

1. Last element was picked

2. Last was not, second last was picked

3. None of the previous two were picked

asked Aug 23, 2016 by anon

1 Answer

Can you please give more examples to elaborate ?

answered Aug 24, 2016 by Mazhar MIK

Well, nevermind now, I solved it.

However, if you still want to try:

3 2 3 2 3 5 1 3
17

(3+3+3+5+3)

 

10 3 5 7 3 
23

(10+3+7+3)

...