We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

The problem with this implementarion is the complexity. You are doing it in O(n²), while you can do this in O(n). Even sorting the list have a better complexity O(n*log(n)).
An example of a patological case is the list [5, 5, 5, 5, 5 ,6] or something like that.

## Find the Runner-Up Score!

You are viewing a single comment's thread. Return to all comments →

The problem with this implementarion is the complexity. You are doing it in O(n²), while you can do this in O(n). Even sorting the list have a better complexity O(n*log(n)). An example of a patological case is the list [5, 5, 5, 5, 5 ,6] or something like that.

gtorrecilla. What do you think of this? Its not the best by line count, but I believe it finds the 2nd largest within O(n).

You're doing a bit more work than you need to. Instead of iterating over a range, just iterate over the number list.

I think this is the best approach too to stay within O(n). My solution was something similar:

You don't need that tmp var, just reorder your first condition:

works!but a little improvement is needed