14 lines cpp simple solution


  • 3
    I
    class Solution {
    public:
        int maxSubArray(int A[], int n) {
            int max = A[0], sum = 0;
            for(int i = 0; i < n; i++){
              sum += A[i];
              if(max < sum)
                max = sum;
              if(sum < 0)
                sum = 0;
            }
            return max;
        }
    };

Log in to reply
 

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