-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximumAscendingSubarraySum.java
More file actions
31 lines (26 loc) · 1007 Bytes
/
MaximumAscendingSubarraySum.java
File metadata and controls
31 lines (26 loc) · 1007 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class MaximumAscendingSubarraySum {
public int maxAscendingSum(int[] nums) {
int max = nums[0];
int curr_max = nums[0];
for(int i=1; i<nums.length; i++){
if(nums[i-1] < nums[i]){
curr_max =curr_max + nums[i];
}
else{
max = Math.max(max, curr_max);
curr_max = nums[i];
}
}
max = Math.max(max, curr_max);
return max;
}
public static void main(String[] args) {
MaximumAscendingSubarraySum solution = new MaximumAscendingSubarraySum();
int[] nums1 = {10, 20, 30, 5, 10, 50};
System.out.println(solution.maxAscendingSum(nums1)); // Output: 65
int[] nums2 = {10, 20, 30, 40, 50};
System.out.println(solution.maxAscendingSum(nums2)); // Output: 150
int[] nums3 = {12, 17, 15, 13, 10, 11, 12};
System.out.println(solution.maxAscendingSum(nums3)); // Output: 33
}
}