GithubHelp home page GithubHelp logo

Problem about algorithmic-toolbox HOT 2 OPEN

mablatnik avatar mablatnik commented on July 24, 2024
Problem

from algorithmic-toolbox.

Comments (2)

maayaankk avatar maayaankk commented on July 24, 2024

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;

}

from algorithmic-toolbox.

mohammedsaad01 avatar mohammedsaad01 commented on July 24, 2024

Wrong answers

from algorithmic-toolbox.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.