Saturday, December 28, 2024
Google search engine
HomeLanguagesDynamic ProgrammingMaximum subarray sum in O(n) using prefix sum

Maximum subarray sum in O(n) using prefix sum

Given an Array of Positive and Negative Integers, find out the Maximum Subarray Sum in that Array.

Examples: 

Input1 : arr = {-2, -3, 4, -1, -2, 1, 5, -3}
Output1 : 7

Input2 : arr = {4, -8, 9, -4, 1, -8, -1, 6}
Output2 : 9

Kadane’s Algorithm solves this problem using Dynamic Programming approach in linear time. Here is another approach using Dynamic Programming and Prefix Sum to find out maximum subarray sum in Linear time.

Prerequisite: Prefix Sum Array

  1. First calculate the prefix sum (prefix_sum) of the input array. 
  2. Sum of a subarray from index x to y can be presented as, 

        $$\sum\limits_{ele=x}^{y} arr[ele] = prefix\_sum[y] - prefix\_sum[x-1] $$

  3. Now maximum of these subarrays is, 

        $$max(\sum\limits_{ele=x}^{y} arr[ele]) = max(prefix\_sum[y] - prefix\_sum[x-1]) =$$ $$max_{1<=y<=n}(prefix\_sum[y] - min_{x<=y}(prefix\_sum[x-1])) $$

    That is, we keep track of minimum prefix sum for x <= y and maximum subarray sum so far. 

Implementation: 

  1. Calculate the prefix sum of the input array. 
  2. Initialize : min_prefix_sum = 0, res = -infinite 
  3. Maintain a loop for i = 0 to n. (n is the size of the input array). 
    1. cand = prefix_sum[i] – mini
    2. If cand is greater than res (maximum subarray sum so far), then update res by cand.
    3. If prefix_sum[i] is less than min_prefix_sum (minimum prefix sum so far), then update min_prefix_sum by prefix_sum[i].
  4. Return res.

C++




// C++ program to find out maximum subarray
// sum in linear time using prefix sum.
#include <iostream>
#include <limits>
using namespace std;
 
// Function to compute maximum subarray
// sum in linear time.
int maximumSumSubarray(int arr[], int n)
{
    // Initialize minimum prefix sum to 0.
    int min_prefix_sum = 0;
 
    // Initialize maximum subarray sum so
    // far to -infinity.
    int res = numeric_limits<int>::min();
 
    // Initialize and compute the prefix
    // sum array.
    int prefix_sum[n];
    prefix_sum[0] = arr[0];
    for (int i = 1; i < n; i++)
        prefix_sum[i] = prefix_sum[i - 1] + arr[i];       
 
    // loop through the array, keep track
    // of minimum prefix sum so far and
    // maximum subarray sum.
    for (int i = 0; i < n; i++) {
        res = max(res, prefix_sum[i] -
                       min_prefix_sum);
        min_prefix_sum = min(min_prefix_sum,
                             prefix_sum[i]);
    }
     
    return res;
}
 
// Driver Program
int main()
{
    // Test case 1
    int arr1[] = { -2, -3, 4, -1, -2, 1, 5, -3 };
    int n1 = sizeof(arr1) / sizeof(arr1[0]);
    cout << maximumSumSubarray(arr1, n1) << endl;
 
    // Test case 2
    int arr2[] = { 4, -8, 9, -4, 1, -8, -1, 6 };
    int n2 = sizeof(arr2) / sizeof(arr2[0]);
    cout << maximumSumSubarray(arr2, n2);
 
    return 0;
}


Java




// Java program to find
// out maximum subarray
// sum in linear time
// using prefix sum.
 
class GFG
{
    // Function to compute maximum
    // subarray sum in linear time.
    static int maximumSumSubarray(int arr[], int n)
    {
        // Initialize minimum
        // prefix sum to 0.
        int min_prefix_sum = 0;
     
        // Initialize maximum subarray
        // sum so far to -infinity.
        int res = Integer.MIN_VALUE;
     
        // Initialize and compute
        // the prefix sum array.
        int prefix_sum[] = new int[n];
        prefix_sum[0] = arr[0];
        for (int i = 1; i < n; i++)
            prefix_sum[i] = prefix_sum[i - 1]
                            + arr[i];    
     
        // loop through the array, keep
        // track of minimum prefix sum so
        // far and maximum subarray sum.
        for (int i = 0; i < n; i++)
        {
            res = Math.max(res, prefix_sum[i] -
                           min_prefix_sum);
            min_prefix_sum = Math.min(min_prefix_sum,
                                     prefix_sum[i]);
        }
         
        return res;
    }
     
    // Driver Program
    public static void main (String[] args)
    {
        // Test case 1
        int arr1[] = { -2, -3, 4, -1, -2, 1, 5, -3 };
        int n1 = arr1.length;
        System.out.println(maximumSumSubarray(arr1, n1));
     
        // Test case 2
        int arr2[] = { 4, -8, 9, -4, 1, -8, -1, 6 };
        int n2 = arr2.length;
        System.out.println(maximumSumSubarray(arr2, n2));
    }
}
 
// This code is contributed by Ansu Kumari.


Python3




# Python3 program to find out
# maximum subarray sum in
# linear time using prefix
# sum.
import math
 
# Function to compute maximum
# subarray sum in linear time.
def maximumSumSubarray(arr, n):
     
    # Initialize minimum prefix
    # sum to 0.
    min_prefix_sum = 0
 
    # Initialize maximum subarray
    # sum so far to -infinity.
    res = -math.inf
 
    # Initialize and compute the
    # prefix sum array.
    prefix_sum = []
    prefix_sum.append(arr[0])
     
    for i in range(1, n):
        prefix_sum.append(prefix_sum[i - 1] + arr[i])    
 
    # loop through the array keep
    # track of minimum prefix sum
    # so far and maximum subarray
    # sum.
    for i in range(n):
         
        res = max(res, prefix_sum[i] - min_prefix_sum)
        min_prefix_sum = min(min_prefix_sum, prefix_sum[i])
     
    return res
 
