Maximize Expression Value

Given an arithmetic expression EXP containing integer values separated by any of the operators ‘+’, ‘-’, and ‘*’, your task is to place parentheses such that the value of the expression is maximized. Return the maximum value obtained from such parenthetical placement.

Example:

Input:
Expression: '3*5+2'
Output:
21
Explanation:

The maximum value 21 can be achieved by placing the parentheses as 3*(5+2).

Constraints:

  • 1 ≤ T ≤ 5
  • 1 ≤ |EXP| ≤ 100
  • Time limit: 1 sec

Input:

The first line contains an integer ‘T’ representing the number of test cases. Each of the following T lines contains a string denoting an expression EXP.

Output:

For each test case, return a single integer indicating the maximum value of the given expression.
Note:
You don’t need to print anything; it has already been taken care of. Implement the function to compute the result.
AnswerBot
2d

Given an arithmetic expression, place parentheses to maximize the value of the expression.

  • Identify the operators in the expression ('+', '-', '*').

  • Consider the precedence of operators to determine whe...read more

Help your peers!
Add answer anonymously...
Paxcom India Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter