Maximum product of sum of two contiguous subarrays of an array

Given an array arr[] of N positive integers, the task is to split the array into two contiguous subarrays such that the product of the sum of two contiguous subarrays is maximum.

Examples:

Input: arr[] = {4, 10, 1, 7, 2, 9}
Output: 270
All possible partitions and their product of sum are:
{4} and {10, 1, 7, 2, 9} -> product of sum = 116
{4, 10} and {1, 7, 2, 9} -> product of sum = 266
{4, 10, 1} and {7, 2, 9} -> product of sum = 270
{4, 10, 1, 7} and {2, 9} -> product of sum = 242
{4, 10, 1, 7, 2} and {9} -> product of sum = 216

Input: arr[] = {4, 10, 11, 10, 4}
Output: 350

Naive approach: A simple approach is to consider all the possible partitions for the subarrays one by one and calculate the maximum product of sum for subarrays.



Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the maximum
// product of sum for any partition
int maxProdSum(int arr[], int n)
{
    int leftArraySum = 0, maxProduct = 0;
  
    // Traversing the array
    for (int i = 0; i < n; i++) {
  
        // Compute left array sum
        leftArraySum += arr[i];
  
        // Compute right array sum
        int rightArraySum = 0;
        for (int j = i + 1; j < n; j++) {
            rightArraySum += arr[j];
        }
  
        // Multiplying left and right subarray sum
        int k = leftArraySum * rightArraySum;
  
        // Checking for the maximum product
        // of sum of left and right subarray
        if (k > maxProduct) {
            maxProduct = k;
        }
    }
  
    // Printing the maximum product
    return maxProduct;
}
  
