5 5 votes A min heap having 1024 distinct elements with keys ranging from 0 to 1023 is stored in array of 1024 indices. The maximum difference between the keys of all the element that can possibly be stored at (n/2)th index of the array is........... Data Structures data-structures binary-heap numerical-answers + – Ashwani Kumar 2 5.0k views answer comment Share Follow Print See all 21 Comments 21 21 Comments reply saurabh rai commented Jan 31, 2017 reply Follow flag is it 1013 ? 0 0 replyShare Ashwani Kumar 2 commented Jan 31, 2017 reply Follow flag Answer given is 1014. Could you please explaint the approach? 0 0 replyShare saurabh rai commented Jan 31, 2017 reply Follow flag ^it is a 10 level min heap root at 0 there will n/2 internal nodes nd n/2 leaf nodes at n/2 ie last internal node with value 9 bcoz it is at 9th level so max diff =1023-9=1014 0 0 replyShare dd commented Feb 1, 2017 reply Follow flag ....................... 7 7 replyShare smsubham commented Dec 27, 2017 i edited by smsubham Feb 18, 2018 reply Follow flag In that case, answer will be 1023 - 10 = 1013 height = $\left \lfloor \log 1024 \right \rfloor = 10$ n/2 will be the last non-leaf node so the maximum value we can place in it will be 1023 and minimum value will be 10. 0 0 replyShare akash.dinkar12 commented Jun 24, 2018 reply Follow flag how 512 numbered element can be on level 1?? Because it is min heap, on the top of it (level 1), there will be only 1 element which will be the root of a tree and that element will be minimum among all the elements in heap 1 1 replyShare Anu007 commented Jun 24, 2018 reply Follow flag why minimum level 2, and why it is not 1? Because at root only 0 will come since it is min heap, so 512 can be at level-2 but not at level-1. 2 2 replyShare srestha commented Jun 24, 2018 reply Follow flag @akash @Anu but how do u know 512th element is not the shortest element? 0 0 replyShare Anu007 commented Jun 24, 2018 reply Follow flag question ask element 512 not 512th element. 1 1 replyShare akash.dinkar12 commented Jun 24, 2018 reply Follow flag @Srestha Yes, u are asking about 512 numbered element right!!! 1 1 replyShare srestha commented Jun 24, 2018 reply Follow flag yes , I got it the elements in the array 0 to 1023 now 0 is root of heap Now from 1 to 511 in left subtree and rest are in right subtree right? 0 0 replyShare Shubhgupta commented Jun 24, 2018 i edited by Shubhgupta Jun 24, 2018 reply Follow flag Can anyone please explain for maximum how it will be on 11th level? Shouldn't be the 512 present on 10th level if it will be on 11th level then no. of elements required will be 2048. 0 0 replyShare srestha commented Jun 24, 2018 reply Follow flag 11 because it started at 1 1 1 replyShare Harshita Sinha commented Sep 24, 2018 reply Follow flag can any one please explain .......how to detect maximum level or minimum level in min heap. Please explain how 9 is the answer. 0 0 replyShare MiNiPanda commented Nov 20, 2018 reply Follow flag Total no. of levels with 1024 elements is 11.Its better if you try drawing the heap. For 512 to be at level 2 (on left subtree) the heap will be like, 0,512,1,513,514,2,3,515,516,517,518,4,5,6,7… Since from 0 to 1023 there are 1024 elements so the heap will have just one element at the last level. We try to make this element as 512. I take a small example. For 16 elements 0 to 15, I need to make 8 come at the last level. This is how I can do that: 0,1,2,3,4,5,6,7,9,10,11,12,13,14,15,8. Similarly in the heap with 1024 elements, the 2nd last level will contain 512 elements (29) and those will be 511,513,514…1023 and at the last level, the left child of 511 will be 512. 1 1 replyShare nilubabu2 commented Nov 20, 2018 reply Follow flag Thanks.. 0 0 replyShare `JEET commented Nov 24, 2018 reply Follow flag @MiNiPanda In the last line you mentioned " the left child of 511 2ill be 512" Did you mean left child or the right child? 0 0 replyShare MiNiPanda commented Nov 24, 2018 reply Follow flag @`JEET, Since this is a heap so all the nodes should be placed towards left as much as possible. So 512 will be the left child of 511. Not to be confused with BST. 0 0 replyShare `JEET commented Nov 24, 2018 reply Follow flag Yeah..exactly. Spot on :D Thanks. 0 0 replyShare Prateek Raghuvanshi commented Nov 26, 2018 reply Follow flag It could be helpful. 5 5 replyShare mehul vaidya commented Feb 17, 2019 i edited by mehul vaidya Feb 17, 2019 reply Follow flag I understood answer I have very basic doubt Given number , what is formulae to find it's level I know this is very basic , but I am struggling to find one for this example , assuming we haven't twisted graph and drawn just in sequence 1,2,3,4,5,1023 in this increasing order only. 0 0 replyShare Please log in or register to add a comment.
1 1 vote There are 1024 elements, there will be total 11 levels, 512 is the middle element. minimum level possible is Level 2 because 0 is the minimum element, Root should be 0, in the second level we can have 512. Maximum level possible is Level 11. Difference is 11-2 = 9 Aakash_ answered Oct 26, 2018 Aakash_ comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes since there are 1024 elements, there will be ceil{log(1+1024)}=11 levels, so in worst case, the 512 can be present at level 11(last level) in best case the element 512 can be present only at level 2, it root is at level 1 then the difference = 11-2=9 Answer is 9 DevKant Sharma answered Nov 13, 2018 DevKant Sharma comment Share Follow 0 reply Please log in or register to add a comment.