Elixir Max Product Of Three
defmodule MaxProductOfThree do
def max_product_of_three(a) do
sorted = Enum.sort(a)
c = length(sorted)
max(
Enum.at(sorted, c - 1) * Enum.at(sorted, c - 2) * Enum.at(sorted, c - 3),
Enum.at(sorted, 0) * Enum.at(sorted, 1) * Enum.at(sorted, c - 1)
)
end
end
This checks the useful extremes, because the best product can come from either the three largest numbers or two negatives plus one large positive.