-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxProductSubarray.java
More file actions
46 lines (38 loc) · 1.21 KB
/
MaxProductSubarray.java
File metadata and controls
46 lines (38 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/* Given an array that contains both positive and negative integers, find
* the product of the maximum product subarray. Expected Time complexity is
* O(n) and only O(1) extra space can be used.
* http://www.geeksforgeeks.org/maximum-product-subarray/
*/
import java.io.*;
import java.util.*;
public class MaxProductSubarray {
public static void main(String[] args) {
//int[] arr = {6, -3, -10, 0, 2};
//int[] arr = {-1, -3, -10, 0, 60};
int[] arr = {-2, -3, 0, -2, -40};
solve(arr);
}
public static void solve(int[] arr) {
int prod = 1;
int max = Integer.MIN_VALUE;
int last_neg_prod = 0;
for(int i=0; i<arr.length; i++) {
prod *= arr[i];
if(prod < 0) {
if(last_neg_prod < 0) {
prod = prod * last_neg_prod;
last_neg_prod = 0;
} else {
last_neg_prod = prod;
prod = 1;
}
} else if(prod == 0) {
last_neg_prod = 0;
prod = 1;
}
if(max < prod)
max = prod;
}
System.out.println("Max: " + max);
}
}