Array consisting all 0's
Array having negative values(it's bit tricky since it's about finding product as two negative ints give positive value)
public static int LargestProduct(int[] arr) { //returning arr[0] if it has only one element if (arr.Length == 1) return arr[0]; int product = 1; int maxProduct = Int32.MinValue; for (int i = 0; i < arr.Length; i++) { //this block store the largest product so far when it finds 0 if (arr[i] == 0) { if (maxProduct < product) { maxProduct = product; } product = 1; } else { product *= arr[i]; } } if (maxProduct > product) return maxProduct; else return product; }
-1 * -15 * -30
是-450
。此外,这应该是对那个答案的评论。 - Anthony Pegram