DEV Community

Prashant Mishra
Prashant Mishra

Posted on

Maximum sub array sum

Maximum sub array sum

tc : O(N), sc: O(1)

class Solution {
    public int maxSubArray(int[] nums) {
        //kadane approach
        int currentSum=0;
        int maxSum = Integer.MIN_VALUE;
        for(int i =0;i< nums.length;i++){
            currentSum+=nums[i];
            if(currentSum>maxSum){
                maxSum = currentSum;
            }
            if(currentSum<0) currentSum =0;
        }
        return maxSum;
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

👋 Kindness is contagious

Dive into this thoughtful article, cherished within the supportive DEV Community. Coders of every background are encouraged to share and grow our collective expertise.

A genuine "thank you" can brighten someone’s day—drop your appreciation in the comments below!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found value here? A quick thank you to the author makes a big difference.

Okay