Maximum Ascending Subarray Sum

Updated on 10 June, 2025
Maximum Ascending Subarray Sum header image

Problem Statement

Given an array comprised of positive integers called nums, the objective is to determine the maximum sum achievable by any strictly increasing continuous subarray within nums. Here, a subarray is defined as a contiguous segment of the array; it does not include elements with indices outside the selected range. The distinct challenge lies in the constraint that the sequence of numbers in the subarray must be strictly increasing, meaning each number in the sequence is greater than the previous one.

Examples

Example 1

Input:

nums = [10,20,30,5,10,50]

Output:

65

Explanation:

[5,10,50] is the ascending subarray with the maximum sum of 65.

Example 2

Input:

nums = [10,20,30,40,50]

Output:

150

Explanation:

[10,20,30,40,50] is the ascending subarray with the maximum sum of 150.

Example 3

Input:

nums = [12,17,15,13,10,11,12]

Output:

33

Explanation:

[10,11,12] is the ascending subarray with the maximum sum of 33.

Constraints

  • 1 <= nums.length <= 100
  • 1 <= nums[i] <= 100

Approach and Intuition

To tackle this problem, the main goal is to identify all possible strictly increasing subarrays and find out which one has the highest sum. Here's a simplified breakdown of how one might approach finding the solution:

  1. Initialization: Start with variables to store the maximum sum encountered (max_sum) and the current sum of the increasing subarray (current_sum).

  2. Traverse through the array: As you progress from the first to the last element in the array:

    • If the current element is greater than the previous one (i.e., the sequence continues to be strictly increasing), add the current element's value to current_sum.
    • If not, this indicates the end of the current increasing subarray. At this point, compare current_sum with max_sum and update max_sum if necessary. Then reset current_sum to the current element's value, as this could potentially be the start of a new strictly increasing subarray.
  3. Final Comparison: Post traversal, there is a need for a final comparison of current_sum with max_sum to ensure that an increasing subarray, which might terminate at the last element of the array, is considered.

By considering the above steps, you can efficiently find the strictly increasing subarray with the maximum sum. This approach utilizes a single pass through the data (O(n) time complexity), making it efficient for the given constraints.

Examples help illustrate this method:

  • In the sequence [10,20,30,5,10,50], the increasing subarray [5,10,50] provides the highest sum of 65, being identified once the break in increase between 30 and 5 is noted.
  • Conversely, the array [10,20,30,40,50] is entirely increasing, so the maximum sum is simply the sum of all elements.
  • In more fragmented cases like [12,17,15,13,10,11,12], identifying shorter segments like [10,11,12] that provide the optimal sum requires careful comparisons at each step where the sequence breaks.

Solutions

  • C++
  • Java
  • Python
cpp
class Solution {
public:
    int findMaxAscendingSum(vector<int>& arr) {
        int highestSum = 0;
        int tempSum = arr[0];
        
        for (int i = 1; i < arr.size(); ++i) {
            if (arr[i] <= arr[i - 1]) {
                highestSum = max(highestSum, tempSum);
                tempSum = 0;
            }
            tempSum += arr[i];
        }
        
        return max(highestSum, tempSum);
    }
};

To solve the problem of finding the Maximum Ascending Subarray Sum using C++, the provided solution utilizes a simple yet effective iterative approach to traverse the array while keeping track of sum accumulations for ascending sequences.

  • Start by initializing two integer variables: highestSum to hold the maximum sum found and tempSum to store the current sum, starting with the first element of the array.
  • Traverse the array starting from the second element. For each element:
    • Compare the current element with the previous one.
    • If the current element is less than or equal to the previous, update highestSum with the maximum of highestSum and tempSum, and reset tempSum to zero since the ascending sequence has ended.
    • Regardless of the condition, always add the current element's value to tempSum.
  • After the loop concludes, compare highestSum with tempSum one last time to ensure no ascending sequence at the end of the array is missed.
  • Return highestSum, which now holds the maximum sum of an ascending subarray in the provided array.

Consider implementing these steps to efficiently find the maximum sum of any ascending subarray within a given list of integers. With its rudimentary control structure and effective use of conditional checks and updates, this C++ solution elegantly addresses the problem without the need for complex data structures.

java
public class Solution {

    public int findMaxAscendingSum(int[] values) {
        int highestSum = 0;
        int sumOfSubarray = values[0];

        for (int i = 1; i < values.length; ++i) {
            if (values[i] <= values[i - 1]) {
                highestSum = Math.max(highestSum, sumOfSubarray);
                sumOfSubarray = 0;
            }
            sumOfSubarray += values[i];
        }

        return Math.max(highestSum, sumOfSubarray);
    }
}

The code in Java defines a method findMaxAscendingSum which calculates the maximum sum of any continuously ascending subarray within the given array values. Below highlights the approach taken within the method:

  1. Initialize highestSum to store the maximum sum encountered during the iteration through the array.
  2. Start by setting sumOfSubarray with the first element of the array, as this will begin the process of iterating and summing potential ascending sequences.

Continue iterating through the array from the second element using the following mechanism:

  • Check if the current element is greater than the previous element to determine if the sequence is still ascending.
  • If the sequence breaks (current element is less than or equal to the previous one), compare sumOfSubarray with highestSum and update highestSum if sumOfSubarray is greater.
  • Reset sumOfSubarray for a new potential ascending sequence starting from the current element.
  • If the sequence has not broken, simply add the current element’s value to sumOfSubarray.

After completing the loop, ensure to check one last time if the last counted sumOfSubarray is the highest sum found during the iteration.

This method ensures that all possible ascending sequences within the array are considered and that the highest sum of such sequences is efficiently found and returned.

python
class Solution:
    def findMaxAscendingSum(self, data: List[int]) -> int:
        highestSum = 0
        sumOfSubarray = data[0]

        for index in range(1, len(data)):
            if data[index] > data[index - 1]:
                sumOfSubarray += data[index]
            else:
                highestSum = max(highestSum, sumOfSubarray)
                sumOfSubarray = data[index]

        return max(highestSum, sumOfSubarray)

This code defines a method findMaxAscendingSum within the Solution class that computes the maximum sum of an ascending subarray within a given list of integers. Follow the steps below to understand the implementation in detail:

  1. Initialize highestSum to 0 to track the highest ascending subarray sum.
  2. Start the sumOfSubarray with the first element in the list as this will be used to accumulate the sum of the current ascending subarray.
  3. Iterate over the list starting from the second element. Use an index to help track positions in the list:
    • If the current element is greater than the previous element, it means the sequence is still ascending. Thus, add the current element to sumOfSubarray.
    • Otherwise, update highestSum with the greater of highestSum or sumOfSubarray then reset sumOfSubarray to the current element, as the sequence has broken.
  4. After the loop, there might be an ongoing ascending subarray not yet considered in highestSum. Resolve this by taking the maximum of highestSum and sumOfSubarray one last time.
  5. Return highestSum as the maximum sum of an ascending subarray found.

Use this method to identify the maximum sum of any contiguous ascending subarray within a list of numbers, thereby optimizing the processing of sequential data analysis tasks.

Comments

No comments yet.