Q42 Maximum Subarray - LeetCode 53

PHOTO EMBED

Tue Mar 28 2023 12:43:34 GMT+0000 (Coordinated Universal Time)

Saved by @Ayush_dabas07

class Solution {
    public int maxSubArray(int[] nums) {
        //kadane's algorithm will be used to find maximum subarray
        
        //current sum , overall max subarray
        int csum = nums[0] , omax = nums[0] , n = nums.length;

        for(int i =1 ; i < n ; i++){
            
            int value = nums[i];

            //if previous train is +ve jump into it 
            if(csum >= 0)
            csum += value;

            //else start your own
            else
            csum = value;

            //update max
            omax = Math.max(omax , csum);
        }

        return omax;
    }
}
content_copyCOPY

https://leetcode.com/problems/maximum-subarray/