Maximize product of subarray sum with its maximum element
Last Updated :
07 Jul, 2021
Given an array arr[] consisting of N positive integers, the task is to find the maximum product of the subarray sum with the maximum element of that subarray.
Examples:
Input: arr[] = {2, -3, 8, -2, 5}
Output: 88
Explanation:
The required maximum product can be obtained using subarray {8, -2, 5}
Therefore, maximum product = (8 + (-2) + 5) * (8) = 88.
Input: arr[] = {-4, 1, -5, 3, 5}
Output: 40
Naive Approach: The simplest approach to solve the problem is to generate all subarrays of the given array and for each subarray, calculate the sum of the subarray, and multiply it with the maximum element in the subarray. Update the maximum product by comparing it with the product calculated. After checking for all the subarrays, print the maximum product obtained after processing all the subarray.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized by modifying Kadane’s Algorithm to get the resultant maximum value. Follow the steps below to solve the problem:
- Perform Kadane’s Algorithm according to the following steps:
- Initialize three variables say, largestSum, currSum, currMax as 0.
- Traverse the given array arr[] and perform the following steps;
- Update currSum as currSum + arr[i] and the currMax as max(currMax, arr[i]).
- Update the value of largestSum as max(largestSum, currMax * currSum).
- If the value of currSum is less than 0, then update the value of currMax and currSum as 0.
- After completing the above steps, return the value of largestSum as the resultant maximum product of the sum of the subarray with its maximum element.
- Initialize a variable, say maximumSum as 0 that stores the maximum product of the sum of the subarray with its maximum element.
- Perform the updated Kadane’s Algorithm with the given array and store the returned value in maximumSum.
- Now, update each array element by multiplying each element by (-1) and again perform the updated Kadane’s Algorithm with the updated array so that if the maximum element of any subarray is negative then that combination must be taken into account.
- Update the value of maximumSum to the maximum of maximumSum and the value returned by the above step.
- After completing the above steps, print the value of maximumSum as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int Kadane( int arr[], int n)
{
int largestSum = 0, currMax = 0;
int currSum = 0;
for ( int i = 0; i < n; i++) {
currSum += arr[i];
currMax = max(currMax, arr[i]);
largestSum = max(largestSum,
currMax * currSum);
if (currSum < 0) {
currMax = 0;
currSum = 0;
}
}
return largestSum;
}
int maximumWeight( int arr[], int n)
{
int largestSum = Kadane(arr, n);
for ( int i = 0; i < n; i++) {
arr[i] = -arr[i];
}
largestSum = max(largestSum,
Kadane(arr, n));
return largestSum;
}
int main()
{
int arr[] = { 2, -3, 8, -2, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << maximumWeight(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int Kadane( int arr[], int n)
{
int largestSum = 0 , currMax = 0 ;
int currSum = 0 ;
for ( int i = 0 ; i < n; i++) {
currSum += arr[i];
currMax = Math.max(currMax, arr[i]);
largestSum
= Math.max(largestSum, currMax * currSum);
if (currSum < 0 ) {
currMax = 0 ;
currSum = 0 ;
}
}
return largestSum;
}
static int maximumWeight( int arr[], int n)
{
int largestSum = Kadane(arr, n);
for ( int i = 0 ; i < n; i++) {
arr[i] = -arr[i];
}
largestSum = Math.max(largestSum, Kadane(arr, n));
return largestSum;
}
public static void main(String[] args)
{
int arr[] = { 2 , - 3 , 8 , - 2 , 5 };
int N = arr.length;
System.out.println(maximumWeight(arr, N));
}
}
|
Python3
def Kadane(arr, n):
largestSum = 0
currMax = 0
currSum = 0
for i in range (n):
currSum + = arr[i]
currMax = max (currMax, arr[i])
largestSum = max (largestSum,
currMax * currSum)
if (currSum < 0 ):
currMax = 0
currSum = 0
return largestSum
def maximumWeight(arr, n):
largestSum = Kadane(arr, n)
for i in range (n):
arr[i] = - arr[i]
largestSum = max (largestSum,
Kadane(arr, n))
return largestSum
if __name__ = = '__main__' :
arr = [ 2 , - 3 , 8 , - 2 , 5 ]
N = len (arr)
print (maximumWeight(arr, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int Kadane( int []arr, int n)
{
int largestSum = 0, currMax = 0;
int currSum = 0;
for ( int i = 0; i < n; i++)
{
currSum += arr[i];
currMax = Math.Max(currMax, arr[i]);
largestSum = Math.Max(largestSum,
currMax * currSum);
if (currSum < 0)
{
currMax = 0;
currSum = 0;
}
}
return largestSum;
}
static int maximumWeight( int []arr, int n)
{
int largestSum = Kadane(arr, n);
for ( int i = 0; i < n; i++)
{
arr[i] = -arr[i];
}
largestSum = Math.Max(largestSum,
Kadane(arr, n));
return largestSum;
}
public static void Main()
{
int []arr = { 2, -3, 8, -2, 5 };
int N = arr.Length;
Console.Write(maximumWeight(arr, N));
}
}
|
Javascript
<script>
function Kadane(arr, n) {
let largestSum = 0, currMax = 0;
let currSum = 0;
for (let i = 0; i < n; i++) {
currSum += arr[i];
currMax = Math.max(currMax, arr[i]);
largestSum = Math.max(largestSum,
currMax * currSum);
if (currSum < 0) {
currMax = 0;
currSum = 0;
}
}
return largestSum;
}
function maximumWeight(arr, n) {
let largestSum = Kadane(arr, n);
for (let i = 0; i < n; i++) {
arr[i] = -arr[i];
}
largestSum = Math.max(largestSum,
Kadane(arr, n));
return largestSum;
}
let arr = [2, -3, 8, -2, 5];
let N = arr.length;
document.write(maximumWeight(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...