Construct the Max Heap assuming the following set of integers were inserted into it in given order
20, 32, 1, 3, 4, 5, 6, 7, 10, 23, 45
Postorder traversal of the resultant max heap was stored in a array A with an index variable inorder (Starting from 0). Similarly level order traversal was stored in the array B using index variable j in order (Starting from 0). For particular element, respective i and j location values from A and B were obtained and |i – j| is calculated. What could be the maximum possible value for |i – j|?
according to ques array A for postorder traversal of the resultant max heap.
and level order traversal was stored in the array B
A
B
from arrays A and B as shown above we can clearly find max value of |i-j| .
in array A 45 is in index 10 whereas in array B 45 is in index 0 so max value of |i-j| is 10-0=10
answer will be 10 , but your solution is incorrect . Resultant max heap/Level order traversal will be -: $45,32,6,10,23,5,1,7,3,20,4$ and Post order traversal of resultant max heap will be -: $7,3,10,20,4,23,32,5,1,6,45$ maximum size of $| i-j |=10$(for $45$)
https://visualgo.net/en/heap
thank u @ sourav.for point out my mistake now it is correct you can check
postorder : 3,7,10,4,20,23,32,1,5,6,45
Levelorder :45,32,6,10,23,1,5,3,7,4,20
i-j = 10-0= 10
64.3k questions
77.9k answers
244k comments
80.0k users