Open In App

Partition Array into 3 Subarrays to maximize the product of sums

You are given an array A[] of size N, the task is to divide the array into exactly three subarrays such that every element belongs to exactly one subarray such that the product of the sum of the subarrays is the maximum.

Examples:



Input: N = 4, A[] = { 1, 2, 2, 3}
Output: 18
Explanation: The optimal partitions are {1, 2}, {2}, {3}

Input: N = 3, A[] = { 3, 5, 7}
Output: 105
Explanation: There is only one possible partition {3}, {5}, {7}.



Approach: This problem can be solved using the concept of sliding window and prefix-suffix array.

First, calculate the maximum product of 2 subarrays considering the size of the array from 0 to N starting from right. Now once we have the maximum product of 2 subarrays then the 3rd subarray will be on the left side.

For example, if we have calculated the maximum product of two subarrays for all the arrays starting from i to N where 0 < i < N – 1 then the third subarray will be subarray from 0 to i. And now we can calculate the maximum product of these two subarrays to get maximum product of 3 subarrays.

Follow the steps mentioned below to implement the idea.

Below is the implementation of the above approach:




#include <bits/stdc++.h>
using namespace std;
 
// Function to find the maximum product
long long subarrayProduct(int n, int a[])
{
    // Array to store the maximum product of
    // sum of two subarrays from i to N-1
    vector<long long> suff(n, 0);
 
    long long x = a[n - 2], y = a[n - 1];
    suff[n - 2] = x * y;
    int j = n - 1;
 
    // Loop to calculate the value of suff array
    for (int i = n - 3; i >= 0; i--) {
        x += a[i];
        suff[i] = x * y;
        while (suff[i] < (x - a[j - 1]) * (y + a[j - 1])) {
            j--;
            x -= a[j];
            y += a[j];
            suff[i] = x * y;
        }
    }
    long long l = 0, ans = 0;
 
    // Loop to calculate the maximum product of sums
    for (int i = 0; i + 2 < n; i++) {
        l += a[i];
        ans = max(ans, l * suff[i + 1]);
    }
    return ans;
}
 
// Driver code
int main()
{
    int A[] = { 1, 2, 2, 3 };
    int N = sizeof(A) / sizeof(A[0]);
 
    // Function call
    cout << subarrayProduct(N, A) << endl;
    return 0;
}




public class GFG
{
 
  // Function to find the maximum product
  static int subarrayProduct(int n, int a[])
  {
 
    // Array to store the maximum product of
    // sum of two subarrays from i to N-1
    int suff[] = new int[n];
    for (int i = 0; i < n; i++)
      suff[i] = 0;
 
    int x = a[n - 2], y = a[n - 1];
    suff[n - 2] = x * y;
    int j = n - 1;
 
    // Loop to calculate the value of suff array
    for (int i = n - 3; i >= 0; i--) {
      x += a[i];
      suff[i] = x * y;
      while (suff[i]
             < (x - a[j - 1]) * (y + a[j - 1])) {
        j--;
        x -= a[j];
        y += a[j];
        suff[i] = x * y;
      }
    }
    int l = 0, ans = 0;
 
    // Loop to calculate the maximum product of sums
    for (int i = 0; i + 2 < n; i++) {
      l += a[i];
      ans = Math.max(ans, l * suff[i + 1]);
    }
    return ans;
  }
 
  public static void main(String[] args)
  {
    int A[] = { 1, 2, 2, 3 };
 
    // Function call
    System.out.println(subarrayProduct(4, A));
  }
}
 
// This code is contributed by garg28harsh.




# Python3 code for the above approach
 
# Function to find the maximum product
def subarrayProduct(n, a) :
     
    # Array to store the maximum product of
    # sum of two subarrays from i to N-1
    suff = [0] * n;
 
    x = a[n - 2];
    y = a[n - 1];
    suff[n - 2] = x * y;
    j = n - 1;
 
    # Loop to calculate the value of suff array
    for i in range(n - 3, -1, -1) :
        x += a[i];
        suff[i] = x * y;
        while (suff[i] < (x - a[j - 1]) * (y + a[j - 1])) :
            j -= 1;
            x -= a[j];
            y += a[j];
            suff[i] = x * y;
     
    l = 0;
    ans = 0;
 
    # Loop to calculate the maximum product of sums
    for i in range(n-2) :
        l += a[i];
        ans = max(ans, l * suff[i + 1]);
         
    return ans;
 
# Driver code
if __name__ == "__main__" :
 
    A = [ 1, 2, 2, 3 ];
    N = len(A);
 
    # Function call
    print(subarrayProduct(N, A));
   
    # This code is contributed by AnkThon




using System;
using System.Collections.Generic;
 
public class GFG {
 
  // Function to find the maximum product
  public static long subarrayProduct(int n, int[] a)
  {
    // Array to store the maximum product of
    // sum of two subarrays from i to N-1
    List<long> suff = new List<long>();
    for (int i = 0; i < n; i++) {
      suff.Add(0);
    }
    long x = a[n - 2];
    long y = a[n - 1];
    suff[n - 2] = x * y;
    int j = n - 1;
 
    // Loop to calculate the value of suff array
    for (int i = n - 3; i >= 0; i--) {
      x += a[i];
      suff[i] = x * y;
      while (suff[i]
             < (x - a[j - 1]) * (y + a[j - 1])) {
        j--;
        x -= a[j];
        y += a[j];
        suff[i] = x * y;
      }
    }
    long l = 0;
    long ans = 0;
 
    // Loop to calculate the maximum product of sums
    for (int i = 0; i + 2 < n; i++) {
      l += a[i];
      ans = Math.Max(ans, l * suff[i + 1]);
    }
    return ans;
  }
 
  // Driver code
  static public void Main()
  {
 
    int[] A = { 1, 2, 2, 3 };
    int N = A.Length;
 
    // Function call
    Console.WriteLine(subarrayProduct(N, A));
  }
}
 
// This code is contributed by akashish__




       // JavaScript code for the above approach
 
       // Function to find the maximum product
       function subarrayProduct(n, a) {
           // Array to store the maximum product of
           // sum of two subarrays from i to N-1
           let suff = new Array(n).fill(0);
 
           let x = a[n - 2], y = a[n - 1];
           suff[n - 2] = x * y;
           let j = n - 1;
 
           // Loop to calculate the value of suff array
           for (let i = n - 3; i >= 0; i--) {
               x += a[i];
               suff[i] = x * y;
               while (suff[i] < (x - a[j - 1]) * (y + a[j - 1])) {
                   j--;
                   x -= a[j];
                   y += a[j];
                   suff[i] = x * y;
               }
           }
           let l = 0, ans = 0;
 
           // Loop to calculate the maximum product of sums
           for (let i = 0; i + 2 < n; i++) {
               l += a[i];
               ans = Math.max(ans, l * suff[i + 1]);
           }
           return ans;
       }
 
       // Driver code
 
       let A = [1, 2, 2, 3];
       let N = A.length;
 
       // Function call
       console.log(subarrayProduct(N, A) + "<br>");
 
// This code is contributed by Potta Lokesh

Output
18

Time Complexity: O(N)
Auxiliary Space: O(N)


Article Tags :