Q:
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array [−2,1,−3,4,−1,2,1,−5,4],
the contiguous subarray [4,−1,2,1] has the largest sum = 6.
Solution:
public class Solution { public int maxSubArray(int[] A) { // Start typing your Java solution below // DO NOT write main() function int maxSum = Integer.MIN_VALUE; int sum =0; for(int a : A){ sum += a; if(sum >maxSum){ maxSum =sum ; } if(sum<0){ sum =0; } } return maxSum; }}