465 views
0 votes
0 votes

There are Insert and Retrieve_Max operations on a set {}. for n such operations what is the time complexity of the efficient algorithm possible?

  1. $n^{2}$
  2. nlogn   
  3. n   
  4. logn

1 Answer

Best answer
1 votes
1 votes
The time complexity of the efficient algorithm for n Insert and Retrieve_Max operations on a set {} depends on the data structure used to implement the set.

If a balanced binary search tree, such as AVL Tree, Red-Black Tree or Splay Tree is used to implement the set, the time complexity of each operation would be O(log n). The Insert operation would take O(log n) time to find the correct position to insert the new element, and Retrieve_Max operation would take O(log n) time to traverse the rightmost path in the tree to find the maximum element.

Therefore, the time complexity of n Insert and Retrieve_Max operations on a set implemented using a balanced binary search tree would be O(n log n).

If a heap is used to implement the set, the Insert operation would take O(log n) time to maintain the heap property after inserting the new element. The Retrieve_Max operation would take O(1) time to return the maximum element, which is the root of the heap.

Therefore, the time complexity of n Insert and Retrieve_Max operations on a set implemented using a heap would be O(n log n) for Insert operations and O(n) for Retrieve_Max operations, resulting in a total time complexity of O(n log n).
selected by

Related questions

745
views
1 answers
0 votes
itachi asked May 9, 2017
745 views
In which segment of memory layout the information about dynamic linked libraries is stored?
1.3k
views
1 answers
1 votes
pC asked Jul 18, 2016
1,284 views
Implement Longest Increasing Subsequence with the help of 1-D array for dynamic programming. (Hint : MaxTill 1-D array)
2.1k
views
1 answers
1 votes
pC asked Jul 18, 2016
2,111 views
Ms. Dany wants to clean the house having many rooms. She moves from one room to the next which takes 1 time unit. Each room has only one exit door. After some time ... ' and 'k'.(do not condiser time taken to clean the room) (Hint : DFS)
1.4k
views
1 answers
1 votes
pC asked Jul 18, 2016
1,409 views
Implement a^b mod m where a,b and m can be huge. (Hint : O(log n))