Program to find maximum ascending subarray sum using Python

Suppose we have an array of positive values called nums, we have to find the maximum possible sum of an ascending subarray in nums. We can say a subarray [nums_l, nums_l+1, ..., nums_r-1, nums_r] is ascending when for all i where l <= i < r, nums_i < nums_i+1.

So, if the input is like nums = [15,25,35,5,15,55], then the output will be 75 as [5,15,55] is increasing subarray with maximum sum.

Algorithm

To solve this problem, we will follow these steps ?

  • current_sum := nums[0]

  • max_sum := nums[0]

  • for i in range 1 to size of nums, do

    • if nums[i] > nums[i-1], then

      • current_sum := current_sum + nums[i]

    • otherwise,

      • current_sum := nums[i]

    • if current_sum > max_sum, then

      • max_sum := current_sum

  • return max_sum

Example

Let us see the following implementation to get better understanding ?

def solve(nums):
    current_sum = nums[0]
    max_sum = nums[0]
    
    for i in range(1, len(nums)):
        if nums[i] > nums[i-1]:
            current_sum += nums[i]
        else:
            current_sum = nums[i]
        
        if current_sum > max_sum:
            max_sum = current_sum
    
    return max_sum

# Test the function
nums = [15, 25, 35, 5, 15, 55]
result = solve(nums)
print(f"Input: {nums}")
print(f"Maximum ascending subarray sum: {result}")
Input: [15, 25, 35, 5, 15, 55]
Maximum ascending subarray sum: 75

How It Works

The algorithm works by maintaining two variables:

  • current_sum − tracks the sum of the current ascending subarray

  • max_sum − keeps track of the maximum sum found so far

For the example [15, 25, 35, 5, 15, 55]:

  • Start with current_sum = 15, max_sum = 15

  • 25 > 15, so current_sum = 15 + 25 = 40, max_sum = 40

  • 35 > 25, so current_sum = 40 + 35 = 75, max_sum = 75

  • 5 < 35, so reset current_sum = 5

  • 15 > 5, so current_sum = 5 + 15 = 20

  • 55 > 15, so current_sum = 20 + 55 = 75

Additional Example

Let's test with another example ?

def solve(nums):
    current_sum = nums[0]
    max_sum = nums[0]
    
    for i in range(1, len(nums)):
        if nums[i] > nums[i-1]:
            current_sum += nums[i]
        else:
            current_sum = nums[i]
        
        if current_sum > max_sum:
            max_sum = current_sum
    
    return max_sum

# Test with different examples
test_cases = [
    [10, 20, 30, 5, 10, 50],
    [5, 4, 3, 2, 1],
    [1, 3, 6, 7, 9]
]

for nums in test_cases:
    result = solve(nums)
    print(f"Input: {nums}, Max sum: {result}")
Input: [10, 20, 30, 5, 10, 50], Max sum: 65
Input: [5, 4, 3, 2, 1], Max sum: 5
Input: [1, 3, 6, 7, 9], Max sum: 26

Conclusion

This algorithm efficiently finds the maximum ascending subarray sum in O(n) time complexity. It uses a sliding window approach to track current ascending sequences and maintains the maximum sum encountered.

Updated on: 2026-03-25T20:52:03+05:30

690 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements