Challenge
Implement non_adjacent_sum(nums) for a list of integers. Return the greatest sum obtainable by choosing elements with no two adjacent indices. The empty selection is allowed, so an empty or all-negative list returns 0. The list is linear, not circular. Aim for O(n) time and constant auxiliary space.
Target complexity: O(n)
Return values are checked by type. Use the exact function name. Print output is not your answer. Tests have time and memory limits.
Public examples
Arguments
[[3, 2, 5, 10, 7]]
Expected return
15
Arguments
[[1, 2, 3, 4]]
Expected return
6
Hint
Compare taking this element with skipping it.
The worked solution unlocks after completion, or with Premium access.