TypeScript Max Product Of Three
function maxProductOfThree(a: number[]): number {
const sorted = [...a].sort((x, y) => x - y);
const c = sorted.length;
return Math.max(
sorted[c - 1] * sorted[c - 2] * sorted[c - 3],
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.