Maximum Subarray (Leetcode)

PHOTO EMBED

Mon Oct 04 2021 08:26:09 GMT+0000 (UTC)

Saved by @Sakshamkashyap7 #c++

class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        int n = nums.size(), lMax = 0, gMax = nums[0];
        for(int i = 0; i < n; ++i)    {
            lMax += nums[i];
            gMax = max(gMax, lMax);
            if(lMax<0)  
                lMax = 0;
        }
        return gMax;
    }
};
content_copyCOPY

Kadane's Algorithm- Maximum sum in array

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