Understand Maximum Ascending Order Subarray Sum Problem

Problem Name: Maximum Ascending Order Subarray Sum
Problem Description:

Problem : Maximum Ascending Order Subarray Sum

Given an array of positive integers nums, find the maximum sum of an ascending subarray.

  • A subarray is a contiguous sequence of numbers in the array.
  • A subarray is ascending if each number is strictly greater than the previous one.
  • A single-element subarray is also considered ascending.

Return the maximum possible sum among all ascending subarrays.


Example 1

Input:

nums = [5, 15, 25, 10, 20, 30]

Output:

60

Explanation:

The subarray [10, 20, 30] has the maximum sum of 50.


Example 2

Input:

nums = [3, 6, 9, 12]

Output:

30

Explanation:

The entire array [3, 6, 9, 12] is ascending, with a sum of 30.


Example 3

Input:

nums = [8, 7, 6, 5, 4]

Output:

8

Explanation:

Each element is in descending order, so the maximum sum is the largest single element: 8.


Example 4

Input:

nums = [2, 2, 2, 2]

Output:

2

Explanation:

All elements are equal, so no two consecutive elements form an ascending sequence. The maximum sum is any single element: 2.


Constraints

  • 1 <= nums.length <= 100
  • 1 <= nums[i] <= 100
Category:
  • Leetcode Problem of the Day
  • Arrays
Programming Language:
  • Java
Reference Link:

https://leetcode.com/problems/maximum-ascending-subarray-sum

Online IDE

Scroll down for output
Java
Output:

Loading component...

Loading component...

Tracking code (View only. In case you want to track the code, click this button):
Main Function:

Main Function is not defined.

Helper Function:

INPUT: nums = [5, 15, 25, 10, 20, 30]

OUTPUT: 60

public static int maxAscendingSum(int[] nums) {

int res = nums[0];

int temp = nums[0];

for(int i = 1;i<nums.length;i++){

if(nums[i] > nums[i-1])

temp+=nums[i];

}//If End

else{

temp = nums[i];

}//Else End

res = Math.max(res,temp);

}//Loop End

return res;

}//function end

Utility Functions and Global variables:

Utility Function is not required.