The minimum number of interchanges needed to convert the array into a max-heap is
View worked solution
Answer: C
To convert the array into a max-heap, we represent it as a complete binary tree using 1-based indexing:
Array:
The tree representation and the heapification process (starting from the last non-leaf node at index ) is as follows:
- Node 6 (value 10): Its children are at index 12 (value 9) and index 13 (value 70). Since , we swap 10 and 70.
Interchange 1: Swap .
The array becomes: . - Node 5 (value 12): Children are 11 and 6. Already satisfies the max-heap property ( and ).
- Node 4 (value 17): Children are 5 and 7. Already satisfies the max-heap property ( and ).
- Node 3 (value 40): Children are index 6 (now value 70) and index 7 (value 2). Since , we swap 40 and 70.
Interchange 2: Swap .
The array becomes: .
The sub-tree at index 6 with value 40 and children 9 and 10 is already a valid max-heap ( and ). - Node 2 (value 19): Children are 17 and 12. Already satisfies the max-heap property ( and ).
- Node 1 (value 89): Children are 19 and 70. Already satisfies the max-heap property ( and ).
Thus, the minimum number of interchanges needed to convert the array into a max-heap is 2.
Free practice · Your answers on this page are not saved.
