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.
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
This problem can be solved by using dynamic programming.
- Assuming 2D array dp of dimensions n * 2.
- dp[i] represents the maximum value of the array up to ith position if the last operation is addition.
- dp[i] 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] = max(dp[ i - 1], dp[ i - 1]) + a[ i + 1]; dp[i] = dp[i - 1] - a[i] + a[i] * a[i + 1];
The base cases are:
dp = a + a; dp = a * a;
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Arrange the array such that upon performing given operations an increasing order is obtained
- Check if K can be obtained by performing arithmetic operations on any permutation of an Array
- Cost required to empty a given array by repeated removal of maximum obtained by given operations
- Find the final number obtained after performing the given operation
- Maximum sum possible from given Matrix by performing given operations
- Maximum Possible Product in Array after performing given Operations
- Maximum count of equal numbers in an array after performing given operations
- Maximum sum of all elements of array after performing given operations
- Maximum number of unique values in the array after performing given operations
- Maximum score possible after performing given operations on an Array
- Maximum possible Array sum after performing given operations
- Maximum count of values of S modulo M lying in a range [L, R] after performing given operations on the array
- Maximum inversions in a sequence of 1 to N after performing given operations at most K times
- Check if sum Y can be obtained from the Array by the given operations
- Minimum element left from the array after performing given operations
- Find the modified array after performing k operations of given type
- Number of elements from the array which are reachable after performing given operations on D
- Find the final sequence of the array after performing given operations
- Maximize sum of array elements removed by performing the given operations
- Count of distinct possible strings after performing given operations
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : AnkitRai01