Solution in O(N) time in c++


#1

int Solution::maxProduct(const vector &A) {
int maximum = 1;
int maxtill = 1;
int mintill = 1;
int flag = 0;
for(int i=0;i<A.size();i++){
if(A[i]<0){
int temp = maxtill;
maxtill = max(1,mintillA[i]);
mintill = temp * A[i];
}else if(A[i] == 0){
maxtill = 1;
mintill = 1;
}else{
flag = 1;
maxtill = maxtill
A[i];
mintill = min(1,mintill*A[i]);
}
maximum = max(maxtill,maximum);

}
if(maximum == 1 && flag == 0){
    return 0;
}
return maximum;

}