// Driver code
int main()
{
    int arr[] = { 4, 10, 1, 7, 2, 9 };
    int n = sizeof(arr) / sizeof(int);
  
    cout << maxProdSum(arr, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG
{
  
// Function to return the maximum
// product of sum for any partition
static int maxProdSum(int arr[], int n)
{
    int leftArraySum = 0, maxProduct = 0;
  
    // Traversing the array
    for (int i = 0; i < n; i++)
    {
  
        // Compute left array sum
        leftArraySum += arr[i];
  
        // Compute right array sum
        int rightArraySum = 0;
        for (int j = i + 1; j < n; j++)
        {
            rightArraySum += arr[j];
        }
  
        // Multiplying left and right subarray sum
        int k = leftArraySum * rightArraySum;
  
        // Checking for the maximum product
        // of sum of left and right subarray
        if (k > maxProduct)
        {
            maxProduct = k;
        }
    }
  
    // Printing the maximum product
    return maxProduct;
}
  
// Driver code
public static void main(String[] args)
{
    int arr[] = { 4, 10, 1, 7, 2, 9 };
    int n = arr.length;
  
    System.out.print(maxProdSum(arr, n));
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to return the maximum
# product of sum for any partition
def maxProdSum(arr, n):
    leftArraySum = 0;
    maxProduct = 0;
  
    # Traversing the array
    for i in range(n):
  
        # Compute left array sum
        leftArraySum += arr[i];
  
        # Compute right array sum
        rightArraySum = 0;
        for j in range(i + 1, n):
            rightArraySum += arr[j];
          
        # Multiplying left and right subarray sum
        k = leftArraySum * rightArraySum;
  
        # Checking for the maximum product
        # of sum of left and right subarray
        if (k > maxProduct):
            maxProduct = k;
          
    # Printing the maximum product
    return maxProduct;
  
# Driver code
if __name__ == '__main__':
    arr = [ 4, 10, 1, 7, 2, 9 ];
    n = len(arr);
  
    print(maxProdSum(arr, n));
  
# This code is contributed by Rajput-Ji

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG
{
  
// Function to return the maximum
// product of sum for any partition
static int maxProdSum(int []arr, int n)
{
    int leftArraySum = 0, maxProduct = 0;
  
    // Traversing the array
    for (int i = 0; i < n; i++)
    {
  
        // Compute left array sum
        leftArraySum += arr[i];
  
        // Compute right array sum
        int rightArraySum = 0;
        for (int j = i + 1; j < n; j++)
        {
            rightArraySum += arr[j];
        }
  
        // Multiplying left and right subarray sum
        int k = leftArraySum * rightArraySum;
  
        // Checking for the maximum product
        // of sum of left and right subarray
        if (k > maxProduct)
        {
            maxProduct = k;
        }
    }
  
    // Printing the maximum product
    return maxProduct;
}
  
// Driver code
public static void Main(String[] args)
{
    int []arr = { 4, 10, 1, 7, 2, 9 };
    int n = arr.Length;
  
    Console.Write(maxProdSum(arr, n));
}
}
  
// This code is contributed by PrinciRaj1992

chevron_right


Output:

270

Time Complexity: O(N2)

Efficient approach: A better approach is to use the concept of prefix array sum which helps in calculating the sum of both the contiguous subarrays.

  • Prefix sum of the elements can be calculated.
  • The left array sum is the value at ith position.
  • The right array sum is the value at the last position – ith position.
  • Now calculate k, the product of sum of these left and right subarrays.
  • Find and print the maximum of the product of these arrays.

Below is the implementation of the above approach:

CPP

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the maximum
// product of sum for any partition
int maxProdSum(int arr[], int n)
{
    int prefixArraySum[n], maxProduct = 0;
  
    // Initialise prefixArraySum[0]
    // with arr[0] element
    prefixArraySum[0] = arr[0];
  
    // Traverse array elements
    // to compute prefix array sum
    for (int i = 1; i < n; i++) {
        prefixArraySum[i] = prefixArraySum[i - 1]
                            + arr[i];
    }
  
    for (int i = 0; i < n - 1; i++) {
        // Compute left and right array sum
        int leftArraySum = prefixArraySum[i];
        int rightArraySum = prefixArraySum[n - 1]
                            - prefixArraySum[i];
  
        // Multiplying left and right subarray sum
        int k = leftArraySum * rightArraySum;
  
        // Checking for maximum product of
        // the sum of left and right subarray
        if (k > maxProduct) {
            maxProduct = k;
        }
    }
  
    // Printing the maximum value
    return maxProduct;
}
  
// Driver code
int main()
{
    int arr[] = { 4, 10, 1, 7, 2, 9 };
    int n = sizeof(arr) / sizeof(int);
  
    cout << maxProdSum(arr, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG
{
  
// Function to return the maximum
// product of sum for any partition
static int maxProdSum(int arr[], int n)
{
    int []prefixArraySum = new int[n];
    int maxProduct = 0;
  
    // Initialise prefixArraySum[0]
    // with arr[0] element
    prefixArraySum[0] = arr[0];
  
    // Traverse array elements
    // to compute prefix array sum
    for (int i = 1; i < n; i++)
    {
        prefixArraySum[i] = prefixArraySum[i - 1]
                            + arr[i];
    }
  
    for (int i = 0; i < n - 1; i++)
    {
        // Compute left and right array sum
        int leftArraySum = prefixArraySum[i];
        int rightArraySum = prefixArraySum[n - 1]
                            - prefixArraySum[i];
  
        // Multiplying left and right subarray sum
        int k = leftArraySum * rightArraySum;
  
        // Checking for maximum product of
        // the sum of left and right subarray
        if (k > maxProduct) 
        {
            maxProduct = k;
        }
    }
  
    // Printing the maximum value
    return maxProduct;
}
  
// Driver code
public static void main(String[] args)
{
    int arr[] = { 4, 10, 1, 7, 2, 9 };
    int n = arr.length;
  
    System.out.print(maxProdSum(arr, n));
}
}
  
// This code is contributed by PrinciRaj1992

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python implementation of the approach
  
# Function to return the maximum
# product of sum for any partition
def maxProdSum(arr, n):
      
    prefixArraySum = [0] * n
    maxProduct = 0
      
    # Initialise prefixArraySum[0]
    # with arr[0] element
    prefixArraySum[0] = arr[0]
      
    # Traverse array elements
    # to compute prefix array sum
    for i in range(1, n):
        prefixArraySum[i] = prefixArraySum[i - 1] + arr[i]
      
    for i in range(n - 1):
          
        # Compute left and right array sum
        leftArraySum = prefixArraySum[i]
        rightArraySum = prefixArraySum[n - 1] - \
                        prefixArraySum[i]
          
        # Multiplying left and right subarray sum
        k = leftArraySum * rightArraySum
          
        # Checking for maximum product of
        # the sum of left and right subarray
        if (k > maxProduct):
            maxProduct = k
      
    # Printing the maximum value
    return maxProduct
  
# Driver code
arr = [4, 10, 1, 7, 2, 9]
n = len(arr)
print(maxProdSum(arr, n))
  
# This code is contributed by SHUBHAMSINGH10

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG
{
  
// Function to return the maximum
// product of sum for any partition
static int maxProdSum(int []arr, int n)
{
    int []prefixArraySum = new int[n];
    int maxProduct = 0;
  
    // Initialise prefixArraySum[0]
    // with arr[0] element
    prefixArraySum[0] = arr[0];
  
    // Traverse array elements
    // to compute prefix array sum
    for (int i = 1; i < n; i++)
    {
        prefixArraySum[i] = prefixArraySum[i - 1]
                            + arr[i];
    }
  
    for (int i = 0; i < n - 1; i++)
    {
        // Compute left and right array sum
        int leftArraySum = prefixArraySum[i];
        int rightArraySum = prefixArraySum[n - 1]
                            - prefixArraySum[i];
  
        // Multiplying left and right subarray sum
        int k = leftArraySum * rightArraySum;
  
        // Checking for maximum product of
        // the sum of left and right subarray
        if (k > maxProduct) 
        {
            maxProduct = k;
        }
    }
  
    // Printing the maximum value
    return maxProduct;
}
  
// Driver code
public static void Main(String[] args)
{
    int []arr = { 4, 10, 1, 7, 2, 9 };
    int n = arr.Length;
  
    Console.Write(maxProdSum(arr, n));
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


Output:

270

Time Complexity: O(n)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.