BackmediumArraysAmazon

Galactic Expedition Budgeting Solution

Problem Statement

Given an integer array nums, select a subsequence (preserving original order) such that the first chosen element is positive and the sign of consecutive chosen elements strictly alternates (positive, negative, positive, …). Maximise the sum of the selected elements. If the array contains no positive element, the answer is 0.

Example 1
Input
[5,-2,3,-1,4]
Output
9

Explanation: Pick indices 0,1,2,3,4 → 5‑2+3‑1+4=9, which follows the required sign pattern and yields the highest possible total.

Example 2
Input
[-3,-1,-2]
Output
0

Explanation: No positive number can start the subsequence, therefore the optimal sum is 0.

Example 3
Input
[10,-5,-2,8,-1]
Output
13

Explanation: Choosing 10 (index0), -5 (index1) and 8 (index3) gives 10‑5+8=13, larger than any other alternating subsequence.

Constraints

  • 1 <= nums.length <= 100000
  • -1000000000 <= nums[i] <= 1000000000
  • Time complexity O(n)
  • Auxiliary space O(1)
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Galactic Expedition Budgeting — Problem Statement & Solution Guide

ArraysMediumprefix sum modification
TimeO(n)
|
SpaceO(1)

Problem Description

Given an integer array nums, select a subsequence (preserving original order) such that the first chosen element is positive and the sign of consecutive chosen elements strictly alternates (positive, negative, positive, …). Maximise the sum of the selected elements. If the array contains no positive element, the answer is 0.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Galactic Expedition Budgeting"

medium

WHY DOES IT MATTER?

This pattern is essential for problems involving sequential decisions with state-dependent constraints. It teaches how to model state transitions in DP, which is a fundamental skill for optimizing complex systems where past decisions influence future choices.

OPTIMIZATION CHALLENGE

The key insight is recognizing that the DP state can be reduced to two variables (last sign positive or negative) rather than storing the entire DP table, reducing space complexity to $O(1)$.

REAL-WORLD CONNECTION

This is analogous to financial portfolio optimization where you alternate between buying and selling assets to maximize profit, or in network routing where you alternate between sending and receiving data packets to minimize latency.

In interviews, clearly define your DP states and transitions. Start with a brute-force approach, then optimize by identifying redundant states. Emphasize the space optimization to show depth of understanding.

COMPLEXITY AT A GLANCE

⏱ Time:O(n)
💾 Space:O(1)

Core Theory — Why This Approach?

The problem 'Galactic Expedition Budgeting' is a variant of the classic 'Maximum Alternating Subsequence Sum' problem, which falls under the domain of Dynamic Programming (DP) on arrays. The core challenge lies in selecting a subsequence where the signs of the elements strictly alternate (positive, negative, positive, etc.), starting with a positive number, to maximize the total sum. A naive approach might involve checking all possible subsequences, but this leads to exponential time complexity $O(2^n)$, which is infeasible for large inputs. The key insight is that the order of elements is preserved, but we are not required to pick contiguous elements, which suggests a state-based DP approach where the state depends on the last chosen element's sign.

Interview Questions on This Problem

Q1At a fintech platform like Stripe, how would you adapt this algorithm to handle a stream of transactions where you need to maximize profit from alternating buy/sell operations, but with a constraint that you can only hold one position at a time?

You can model this as a state machine DP. Define two states: cash (max profit when not holding a stock) and hold (max profit when holding a stock). For each price, update hold = max(hold, cash - price) and cash = max(cash, hold + price). This is analogous to the alternating sum problem where 'positive' corresponds to buying (negative cost) and 'negative' corresponds to selling (positive gain), but the state transitions are simplified to two variables instead of tracking the last sign explicitly.

Q2In a high-growth engineering startup building a recommendation engine, how would you optimize memory usage if the input array size is extremely large (e.g., 10^7 elements) and you cannot store the entire DP table?

Since the DP state only depends on the previous state (whether the last chosen element was positive or negative), you can use space optimization by maintaining only two variables: maxPos (max sum ending with a positive element) and maxNeg (max sum ending with a negative element). This reduces space complexity from $O(n)$ to $O(1)$, making it feasible for large-scale data processing.

Q3At a global product company like Google, how would you handle the edge case where the array contains no positive elements, and how does this affect the DP initialization?

If there are no positive elements, the answer is 0 because the subsequence must start with a positive element. In the DP approach, initialize maxPos and maxNeg to 0. As you iterate, maxPos will only update if a positive number is found, and maxNeg will only update if a negative number is found and maxPos is valid. If no positive number is encountered, maxPos remains 0, and the final answer is maxPos.

Examples

Example 1

Input

[5,-2,3,-1,4]

Output

9

Explanation: Pick indices 0,1,2,3,4 → 5‑2+3‑1+4=9, which follows the required sign pattern and yields the highest possible total.

Example 2

Input

[-3,-1,-2]

Output

0

Explanation: No positive number can start the subsequence, therefore the optimal sum is 0.

Example 3

Input

[10,-5,-2,8,-1]

Output

13

Explanation: Choosing 10 (index0), -5 (index1) and 8 (index3) gives 10‑5+8=13, larger than any other alternating subsequence.

Constraints

  • 1 <= nums.length <= 100000
  • -1000000000 <= nums[i] <= 1000000000
  • Time complexity O(n)
  • Auxiliary space O(1)

Optimal Approach & Strategy

Use dynamic programming with two variables: maxPos for the maximum sum ending with a positive element and maxNeg for the maximum sum ending with a negative element. Iterate through the array, updating maxPos and maxNeg based on the current element's sign.

Brute Force Approach

Generate all possible subsequences of the array and check if they satisfy the alternating sign constraint, starting with a positive element. Calculate the sum for each valid subsequence and keep track of the maximum sum.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function maxAlternatingSum(nums){
    const NEG_INF = -1e18;
    let bestPos = 0; // max sum ending with positive
    let bestNeg = NEG_INF; // max sum ending with negative
    for(const x of nums){
        if(x>0){
            const candFromNeg = bestNeg===NEG_INF? x : bestNeg + x;
            const candStart = x;
            bestPos = Math.max(bestPos, candFromNeg, candStart);
        }else if(x<0){
            if(bestPos>0){
                const cand = bestPos + x;
                if(cand>bestNeg) bestNeg = cand;
            }
        }
        // zeros are ignored
    }
    return bestPos;
}
const fs = require('fs');
const data = fs.readFileSync(0,'utf8').trim().split(/\s+/).map(Number);
if(data.length){
    const n = data[0];
    const nums = data.slice(1,1+n);
    console.log(maxAlternatingSum(nums));
}

Asked in Top Tech Interviews

Amazon

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.