Fast Solution: Maximum Pairwise Product
Fast solution for the Maximum Pairwise Product Problem.
We'll cover the following
Fast algorithm
In search of a faster algorithm, we play with small examples like . Eureka—it suffices to multiply the two largest elements of the array: and ! Since we need to find the largest and the second-largest elements, we need only two scans of the sequence. During the first scan, we find the largest element. During the second scan, we find the largest element among the remaining ones by skipping the element found at the previous scan.
:
for from to :
if :
for from to :
if and :
return
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.