How to find the maximum product of two elements in a list?
<p>I was trying out a problem on hackerrank contest for fun, and there came this question.
I used itertools for this, here is the code:</p>

<pre><code>import itertools

l = []

for _ in range(int(input())):
    l.append(int(input()))


max = l[0] * l[len(l)-1]

for a,b in itertools.combinations(l,2):
    if max &lt; (a*b):
        max = (a*b)
print(max)
</code></pre>

<p>Is their any other efficient way than this? As I am getting time out error on some test cases which I cant access (as its a small contest).</p>

<p>Iterate over the list and find the following:</p>

<p>Largest Positive number(a)</p>

<p>Second Largest Positive number(b)</p>

<p>Largest Negative number(c)</p>

<p>Second Largest Negative number(d)</p>

<p>Now, you will be able to figure out the maximum value upon multiplication, either <code>a*b</code> or <code>c*d</code></p>

python python-2.7 python-3.x itertools