in Algorithms edited by
727 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)$
in Algorithms edited by
by
727 views

2 Comments

We can use the max heap of size p.We will scan the elements from left to right.

1) Firstly we will build max heap of first p elements. It just takes O(p) time.

2) Now after that, we will start from (p+1)th element

a)If the root is smaller than the current element we will delete the root and then place the current element at the root. Then we have to call the max heapify.

b)If the root is greater then the current element then we simply move to next element.

Repeat till we hit the right end of the array.

At the end, the heap will contain p smallest elements and whose root will give pth smallest element.

step 1 takes O(p)

step 2 In worst case will take (n-p)*(logp)

Total= O(p) + O((n-p)*logp) In worst case p==n

so we get O(n)
0
0
Can you please make it a bit more clearer?
0
0

3 Answers

4 votes
4 votes
Best answer
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

12 Comments

got confused with the term 'linearly ordered' which made me assume that the elements are already in an array, so was finding for O(1) in the options :)

Sir the question would be much more interesting if you could frame it as

" if n distinct elements need to be  added to a min heap , not necessarily one after the other to find the pth smallest element where ( 1≤ p ≤ n ) from these n elements when n > 50 . What is the worst case Time Complexity of the entire procedure."

4
4
what is linear order means?
0
0
@Bikram sir

What does "Linearly Ordered" means in this que??

Can I interprete Linearly ordered as a "set" which contains sorted distinct elements,then we have to make a build-heap which will take O(n) time then to find pth smallest element it takes p(p-1)/2 which is constant time. So overall time complexity is O(n).

Correct me sir if I m wrong
0
0
@shraddha How is p becoming a constant here?
0
0
@Arjun sir, In the question we are given 'p' and also that it lies between 1 to n, so it's a constant I think.
0
0

@Bikram sir, @Arjun sir

We have to find pth smallest element ( 1≤ p ≤ n ) from these n elements. So in worst case we have to find pth smallest element i.e. nth smallest element which is first maximum element. So in this scenario it will take n(n-1)/2 comparisons which is O(n2).

Plz correct me sir if I am wrong?

1
1
@shraddha no. Anything that is derived from input length - n - cannot be a constant. A constant is something which never changes whatever be the input.
1
1
@amit O(n^2) is obviously correct. But what happens when it becomes tight upper bound - theta? This is not so trivial and analogous to the complexity of build heap.
0
0
@Arjun sir

As it is given "n" number of linearly ordered distinct elements,so in worst case we have to find pth smallest element i.e. nth smallest element which will take O(n ) time, As it is the upper bound I think??
0
0
So how is the answer O(n)? Please provide the answer,someone, anyone.
0
0
This answer is incorrect.

Even using min-heap or max-heap, the complexity should be $O(n + k \log n)$, and not $O(n)$.

Using QuickSelect, we can get it in $O(n)$ in average, but the worst case still remains the same of $O(n^2)$.
1
1

Yeah exactly @goxul. Can someone tell me how p(p-1)/2 comparisons for min heap?

0
0
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