Simple 3 line answer in C++


#1
int Solution::maxp3(vector<int> &A) {
    // find 5 numbers chaiye max, max, max, min, min
    int n = A.size();
    if(n < 3) {
        return 0;
    }
    sort(A.begin(), A.end());
    return max(A[0]*A[1]*A[n-1]      , A[n-1]* A[n-2]*A[n-3]);
}

#2

Hey, can you explain your return statement? I didn’t get it.
Means I did everything same but my failed approach’s looked like below:

int Solution::maxp3(vector &A) {
if(A.size()<3)
return 0;
sort(A.begin(), A.end());
int l = A.size();
return A[l-1] * A[l-2] * A[l-3];
}

Can you tell me what I just missed out


#3

if the input is : -6 -5 1 2 3
then your result will be 321 but correct output is 3*-6*-5.
you are not including the case when product of two negative numbers is greater than the product of 2nd highest and 3rd highest.