Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem #6

Open
Hemantsharma99-developer opened this issue May 20, 2020 · 2 comments
Open

Problem #6

Hemantsharma99-developer opened this issue May 20, 2020 · 2 comments

Comments

@Hemantsharma99-developer

Wrong answers

@maayaankk
Copy link

Just add this code, the previous code exceeds the time

#include
#include
#include

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

long long MaxPairwiseProduct(const vector &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 &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_index2])))
        max_index2 = j;

return ((long long)(numbers[max_index1])) * numbers[max_index2];

}

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

long long result = MaxPairwiseProductFast(numbers);
cout << result << "\n";
return 0;

}

@mohammedsaad01
Copy link

Wrong answers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants