edited by
733 views
0 votes
0 votes

Given $n$ number of linearly ordered distinct elements, what will be the worst case time complexity to find

$p$-th smallest element $(1 \leq p \leq n)$ from these $n$ elements when $n > 50$?

  1. $O(n \log n)$
  2. $O(n^2)$
  3. $O(n)$
  4. $O(\log n)$
edited by

3 Answers

Best answer
4 votes
4 votes
Build a min - heap of these n elements in O(n) time. Then to find pth smallest element it takes p(p-1)/2 comparisons i.e. constant time. So overall O(n).
selected by
0 votes
0 votes
answer should be log N bcoz it is given in question that array is sorted . so to find any element we can apply binary search.   will it works??
0 votes
0 votes
I Googled "linearly ordered" and it resulted in it meaning a TOSET. I don't know how a total order would be applicable here, tbh. If "linearly ordered" means "sorted" in an array here, then we can use random access and get the pth element in $O(1)$time. That isn't in the options.

Here's what I think is the answer.

Extracting the minimum element is what heap is for. So, build a min heap in $O(n)$ time.

After that extract the minimum element p times. Each such extraction would invoke heapify, and cost us $O(logn)$ time.

So, Time Complexity would be $O(n + plogn)$

In the worst case, p = n. So, $O(n + nlogn)$

=> $O(nlogn)$
Answer:

Related questions

0 votes
0 votes
1 answer
1
Bikram asked May 26, 2017
487 views
The cost of optimal binary search tree for the identifier set $(a1, a2, a3) =$ (do, if, while) with $p(1) = 0.3, \ p(2) = 0.2, $ $p(3) = 0.15, q (0) = 0.05, q(1) = 0.15...
2 votes
2 votes
2 answers
3
Bikram asked May 26, 2017
371 views
Assume Dijkstra's Algorithm is used to find the shortest paths from node G in the above graph. The total number of edges which are not included in any of the shortest pat...
1 votes
1 votes
2 answers
4
Bikram asked May 26, 2017
486 views
The total number of LCS (Longest Common Subsequences) of $P = abcd123$ and $Q= badc321$ that can be formed are ______.