> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Maximum Alternating Subsequence Sum

> Tested Python solution for LeetCode 1911 with 44 pytest cases. Generate a practice environment with lcpy.

LeetCode 1911, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Dynamic Programming](/catalog/topics/dynamic-programming). [View on LeetCode](https://leetcode.com/problems/maximum-alternating-subsequence-sum/description/).

Generate this problem as a practice environment: tested reference solution, 44 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1911   # by problem number
lcpy gen -s maximum_alternating_subsequence_sum   # by problem name
```

## Problem

The \<strong>alternating sum\</strong> of a \<strong>0-indexed\</strong> array is defined as the \<strong>sum\</strong> of the elements at \<strong>even\</strong> indices \<strong>minus\</strong> the \<strong>sum\</strong> of the elements at \<strong>odd\</strong> indices.

\<ul>
\<li>For example, the alternating sum of \<code>\[4,2,5,3]\</code> is \<code>(4 + 5) - (2 + 3) = 4\</code>.\</li>
\</ul>

Given an array \<code>nums\</code>, return \<em>the \<strong>maximum alternating sum\</strong> of any subsequence of \</em>\<code>nums\</code>\<em> (after \<strong>reindexing\</strong> the elements of the subsequence)\</em>.

\<p>A \<strong>subsequence\</strong> of an array is a new array generated from the original array by deleting some elements (possibly none) without changing the remaining elements' relative order. For example, \<code>\[2,7,4]\</code> is a subsequence of \<code>\[4,2,\<u>3\</u>,7,2,1,\<u>4\</u>]\</code> (the underlined elements), while \<code>\[2,4,2]\</code> is not.\</p>

### Examples

```
Input: nums = [4,2,5,3]
Output: 7
Explanation: It is optimal to choose the subsequence [4,2,5] with alternating sum (4 + 5) - 2 = 7.
```

```
Input: nums = [5,6,7,8]
Output: 8
Explanation: It is optimal to choose the subsequence [8] with alternating sum 8.
```

```
Input: nums = [6,2,1,2,4,5]
Output: 10
Explanation: It is optimal to choose the subsequence [6,1,5] with alternating sum (6 + 5) - 1 = 10.
```

### Constraints

* 1 \<= nums.length \<= 10^5
* 1 \<= nums\[i] \<= 10^5

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_alternating_subsequence_sum/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_alternating_subsequence_sum/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def max_alternating_sum(self, nums: list[int]) -> int:
        even = 0
        odd = 0
        for num in nums:
            even, odd = max(even, odd + num), max(odd, even - num)
        return even
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.