Find 2nd largest value in given array- Interview Python Question

preview_player
Показать описание

Рекомендации по теме
Комментарии
Автор

Done = sorted(array1)
print(Done[-2])

ChhayaKamble-ftfd
Автор

The code can be improved further. Just use sorted() function in list and get the second last index with -2

ChhayaKamble-ftfd
Автор

Array1.sort()
Print(-1)
#done in two lines

praveenrockcse
Автор

Create a number question with 5 rows and make all options as unique values also extract first highest and second highest number in next follow up question....plz programm this in python

faaiqrahman
Автор

It can be done in O(n) time complexity using two separate for loops.

max = float("-inf'0
for i in range (0, len(array1)):
if (array1[i] > max):
max = array1[i]


max2 = float("-inf")
for i in range(0, len(array1)):
if (array[i] > max2 and array[i] != max):
max2 = array[i]

return max2

Ruben-hojd
Автор

Why do it in linear time if you could also Implement BubbleSort and do it in O(n^2). At least use sorted(my_list)[-2] if you need a quick and dirty solution.

Best solution is to iterate over the array once, always store the two biggest numbers up to that index and print the result at the end.

julianschubert
Автор

Why use bubble sort. When you have timsort inbuilt😅

ramuannadurai
Автор

It's doesn't work if have same elements

JustforFun-qjfg
Автор

Time complexity is n2 terrible solution

adityaanuragi
Автор

Can someone pls explain the working of nested for loop here in this program . Will be glad if someone clarifies me!!. Got stuck in this program. :(

PoosuLunda
Автор

We can simply sort and take last two values.

sarathchandra
Автор

if list = [2, 3, 4, 5, 6, 6, 6]
what will be the solution in this senario ?

sourabhmori
Автор

I think it's not an array it's list

rajsekharmuppidi
Автор

You can do it in single loop in n times

Rubyd