forked from Harshita-Kanal/Data-Structures-and-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaximum_pairwise_product.cpp
More file actions
32 lines (28 loc) · 817 Bytes
/
maximum_pairwise_product.cpp
File metadata and controls
32 lines (28 loc) · 817 Bytes
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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
long long MaxPairwiseProduct(vector<long int>& numbers) {
long long max_product = 0;
int n = numbers.size();
// for (int first = 0; first < n; ++first) {
// for (int second = first + 1; second < n; ++second) {
// max_product = max(max_product,
// numbers[first] * numbers[second]);
// }
// }
sort(numbers.begin(), numbers.end());
max_product = (long long int)(numbers.back()) * ((long long int)(numbers[n-2]));
return max_product;
}
int main() {
int n;
cin >> n;
vector<long int> numbers(n);
for (int i = 0; i < n; ++i) {
cin >> numbers[i];
}
long long int max_product = MaxPairwiseProduct(numbers);
cout << max_product << "\n";
return 0;
}