The maximum product of the subsequence of non-zero numbers is either the product of all numbers (if there is an even number of negative numbers), or it is greater than the product of all numbers after the first negative number, and the product of all numbers to the last negative number.
This gives you an O (N) solution: break the sequence into runs of nonzero numbers and apply the rule in the first paragraph to each. Select the maximum value.
C-like Python code for this:
def prod(seq, a, b): r = 1 for i in xrange(a, b): r *= seq[i] return r def maxprodnon0(seq, a, b): firstneg = -1 negs = 0 for i in xrange(a, b): if seq[i] >= 0: continue negs += 1 if firstneg < 0: firstneg = i lastneg = i if negs % 2 == 0: return prod(seq, a, b) return max(prod(seq, firstneg + 1, b), prod(seq, a, lastneg)) def maxprod(seq): best = 0 N = len(seq) i = 0 while i < N: while i < N and seq[i] == 0: i += 1 j = i while j < N and seq[j] != 0: j += 1 best = max(best, maxprodnon0(seq, i, j)) i = j return best for case in [2,5,-1,-2,-4], [1,2,0,-4,5,6,0,7,1], [1,2,0,-4,5,6,-1,-1,0,7,1]: print maxprod(case)