Fezofchekov
Fezofchekov

Reputation: 69

problem calculating the Maximum Pairwise Product C++

This is my code for the maximum pairwise product. It takes an array of numbers, sorts them to find the first and second maximum numbers, then return the product of them. The code works for small arrays and small values. But it fails with some numbers, and also it epically fails with large numbers such as 10000 and so.

i thought it was a problem with the data type i used, so i defined the data type to be int_64t so it can handle large numbers, but still i get the same wrong results! Can anyone help me with that?

#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>

int64_t MaxPairwiseProduct(const std::vector<int64_t>& numbers) {

    int n = numbers.size();
    if(n<2)
        return;
int maxind1=-1;
for (int i=0; i<=n; i++)
{
    if(maxind1==-1 || numbers[i]>numbers[maxind1])
        maxind1=i;

int maxind2=-1;
for (int j=0; j<=n; j++)
   {

    if(maxind1!=j && maxind2==-1 || numbers[j]>numbers[maxind2])
    maxind2=j;}
int64_t restult=numbers[maxind1]*numbers[maxind2];
    return restult;
}

int main() {
    int n;
    std::cin >> n;
    std::vector<int64_t> numbers(n);
    for (int i = 0; i < n; ++i) {
        std::cin >> numbers[i];
    }

   cout << MaxPairwiseProduct(numbers) << "\n";
    return 0;
}

Upvotes: 0

Views: 3756

Answers (2)

v01d
v01d

Reputation: 189

(EDITED as per comments from others)

Besides what @dyukha noted - for (int j=0; j<=n; j++) - also: maxind2 could be negative in the code below, which is used to subscript the vector.

int maxind1=-1;
for (int i=0; i<=n; i++)
{
    if(maxind1==-1 || numbers[i]>numbers[maxind1])
        maxind1=i;

int maxind2=-1;
for (int j=0; j<=n; j++)
   {

    if(maxind1!=j && maxind2==-1 || numbers[j]>numbers[maxind2])

Solution is to change the precedence in logical expression inside if to:

if(maxind1!=j && (maxind2==-1 || numbers[j]>numbers[maxind2]))

Upvotes: 3

Fezofchekov
Fezofchekov

Reputation: 69

SOLVED Thank you all for helping me out, it finally worked properly. The source of the problem was that i didn't use parenthesis in the second if conditions, and the condition of the for loop (use < instead of <=) Code after solving the errors:

#include <cstdlib>
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>

int64_t MaxPairwiseProduct(const std::vector<int64_t>& numbers) {

    int n = numbers.size();


int maxind1=-1;
for (int i=0; i<n; i++)
{
    if(maxind1==-1 || numbers[i]>numbers[maxind1])
        maxind1=i;
}
int maxind2=-1;
for (int j=0; j<n; j++)
   {

    if(j!=maxind1 && (maxind2==-1 || numbers[j]>numbers[maxind2]))
    maxind2=j;}
int64_t restult=numbers[maxind1]*numbers[maxind2];
    return restult;
}

int main() {
    int n;
    std::cin >> n;
    std::vector<int64_t> numbers(n);
    for (int i = 0; i < n; ++i) {
        std::cin >> numbers[i];
    }

   cout << MaxPairwiseProduct(numbers) << "\n";
return 0;}

Upvotes: 1

Related Questions