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

  Рет қаралды 44,855

Python coders

Python coders

2 жыл бұрын

Пікірлер: 38
@ChhayaKamble-ft1fd
@ChhayaKamble-ft1fd Жыл бұрын
Done = sorted(array1) print(Done[-2])
@ChhayaKamble-ft1fd
@ChhayaKamble-ft1fd Жыл бұрын
The code can be improved further. Just use sorted() function in list and get the second last index with -2
@immor8al808
@immor8al808 Жыл бұрын
Bro He has used Bubble sort to sort the list
@ramisettyharsha9251
@ramisettyharsha9251 9 ай бұрын
not lists we need to solve in arrays
@azziazzi9810
@azziazzi9810 5 ай бұрын
array.sort(reverse=True) for x in range(1,len(a)): if a[x]!=a[x-1]: print(a[x]) break
@praveenrockcse2192
@praveenrockcse2192 Жыл бұрын
Array1.sort() Print(-1) #done in two lines
@styleremo2879
@styleremo2879 Жыл бұрын
-2 bro
@sofyanalwi35
@sofyanalwi35 Жыл бұрын
@@styleremo2879 Bruh. Starts from 0
@faridayasmeen7432
@faridayasmeen7432 Жыл бұрын
@@sofyanalwi35 it's -2 you can check it
@SN-oo2fq
@SN-oo2fq Жыл бұрын
School marks
@dannyezechukwu1175
@dannyezechukwu1175 Жыл бұрын
Definitely -2. -1 is the last element in the list which would be the max value after the list is sorted.
@faaiqrahman669
@faaiqrahman669 Жыл бұрын
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
@monnichoudhory4406
@monnichoudhory4406 Жыл бұрын
Runtime error
@julianschubert1772
@julianschubert1772 2 жыл бұрын
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.
@Darklegend_royal-metamorphosis
@Darklegend_royal-metamorphosis Жыл бұрын
O(n²) is worse than linear time. What are u on about?
@Ruben-ho9jd
@Ruben-ho9jd Жыл бұрын
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
@ramisettyharsha9251
@ramisettyharsha9251 9 ай бұрын
chatgpt wala
@karthikmadhavan4098
@karthikmadhavan4098 Жыл бұрын
L.sort() print(L[-2])
@ramuannadurai
@ramuannadurai 4 ай бұрын
Why use bubble sort. When you have timsort inbuilt😅
@adityaanuragi6916
@adityaanuragi6916 2 ай бұрын
Time complexity is n2 terrible solution
@JustforFun-qj3fg
@JustforFun-qj3fg 8 ай бұрын
It's doesn't work if have same elements
@sourabhmori8129
@sourabhmori8129 9 ай бұрын
if list = [2,3,4,5,6,6,6] what will be the solution in this senario ?
@nsshinde8520
@nsshinde8520 8 ай бұрын
my_list = [2, 3, 4, 5, 6, 6, 6] new_list = set(my_list) my_list = list(new_list) print(my_list) my_list.sort() print(my_list[-2])
@Hate_to_be_indian
@Hate_to_be_indian 5 ай бұрын
#for eliminating duplicate values L=[ 4,4,6,8] L2=[] for I in l: If l.count(i)==1: L2. append(i) print(L2) # for sorting list& wanna get 2nd highest value L2. sort() print(L2[-1])
@rajsekharmuppidi736
@rajsekharmuppidi736 Жыл бұрын
I think it's not an array it's list
@herzeg6146
@herzeg6146 Жыл бұрын
arrays can be list, tuple, str, bytearray and e.tc
@guruyadavraj636
@guruyadavraj636 Жыл бұрын
Before square brackets he forgot to mention paranthesis
@sarathchandra9354
@sarathchandra9354 2 жыл бұрын
We can simply sort and take last two values.
@vice-108
@vice-108 Жыл бұрын
Congrats you just got rejected
@vanshikarajsharma
@vanshikarajsharma Жыл бұрын
When we have same value this won't work
@a2zfunbegins273
@a2zfunbegins273 Жыл бұрын
@@vice-108 😂
@Developerf5
@Developerf5 Жыл бұрын
@@vice-108 Yes🤣
@-deepakganapathi
@-deepakganapathi 11 ай бұрын
ok than hoe will you get he 5'th highest value
@PoosuLunda69
@PoosuLunda69 Жыл бұрын
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. :(
@immor8al808
@immor8al808 Жыл бұрын
Bubble sort Take a list as an example Like [62 , 42 , 21 , 9] Using for loop For i in range(len(list1)): Then for Sorting the list in Ascending order and to Compare it with Other numbers in the list we will Use another loop For j in range(i+1 , len(list1)): If list1[i] which is 62 and list1[j] which is 42 here If list1[i] > list1[j]: Just swap it So list1[i] , list1[j] = list1[j] , list1[i] I hope this helps
@immor8al808
@immor8al808 Жыл бұрын
Time Complexity for this program will be O(n²)
@Rubyd777
@Rubyd777 Жыл бұрын
You can do it in single loop in n times
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 620 М.
The Fastest Way to Loop in Python - An Unfortunate Truth
8:06
mCoding
Рет қаралды 1,4 МЛН
NERF WAR HEAVY: Drone Battle!
00:30
MacDannyGun
Рет қаралды 58 МЛН
Heartwarming: Stranger Saves Puppy from Hot Car #shorts
00:22
Fabiosa Best Lifehacks
Рет қаралды 22 МЛН
Русалка
01:00
История одного вокалиста
Рет қаралды 6 МЛН
5 Good Python Habits
17:35
Indently
Рет қаралды 431 М.
*Args and **Kwargs in Python
3:49
b001
Рет қаралды 255 М.
Find maximum element in an array (Largest element)
5:42
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 140 М.
If __name__ == "__main__" for Python Developers
8:47
Python Simplified
Рет қаралды 386 М.
Programming vs Coding - What's the difference?
5:59
Aaron Jack
Рет қаралды 1,9 МЛН
Why I Chose Rust Over Zig
33:18
ThePrimeTime
Рет қаралды 19 М.
R vs Python
7:07
IBM Technology
Рет қаралды 310 М.
16 - Java program to find Second Largest/Smallest element in an array
14:14
Software Testing And Automation
Рет қаралды 21 М.
NERF WAR HEAVY: Drone Battle!
00:30
MacDannyGun
Рет қаралды 58 МЛН