Maximum and minimum value of an expression with parentheses

Updated
Input Format: The line of the input contains a string s of length 2n + 1 for some n.

Constraints: 1 <= n <= 14

Output Format: Output the maximum and minimum possible value of the given arithmetic
expression among different orders of applying arithmetic operations.

In this problem, your goal is to add parentheses to a given arithmetic expression to maximize its value.

Two tables of maximum M and minimum m values for each part of the expression, ending with 200 as maximum and minus 94 as minimum.
Input : 5-8+7*4-8+9
Output : Maximun Value is 200 and Minimum value is -94
Explanation :
(5 - ((8 + 7) × (4 - (8 + 9)))) = 200

Input : 1+2-3*4-5
Output : Maximun Value is 6 and Minimum value is -14
Explanation :
((1 + 2) - (3 × (4 - 5))) = 6

Intuition :

  • Assume that the last operation in an optimal parenthesizing of 5 – 8 + 7 × 4 – 8 + 9 is × : (5 – 8 + 7) × (4 – 8 + 9).
  • It would help to know optimal values for sub-expressions 5 – 8 + 7 and 4 – 8 + 9.
  • We need to keep track for both the minimal and the maximal values of subexpressions.

Subproblems :

  • Let Ei,j be the subexpression di opi· · · opj-1 dj
  • M(i, j) = maximum value of Ei,j m(i, j) = minimum value of Ei,j

Recurrence Relation :

  • M(i, j) = max(i<=k<=j-1) = { M(i, k) opk M(k + 1, j), M(i, k) opk m(k + 1, j), m(i, k) opk M(k + 1, j), m(i, k) opk m(k + 1, j)}
  • m(i, j) = min(i<=k<=j-1) = { M(i, k) opk M(k + 1, j), M(i, k) opk m(k + 1, j), m(i, k) opk M(k + 1, j), m(i, k) opk m(k + 1, j)}

Order of Subproblems :

  • When computing M(i, j), the values of M(i, k) and M(k + 1, j) should be already computed.
  • Solve all subproblems in order of increasing (j – i).

Implementation :

#include<bits/stdc++.h>
using namespace std;

long long evaluate(long long a, long long b, char op) {
        if (op == '*') {
                return a * b;
        } else if (op == '+') {
                return a + b;
        } else if (op == '-') {
                return a - b;
        } else {
                assert(0);
        }
}

long long  *maxandminvalue(const string &exp) {
        int length = exp.size();
        int num = (length + 1) / 2;
        long long minimumArray[num][num];
        long long maximumArray[num][num];
        memset(minimumArray,0,sizeof(minimumArray)); // initialize to 0
        memset(maximumArray,0,sizeof(maximumArray));
        for (int i = 0; i < num; i++)
        {
                //The values on the main diagonal of minArray and maxArray is  the number themselves only
                minimumArray[i][i] = stoll(exp.substr(2*i,1));    //string substr (pos, len ) return string
                maximumArray[i][i] = stoll(exp.substr(2*i,1));    //This function converts string to long long int.
        }

        for (int s = 0; s < num - 1; s++)
        {
                for (int i = 0; i < num - s - 1; i++)
                {
                        int j = i + s + 1;
                        long long minVal = LLONG_MAX; //Maximum value for an object of type long long int
                        long long maxVal = LLONG_MIN; //Minimum value for an object of type long long int
                        // find the minimum and maximum values for the expression
                        // between the ith number and jth number
                        for (int k = i; k < j; k++ )
                        {
                                long long a = evaluate(minimumArray[i][k],minimumArray[k + 1][j],exp[2 * k + 1]);
                                long long b = evaluate(minimumArray[i][k],maximumArray[k + 1][j],exp[2 * k + 1]);
                                long long c = evaluate(maximumArray[i][k],minimumArray[k + 1][j],exp[2 * k + 1]);
                                long long d = evaluate(maximumArray[i][k],maximumArray[k + 1][j],exp[2 * k + 1]);
                                minVal = min({minVal,a,b,c,d});
                                maxVal = max({maxVal,a,b,c,d});
                        }
                        minimumArray[i][j] = minVal;
                        maximumArray[i][j] = maxVal;
                }
        }
        long long *result = new long long[2];
          result[0]=maximumArray[0][num - 1];
          result[1]=minimumArray[0][num - 1];

        return result;
}

int main() {
        string str;

        cin >> str;
        long long *ans = maxandminvalue(str);
        cout << "Maximun Value is "<< ans[0] <<" and Minimum value is "<<ans[1]<< endl;
}

 
Comment
Mithlesh Upadhyay Published 2 Dec 2026 · 4 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Related tutorials