Find Maximum Value of Any Algebraic Expression in C++



The algebraic expression is a combination of numbers, variables (x or y), and arithmetic operators like +, -, *, and /. In this article, we'll write a C++ program to find the maximum value of an expression in the form (x1 + x2 + x3 + . . . + xa) * (y1 + y2 + . . . + yb) where a total of a+b integers are given.

Our task is to split these integers into two groups, one with a numbers and the other with b numbers, and calculate the product of their sums. By trying all possible splits, we will find the maximum value.

Let's understand this with an example:

Input: a = 3, b = 1
Numbers: 5, 2, 8, 4

We split the numbers into two groups: one with 3 numbers and the other with 1 number and calculate the product of their sums:

(5 + 2 + 8) * 4 = 15 * 4 = 60
(5 + 2 + 4) * 8 = 11 * 8 = 88
(2 + 8 + 4) * 5 = 14 * 5 = 70
(5 + 8 + 4) * 2 = 17 * 2 = 34

Among these, the maximum value is 34.
Output: 88

Finding Maximum Value of an Algebraic Expression

To solve this problem, we use dynamic programming. It helps us break the problem into smaller parts and store results so we don't repeat the same work again.

Below are the steps we took:

  • We first find the total sum of the first x + y elements and add 25 to each element to make sure all values are positive.
  • Then, we use a 2D boolean array p where p[k][j] tells if it's possible to select exactly k elements that sum to j.
  • We set p[0][0] = true, meaning sum zero with zero elements is always possible.
  • For each element, we update p in reverse order to mark all possible sums that can be made by including that element.
  • After processing all elements, we look through all sums reachable by selecting exactly x elements. For each sum, we subtract the added shift (25 * x) to get the original sum.
  • We then calculate the product of this sum and the difference between the total sum and this sum and return the maximum product from all these calculations.

C++ Program to Find Maximum Value of any Algebraic Expression

Below is a complete C++ program where we use dynamic programming to try all possible sums from selected elements and finds the smallest product to get the maximum value.

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

#define INF 1e9
#define MAX 25

int MaxValue(int a[], int x, int y) {
    int s = 0;
    cout << "Input array: ";
    for (int i = 0; i < (x + y); i++) {
        cout << a[i] << " ";
        s += a[i];
        a[i] += 25;  // Shift elements to make all positive  
    }
    cout << endl;

    bool p[MAX+1][MAX * MAX + 1];
    memset(p, 0, sizeof(p));
    p[0][0] = true;  // It is always possible to select 0 elements to get sum 0

    for (int i = 0; i < (x + y); i++) {
        // Update sums in reverse order to avoid using the same element multiple times in one iteration
        for (int k = min(x, i + 1); k >= 1; k--) {
            for (int j = 0; j < MAX * MAX + 1; j++) {
                if (p[k - 1][j]) {
                    p[k][j + a[i]] = true;
                }
            }
        }
    }

    int max_value = -INF;
    for (int i = 0; i < MAX * MAX + 1; i++) {
        if (p[x][i]) {
            int tmp = i - 25 * x;  // Remove the shift to get the actual sum
            max_value = max(max_value, tmp * (s - tmp));  // Calculate product and track max value
        }
    }
    cout << "Maximum Value: " << max_value << endl;
    return max_value;
}

int main() {
    int x = 2, y = 2;
    int ar[] = {7, 6, 4, 3};
    MaxValue(ar, x, y);
    return 0;
}

The output shows the maximum product we can get by choosing exactly x numbers from the array and multiplying their sum with the sum of the remaining numbers.

Input array: 7 6 4 3 
Maximum Value: 100

Time Complexity: O((x + y) * x * MAX^2) because we update the DP array for each element, subset size, and possible sum.

Space Complexity: O(MAX^3) due to the 2D boolean array p sized by MAX and MAX^2, with MAX fixed at 25.

Updated on: 2025-05-29T19:24:10+05:30

273 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements