forked from iamAnki/CPP-Programs-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximum pairwise product.cpp
More file actions
42 lines (38 loc) · 959 Bytes
/
Maximum pairwise product.cpp
File metadata and controls
42 lines (38 loc) · 959 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
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <vector>
#include <algorithm>
using std:: vector;
using std:: cin;
using std:: cout;
long long MaxPairwiseProduct(const vector<long long> &numbers)
{
long long max_product = 0;
long long n = numbers.size();
int i,j ;
int max_index1 = -1, max_index2 = -1;
for (i = 0; i < n; ++i)
{
if ((max_index1 == -1) || numbers[i] > numbers[max_index1])
max_index1 = i;
}
for (j = 0; j < n; ++j)
{
if ((j != max_index1) && ((max_index2 == -1) || (numbers[j] > numbers[max_index2])))
max_index2 = j;
}
max_product = numbers[max_index1] * numbers[max_index2];
return max_product;
}
//if(maxind1!=j && (maxind2==-1 || numbers[j]>numbers[maxind2]))
int main()
{
int n;
cin >> n;
vector<long long> numbers(n);
for (int i = 0; i < n; ++i)
{
cin >> numbers[i];
}
cout << MaxPairwiseProduct(numbers)<< "\n";
return 0;
}