retagged by
14,929 views
28 votes
28 votes
Consider the array representation of a binary min-heap containing $1023$ elements. The minimum number of comparisons required to find the maximum in the heap is ___________.
retagged by

6 Answers

Best answer
48 votes
48 votes

If a heap has $1023$ elements, it'll contain $512$ leaves and since it is a MIN-HEAP, the maximum will be present in the leaves. (Why? Assume it is not, then there will be some elements present under it and this will violate the min-heap property.)

We can visualise it this way. At each level starting with root level, number of elements are

$1-2-4-8-16-32-64-128-256-512$ (this is the last level, hence leaves)

So if we have $n$ elements in an array, we know that to find the maximum it takes $n-1$ comparisons.

In this case, $n = 512$, so the answer should be $511$.

Some other excellent questions on finding maximum-minimum:

edited by
10 votes
10 votes

Answer : 511

In Binary min-heap , Maximum element will be present in the leaf nodes .

so, we already know that from "floor(n/2)+1 to n" leaf nodes are present

floor(1023/2)+1 to 1023 = 512 to 1023 => total 512 elements (put these all in an array i:e,[all the elements in array are unsorted] ) .

 

now we know that to find maximum in an array we will surely require minimum n-1 comparisons(you can apply any searching algorithm , n-1 comparison must be needed) .

so, 512-1 = 511 

2 votes
2 votes

Minimum 511 comparisons  required.

A heap with n nodes satisfies a property that it has (1 to floor(n/2)) non leaf elements & (floor(n/2)+1 to n) leaf elements.

So, the non leaf node cannot contain the maximum element. To look for max element we have to search the leaves.

applying the above formulae, we'll get 512 leaves.

as we know for m elements, minimum m-1 comparison is required to find min/max element. 

so we need minimum of 512-1=511 comparisons to find the max element of heap.

 

1 votes
1 votes
In a binary min-heap the maximum element will always remain in the leaf node

Let us consider that all the elements in the heap is stored in an array of n+1 elements starting from index 1.(We are keeping index 0 free for easier and minimal calculations, it can be done with index 0 also)

For a given node arr[i],

the left child will be – arr[2*i]

the right child will be – arr[2*i + 1]

 

The leaf nodes cannot have any child, so the value of 2*i will always exceed n, that means any leaf node will come after any non leaf node in the array representation, so we have to traverse the last floor(n/2) + 1 elements to get the maximum element.

Total number of leaves = 511 + 1 = 512

To find the maximum of 512 elements we have to do atleast 511 comparisons.
Answer:

Related questions

28 votes
28 votes
8 answers
1
Arjun asked Feb 12, 2020
16,505 views
The number of permutations of the characters in LILAC so that no character appears in its original position, if the two L’s are indistinguishable, is ______.
24 votes
24 votes
2 answers
4
Arjun asked Feb 12, 2020
12,240 views
For $n>2$, let $a \in \{0,1\}^n$ be a non-zero vector. Suppose that $x$ is chosen uniformly at random from $\{0,1\}^n$. Then, the probability that $\displaystyle{} \Sigm...