This question is pretty different from Maximum Sum of Subarray. The reason is that a negative value in array will revolute the previous **maximum** product to the **minimum** and vice versa. So both temporary results should be kept.

```
class Solution {
public:
int maxProduct(int A[], int n) {
if (n==1) return A[0];
int max_temp=0,min_temp=0,result=std::numeric_limits<int>::min();
for (int i=0;i<n;i++){
if (A[i]>0){
max_temp=max(max_temp*A[i],A[i]);//Assign the Temporary Maximum Product
min_temp=min_temp*A[i];
}
else if (A[i]==0){
max_temp=0;
min_temp=0;
}
else{//Negative value, **maximum** and **minimum** products will be revoluted.
int temp=max_temp;
max_temp=min_temp*A[i];
min_temp=min(temp*A[i],A[i]);//Assign the Temporary Minimum Product
}
result=max(max_temp,result);
}
return result;
}
};
```