Erlang Max Product Of Three
-module(max_product_of_three).
-export([max_product_of_three/1]).
max_product_of_three(A) ->
Sorted = lists:sort(A),
N = length(Sorted),
Top3 = lists:nth(N, Sorted) * lists:nth(N - 1, Sorted) * lists:nth(N - 2, Sorted),
TwoLowOneHigh = lists:nth(1, Sorted) * lists:nth(2, Sorted) * lists:nth(N, Sorted),
max(Top3, TwoLowOneHigh).
This checks the useful extremes, because the best product can come from either the three largest numbers or two negatives plus one large positive.