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.
- Create a suffix array of size N.
- Create two variables x and y to store the sum of the first two subarrays and initialize them as A[N-2] and A[N-1].
- Initialize suff[N-1] as the product of x and y.
- Now expand the subarray with sum x by 1 and keep sliding the subarray with sum y towards the subarray with sum x until suff[i] is less than x*y and update suff[i] as x*y.
- Finally, run a loop to calculate the maximum product between subarray 0 to i and the maximum product of two subarrays from i to N i.e. suff[i].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long long subarrayProduct( int n, int a[])
{
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;
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;
for ( int i = 0; i + 2 < n; i++) {
l += a[i];
ans = max(ans, l * suff[i + 1]);
}
return ans;
}
int main()
{
int A[] = { 1, 2, 2, 3 };
int N = sizeof (A) / sizeof (A[0]);
cout << subarrayProduct(N, A) << endl;
return 0;
}
|
Java
public class GFG
{
static int subarrayProduct( int n, int a[])
{
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 ;
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 ;
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 };
System.out.println(subarrayProduct( 4 , A));
}
}
|
Python3
def subarrayProduct(n, a) :
suff = [ 0 ] * n;
x = a[n - 2 ];
y = a[n - 1 ];
suff[n - 2 ] = x * y;
j = n - 1 ;
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 ;
for i in range (n - 2 ) :
l + = a[i];
ans = max (ans, l * suff[i + 1 ]);
return ans;
if __name__ = = "__main__" :
A = [ 1 , 2 , 2 , 3 ];
N = len (A);
print (subarrayProduct(N, A));
|
C#
using System;
using System.Collections.Generic;
public class GFG {
public static long subarrayProduct( int n, int [] a)
{
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;
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;
for ( int i = 0; i + 2 < n; i++) {
l += a[i];
ans = Math.Max(ans, l * suff[i + 1]);
}
return ans;
}
static public void Main()
{
int [] A = { 1, 2, 2, 3 };
int N = A.Length;
Console.WriteLine(subarrayProduct(N, A));
}
}
|
Javascript
function subarrayProduct(n, a) {
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;
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;
for (let i = 0; i + 2 < n; i++) {
l += a[i];
ans = Math.max(ans, l * suff[i + 1]);
}
return ans;
}
let A = [1, 2, 2, 3];
let N = A.length;
console.log(subarrayProduct(N, A) + "<br>" );
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
14 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...