This accounts for negative numbers in the array. The sort will run in (n log n) time, everything else is constant time.
a2 = [1,2,3,4,5,6,7,8,9,-11,-32,32]
def max_product(a):
# returns the largest product of 2 numbers from the given array
# sort array - return max(a[0] * a[1], a[-1], a[-2])
if len(a) < 2:
return None
a_sorted = sorted(a)
print a_sorted, a_sorted[0] * a_sorted[1], a_sorted[-2] * a_sorted[-1]
return max(a_sorted[0] * a_sorted[1], a_sorted[-2] * a_sorted[-1])
if __name__ == '__main__':
print max_product(a2)