Example – A = [-3,-5, 1,2,-1,5,3]
Output – 15 (-3,-5) or (3,5)
- Naive O(n^2) – Check for each and every pair and find the maximum product.
- Sorting O(nlogn) – Sort the array and get the values of the maximum, second maximum, minimum and second minimum. Evaluate maximum with second max, and minimum with second min. Find the max of the two results.
- Linear O(n) – Get the maximum, second maximum, minimum, second minimum on single traversal of the array and evaluate the max out of the two.