Easy c++ basic solution


#1

//check between the intervals of 0’s and ans will either be maximum positive or if it is negative then the number which is just occuring behind the 0 divided by the maximum negative number(this will divide the negative product by minimum value) as we will exclude that in order to get the maximum positive)
corner cases can be included by flag variable
int Solution::maxProduct(const vector &A) {
int b[A.size()];
b[0]=A[0];
int flag=0;
for(int i=1;i<A.size();i++)
{
if(A[i-1]==0)
{
b[i]=A[i];
flag=1;
}
else
b[i]=b[i-1]*A[i];
}
int ans=-INT_MAX;
int maxi=-INT_MAX;
int mini=INT_MAX;
for(int i=0;i<A.size();i++)
{
while(b[i]!=0 && i<A.size())
{
maxi=max(maxi,b[i]);
mini=min(mini,A[i]);
i++;
}
if(b[i-1]!=mini)
maxi=max(maxi,(b[i-1]/mini));
ans=max(ans,maxi);
}
if(flag==1)
ans=max(ans,0);
return ans;

}