Given an array a contains integers, return
the maximum consecutive multiplication
Example:
int[] a = {2, -2, -3, 4, -5, 6} ;
return 360 (-3 * 4 * -5 * 6)
code
Code
1 // Use two variable max and min to record the max positive multiplication
2 // and min negetive multiplication at current position
3 public long Multiplication(long[] a)
4 {
5 long max = 1; // max positive multiplication at current position
6 long min = 1; // min negetive multiplication at current position
7 long r = 1; // result, max multiplication totally
8
9 for (int i = 0; i < a.Length; i++)
10 {
11 if (a[i] > 0)
12 {
13 max *= a[i];
14 min = Math.Min(min * a[i], 1);
15 }
16 else if (a[i] == 0)
17 {
18 max = 1;
19 min = 1;
20 }
21 else // i < 0
22 {
23 long temp = max;
24 max = Math.Max(min * a[i], 1);
25 min = temp * a[i];
26 }
27 r = Math.Max(r, max);
28 }
29 return r;
30 }
31