public class MaxSubArray {
public int solution(int[] nums) {
int sum = nums[0];
int max = nums[0];
for (int i = 1; i < nums.length; i++) {
sum = Math.max(nums[i], sum + nums[i]);
max = Math.max(max, sum);
}
return max;
}
public static void main(String[] args) {
int[] nums = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
MaxSubArray problem = new MaxSubArray();
System.out.println(problem.solution(nums));
}
}