Simple CPP solution with constant space


#1

Keep max and min value at every index .

int Solution::maxProduct(const vector &A) {
int res,minhere,maxhere;
res=minhere=maxhere=A[0];

for(int i=1;i<A.size();i++)
{
    int temp=maxhere;
    maxhere=max(max(minhere*A[i],maxhere*A[i]),A[i]);
    minhere=min(min(minhere*A[i],temp*A[i]),A[i]);
    if(maxhere>res)res=maxhere;
}
return res;

}

Time Complexity: O(n)
Space Complexity: O(1)