Maximum value obtained by performing given operations in an Array

05 May, 2021
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.
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++
Java
Python3
C#
Javascript
// C++ implementation of the above approach#include <bits/stdc++.h>using namespace std; // A function to calculate the maximum valuevoid findMax(int a[], int n){    int dp[n][2];    memset(dp, 0, sizeof(dp));         // basecases    dp[0][0] = a[0] + a[1];    dp[0][1] = a[0] * a[1];        //Loop to iterate and add the max value in the dp array    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]);} // Driver Codeint main(){    int arr[] = { 5, -1, -5, -3, 2, 9, -4 };    findMax(arr, 7);}
// Java implementation of the above approachclass GFG{         // A function to calculate the maximum value    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;         // basecases        dp[0][0] = a[0] + a[1];        dp[0][1] = a[0] * a[1];                 // Loop to iterate and add the        // max value in the dp array        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]));    }         // Driver Code    public static void main (String[] args)    {        int arr[] = { 5, -1, -5, -3, 2, 9, -4 };        findMax(arr, 7);    }} // This code is contributed by AnkitRai01
# Python3 implementation of the above approachimport numpy as np # A function to calculate the maximum valuedef findMax(a, n) :     dp = np.zeros((n, 2));         # basecases    dp[0][0] = a[0] + a[1];    dp[0][1] = a[0] * a[1];         # Loop to iterate and add the max value in the dp array    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 =""); # Driver Codeif __name__ == "__main__" :     arr = [ 5, -1, -5, -3, 2, 9, -4 ];    findMax(arr, 7);     # This code is contributed by AnkitRai01
// C# implementation of the above approachusing System; class GFG{         // A function to calculate the maximum value    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;         // basecases        dp[0, 0] = a[0] + a[1];        dp[0, 1] = a[0] * a[1];                 // Loop to iterate and add the        // max value in the dp array        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]));    }         // Driver Code    public static void Main()    {        int []arr = { 5, -1, -5, -3, 2, 9, -4 };        findMax(arr, 7);    }} // This code is contributed by AnkitRai01
<script>// javascript implementation of the above approach// A function to calculate the maximum value    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;         // basecases        dp[0][0] = a[0] + a[1];        dp[0][1] = a[0] * a[1];         // Loop to iterate and add the        // max value in the dp array        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]));    }     // Driver Code             var arr = [ 5, -1, -5, -3, 2, 9, -4 ];        findMax(arr, 7); // This code contributed by Rajput-Ji</script>
33
 
ankthon
Rajput-Ji
Arrays
Dynamic Programming
Arrays
Dynamic Programming
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Data Structures
Search, insert and delete in an unsorted array
Window Sliding Technique
Chocolate Distribution Problem
Find duplicates in O(n) time and O(1) extra space | Set 1
Program for Fibonacci numbers
0-1 Knapsack Problem | DP-10
Longest Common Subsequence | DP-4
Longest Palindromic Substring | Set 1
Longest Increasing Subsequence | DP-3