-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem 628.java
31 lines (28 loc) · 989 Bytes
/
Problem 628.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
/* Given an integer array nums, find three numbers whose product is maximum and return the maximum product.
case 1 - product of 3 maximum numbers
case 2 - product of 2 min (-ve numbers) and 1 max (+ve) */
class Solution {
public int maximumProduct(int[] nums) {
int max1 = Integer.MIN_VALUE, max2 = Integer.MIN_VALUE, max3 = Integer.MIN_VALUE;
int min1 = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE, n;
for(int i=0;i<nums.length;i++)
{
n = nums[i];
if (n > max1) {
max3 = max2;
max2 = max1;
max1 = n;
} else if (n > max2) {
max3 = max2;
max2 = n;
} else if (n > max3)
max3 = n;
if (n < min1) {
min2 = min1;
min1 = n;
} else if (n < min2)
min2 = n;
}
return Math.max(max1*max2*max3, max1*min1*min2);
}
}