← Programming questions
Medium / PYTHON

Non-Adjacent Sum

arrays · dynamic_programming 20 minute study target

Not completed

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

Example 1

Arguments

[[3, 2, 5, 10, 7]]

Expected return

15
Example 2

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.

non_adjacent_sum.pyPython 3.12

Drafts are saved to your account, not to shared browser storage.

Sign in to submit code and save progress.

Results

Your results will appear here.

Hidden tests reveal only their pass/fail status, never their inputs or expected outputs. Runtime is observed wall time—not an inferred complexity class.

© 2026 EverythingQuant. All Rights Reserved.