1800. Maximum Ascending Subarray Sum - Detailed Explanation
Problem Statement
Description:
You are given an array of positive integers, nums
. An ascending subarray is defined as a contiguous subarray where each element is strictly greater than the previous one. Your task is to find the maximum sum of any ascending subarray in nums
.
Key Points:
- A subarray is contiguous.
- Ascending means each subsequent element is greater than the previous one.
- If the array contains only one element, that element is the sum.
- If no ascending subarray (other than single elements) exists, the maximum sum is the maximum single element.
Example 1:
Input: nums = [10, 20, 30, 5, 10, 50]
Output: 65
Explanation:
- The subarray
[10, 20, 30]
is ascending, and its sum is 60. - When the sequence breaks at 5 (since 5 < 30), we start a new ascending subarray.
- The subarray
[5, 10, 50]
is ascending, and its sum is 65, which is the maximum.
Example 2:
Input: nums = [10, 20, 30, 40, 50]
Output: 150
Explanation:
- The entire array is ascending. The sum is 10 + 20 + 30 + 40 + 50 = 150.
Example 3:
Input: nums = [12, 17, 15, 13, 10, 11, 12]
Output: 29
Explanation:
- The subarray
[12, 17]
is ascending with sum = 29. - Other subarrays (like
[10, 11, 12]
) have a sum of 33, but note that in this case the correct maximum would be 33 if you compare all ascending segments. - (Double-check your examples; the key idea is to compute the sum for every ascending contiguous subarray and pick the maximum.)
Constraints:
1 <= nums.length <= 100
1 <= nums[i] <= 100
Hints
-
Scan the Array:
Iterate through the array once while maintaining a running sum of the current ascending subarray. -
Reset When Needed:
If the next number is not greater than the current number, the ascending property breaks. Update your maximum sum and reset your current sum to start a new subarray from the current number. -
Update at the End:
Don’t forget to update the maximum sum after the loop in case the best subarray ends at the last element.
Multiple Approaches
Approach 1: Greedy (One-Pass Iteration)
- Initialize:
- Set
current_sum
to the first element. - Set
max_sum
to the first element.
- Set
- Iterate Through the Array:
- For each element starting from index 1:
- If
nums[i]
>nums[i-1]
, addnums[i]
tocurrent_sum
. - Otherwise, update
max_sum
(ifcurrent_sum
is larger) and resetcurrent_sum
tonums[i]
.
- If
- For each element starting from index 1:
- Final Update:
- After the loop, update
max_sum
withcurrent_sum
one last time.
- After the loop, update
Approach 2: Brute Force (Not Recommended)
- Generate All Subarrays:
Check every possible contiguous subarray, verify if it is strictly ascending, and compute its sum. - Drawback:
This approach runs in O(n²) time and is less efficient given that a single pass solution exists.
Step-by-Step Walkthrough
Let's walk through the greedy approach with the example: nums = [10, 20, 30, 5, 10, 50]
.
-
Initialization:
current_sum = 10
max_sum = 10
-
Index 1 (value 20):
- Check: 20 > 10 → Yes
- Update:
current_sum = 10 + 20 = 30
max_sum
becomes max(10, 30) = 30
-
Index 2 (value 30):
- Check: 30 > 20 → Yes
- Update:
current_sum = 30 + 30 = 60
max_sum
becomes max(30, 60) = 60
-
Index 3 (value 5):
- Check: 5 > 30 → No (Ascending property breaks)
- Update:
max_sum
remains max(60, 60) = 60 - Reset
current_sum = 5
-
Index 4 (value 10):
- Check: 10 > 5 → Yes
- Update:
current_sum = 5 + 10 = 15
max_sum
remains max(60, 15) = 60
-
Index 5 (value 50):
- Check: 50 > 10 → Yes
- Update:
current_sum = 15 + 50 = 65
max_sum
becomes max(60, 65) = 65
-
After Loop Completion:
- Final
max_sum
is 65, which is our answer.
- Final
Code Implementations
Python Implementation
Java Implementation
Complexity Analysis
- Time Complexity: O(n) – We traverse the array once.
- Space Complexity: O(1) – We use only a few extra variables.
Common Mistakes
-
Forgetting to Reset the Sum:
When the ascending property breaks, ensure you reset thecurrent_sum
to the current element. -
Not Updating Max Sum After the Loop:
If the best ascending subarray ends at the end of the array, you must updatemax_sum
after the loop. -
Comparing Incorrect Elements:
Be sure to compare the current element with the previous element, not with the first element of the subarray.
Edge Cases
-
Single Element Array:
E.g.,nums = [7]
→ The maximum sum is 7. -
Strictly Ascending Array:
E.g.,nums = [10, 20, 30, 40]
→ The maximum sum is the sum of the entire array. -
Strictly Descending Array:
E.g.,nums = [50, 40, 30, 20]
→ No two consecutive elements form an ascending pair; the maximum sum is the maximum single element, 50. -
Array with All Equal Elements:
Since ascending requires strictly greater values, each element stands alone; the answer is the maximum element.
Alternative Variations & Related Problems
-
Maximum Subarray Sum (Kadane's Algorithm):
Instead of strictly ascending subarrays, this problem finds the contiguous subarray with the maximum sum regardless of order. -
Longest Increasing Subsequence:
A similar problem that finds the longest strictly increasing subsequence (not necessarily contiguous). -
Longest Continuous Increasing Subsequence:
Which finds the length (or sum) of the longest contiguous strictly increasing subarray.
(This is very similar to our problem except here we care about length rather than sum.)
GET YOUR FREE
Coding Questions Catalog
