My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

Can you explain the time complexity of finding a three largest number in an array?

Vikas Poonia's photo
Vikas Poonia
·May 22, 2019

I have two solution in mind:

The first one is the naive one in which we can sort an array in O(n*log(n)) time and after that we just take the last three elements and return it.

The second one is just to sort the element up to 3 element using bubble sort so we can get three largest value on the right side and return the array . But my doubt is what is the time complexity according to you. From my point of view it is F(n) = 3n (neglecting the constants). Kindly let me know