The Gateway to Computer Science Excellence

First time here? Checkout the FAQ!

x

+3 votes

In a min-heap with n elements

1). The 7th smallest element can be found in time, if duplicates are allowed ?

2). The 7th distinct smallest element can be found in time, If duplicates are allowed ?

1). The 7th smallest element can be found in time, if duplicates are allowed ?

2). The 7th distinct smallest element can be found in time, If duplicates are allowed ?

For 1st, O(lgn) is given . I think it should be O(1) .

0 votes

For first one :

O(log n)

Reason : To delete an element , it takes O(1), then we have to heapify , which takes O(log n);

Now for 7th smallest element, we need 7 such operations, hence O(7 log n) = O(log n)

For 2nd one :

O(n)

O(log n)

Reason : To delete an element , it takes O(1), then we have to heapify , which takes O(log n);

Now for 7th smallest element, we need 7 such operations, hence O(7 log n) = O(log n)

For 2nd one :

O(n)

@kapilp

are you sure for first one it is O(logn)

If we will go with simple procedure like 1+2+3+....2^n-1 comaprison to find nth smallest element then it is O(1)

another one is start deleting the element till kth level to reach the kth smallest element which is O(logn)

because everywhere I am seeing O(1) as well O(logn) time...can u plz confirm?? even I am bit confuse

see this:

https://gateoverflow.in/5915/complexity-finding-smallest-element-already-constructed

http://stackoverflow.com/questions/21600312/searching-the-7th-largest-element-in-a-max-heap

- All categories
- General Aptitude 1.1k
- Engineering Mathematics 4k
- Digital Logic 1.7k
- Programming & DS 3k
- Algorithms 2.6k
- Theory of Computation 3.2k
- Compiler Design 1.2k
- Databases 2.4k
- CO & Architecture 2.1k
- Computer Networks 2.4k
- Non GATE 795
- Others 1.2k
- Admissions 244
- Exam Queries 419
- Tier 1 Placement Questions 16
- Job Queries 39
- Projects 4

29,154 questions

36,976 answers

92,139 comments

34,816 users