public static int[] getProdArray(int[] data){
int numberOfZeros = 0, prod = 1;
for(int i=0;i<data.length;i++){
if(data[i]!=0)
prod *= data[i];
else
numberOfZeros++;
if(numberOfZeros>=2){
prod = 0;
break;
}
}
int[] result = new int[data.length];
for(int i=0;i<data.length;i++){
if(data[i]!=0)
result[i] = prod / data[i];
else
result[i] = prod;
}
return result;
}
No comments:
Post a Comment