


If the Input Expression is 3*5+2, then the maximum value of ‘21’ can be obtained by placing the parentheses as 3*(5+2). So you need to return 21.
The first line contains a single integer ‘T’ denoting the number of test cases to be performed.
The first line of each test case contains a string denoting the given expression.
For each test case, return a single integer denoting the maximum value obtained from the given expression.
You don't need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= |Exp| <= 100
Time limit: 1 sec
The basic idea to solve this problem is to recursively place parentheses at all possible positions and find the maximum value obtained among all.
We can divide the input expression into smaller expressions recursively and find the maximum value.
If the input expression is ( EXP[ 0 ] opr EXP[ 1 ] opr EXP[ 2 ].............opr EXP[ n-1 ] ), then we can divide this expression into two as -
( EXP[ 0 ] opr……….opr EXP[ k-1 ] ) opr ( EXP[ k+1 ] opr……….opr EXP[ n-1 ] ).
The input expression can be computed by first computing the two sub-expressions and then combining them based on the operator between them.
We can divide the expression into two expressions around all the operators in the expression.
For Example- If EXP = a + b * c -d then this can be divided into two expressions as ( a ) + ( b*c-d) , ( a+b ) * ( c-d), ( a+b*c) - d.
We will create a recursive function that will return an array of integers storing all the possible values that can be computed from the given expression by placing parentheses at all possible positions. The maximum value of all the values in this array will be the required answer.
Algorithm
Following is a partial recursion tree of the recursion approach for Expression = “5+14-2*8”

We can observe that in this partial recursion tree, (4,6) is solved two times. This problem of overlapped sub-problems can be solved by memoization. We will solve the problem as we did in the recursive approach and store the recursive call results in an unordered map ‘VALUE’.
For every recursive call, we will first check the ‘VALUE’ map. If the value is already computed, we will return it; otherwise, solve it and put it in the ‘value’ map.
The idea to solve this problem is the same as we did in the recursive approach, i.e., splitting the expression into sub-expressions whenever any operator is encountered.
We will separate the operands and operators in the expression and store them in the linear array of integers and characters.
If the operator between two sub-expressions is ‘+’ we can simply combine the two sub-expressions according to the operator between them.
But if the operator is ‘-’, then the maximum value will be obtained by subtracting the minimum value of the second subexpression from the maximum value of the first sub-expression.
And for the ‘ * ’ operator, the maximum value can be obtained by either multiplying the maximum values of both the expressions or multiplying the minimum value of the two sub-expressions.
Algorithm