So I have to find the second largest number from list. I am doing it through simple loops.
My approach is to divide a list into two parts and then find the largest n
O(n^2) algorithm:
In [79]: alist=[-45,0,3,10,90,5,-2,4,18,45,100,1,-266,706] In [80]: max(n for n in alist if n!=max(alist)) Out[80]: 100
O(n) algorithm:
In [81]: alist=[-45,0,3,10,90,5,-2,4,18,45,100,1,-266,706] In [82]: M = max(alist) In [83]: max(n for n in alist if n!=M) Out[83]: 100