40 40 votes The minimum number of interchanges needed to convert the array into a max-heap is $89, 19, 40, 17, 12, 10, 2, 5, 7, 11, 6, 9, 70$ $0$ $1$ $2$ $3$ Data Structures gate1996 data-structures binary-heap easy + – Kathleen 20.4k views answer comment Share Follow Print See 1 comment 1 1 comment reply Flair_Pen commented Jun 28 reply Follow flag https://youtu.be/E1T3VQp4wGY?si=V9m2zhb2Qo2pqcIm this is a similar problem . 0 0 replyShare Please log in or register to add a comment.
Best answer 40 40 votes "The minimum number of interchanges needed to convert the array $89, 19, 40, 17, 12, 10, 2, 5, 7, 11, 6, 9, 70$ into a heap with the maximum element at the root node is:" This is the correction. Answer: C. Only element $70$ violates the rule. Hence, it must be shifted to its proper position. Step1: $swap(10, 70)$ Step2: $swap(40, 70)$ Hence, only $2$ interchanges are required. Gate Keeda answered Oct 9, 2014 • edited Jan 2, 2018 by kenzou Gate Keeda comment Share Follow See all 7 Comments 7 7 Comments reply Show 4 previous comments Amcodes commented Sep 29, 2020 reply Follow flag @Jeet i think no of comparisons will be much more as heap sort will apply max heapify until the ROOT (that is from all Non leaves until the Root. 1 1 replyShare Beyonder commented Oct 17, 2020 reply Follow flag if the sequence is 89,19,40,17,12,10,2,5,7,11,6,9,70,90 than what would be the answer? 0 0 replyShare Shiva Sagar Rao commented Oct 28, 2020 i edited by Shiva Sagar Rao Oct 28, 2020 reply Follow flag @Deepakk Poonia (Dee) For example, in this question we could have also done the following : Step1: swap(19,70) Step2: swap(10,19) Hence, only 2 interchanges are required I couldn’t understand how you got Step $1$ and $2$. Could you give more explanation? 89,19,40,17,12,25,2,5,7,11,6,9,70 Try the same question for this array. Answer will be 1 as we can just interchange 19 and 70, and we'll get max-heap. Am getting a minimum of $2$ interchanges Swap$(25,70)$, Swap$(40,70)$. Could you explain how you got only $1$ interchange? 0 0 replyShare Please log in or register to add a comment.
27 27 votes 2 interchanging is required. abhishekmehta4u answered May 15, 2018 abhishekmehta4u comment Share Follow See all 4 Comments 4 4 Comments reply talha hashim commented Aug 8, 2018 reply Follow flag nice explanation abhishek 2 2 replyShare Akshaydd1 commented Dec 27, 2018 reply Follow flag How left side part is correct ? Left side element of 19 is 17and right side element is 12 then they why they not interchange?? 1 1 replyShare Kiyoshi commented Apr 29, 2021 reply Follow flag This should be the best answer... 3 3 replyShare Thadymademe commented Jul 12, 2022 reply Follow flag @Akshaydd1 in max heap we need the the root element at every subtree to be maximum . 1 1 replyShare Please log in or register to add a comment.
1 1 vote To get minimum number of exchanges ,just apply heapify function on each of the non leaf node starting from the last non leaf node and proceeding up the tree towards the root.In doing so,we encounter two interchanges Swap(70,10) Swap(40,70) Therefore,minimum number of interchanges needed are 2. LIKITH P answered Sep 7, 2021 LIKITH P comment Share Follow 0 reply Please log in or register to add a comment.