-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaximumProductSubarray.java
77 lines (61 loc) · 2.15 KB
/
maximumProductSubarray.java
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package arrays;
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main2 {
public static void main(String[] args) throws Exception {
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
int tc = Integer.parseInt(br.readLine());
while (tc-- > 0) {
int n = Integer.parseInt(br.readLine());
int[] arr = new int[n];
String[] inputLine = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(inputLine[i]);
}
System.out.println(new Solution().maxProduct(arr, n));
}
}
}
// } Driver Code Ends
// Wrong Answer. !!!Wrong Answer
// Possibly your code doesn't work correctly for multiple test-cases (TCs).
// The first test case where your code failed:
// Input:
// 10
// 90 91 -91 91 -91 -90 90 90 -90 -90
// Its Correct output is:
// 404928287208900000
// And Your Code's output is:
// 449942298618103232
//soln is overflow while using long so using BigInteger
// modified kadanes algorithm
class Solution {
// Function to find maximum product subarray
long maxProduct(int[] arr, int n) {
//modified kadanes algoritm
BigInteger maxProduct1 = BigInteger.ZERO;
BigInteger currProduct = BigInteger.ONE;
for(int i =0; i<n;i++){
currProduct = currProduct.multiply(BigInteger.valueOf(arr[i]));
maxProduct1 = currProduct.max(maxProduct1);
int res = currProduct.compareTo(BigInteger.ZERO);
if(res==0) {
currProduct = BigInteger.ONE;
}
}
//reverse bcz in case of [3,-1,25] gives wrong answer
currProduct = BigInteger.ONE;
for(int i= n-1;i>=0;i--){
currProduct = currProduct.multiply(BigInteger.valueOf(arr[i]));
maxProduct1 = currProduct.max(maxProduct1);
int res = currProduct.compareTo(BigInteger.ZERO);
if(res==0) {
currProduct = BigInteger.ONE;
}
}
long answer = maxProduct1.longValue();
return answer;
}
}