mediumArraysPattern: Alternating sum subarray

Alternating Sum Subarray Maximum Solution

Problem Statement

Given an array of integers, find the maximum sum of a subarray where the signs of the elements alternate, ending with either a positive or negative value.

Examples

Example 1:
Input:[1, -2, 3, -4, 5]
Output:12
Explanation: The subarray [1, -2, 3, -4, 5] has alternating signs and the maximum sum.
Example 2:
Input:[-1, 2, -3, 4, -5]
Output:12
Explanation: The subarray [-1, 2, -3, 4, -5] has alternating signs and the maximum sum.

Constraints

  • Input array will have at least one element and at most 100 elements.
  • Each element in the array will be an integer between -1000 and 1000.
Time: O(N) Space: O(1)
Analyze constraints and compute optimal solutions step-by-step.

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

No solution code is currently loaded.
Complete this code in the workspace editor.