Kadane's Algorithm

Tags : array, algorithm, geeksforgeeks, cpp, medium

Given an array Arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the maximum sum and return its sum.

Your Task: You don’t need to read input or print anything. The task is to complete the function maxSubarraySum() which takes Arr[] and N as input parameters and returns the sum of subarray with maximum sum.

Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).

Examples #

Example 1:

Input:
N = 5
Arr[] = {1,2,3,-2,5}
Output:
9
Explanation:
Max subarray sum is 9
of elements (1, 2, 3, -2, 5) which 
is a contiguous subarray.

Example 2:

Input:
N = 4
Arr[] = {-1,-2,-3,-4}
Output:
-1
Explanation:
Max subarray sum is -1 
of element (-1)

Constraints #

Solutions #

class Solution
{
    public:
    // arr: input array
    // n: size of array
    //Function to find the sum of contiguous subarray with maximum sum.
    long long maxSubarraySum(int arr[], int n){
        
        // Your code here
        int curSum=arr[0], maxSum=arr[0];
        for(int i=1; i<n; i++){
            if(arr[i] > (curSum+arr[i]))
                curSum = arr[i];
            else
                curSum += arr[i];
            maxSum = max(maxSum, curSum);
        }
        
        return maxSum;
        
    }
};