C# Max Product Of Three
static long MaxProductOfThree(int[] a)
{
var sorted = (int[])a.Clone();
Array.Sort(sorted);
var c = sorted.Length;
return Math.Max(
(long)sorted[c - 1] * sorted[c - 2] * sorted[c - 3],
(long)sorted[0] * sorted[1] * sorted[c - 1]
);
}
This checks the useful extremes, because the best product can come from either the three largest numbers or two negatives plus one large positive.