-
Notifications
You must be signed in to change notification settings - Fork 1
/
2-max_pairwise_product.cpp
45 lines (38 loc) · 1.12 KB
/
2-max_pairwise_product.cpp
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
#include <bits/stdc++.h>
using namespace std;
long long MaxPairwiseProductSuperFast(const std::vector<int>& numbers){
int poslarge1 = -1, poslarge2 = -1;
int neglarge1 = 0, neglarge2 = 0;
int n = numbers.size();
if(n < 2){
return 0;
}
if(n == 2){
return ((long long)numbers[0]*numbers[1]);
}
for (int i = 0; i < n; ++i){
if(numbers[i] > poslarge1){
poslarge2 = poslarge1;
poslarge1 = numbers[i];
}
else if(numbers[i] > poslarge2)
poslarge2 = numbers[i];
if(numbers[i] < 0 && abs(numbers[i]) > abs(neglarge1)){
neglarge2 = neglarge1;
neglarge1 = numbers[i];
}
else if(numbers[i] < 0 && abs(numbers[i]) > abs(neglarge2))
neglarge2 = numbers[i];
}
return (std::max((long long)poslarge1*poslarge2, (long long)neglarge1*neglarge2));
}
int main() {
int n;
std::cin >> n;
std::vector<int> numbers(n);
for (int i = 0; i < n; ++i) {
std::cin >> numbers[i];
}
std::cout << MaxPairwiseProductSuperFast(numbers) << "\n";
return 0;
}