Intuition
Try every contiguous subarray and track the maximum sum.
Explanation
Approach
For each start i, accumulate sum as we extend the end j.
Implementation
class Solution {
public int maxSubArray(int[] nums) {
int n = nums.length;
int best = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
int s = 0;
for (int j = i; j < n; j++) {
s += nums[j];
best = Math.max(best, s);
}
}
return best;
}
}