BackmediumArraysMicrosoft

Alternating Subarray Sum Solution

Problem Statement

Given an array of integers asteroidValues where positive values represent valuable resources and negative values represent hazardous asteroids, find the maximum sum of a subarray that alternates between positive and negative integers. The subarray must start with a positive integer and alternate between positive and negative integers.

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

Explanation: Step-by-step: Given the input array [3, -2, 5, -1, 4], we start with a positive integer 3. Then we encounter a negative integer -2, so we add 3 and -2 to get 1. Next, we encounter a positive integer 5, so we add 1 and 5 to get 6. Then, we encounter a negative integer -1, so we add 6 and -1 to get 5. Finally, we encounter a positive integer 4, but since the problem statement says the subarray must alternate between positive and negative integers, we should not include the last positive integer 4. Therefore, the correct output is indeed 8.

Example 2
Input
[5, -3, 7, -2, 10]
Output
9

Explanation: Step-by-step: Given the input array [5, -3, 7, -2, 10], we start with a positive integer 5. Then we encounter a negative integer -3, so we add 5 and -3 to get 2. Next, we encounter a positive integer 7, so we add 2 and 7 to get 9. Then, we encounter a negative integer -2, so we add 9 and -2 to get 7. Finally, we encounter a positive integer 10, but since the problem statement says the subarray must alternate between positive and negative integers, we should not include the last positive integer 10. Therefore, the correct output is indeed 9.

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

Alternating Subarray Sum — Problem Statement & Solution Guide

ArraysMediummax-subarray-sum
TimeO(n)
|
SpaceO(1)

Problem Description

Given an array of integers asteroidValues where positive values represent valuable resources and negative values represent hazardous asteroids, find the maximum sum of a subarray that alternates between positive and negative integers. The subarray must start with a positive integer and alternate between positive and negative integers.

Examples

Example 1

Input

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

Output

8

Explanation: Step-by-step: Given the input array [3, -2, 5, -1, 4], we start with a positive integer 3. Then we encounter a negative integer -2, so we add 3 and -2 to get 1. Next, we encounter a positive integer 5, so we add 1 and 5 to get 6. Then, we encounter a negative integer -1, so we add 6 and -1 to get 5. Finally, we encounter a positive integer 4, but since the problem statement says the subarray must alternate between positive and negative integers, we should not include the last positive integer 4. Therefore, the correct output is indeed 8.

Example 2

Input

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

Output

9

Explanation: Step-by-step: Given the input array [5, -3, 7, -2, 10], we start with a positive integer 5. Then we encounter a negative integer -3, so we add 5 and -3 to get 2. Next, we encounter a positive integer 7, so we add 2 and 7 to get 9. Then, we encounter a negative integer -2, so we add 9 and -2 to get 7. Finally, we encounter a positive integer 10, but since the problem statement says the subarray must alternate between positive and negative integers, we should not include the last positive integer 10. Therefore, the correct output is indeed 9.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function maxAlternatingSum(asteroidValues) {
  if (asteroidValues.length === 0) return 0;
  let maxSum = -Infinity;
  let currentSum = 0;
  let isPositive = true;

  for (let i = 0; i < asteroidValues.length; i++) {
    if (asteroidValues[i] > 0) {
      if (!isPositive) {
        currentSum = asteroidValues[i];
        isPositive = true;
      } else {
        currentSum += asteroidValues[i];
      }
    } else {
      if (isPositive) {
        currentSum = 0;
        isPositive = false;
      } else {
        currentSum += asteroidValues[i];
      }
    }

    maxSum = Math.max(maxSum, currentSum);
  }

  return maxSum;
}

Asked in Top Tech Interviews

Microsoft

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.