Highest Product

Given an array A, of N integers A.

Return the highest product possible by multiplying 3 numbers from the array.

NOTE: Solution will fit in a 32-bit signed integer.


Input Format:

The first and the only argument is an integer array A.

Output Format:

Return the highest possible product.

Constraints:

1 <= N <= 5e5

Example:

Input 1:
A = [1, 2, 3, 4]

Output 1:
24

Explanation 1:
2 * 3 * 4 = 24

Input 2:
A = [0, -1, 3, 100, 70, 50]

Output 2:
350000

Explanation 2:
70 * 50 * 100 = 350000
Method:

Two possibilities: 
1. biggest three positive numbers
2. smallest two negative numbers * biggest number

Solution:

Time: O(nlogn)
Space: O(1)

public class Solution {
    public int maxp3(ArrayList<Integer> A) {
        Collections.sort(A, Collections.reverseOrder());
        int p = 1;
        for (int i = 0; i < 3; i ++) {
            p *= A.get(i);
        }
        int m = p;
        if (A.get(A.size() - 1) < 0 && A.get(A.size() - 2) < 0) {
            m = A.get(A.size() - 1) * A.get(A.size() - 2) * A.get(0);
        }
        return Math.max(p, m);
    }
}