All solutions here are wrong. Here's the correct one in Java


#1

if (A.size() < 3) {
return 0;
}
if (A.size() == 3) {
return A.get(0) * A.get(1) * A.get(2);
}

    Collections.sort(A);
    int max = Integer.MIN_VALUE;
    
    int firstTwo = A.get(0) * A.get(1);
    int lastTwo = A.get(A.size() - 1) * A.get(A.size() - 2);
    
    for (int i=2; i<A.size()-2; i++) {
        int a = firstTwo * A.get(i);
        int b = lastTwo * A.get(i);
        int c = Math.max(a, b);
        max = Math.max(max, c);
    }
    
    return max;