Maximum Ascending Order Subarray Sum
Given an array of positive integers nums
, find the maximum sum of an ascending subarray.
Return the maximum possible sum among all ascending subarrays.
nums = [5, 15, 25, 10, 20, 30]
60
The subarray [10, 20, 30]
has the maximum sum of 50
.
nums = [3, 6, 9, 12]
30
The entire array [3, 6, 9, 12]
is ascending, with a sum of 30
.
nums = [8, 7, 6, 5, 4]
8
Each element is in descending order, so the maximum sum is the largest single element: 8
.
nums = [2, 2, 2, 2]
2
All elements are equal, so no two consecutive elements form an ascending sequence. The maximum sum is any single element: 2
.
1 <= nums.length <= 100
1 <= nums[i] <= 100
https://leetcode.com/problems/maximum-ascending-subarray-sum
Loading component...
Loading component...
Main Function is not defined.
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 Function is not required.