## Maximum Product

### June 11, 2019

There are four ways the maximum product can be formed:

1) take the maximum from each of the three arrays

2) take the maximum from the first array and the minimum from the second and third arrays

3) take the maximum from the second array and the minimum from the first and third arrays

4) take the maximum from the third array and the minimum from the first and second arrays:

(define (max-prod xs ys zs) (let ((min-x (apply min xs)) (max-x (apply max xs)) (min-y (apply min ys)) (max-y (apply max ys)) (min-z (apply min zs)) (max-z (apply max zs))) (max (* max-x max-y max-z) (* max-x min-y min-z) (* min-x max-y min-z) (* min-x min-y max-z))))

And here is the program applied to the sample problem.

> (max-prod '(10 -10 15 -12) '(10 -12 13 -12) '(-11 -10 9 -12)) 2160

It is not correct, as one commenter suggested, to sort all three arrays, sort the six-element array built as the first and last item from each sorted array, and then take either the three maximum items or the maximum plus two minimum items from the six-element array, as that does not require all three elements to come from three different arrays.

You can run the program https://ideone.com/GxKajf.

Cool problem. Here is my take on it using Julia 1.0.2:

function fmp(A::Array{Int64, 1}, B::Array{Int64, 1}, C::Array{Int64, 1})

Aa = abs.(A)

na = length(A)

ind_a = sortperm(Aa, rev = true)

Ba = abs.(B)

nb = length(B)

ind_b = sortperm(Ba, rev = true)

Ca = abs.(C)

nc = length(C)

ind_c = sortperm(Ca, rev = true)

mp = typemin(Int64)

Z = Array{Int64}(undef, 3)

counter = 0

iwp = round(Int64, sqrt(na

nbnc)) # iterations without progressend

Note that to avoid unnecessary iterations, I limited the total number of iteration by including the iwp threshold (iterations without progress). This is heuristically calculated using the sizes of the original arrays as an input. Tried it with larger arrays and it works, though I cannot vouch for its effectiveness for all sizes. Cheers!

Here is a simple R7RS Scheme solution to a slightly more general

problem; it works for one or more “arrays” (lists) of numbers as

input.

Slightly more general in Perl – but uses a similar technique – written to allow any number of arrays

In Python. Only eight product are possible. Simple calculate the maximum of these eight. Reducing the number of products to four hardly reduces calculation time and only makes things more complicated.

@Zack: I don’t understand how the ‘iwp’ cutoff is correct. If there is a very large item at the end of array C, won’t it be missed? Also, your method seems to be Theta(n^3) instead of the other Theta(n) solutions.

@Paul: that’s a good question. That’s why I sort the arrays first, in terms of absolute values. Chances are that the first iterations of the loops are going to yield the optimum result, so we never have to go through all possible combinations (something wasteful for large arrays). The latter are more than 8 for the original arrays, btw; namely Total_Combinations = 4 x 4 x 4 = 64. Cheers

Rust version:

Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=377bd1520d7bd51bb59bd268c654eab4

Here’s a solution in C.

Example Usage: