1 1 vote Deleting any random element in heap would take n+logn or n+n? Algorithms time-complexity + – Raghav Khajuria 1.4k views answer comment Share Follow Print See all 11 Comments 11 11 Comments reply nephron commented Sep 17, 2018 i moved by Shaik Masthan Sep 17, 2018 reply Follow flag n+logn 0 0 replyShare Shaik Masthan commented Sep 17, 2018 reply Follow flag @nephron don't post direct answers, give some explanation on them if you don't want to give explanation then add in comment section. 0 0 replyShare srestha commented Sep 17, 2018 reply Follow flag and it is also not a correct answer ans will be O(log n) 0 0 replyShare himgta commented Sep 17, 2018 reply Follow flag O(log n)?? I think for deleting a random element it should be O(n) @srestha mam! 1 1 replyShare Shaik Masthan commented Sep 17, 2018 reply Follow flag @srestha mam, it is heap (not BST), how much time to find a element? 1 1 replyShare srestha commented Sep 17, 2018 reply Follow flag Oh yes O(n) to find the element and O(log n) to delete So, O(n log n) right? 0 0 replyShare Shaik Masthan commented Sep 17, 2018 reply Follow flag to find O(n) for finding + O(logn) for heapify = O(n) 2 2 replyShare Magma commented Sep 17, 2018 reply Follow flag let , random number is at index = randomIndex To finding the random number = O(n) A[randomIndex] = A[heap_size] A.heap_size = A.heap_size-1 heapify (A , randomIndex) which takes O(log n) total time : O(n) + O(log n) = O(n) 0 0 replyShare srestha commented Sep 17, 2018 reply Follow flag Is it not multiplying requires @Shaik? because we need both first finding and then heapify operation It is not an OR operation right? 0 0 replyShare nephron commented Sep 17, 2018 reply Follow flag No, addition is required not multiplication. U search an element in O(n) and then heapify it O(logn). Nlogn means u r repeating heapify function N times. 1 1 replyShare Shaik Masthan commented Sep 17, 2018 reply Follow flag @srestha mam because we need both first finding and then heapify operation yes, in normal language, but in Algorithms first loop finds ===>O(n) second heapify ===> O(log n) mam, nephron explained it very well 0 0 replyShare Please log in or register to add a comment.
0 0 votes The deletion of random element takes 0(n) + 0(logn) time. 0(n) as the array traverses to find the last element and assign it to the root; 0(logn) as the element swaps as per max/min heap condition and deletion takes for the root. rish1602 answered Jun 15, 2021 rish1602 comment Share Follow 0 reply Please log in or register to add a comment.