❔ Maximum Subarray Leetcode Help

C#
 public int MaxSubArray(int[] nums) {
        
        //declarations
        int max_ending_here = nums[0];
        int max_so_far = nums[0];

        //logic
        for(int i = 1; i < nums.Length; i++)
        {
            max_ending_here += nums[i];
            if(max_ending_here > max_so_far)
            {
                max_so_far = max_ending_here;
            }
        }

        return max_so_far;
    }


I'm getting the wrong answer with test case
nums =
[-2,1,-3,4,-1,2,1,-5,4]

Im not sure what I should be doing differently but I think I may be misunderstanding something with Kadane's Algorithm and negative numbers
Was this page helpful?