0

smaller time complexity. Find number without array

May I ask, is there an algorithm to find the second largest number and the second smallest number? And with the least number of comparisons and the least time complexity? It's one of the exercises on the net. I did it via array, but it seems to use only if else and def, because they have less time complexity. That seems too complicated to me though. Because I would have to do 6 comparisons for each element. When I wouldn't know which number has what value.

20th Nov 2021, 8:45 PM
Jeanie Snow
Jeanie Snow - avatar
2 Respostas
+ 5
Only an idea. Idk if it's helpfull. nums = {3,8,5,0,4} nums.remove(max(nums)) nums.remove(min(nums)) print(min(nums),max(nums)) # prints ==> 3 5
20th Nov 2021, 10:26 PM
Coding Cat
Coding Cat - avatar
0
Thanks. I solve it different way, but this is better
21st Nov 2021, 9:09 PM
Jeanie Snow
Jeanie Snow - avatar