
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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.
To solve this, we will follow these steps −
total:= nums[0]
max_total:= nums[0]
for i in range 1 to size of nums, do
if nums[i] > nums[i-1], then
total := total + nums[i]
otherwise,
total:= nums[i]
if total > max_total, then
max_total:= total
return max_total
Let us see the following implementation to get better understanding −
Example
def solve(nums): total=nums[0] max_total=nums[0] for i in range(1,len(nums)): if nums[i] > nums[i-1]: total+=nums[i] else: total=nums[i] if total > max_total: max_total=total return max_total nums = [15,25,35,5,15,55] print(solve(nums))
Input
[15,25,35,5,15,55]
Output
75
- Related Articles
- Program to find maximum absolute sum of any subarray in Python
- C++ Program to Find the maximum subarray sum using Binary Search approach
- Program to find range sum of sorted subarray sums using Python
- Program to find the maximum sum of the subarray modulo by m in Python
- Program to find maximum subarray min-product in Python
- Using Kadane’s algorithm to find maximum sum of subarray in JavaScript
- Program to find out the sum of the maximum subarray after a operation in Python
- Python Program to solve Maximum Subarray Problem using Kadane’s Algorithm
- Program to find maximum product of contiguous subarray in Python
- Python Program to solve Maximum Subarray Problem using Divide and Conquer
- Program to find maximum score of a good subarray in Python
- Maximum subarray sum in circular array using JavaScript
- Find Maximum Sum Strictly Increasing Subarray in C++
- C++ Program to find the maximum subarray sum O(n^2) time (naive method)
- Program to find maximum length of subarray with positive product in Python

Advertisements