Java Max Product Of Three
import java.util.Arrays;
public class Solution {
public static long maxProductOfThree(int[] a) {
int[] sorted = a.clone();
Arrays.sort(sorted);
int c = sorted.length;
long p1 = (long) sorted[c - 1] * sorted[c - 2] * sorted[c - 3];
long p2 = (long) sorted[0] * sorted[1] * sorted[c - 1];
return Math.max(p1, p2);
}
}
This checks the useful extremes, because the best product can come from either the three largest numbers or two negatives plus one large positive.