Since all the elements in the array are integers. If there are even number of negative elements, the maximum product will be the multiplication of all the elements. If there are odd number of negative elements, we can just compare the products of the following two:
- multiplication of all the elements until we find the last negative element.
- multiplication of all the elements starting after the first negative element.
Both cases contain even number of negative elements and the result will be positive. Is this a correct analysis?
You're on the right track but make sure you handle the case in which arrays contains zeros.
I think in exactly the same way. But I got caught for a lot of corner cases when zeros are involved. Especially when you have zeros at the beginning and ending of an array. You end up writing more lines of codes than the common way.
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.