-
Notifications
You must be signed in to change notification settings - Fork 16
/
MaximumProductDifferenceBetweenTwoPairs.java
49 lines (44 loc) · 1.54 KB
/
MaximumProductDifferenceBetweenTwoPairs.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
// https://leetcode.com/problems/maximum-product-difference-between-two-pairs
// T: O(N)
// S: O(1)
public class MaximumProductDifferenceBetweenTwoPairs {
public int maxProductDifference(int[] nums) {
Pair largest = getLargestAndSecondLargest(nums);
Pair smallest = getSmallestAndSecondSmallest(nums);
return largest.a * largest.b - smallest.a * smallest.b;
}
private Pair getLargestAndSecondLargest(int[] array) {
int largest = Integer.MIN_VALUE, secondLargest = Integer.MIN_VALUE;
for (int element : array) {
if (element > largest) {
int temp = largest;
largest = element;
secondLargest = temp;
} else if (element > secondLargest) {
secondLargest= element;
}
}
return new Pair(largest, secondLargest);
}
private Pair getSmallestAndSecondSmallest(int[] array) {
int smallest = Integer.MAX_VALUE, secondSmallest = Integer.MAX_VALUE;
for (int element : array) {
if (element < smallest) {
int temp = smallest;
smallest = element;
secondSmallest = temp;
} else if (element < secondSmallest) {
secondSmallest = element;
}
}
return new Pair(smallest, secondSmallest);
}
private static final class Pair {
private final int a;
private final int b;
private Pair(int a, int b) {
this.a = a;
this.b = b;
}
}
}