Maximum subarray problem using Kadane's Algorithm

  • 0
    Here's the link, very simple but intuitive.

    public class Solution {
    public int maxSubArray(int[] nums) {
    int maxEndhere = nums[0], maxSum = nums[0];//Should initialise to first element, not 0
    for (int i=1; i<nums.length; i++){ //Should start on the second element, not first
    maxEndhere = Math.max(nums[i],maxEndhere+nums[i]); //Whether sum or this element
    maxSum = Math.max(maxEndhere, maxSum); //Store the large sum
    return maxSum;


Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.