If current element is negative , then new value of maxval is previous minval * arr [ i ] and new value of minval is previous maxval * arr [ i ] . Before updating maxval , store its previous value in prevMax to be used to update minval .
else if ( $ arr [ $ i ] < 0 ) { $ prevMax = $ maxval ; $ maxval = $ minval * $ arr [ $ i ] ; $ minval = $ prevMax * $ arr [ $ i ] ; }