In [1]:
# Maximum pairwise product
"""
Problem
Given a sequence of non-negative integers a0,…,an−1, find the maximum pairwise product, that is,
the largest integer that can be obtained by multiplying two different elements from the sequence
(or, more formally, max0≤i≠j≤n−1aiaj).
Different elements here mean ai and aj with i≠j (it can be the case that ai=aj).
Input format
The first line of the input contains an integer n.
The next line contains n non-negative integers a0,…,an−1 (separated by spaces).
Constraints
2≤n≤2⋅105; 0≤a0,…,an−1≤105.
Output format
Output a single number — the maximum pairwise product.
"""
import heapq
def maximum_pairwise_product(l):
max_1, max_2 = heapq.nlargest(2, l)
return max_1 * max_2
In [2]:
l = [1, 2, 3, 10, 14, 14, 14, 7, 6, 5]
maximum_pairwise_product(l)
Out[2]: