coder0708
BAN USERsurely ur solution will work........
but the point is for getting the third max again you need to maintain three variables....same for four.....and so on and for each complexity is of o(n)....but once you have structure like as explained in the previous solution after getting first max...all you will need is 0(log n) ...
A tree approach would be appropriate!!!
eg. 10 , 3, 4 ,17 , 12 31 ,5
like
(31)
(17) (31)
(10) (17 ) (31) (5)
(10 ,3 ) (4, 17) (12, 31) (5) find max element from bottom to top.
here 31 is the largest element.
noe replace the 31 with the some other value i.e. very small value says -INF
(17)
(17) (12)
(10) (17 ) (12) (5)
(10 ,3 ) (4, 17) (12, -INF) (5)
This time watever you get will be the second maximum.
- coder0708 June 19, 2012