Arithmetic Operators
Given an arithmetic expression ‘EXP’ containing integer values separated by any of the three operators ‘ + ’, ‘ - ’ and ‘ * ’. You need to place parentheses in the given expression such that the value of expression gets maximized. Your task is to return the maximum value obtained.
For Example:
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.
Input Format:
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.
Output Format:
For each test case, return a single integer denoting the maximum value obtained from the given expression.
Note:
You don't need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 5
1 <= |Exp| <= 100
Time limit: 1 sec
CodingNinjas
author
2y
Recursion
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 small...read more
CodingNinjas
author
2y
Memoization
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 o...read more
CodingNinjas
author
2y
Dynamic Programming
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 sep...read more
Add answer anonymously...
Top Paxcom India Software Engineer interview questions & answers
Popular interview questions of Software Engineer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app