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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the maximum product
// of the sum of the subarray with its
// maximum element
int Kadane(int arr[], int n)
{
int largestSum = 0, currMax = 0;
int currSum = 0;
// Traverse the array arr[]
for (int i = 0; i < n; i++) {
// Increment currSum by a[i]
currSum += arr[i];
// Maximize the value of currMax
currMax = max(currMax, arr[i]);
// Maximize the value of
// largestSum
largestSum = max(largestSum,
currMax * currSum);
// If currSum goes less than 0
// then update currSum = 0
if (currSum < 0) {
currMax = 0;
currSum = 0;
}
}
// Return the resultant value
return largestSum;
}
// Function to maximize the product of
// the sum of the subarray with its
// maximum element
int maximumWeight(int arr[], int n)
{
// Find the largest sum of the
// subarray
int largestSum = Kadane(arr, n);
// Multiply each array element
// with -1
for (int i = 0; i < n; i++) {
arr[i] = -arr[i];
}
// Find the largest sum of the
// subarray with negation of all
// array element
largestSum = max(largestSum,
Kadane(arr, n));
// Return the resultant maximum
// value
return largestSum;
}
// Driver Code
int main()
{
int arr[] = { 2, -3, 8, -2, 5 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << maximumWeight(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to find the maximum product
// of the sum of the subarray with its
// maximum element
static int Kadane(int arr[], int n)
{
int largestSum = 0, currMax = 0;
int currSum = 0;
// Traverse the array arr[]
for (int i = 0; i < n; i++) {
// Increment currSum by a[i]
currSum += arr[i];
// Maximize the value of currMax
currMax = Math.max(currMax, arr[i]);
// Maximize the value of
// largestSum
largestSum
= Math.max(largestSum, currMax * currSum);
// If currSum goes less than 0
// then update currSum = 0
if (currSum < 0) {
currMax = 0;
currSum = 0;
}
}
// Return the resultant value
return largestSum;
}
// Function to maximize the product of
// the sum of the subarray with its
// maximum element
static int maximumWeight(int arr[], int n)
{
// Find the largest sum of the
// subarray
int largestSum = Kadane(arr, n);
// Multiply each array element
// with -1
for (int i = 0; i < n; i++) {
arr[i] = -arr[i];
}
// Find the largest sum of the
// subarray with negation of all
// array element
largestSum = Math.max(largestSum, Kadane(arr, n));
// Return the resultant maximum
// value
return largestSum;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 2, -3, 8, -2, 5 };
int N = arr.length;
System.out.println(maximumWeight(arr, N));
// This code is contributed by Potta Lokesh
}
}
Python3
# Python3 program for the above approach
# Function to find the maximum product
# of the sum of the subarray with its
# maximum element
def Kadane(arr, n):
largestSum = 0
currMax = 0
currSum = 0
# Traverse the array arr[]
for i in range(n):
# Increment currSum by a[i]
currSum += arr[i]
# Maximize the value of currMax
currMax = max(currMax, arr[i])
# Maximize the value of
# largestSum
largestSum = max(largestSum,
currMax * currSum)
# If currSum goes less than 0
# then update currSum = 0
if (currSum < 0):
currMax = 0
currSum = 0
# Return the resultant value
return largestSum
# Function to maximize the product of
# the sum of the subarray with its
# maximum element
def maximumWeight(arr, n):
# Find the largest sum of the
# subarray
largestSum = Kadane(arr, n)
# Multiply each array element
# with -1
for i in range(n):
arr[i] = -arr[i]
# Find the largest sum of the
# subarray with negation of all
# array element
largestSum = max(largestSum,
Kadane(arr, n))
# Return the resultant maximum
# value
return largestSum
# Driver Code
if __name__ == '__main__':
arr = [ 2, -3, 8, -2, 5 ]
N = len(arr)
print(maximumWeight(arr, N))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the maximum product
// of the sum of the subarray with its
// maximum element
static int Kadane(int []arr, int n)
{
int largestSum = 0, currMax = 0;
int currSum = 0;
// Traverse the array arr[]
for(int i = 0; i < n; i++)
{
// Increment currSum by a[i]
currSum += arr[i];
// Maximize the value of currMax
currMax = Math.Max(currMax, arr[i]);
// Maximize the value of
// largestSum
largestSum = Math.Max(largestSum,
currMax * currSum);
// If currSum goes less than 0
// then update currSum = 0
if (currSum < 0)
{
currMax = 0;
currSum = 0;
}
}
// Return the resultant value
return largestSum;
}
// Function to maximize the product of
// the sum of the subarray with its
// maximum element
static int maximumWeight(int []arr, int n)
{
// Find the largest sum of the
// subarray
int largestSum = Kadane(arr, n);
// Multiply each array element
// with -1
for(int i = 0; i < n; i++)
{
arr[i] = -arr[i];
}
// Find the largest sum of the
// subarray with negation of all
// array element
largestSum = Math.Max(largestSum,
Kadane(arr, n));
// Return the resultant maximum
// value
return largestSum;
}
// Driver Code
public static void Main()
{
int []arr = { 2, -3, 8, -2, 5 };
int N = arr.Length;
Console.Write(maximumWeight(arr, N));
}
}
// This code is contributed by ipg2016107
JavaScript
<script>
// JavaScript Program for the above approach
// Function to find the maximum product
// of the sum of the subarray with its
// maximum element
function Kadane(arr, n) {
let largestSum = 0, currMax = 0;
let currSum = 0;
// Traverse the array arr[]
for (let i = 0; i < n; i++) {
// Increment currSum by a[i]
currSum += arr[i];
// Maximize the value of currMax
currMax = Math.max(currMax, arr[i]);
// Maximize the value of
// largestSum
largestSum = Math.max(largestSum,
currMax * currSum);
// If currSum goes less than 0
// then update currSum = 0
if (currSum < 0) {
currMax = 0;
currSum = 0;
}
}
// Return the resultant value
return largestSum;
}
// Function to maximize the product of
// the sum of the subarray with its
// maximum element
function maximumWeight(arr, n) {
// Find the largest sum of the
// subarray
let largestSum = Kadane(arr, n);
// Multiply each array element
// with -1
for (let i = 0; i < n; i++) {
arr[i] = -arr[i];
}
// Find the largest sum of the
// subarray with negation of all
// array element
largestSum = Math.max(largestSum,
Kadane(arr, n));
// Return the resultant maximum
// value
return largestSum;
}
// Driver Code
let arr = [2, -3, 8, -2, 5];
let N = arr.length;
document.write(maximumWeight(arr, N));
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Maximize product of subarray sum with its minimum element Given an array arr[] consisting of N positive integers, the task is to find the maximum product of subarray sum with the minimum element of that subarray. Examples: Input: arr[] = {3, 1, 6, 4, 5, 2}Output: 60Explanation:The required maximum product can be obtained using subarray {6, 4, 5}Therefore,
10 min read
Maximize the sum of maximum elements of at least K-sized subarrays Given an integer array arr[] of length N and an integer K, partition the array in some non-overlapping subarrays such that each subarray has size at least K and each element of the array should be part of a subarray. The task is to maximize the sum of maximum elements across all the subarrays. Examp
7 min read
Maximize the maximum subarray sum after removing atmost one element Given an array arr[] of N integers, the task is to find the maximum sum of a subarray with at most one deletion allowed. The size of subarray to be considered should be at least 1.Examples: Input: arr[] = {1, 2, 3, -2, 3} Output: 9 The maximum sub-array sum is given by the sub-array {2, 3, -2, 3} He
2 min read
Subarray with largest sum after excluding its maximum element Given an array arr[], the task is to find the starting and ending indices of the subarray with the largest sum after excluding its maximum element.Examples: Input: arr[] = {5, -2, 10, -1, 4} Output: 1 5 Explanation: Subarray[1:5] = {5, -2, 10, -1, 4} Sum of subarray excluding maximum element = 5 + (
9 min read
Print subarray with maximum sum Given an array arr[], the task is to print the subarray having maximum sum.Examples:Input: arr[] = {2, 3, -8, 7, -1, 2, 3}Output: 11Explanation: The subarray {7, -1, 2, 3} has the largest sum 11.Input: arr[] = {-2, -5, 6, -2, -3, 1, 5, -6}Output: {6, -2, -3, 1, 5}Explanation: The subarray {6, -2, -3
13 min read
Length of the smallest subarray with maximum possible sum Given an array arr[] consisting of N non-negative integers, the task is to find the minimum length of the subarray whose sum is maximum. Example: Input: arr[] = {0, 2, 0, 0, 12, 0, 0, 0}Output: 4Explanation: The sum of the subarray {2, 0, 0, 12} = 2 + 0 + 0 + 12 = 14, which is maximum sum possible a
6 min read
Sum of maximum of all subarrays by adding even frequent maximum twice Given an array arr[] consisting of N integers (All array elements are a perfect power of 2), the task is to calculate the sum of the maximum elements in all the subarrays. Note: If the frequency of the maximum element in a subarray is even, add twice the value of that element to the sum. Examples: I
15+ min read
Split array into K subarrays such that sum of maximum of all subarrays is maximized Given an array arr[] of size N and a number K, the task is to partition the given array into K contiguous subarrays such that the sum of the maximum of each subarray is the maximum possible. If it is possible to split the array in such a manner, then print the maximum possible sum. Otherwise, print
10 min read
Maximum sum of subarrays having distinct elements of length K Given an array, arr[] and a value k, represent the length of the subarray to be considered. Find the maximum sum that can be obtained from the subarray of length k such that each element of the subarray is unique. If there is no subarray that meets the required condition then return 0. Examples: Inp
13 min read