-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmax_pairwise_product.cpp
More file actions
74 lines (66 loc) · 1.83 KB
/
max_pairwise_product.cpp
File metadata and controls
74 lines (66 loc) · 1.83 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
#include <iostream>
#include <vector>
#include <cstdlib>
using std::vector;
using std::cin;
using std::cout;
using std::cerr;
/* long long MaxPairwiseProduct(const vector<int>& numbers) {
long long result = 0;
int n = numbers.size();
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (((long long)numbers[i]) * ((long long)numbers[j]) > result) {
result = ((long long)numbers[i]) * ((long long)numbers[j]);
}
}
}
return result;
} */
long long MaxPairwiseProductFast (const vector<int>& numbers) {
int n = numbers.size();
int indexmax1 = -1;
for (int i = 0; i < n; ++i) {
if ( indexmax1 == -1 || numbers[i] > numbers[indexmax1]) {
indexmax1 = i;
}
}
int indexmax2 = -1;
for (int j = 0; j < n; ++j) {
if ( (j != indexmax1) && (indexmax2 == -1 || (numbers[j] > numbers[indexmax2]))) {
indexmax2 = j;
}
}
return ((long long)(numbers[indexmax1]))* ((long long)(numbers[indexmax2]));
}
int main() {
/* while (true) {
int n = rand() % 10 + 2;
cout << n << "\n";
vector <int> a;
for (int i=0; i<n; ++i) {
a.push_back (rand() % 100000);
cerr << a[i] << "\n";
}
long long slow = MaxPairwiseProduct(a);
long long fast = MaxPairwiseProductFast(a);
if (slow != fast){
cerr<< "WRONG LOOOOOL:" << slow << fast << "\n";
break;
}
else {
cout << "cool" << "\n";
}
} */
int n;
cin >> n;
vector<int> numbers(n);
for (int i = 0; i < n; ++i) {
cin >> numbers[i];
}
// long long result = MaxPairwiseProduct(numbers);
long long resultfast = MaxPairwiseProductFast(numbers);
// cout << result << "\n";
cout << resultfast << "\n";
return 0;
}