-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathMaxProductSubArray.java
70 lines (46 loc) · 1.81 KB
/
MaxProductSubArray.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
/*
Maximum Product Subarray Problem
Given an interger array, find the subarray that has the maximum product of its elements.
The program should return the maximum product of elements among all possible subarrays.
*/
/*
Test Cases:
Input: { -6, 4, -5, 8, -10, 0, 8 }
Output: 1600
Explanation: The maximum product subarray is {4, -5, 8, -10} having product 1600
Input: { 40, 0, -20, -10 }
Output: 200
Explanation: The maximum product subarray is {-20, -10} having product 200
*/
class Main
{
// Function to return the maximum product of a subarray of a given array
public static int findMaxProduct(int[] A){
// base case
if (A.length == 0) {
return 0;
}
// maintain two variables to store the maximum and minimum product
// ending at the current index
int max_ending = A[0], min_ending = A[0];
// to store the maximum product subarray found so far
int max_so_far = A[0];
// traverse the given array
for (int i = 1; i < A.length; i++){
int temp = max_ending;
// update the maximum product ending at the current index
max_ending = Integer.max(A[i], Integer.max(A[i] * max_ending,
A[i] * min_ending));
// update the minimum product ending at the current index
min_ending = Integer.min(A[i], Integer.min(A[i] * temp,
A[i] * min_ending));
max_so_far = Integer.max(max_so_far, max_ending);
}
// return maximum product
return max_so_far;
}
public static void main(String[] args) {
int[] A = { -6, 4, -5, 8, -10, 0, 8 };
System.out.print("The maximum product of a subarray is " + findMaxProduct(A));
}
}