The following code is to find the Maximum Pairwise Product (MPP) which is:
You're given an array of N integers and a number K. The maximum K-product of the array is the maximum product of any K length subsequence of the array. For example, the maximum 2-product of the array [-5, 3, 4, -6] is 30 because the product of the subsequence [-5, -6] is 30 and it is impossible to achieve a larger subsequence products.
The code below calculates the MPP for every input except for "90000 and 100000
" which must give an output 9,000,000,000, but I got 410065408:
int MaxPairwiseProduct(const vector<int>& numbers) {
int result = 0;
int n = numbers.size();
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (numbers[i] * numbers[j] > result && numbers[i] * numbers[j] % 2 == 0) {
result = numbers[i] * numbers[j];
}
}
}
return result;
}
Sadly, these pages (1 and 2) did not help me with my request.