Say you are given an array of signed integers called NUMBERS of length l. Say NUMBERS is equal to
1, 2, 3, 4, 5
where l = 5.
You want to form a sum X of any m elements of the set NUMBERS and multiply this by the sum Y of the remaining n elements for any given m, to get a result X * Y
This result is thus of the form:
(x1 + x2 + x3 +...+ xm) * (y1 + y2 +...+ yn)
where the xis are the elements of the NUMBERS array in the X sum, and the yis are the elements of the NUMBERS array in the Y sum.
To be clear:
l = m + nsum(NUMBERS) = sum(X) + sum(Y)
For example, with n = 2, one way to build a result would have X be the sum of the elements 1 and 4, and thus the sum Y of the remaining m = 3 elements be 2 + 3 + 5. So we would have X = 5 and Y = 10 The result would then be the product X * Y = 5 * 10 = 50.
We want to find the most efficient way to maximize and minimize this result for a given m.
The naive approach would be to try all possible combinations for X, and return the result where (sum(NUMBERS) - sum(X)) * sum(X) is minimized and maximized. This is of complexity O(lCn).
Formally:
Given (n + m) signed integers, we want to minimize and maximize the result of the algebraic expression of the form:
(x1 + x2 + x3 +...+ xn) * (y1 + y2 +...+ ym)
for a fixed given n.
I have noticed a pattern where the minimum can usually be obtained by segregating the m smallest elements into X and the remaining larger elements into Y, and the maximum can usually be obtained by evenly distributing the smallest and largest elements in X and Y. Is there any rationale behind this, or any way to prove this?
This appears to be a special case of the AM GM Inequality. Simply put, it will always be the case that $\frac{X+Y}{2} \geq \sqrt{X \cdot Y}$. For your application, You already know that $\frac{X+Y}{2} = \frac{n(n+1)}{4}$ since this is the closed form of $\sum_{i=1}^{n}i$. So, any partition that sets the sum of the numbers in $X$ equal to the sum of the numbers in $Y$ will work (e.g. place every other element in $X$).