Maximum value obtained by performing given operations in an Array
Last Updated :
04 Oct, 2023
Given an array arr[], the task is to find out the maximum obtainable value. The user is allowed to add or multiply the two consecutive elements. However, there has to be at least one addition operation between two multiplication operations (i.e), two consecutive multiplication operations are not allowed.
Let the array elements be 1, 2, 3, 4 then 1 * 2 + 3 + 4 is a valid operation whereas 1 + 2 * 3 * 4 is not a a valid operation as there are consecutive multiplication operations.
Examples:
Input : 5 -1 -5 -3 2 9 -4
Output : 33
Explanation:
The maximum value obtained by following the above conditions is 33.
The sequence of operations are given as:
5 + (-1) + (-5) * (-3) + 2 * 9 + (-4) = 33
Input : 5 -3 -5 2 3 9 4
Output : 62
Approach:
This problem can be solved by using dynamic programming.
- Assuming 2D array dp[][] of dimensions n * 2.
- dp[i][0] represents the maximum value of the array up to ith position if the last operation is addition.
- dp[i][1] represents the maximum value of the array up to ith position if the last operation is multiplication.
Now, since consecutive multiplication operation is not allowed, the recurrence relation can be considered as :
dp[i][0] = max(dp[ i - 1][0], dp[ i - 1][1]) + a[ i + 1];
dp[i][1] = dp[i - 1][0] - a[i] + a[i] * a[i + 1];
The base cases are:
dp[0][0] = a[0] + a[1];
dp[0][1] = a[0] * a[1];
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findMax( int a[], int n)
{
int dp[n][2];
memset (dp, 0, sizeof (dp));
dp[0][0] = a[0] + a[1];
dp[0][1] = a[0] * a[1];
for ( int i = 1; i <= n - 2; i++) {
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]) + a[i + 1];
dp[i][1] = dp[i - 1][0] - a[i] + a[i] * a[i + 1];
}
cout << max(dp[n - 2][0], dp[n - 2][1]);
}
int main()
{
int arr[] = { 5, -1, -5, -3, 2, 9, -4 };
findMax(arr, 7);
}
|
Java
import java.util.*;
class GFG
{
static void findMax( int []a, int n)
{
int dp[][] = new int [n][ 2 ];
int i, j;
for (i = 0 ; i < n ; i++)
for (j = 0 ; j < 2 ; j++)
dp[i][j] = 0 ;
dp[ 0 ][ 0 ] = a[ 0 ] + a[ 1 ];
dp[ 0 ][ 1 ] = a[ 0 ] * a[ 1 ];
for (i = 1 ; i <= n - 2 ; i++)
{
dp[i][ 0 ] = Math.max(dp[i - 1 ][ 0 ],
dp[i - 1 ][ 1 ]) + a[i + 1 ];
dp[i][ 1 ] = dp[i - 1 ][ 0 ] - a[i] +
a[i] * a[i + 1 ];
}
System.out.println(Math.max(dp[n - 2 ][ 0 ],
dp[n - 2 ][ 1 ]));
}
public static void main (String[] args)
{
int arr[] = { 5 , - 1 , - 5 , - 3 , 2 , 9 , - 4 };
findMax(arr, 7 );
}
}
|
Python3
import numpy as np
def findMax(a, n) :
dp = np.zeros((n, 2 ));
dp[ 0 ][ 0 ] = a[ 0 ] + a[ 1 ];
dp[ 0 ][ 1 ] = a[ 0 ] * a[ 1 ];
for i in range ( 1 , n - 1 ) :
dp[i][ 0 ] = max (dp[i - 1 ][ 0 ], dp[i - 1 ][ 1 ]) + a[i + 1 ];
dp[i][ 1 ] = dp[i - 1 ][ 0 ] - a[i] + a[i] * a[i + 1 ];
print ( max (dp[n - 2 ][ 0 ], dp[n - 2 ][ 1 ]), end = "");
if __name__ = = "__main__" :
arr = [ 5 , - 1 , - 5 , - 3 , 2 , 9 , - 4 ];
findMax(arr, 7 );
|
C#
using System;
class GFG
{
static void findMax( int []a, int n)
{
int [,]dp = new int [n, 2];
int i, j;
for (i = 0; i < n ; i++)
for (j = 0; j < 2; j++)
dp[i, j] = 0;
dp[0, 0] = a[0] + a[1];
dp[0, 1] = a[0] * a[1];
for (i = 1; i <= n - 2; i++)
{
dp[i, 0] = Math.Max(dp[i - 1, 0], dp[i - 1, 1]) + a[i + 1];
dp[i, 1] = dp[i - 1, 0] - a[i] + a[i] * a[i + 1];
}
Console.WriteLine(Math.Max(dp[n - 2, 0], dp[n - 2, 1]));
}
public static void Main()
{
int []arr = { 5, -1, -5, -3, 2, 9, -4 };
findMax(arr, 7);
}
}
|
Javascript
<script>
function findMax(a , n) {
var dp = Array(n).fill().map(()=>Array(2).fill(0));
var i, j;
for (i = 0; i < n; i++)
for (j = 0; j < 2; j++)
dp[i][j] = 0;
dp[0][0] = a[0] + a[1];
dp[0][1] = a[0] * a[1];
for (i = 1; i <= n - 2; i++) {
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1]) + a[i + 1];
dp[i][1] = dp[i - 1][0] - a[i] + a[i] * a[i + 1];
}
document.write(Math.max(dp[n - 2][0], dp[n - 2][1]));
}
var arr = [ 5, -1, -5, -3, 2, 9, -4 ];
findMax(arr, 7);
</script>
|
Time complexity: O(n) where n is size of given array
Auxiliary space: O(n) because it is using extra space for array “dp”
Efficient approach : Space optimization O(1)
In previous approach the current value ( dp[i] ) is depend upon the ( dp[i-1] ) so which is just the previous computation of DP array. So to optimize the space complexity we only require the computation of previous index.
Implementation Steps:
- Create variables prev_sum and prev_prod to get the computation of just previous index of DP.
- Initialize them with a[0] + a[1] and a[0] * a[1] resp.
- Now create variables curr_sum and curr_prod to get the current value from previous computation.
- Create variable max_val to store the final result.
- Now iterate over subproblems and get the current value from prev_sum and prev_prod.
- After every iteration assign values of curr_sum to prev_sum and curr_prod to prev_prod to iterate further.
- At last print answer stored in max_val.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void findMax( int a[], int n)
{
int prev_sum = a[0] + a[1];
int prev_prod = a[0] * a[1];
int curr_sum, curr_prod;
int max_val;
for ( int i = 1; i <= n - 2; i++) {
curr_sum = max(prev_sum, prev_prod) + a[i + 1];
curr_prod = prev_sum - a[i] + a[i] * a[i + 1];
max_val = max(curr_sum, curr_prod);
prev_sum = curr_sum;
prev_prod = curr_prod;
}
cout << max_val;
}
int main()
{
int arr[] = { 5, -1, -5, -3, 2, 9, -4 };
findMax(arr, 7);
}
|
Java
import java.util.*;
class Main {
static void findMax( int a[], int n)
{
int prev_sum = a[ 0 ] + a[ 1 ];
int prev_prod = a[ 0 ] * a[ 1 ];
int curr_sum, curr_prod;
int max_val = 0 ;
for ( int i = 1 ; i <= n - 2 ; i++) {
curr_sum
= Math.max(prev_sum, prev_prod) + a[i + 1 ];
curr_prod = prev_sum - a[i] + a[i] * a[i + 1 ];
max_val = Math.max(curr_sum, curr_prod);
prev_sum = curr_sum;
prev_prod = curr_prod;
}
System.out.println(max_val);
}
public static void main(String[] args)
{
int arr[] = { 5 , - 1 , - 5 , - 3 , 2 , 9 , - 4 };
findMax(arr, 7 );
}
}
|
Python3
def findMax(a, n):
prev_sum = a[ 0 ] + a[ 1 ]
prev_prod = a[ 0 ] * a[ 1 ]
curr_sum, curr_prod = 0 , 0
max_val = 0
for i in range ( 1 , n - 1 ):
curr_sum = max (prev_sum, prev_prod) + a[i + 1 ]
curr_prod = prev_sum - a[i] + a[i] * a[i + 1 ]
max_val = max (curr_sum, curr_prod)
prev_sum = curr_sum
prev_prod = curr_prod
print (max_val)
arr = [ 5 , - 1 , - 5 , - 3 , 2 , 9 , - 4 ]
findMax(arr, 7 )
|
C#
using System;
public class Program
{
public static void Main()
{
int [] arr = { 5, -1, -5, -3, 2, 9, -4 };
findMax(arr, 7);
}
static void findMax( int [] a, int n)
{
int prev_sum = a[0] + a[1];
int prev_prod = a[0] * a[1];
int curr_sum, curr_prod;
int max_val = 0;
for ( int i = 1; i <= n - 2; i++)
{
curr_sum = Math.Max(prev_sum, prev_prod) + a[i + 1];
curr_prod = prev_sum - a[i] + a[i] * a[i + 1];
max_val = Math.Max(curr_sum, curr_prod);
prev_sum = curr_sum;
prev_prod = curr_prod;
}
Console.WriteLine(max_val);
}
}
|
Javascript
function findMax(arr) {
let prev_sum = arr[0] + arr[1];
let prev_prod = arr[0] * arr[1];
let curr_sum, curr_prod;
let max_val;
for (let i = 1; i < arr.length - 1; i++) {
curr_sum = Math.max(prev_sum, prev_prod) + arr[i + 1];
curr_prod = prev_sum - arr[i] + arr[i] * arr[i + 1];
max_val = Math.max(curr_sum, curr_prod);
prev_sum = curr_sum;
prev_prod = curr_prod;
}
console.log(max_val);
}
const arr = [5, -1, -5, -3, 2, 9, -4];
findMax(arr);
|
Output
33
Time complexity: O(n) where n is size of given array
Auxiliary space: O(1) because no extra space is used.
Share your thoughts in the comments
Please Login to comment...