Introduction
Our goal is to efficiently find the maximum of a large amount of (redundant) data.
We define the outer product of vectors \$A\$ and \$B\$ as a matrix containing the products of all entries of \$A\$ with each entry of \$B\$. (see outer Product)
$$ A\otimes B := A\cdot B^T = \left(\begin{array}{rrr} A(1)\cdot B(1) & A(1)\cdot B(2) & \ldots & A(1)\cdot B(M)\\ A(2)\cdot B(1) & A(2)\cdot B(2) & \ldots & A(2)\cdot B(M)\\ \ldots & & & \ldots \\ A(N)\cdot B(1) & A(N)\cdot B(2) & \ldots & A(N)\cdot B(M)\\ \end{array}\right) $$
Here \$A\$ has \$N\$ entries (size \$N\$) and \$B\$ has \$M\$ entries.
In this way, the outer product of \$K\$ vectors yields a \$K\$-dimensional array (tensor of order \$K\$). If \$N_1,N_2,\ldots,N_K\$ are the sizes of the vectors, we get a total of \$N_1\cdot N_2\cdot\ldots\cdot N_K\$ products.
Example: $$ \left(\begin{array}{r}1\\ 2\\ 0\end{array}\right)\otimes \left(\begin{array}{r}1\\ 3\end{array}\right)\otimes \left(\begin{array}{r}-5\\ 7\end{array}\right)= \left(\begin{array}{rr|rr|rr}-5 & 7 & -10 & 14 & 0 & 0\\ -15 & 21 & -30 & 42 & 0 & 0\end{array}\right) $$
The result is a \$3\$-dimensional array consisting of three \$2\times2\$ slices. We want to determine the maximum of the array, which is \$42\$ here.
An alternative formulation is: Find the maximum of all products that contain exactly one number from each vector.
Of course, we can do all the multiplications and keep track of the maximum. However, it is possible to use only a number of operations that depends linearly on the length of the input sequence (total number of vector entries). This means that we can, for example, process an input of \$13\$ vectors each of size \$37\$ in less than one second on a common computer.
Thus, the problem reads:
Challenge
- "Determine the maximum of the outer product of given integer-valued vectors. Ensure that the algorithm runs in linear time with respect to the length of the input sequence."
- The input contains at least one vector.
- This is code-golf, so shortest answer in bytes wins!
Test cases
We leave the input format to the free decision. In the test cases we use \$,\$ to separate vector entries and \$;\$ to separate vectors. So the example above is coded as: \$\quad 1,2,0;1,3;-5,7\$
- \$0,1,2\qquad ->\qquad 2\$
- \$-2,-1,-2;1,2,3;-1,-2\qquad ->\qquad 12\$
- \$-2,-1;-3,-4;-3,2,1;1,4,-4\qquad ->\qquad 96\$
- \$-1,-2;3,4\qquad ->\qquad -3\$
- \$-1,-2;1,-3,2;1,-1,-2\qquad ->\qquad 8\$
- \$1,0,-2;0,-2,1;-2,1,0;-2,0,1;0,1,-2;1,-2,0;1,0,-2;0,-2,1;-2,1,0;-2,0,1;0,1,-2;1,-2,0;1,0,-2;0,-2,1;-2,1,0;-2,0,1;0,1,-2;1,-2,0;1,0,-2;0,-2,1;-2,1,0;-2,0,1;0,1,-2;1,-2,0;1,0,-2;0,-2,1;-2,1,0;-2,0,1;0,1,-2;1,-2,0;1,-2,0;1,0,1\qquad ->\qquad 1073741824\$