forked from ppsirker/dsalgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxProductSubArray.java
More file actions
108 lines (101 loc) · 2.79 KB
/
MaxProductSubArray.java
File metadata and controls
108 lines (101 loc) · 2.79 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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
For problem and solution description please visit the link below
http://www.dsalgo.com/2013/02/maximum-product-subarray.html
*/
package com.dsalgo;
public class MaxProductSubArray
{
public static void main(String[] args)
{
int[] arr =
{ 1, 2, -1, 4, 0, 5, -6, -5, -6, 2, 0, 3, -4, 3, -2, 4, -3 };
int[] returnIndices = new int[2];
long maxProduct = findMaxProduct(arr, returnIndices);
System.out.println("Maximum product " + maxProduct);
System.out.println("Indices " + returnIndices[0] + " - "
+ returnIndices[1]);
}
private static long findMaxProduct(int[] arr, int[] returnIndices)
{
int startIndex = 0;
long maxProduct = 0;
int[] indices = new int[2];
for (int index = 0; index < arr.length; ++index)
{
if (arr[index] == 0 && index >= startIndex)
{
long product = findMaxProductWithoutZero(arr, startIndex,
index - 1, indices);
if (product > maxProduct)
{
maxProduct = product;
returnIndices[0] = indices[0];
returnIndices[1] = indices[1];
}
startIndex = index + 1;
} else if (index == arr.length - 1)
{
long product = findMaxProductWithoutZero(arr, startIndex, index,
indices);
if (product > maxProduct)
{
maxProduct = product;
returnIndices[0] = indices[0];
returnIndices[1] = indices[1];
}
}
}
return maxProduct;
}
private static long findMaxProductWithoutZero(int[] arr, int startIndex,
int endIndex, int[] returnIndices)
{
if (startIndex > endIndex || startIndex < 0 || endIndex >= arr.length)
return 0;
int negativeCount = 0;
int firstNegativeIndex = -1;
int lastNegativeIndex = -1;
for (int index = startIndex; index <= endIndex; ++index)
{
if (arr[index] < 0)
{
negativeCount++;
if (firstNegativeIndex == -1)
firstNegativeIndex = index;
lastNegativeIndex = index;
}
}
if (negativeCount % 2 == 0)
return findMaxProductWithoutNegative(arr, startIndex, endIndex,
returnIndices);
else
{
int[] indices = new int[2];
long maxProduct = findMaxProductWithoutNegative(arr,
firstNegativeIndex + 1, endIndex, indices);
returnIndices[0] = indices[0];
returnIndices[1] = indices[1];
long maxProduct2 = findMaxProductWithoutNegative(arr, startIndex,
lastNegativeIndex - 1, indices);
if (maxProduct2 > maxProduct)
{
maxProduct = maxProduct2;
returnIndices[0] = indices[0];
returnIndices[1] = indices[1];
}
return maxProduct;
}
}
private static long findMaxProductWithoutNegative(int[] arr,
int startIndex, int endIndex, int[] indices)
{
if (startIndex > endIndex || startIndex < 0 || endIndex >= arr.length)
return 0;
long product = 1;
for (int index = startIndex; index <= endIndex; ++index)
product *= arr[index];
indices[0] = startIndex;
indices[1] = endIndex;
return product;
}
}