Maximum Alternating Subsequence Sum - Problem

Imagine you're playing a strategic game where you can pick numbers from an array to maximize your score, but there's a twist - the positions matter!

The alternating sum of a sequence works like this:

  • Elements at even positions (0, 2, 4, ...) are added to your score
  • Elements at odd positions (1, 3, 5, ...) are subtracted from your score

For example, with array [4,2,5,3], the alternating sum is: (4 + 5) - (2 + 3) = 4

Your challenge: Given an array nums, find the maximum alternating sum of any subsequence you can create. Remember, a subsequence maintains the relative order of elements but can skip some elements.

Goal: Select elements strategically to maximize your alternating sum score!

Input & Output

example_1.py โ€” Basic Case
$ Input: [4,2,5,3]
โ€บ Output: 7
๐Ÿ’ก Note: The optimal subsequence is [4,5]. Alternating sum = 4 + 5 = 9. Wait, let me recalculate... Actually, we can pick [4,2,5] giving us 4 - 2 + 5 = 7, or just [4,5] at positions 0,2 giving us 4 + 5 = 9. The maximum is 7 from subsequence [4,2,5].
example_2.py โ€” Single Element
$ Input: [5]
โ€บ Output: 5
๐Ÿ’ก Note: With only one element, the maximum alternating sum is the element itself at even position: 5.
example_3.py โ€” Optimal Selection
$ Input: [6,2,1,2,4,5]
โ€บ Output: 10
๐Ÿ’ก Note: Optimal subsequence is [6,1,4,5] giving alternating sum: 6 - 1 + 4 - 5 = 4. Actually, better is [6,2,5] = 6 - 2 + 5 = 9, or [6,1,5] = 6 - 1 + 5 = 10.

Visualization

Tap to expand
Stock Trading AnalogyStock Prices Over Time: [4, 2, 5, 3]Day 1$4Day 2$2Day 3$5Day 4$3Trading Strategy๐Ÿ”ต Even Position (Buy): Spend money, reduce profit๐ŸŸข Odd Position (Sell): Gain money, increase profit๐Ÿ’ก Goal: Maximize total profit from buy/sell sequence๐Ÿ“ˆ Optimal: Buy at $2, Sell at $5 โ†’ Profit = $3DP State Transitions:DayPriceNot HoldingHoldingActionProfit CalculationStart-$0$0InitializeStarting with no money, no stock1$4$0$4Buy at $4holding = max(0, 0+4) = 42$2$2$4Sell stock bought at $4not_holding = max(0, 4-2) = 23$5$7$4Buy at $5 (or keep holding)holding = max(4, 2+5) = 74$3$7$4Keep current positionnot_holding = max(2, 7-3) = 4Maximum Profit: $7 (End without holding stock)
Understanding the Visualization
1
Initialize States
Start with zero profit in both 'holding stock' and 'not holding stock' states
2
Process Each Day
For each stock price, decide whether to buy, sell, or hold
3
Update Profits
Calculate maximum profit for each state based on current price
4
Choose Optimal
Return the maximum profit when not holding stock (we want to end with a sale)
Key Takeaway
๐ŸŽฏ Key Insight: This problem is equivalent to finding the maximum profit from stock trading where you can buy and sell multiple times. The DP states track the best profit when you're ready to 'add' (buy) vs 'subtract' (sell) the current price.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the array, constant work per element

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only storing two variables regardless of input size

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • 1 โ‰ค nums[i] โ‰ค 105
  • The subsequence must maintain relative order of original elements
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
42.3K Views
Medium-High Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen