# Surprised that my answer beats 98.78% of java submissions

• The basic idea is to go through the array with multiplication twice, one forward, one backward, in order to get rid of the impact from negative numbers.
Sorry that I'm not a neat coder.

public class Solution {
public int maxProduct(int[] A){
boolean hasZero = false;
if (A.length==1){
return A[0];
}
else {
int max1 =1;
int maxProduct = -2147483648;
for (int i = 0; i<A.length; i++){
if (i==0){
if (A[i]==0){
hasZero=true;
}
else{
max1 *= A[i];
if (max1 >maxProduct){
maxProduct  = max1;
}
}
}
else {
if (A[i]==0){
max1 = 1;
hasZero =true;
}
else {
max1 *= A[i];
if (max1 >maxProduct){
maxProduct  = max1;
}
}
}
}

max1 =1;
for (int i = A.length-1; i>=0; i--){
if (i==A.length-1){
if (A[i]==0){
hasZero=true;
}
else{
max1 *= A[i];
if (max1 >maxProduct){
maxProduct  = max1;
}
}
}
else {
if (A[i]==0){
max1 = 1;
hasZero =true;
}
else {
max1 *= A[i];
if (max1 >maxProduct){
maxProduct  = max1;
}
}
}
}

if (hasZero && maxProduct<0)
return 0;
else return maxProduct;

}
}

}

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.