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:
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Arrange the array such that upon performing given operations an increasing order is obtained
- Maximum Possible Product in Array after performing given Operations
- Maximum score possible after performing given operations on an Array
- Maximum sum of all elements of array after performing given operations
- Maximum count of equal numbers in an array after performing given operations
- Maximum number of unique values in the array after performing given operations
- Find the final sequence of the array after performing given operations
- Minimum element left from the array after performing given operations
- Number of elements from the array which are reachable after performing given operations on D
- Number of array elements derivable from D after performing certain operations
- Check if at least half array is reducible to zero by performing some operations
- Find the modified array after performing k operations of given type
- Count of operations to make all elements of array a equal to its min element by performing a[i] – b[i]
- Maximum array sum that can be obtained after exactly k changes
- Find the final number obtained after performing the given operation
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