Count negatives and keep track of negative number with least absolute value .
if ( $ a [ $ i ] < 0 ) { $ count_neg ++ ; $ max_neg = max ( $ max_neg , $ a [ $ i ] ) ; } $ prod = $ prod * $ a [ $ i ] ; }