# Driver Program
 
# Test case 1
arr1 = [ -2, -3, 4, -1, -2, 1, 5, -3 ]
n1 = len(arr1)
print(maximumSumSubarray(arr1, n1))
 
# Test case 2
arr2 = [ 4, -8, 9, -4, 1, -8, -1, 6 ]
n2 = len(arr2)
print(maximumSumSubarray(arr2, n2))
 
# This code is contributed by Ansu Kuamri.


C#




// C# program to find
// out maximum subarray
// sum in linear time
// using prefix sum.
using System;
 
class GFG
{
    // Function to compute maximum
    // subarray sum in linear time.
    static int maximumSumSubarray(int []arr, int n)
    {
        // Initialize minimum
        // prefix sum to 0.
        int min_prefix_sum = 0;
     
        // Initialize maximum subarray
        // sum so far to -infinity.
        int res = int.MinValue;
     
        // Initialize and compute
        // the prefix sum array.
        int []prefix_sum = new int[n];
        prefix_sum[0] = arr[0];
        for (int i = 1; i < n; i++)
            prefix_sum[i] = prefix_sum[i - 1]
                            + arr[i];
     
        // loop through the array, keep
        // track of minimum prefix sum so
        // far and maximum subarray sum.
        for (int i = 0; i < n; i++)
        {
            res = Math.Max(res, prefix_sum[i] -
                        min_prefix_sum);
            min_prefix_sum = Math.Min(min_prefix_sum,
                                    prefix_sum[i]);
        }
         
        return res;
    }
     
    // Driver Program
    public static void Main ()
    {
        // Test case 1
        int []arr1 = { -2, -3, 4, -1, -2, 1, 5, -3 };
        int n1 = arr1.Length;
        Console.WriteLine(maximumSumSubarray(arr1, n1));
     
        // Test case 2
        int []arr2 = { 4, -8, 9, -4, 1, -8, -1, 6 };
        int n2 = arr2.Length;
        Console.WriteLine(maximumSumSubarray(arr2, n2));
    }
}
 
// This code is contributed by vt_m.


PHP




<?php
// PHP program to find out
// maximum subarray sum in
// linear time using prefix sum.
 
// Function to compute maximum
// subarray sum in linear time.
function maximumSumSubarray($arr, $n)
{
    // Initialize minimum
    // prefix sum to 0.
    $min_prefix_sum = 0;
 
    // Initialize maximum subarray
    // sum so far to -infinity.
    $res = PHP_INT_MIN;
 
    // Initialize and compute
    // the prefix sum array.
    $prefix_sum = array();
    $prefix_sum[0] = $arr[0];
    for ($i = 1; $i < $n; $i++)
        $prefix_sum[$i] = $prefix_sum[$i - 1] +
                                      $arr[$i];
 
    // loop through the array,
    // keep track of minimum
    // prefix sum so far and
    // maximum subarray sum.
    for ($i = 0; $i < $n; $i++)
    {
        $res = max($res, $prefix_sum[$i] -
                         $min_prefix_sum);
        $min_prefix_sum = min($min_prefix_sum,
                              $prefix_sum[$i]);
    }
     
    return $res;
}
 
// Driver Code
 
// Test case 1
$arr1 = array(-2, -3, 4, -1,
              -2, 1, 5, -3);
$n1 = count($arr1);
echo maximumSumSubarray($arr1, $n1), " \n" ;
 
// Test case 2
$arr2 = array(4, -8, 9, -4,
              1, -8, -1, 6);
               
$n2 = count($arr2);
echo maximumSumSubarray($arr2, $n2);
 
// This code is contributed by anuj_67.
?>


Javascript




<script>
 
// JavaScript program to find
// out maximum subarray
// sum in linear time
// using prefix sum.
 
// Function to compute maximum
// subarray sum in linear time.
    function maximumSumSubarray(arr, n)
    {
        // Initialize minimum
        // prefix sum to 0.
        let min_prefix_sum = 0;
       
        // Initialize maximum subarray
        // sum so far to -infinity.
        let res = Number.MIN_VALUE;
       
        // Initialize and compute
        // the prefix sum array.
        let prefix_sum = [];
        prefix_sum[0] = arr[0];
        for (let i = 1; i < n; i++)
            prefix_sum[i] = prefix_sum[i - 1]
                            + arr[i];    
       
        // loop through the array, keep
        // track of minimum prefix sum so
        // far and maximum subarray sum.
        for (let i = 0; i < n; i++)
        {
            res = Math.max(res, prefix_sum[i] -
                           min_prefix_sum);
            min_prefix_sum = Math.min(min_prefix_sum,
                                     prefix_sum[i]);
        }
           
        return res;
    }
 
// Driver code
 
        // Test case 1
        let arr1 = [ -2, -3, 4, -1, -2, 1, 5, -3 ];
        let n1 = arr1.length;
        document.write(maximumSumSubarray(arr1, n1) + "<br/>");
       
        // Test case 2
        let arr2 = [ 4, -8, 9, -4, 1, -8, -1, 6 ];
        let n2 = arr2.length;
        document.write(maximumSumSubarray(arr2, n2));
            
</script>


Output

7
9

Simpler and Efficient Solution 

Time Complexity: O(n). It takes linear time to compute the prefix sum and takes constant time in each iteration of the for loop.
Auxiliary Space: O(n)

Please note that the above problem can be solved in O(n) time and O(1) extra space using Kadane’s algorithm

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments