Longest Sum Contiguous Subarray(Kadane’s Algorithm)

Given an integer array, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

Explanation with Code
int maxSubArray(vector<int>& nums) {
        //meh is maximum ending here
        //msf is maximum so far
        int meh = 0, msf = INT_MIN;
        
        for(int i=0; i<nums.size(); i++){
            meh += nums[i];
            
            if(meh < nums[i])
                meh = nums[i];
            if(msf < meh)
                msf = meh;
        }
        return msf;
    }

Write a comment