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
4mo

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!
Select
Add answer anonymously...

Top Software Engineer Interview Questions Asked at Paxcom India

Q. Determine the Left View of a Binary Tree You are given a binary tree of integers...read more
Q. Sum Tree Conversion Convert a given binary tree into its sum tree. In a sum tree...read more
Q. Kruskal’s Minimum Spanning Tree Algorithm Problem Statement You are given a conn...read more
Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits