# Cpp 4ms solution

• ``````class Solution {
public:
int maxProduct(vector<int>& nums) {
int start = 0, max_prd = nums[0], n = nums.size();
while(start<n){
if(nums[start]==0){ /*For the case that the maximum product is zero*/
max_prd = max(max_prd,0);
start++;
continue;
}
int end = start;
int prd = 1;
while(end<n && nums[end]!=0){
prd *= nums[end];
end++;            /*picking a subarray that is between two zeroes*/
}
if(prd>0||end==start+1) max_prd = max(max_prd,prd);
/*prd = total product of this subarray*/
/*if prd > 0, prd is the largest continuous product of this subarray*/
/*if this subarray only contain 1 element, the largest continuous product is itself*/
else{/*if prd<0, and the subarray contains more than one number, find the first and last negative element*/
int i = start,tmp = prd;
while(tmp<0){
tmp /= nums[i];
i++;
}
max_prd = max(max_prd,tmp);
i = end-1;
tmp = prd;
while(tmp<0){
tmp /= nums[i];
i--;
}
max_prd = max(max_prd,tmp);
}
start = end;
}
return max_prd;
}
};``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.