My logic behind the code and simple o(n) code


#1

Comment body goes here.
in this question our idea is to maintain two variable mincount, maxcount, so that we can max and min value uptill index i, if we encounter negative number then we will swap these values so that our max value become min and min become max.
*** our main idea is just to maintain max and min no upto index i…
for eg::
a = [2,3,-2,4]
maxcount = [2,6,-2,4]
mincount = [2,3,-12, -48]
and we are maintaing globalcount to get the max value out of all the values in maxcount.
indent preformatted text by 4 spaces
int Solution::maxProduct(const vector &A) {
int maxCount = A[0];
int minCount = A[0];
int globalCount = A[0];
for(int i=1;i<A.size();i++){
if(A[i] < 0)
swap(maxCount, minCount);
maxCount = max(A[i], maxCountA[i]);
minCount = min(A[i], minCount
A[i]);
globalCount = max(globalCount, maxCount);
}
return globalCount;
}
indent preformatted text by 4 spaces