-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxSubArray.java
More file actions
24 lines (23 loc) · 802 Bytes
/
MaxSubArray.java
File metadata and controls
24 lines (23 loc) · 802 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
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.List;
public class MaxSubArray {
public static void main(String[] args){
int[] nums = {-2,1,-3,4,-1,2,1,-5,4};
int max_sum = Integer.MIN_VALUE;
int j = 0;
int sum = 0;
while(j < nums.length){
// resets sum if its value is less than 0 and the value at index j is greater than sum.
if(sum < 0 && nums[j] >= sum){
sum = 0;
}
// adds values of array to sum, and resets if things go neg
sum += nums[j];
// This value keeps track of our max sum as we traverse through the array.
max_sum = Math.max(sum, max_sum);
j++;
}
System.out.println(max_sum);
}
}