in DS recategorized by
583 views
2 votes
2 votes

What is the time complexity of finding the kth smallest element in an array?

  1. $O(\log n)$
  2. $o(\log n)$
  3. $\Omega(n\log n)$
  4. $O(n)$
in DS recategorized by
583 views

4 Comments

Build min heap and then check for that element in k levels only. So, it's $O(n) + O(1) = O(n)$

Another ways can be be simple linear search and make an another array of size k that marks the least k elements. Again $O(n)$
0
0
@abcd2 for what O(1)  ??
0
0
0
0

1 Answer

1 vote
1 vote
Build Min heap O(n)

Delete min elements upto k times = k * O(log n)

Insert all k elements back k * O(log n)

Dominating term O(n) so (D) is the answer.

Since Omega() will be the lower bound, but here in worst case k = n, so O(n logn) should be given for n logn to be a correct answer.