Maximum Ascending Subarray Sum - Problem

Given an array of positive integers nums, return the maximum possible sum of a strictly ascending subarray in nums.

A subarray is defined as a contiguous sequence of numbers in an array.

An ascending subarray is one where each element is strictly greater than the previous element: nums[i] < nums[i+1] < nums[i+2] ...

Input & Output

Example 1 — Basic Ascending Sequence
$ Input: nums = [10,20,30,5,10,50]
Output: 65
💡 Note: Two ascending subarrays: [10,20,30] with sum 60 and [5,10,50] with sum 65. Maximum is 65.
Example 2 — Single Elements
$ Input: nums = [10,20,30,40]
Output: 100
💡 Note: Entire array is ascending: 10 < 20 < 30 < 40, sum = 10+20+30+40 = 100.
Example 3 — Decreasing Array
$ Input: nums = [5,4,3,2,1]
Output: 5
💡 Note: No ascending subarray exists except single elements. Maximum single element is 5.

Constraints

  • 1 ≤ nums.length ≤ 100
  • 1 ≤ nums[i] ≤ 100

Visualization

Tap to expand
Maximum Ascending Subarray Sum INPUT nums = [10,20,30,5,10,50] 10 i=0 20 i=1 30 i=2 5 i=3 10 i=4 50 i=5 Ascending Subarrays: [10, 20, 30] = 60 [5, 10, 50] = 65 Find max sum among all ascending subarrays ALGORITHM STEPS 1 Initialize maxSum = nums[0] = 10 currSum = nums[0] = 10 2 Iterate Array For each element from i=1 to end of array 3 Check Ascending If nums[i] > nums[i-1]: currSum += nums[i] Else: currSum = nums[i] 4 Update Max maxSum = max(maxSum, currSum) Trace: 10-->30-->60-->5-->15-->65 maxSum updates: 10,30,60,60,60,65 FINAL RESULT Best Ascending Subarray: 5 10 50 5 + 10 + 50 = 65 Output: 65 OK - Maximum Sum Found! [5,10,50] has greater sum than [10,20,30] (60) Single pass: O(n) time O(1) space complexity Key Insight: Track current ascending sum and reset when sequence breaks. When nums[i] <= nums[i-1], start a new subarray from nums[i]. Update maximum after each step. This greedy approach works because we need contiguous elements and ascending property must be strictly maintained. TutorialsPoint - Maximum Ascending Subarray Sum | Single Pass Approach
Asked in
Amazon 15 Google 8
24.1K Views
Medium Frequency
~8 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen