Computing elements that result in maximum product efficiently →
In a previous post we were wondering if we can compute the elements (from a given unsorted array of integers) that result in maximum product faster than O(n logn). It turns out we can do a linear scan of the array, keeping track of the largest two and the smallest two elements. One of these pairs would result in the maximum product.
The algorithm is very straight-forward that the code itself is self explanatory.