Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

i am not getting this answer of maxpairwiseproduct

+2 votes
asked May 22, 2021 by AYUSH MORE (150 points)
#include <cstdlib>
#include <iostream>
#include <vector>

using std::vector;
using std::cin;
using std::cout;

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])*numbers[j])>result) {
        result = ((long long)(numbers[i]))*numbers[j];  
      }
    }  
  }
  return result;
}

long long MaxPairwiseProductFast(const vector<int>& numbers)
 {int n = numbers.size();
 
 int max_index1 = -1;
 for(int i = 0;i<n;++i)
   if ((max_index1 == -1) || (numbers[i] > numbers[max_index1]))
    max_index1 = i;
    
 int max_index2 = -1;
 for(int j = 0;j<n;++j)
   if ((j !=max_index1) && (max_index2 == -1) || (numbers[j] > numbers[max_index1]))
    max_index2 = j;
    cout <<max_index1 << ' ''<<max_index2 <<"\n";
 }
 
 int main() {
   while (true) {
    int n = rand() % 4 + 2;
    cout << n << "\n";
    vector<int>a;
     for (int i = 0;i<n;++i) {
       a.push_back(rand()% 10);
     }
     for(int i=0 ; i<n ; ++i) {
       cout <<a[i]<< ' ';
     }
     cout <<"\n";
     long long res 1 =MaxPairwiseProduct(a);
     long long res 2 =Fast(a);
     if (res1 !=res2) {
      cout<< "wrong answer: "<< res1 << ' ' << res2 << "\n";
      break;
     }
    else {
        cout << "OK\n";
    }
   }
   int n;
   cin >> n;
   vector<int> numbers(n);
   for (int i=0;i<n; ++i) {
     cin >> numbers[i];  
   }
   long long result = MaxPairwiseProductFast(numbers);
   cout << result << "\n";  
   return = 0;
   }
 }

1 Answer

0 votes
answered May 24, 2021 by Peter Minarik (86,160 points)

Your code has plenty of errors. Just try to run and you'll see them and can fix them one by one:

  • line 34: extra single quote, remove it.
  • line 14: extra closing parenthesis after numbers[i], remove it.
  • line 49: space in variable name is not allowed
  • line 50: space in variable name is not allowed
  • line 50: the method Fast() does not exist. Did you mean MaxPairwiseProductFast()?
  • line 67: setting up the return value to 0 is return 0; not return = 0;
  • extra (unneeded) closing brace in the last line. Remove it.

After you fix these, the code runs. (I didn't have the time to check if it does the right thing -- not even sure what your code is supposed to do -- but the program starts, it has some output and it does not crash.)

#include <iostream>
#include <vector>

using std::vector;
using std::cin;
using std::cout;

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] * numbers[j]) > result) {
                result = ((long long)(numbers[i]))*numbers[j];
            }
        }
    }
    return result;
}

long long MaxPairwiseProductFast(const vector<int>& numbers)
{int n = numbers.size();

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

    int max_index2 = -1;
    for(int j = 0;j<n;++j)
        if ((j !=max_index1) && (max_index2 == -1) || (numbers[j] > numbers[max_index1]))
            max_index2 = j;
    cout <<max_index1 << ' '<<max_index2 <<"\n";
}

int main() {
    while (true) {
        int n = rand() % 4 + 2;
        cout << n << "\n";
        vector<int>a;
        for (int i = 0;i<n;++i) {
            a.push_back(rand()% 10);
        }
        for(int i=0 ; i<n ; ++i) {
            cout <<a[i]<< ' ';
        }
        cout <<"\n";
        long long res1 = MaxPairwiseProduct(a);
        long long res2 = MaxPairwiseProductFast(a);
        if (res1 !=res2) {
            cout<< "wrong answer: "<< res1 << ' ' << res2 << "\n";
            break;
        }
        else {
            cout << "OK\n";
        }
    }
    int n;
    cin >> n;
    vector<int> numbers(n);
    for (int i=0;i<n; ++i) {
        cin >> numbers[i];
    }
    long long result = MaxPairwiseProductFast(numbers);
    cout << result << "\n";
    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...