Skip to content
Official PYQ · 2024

GATE CS 2024 Set 1 Q33 — Binary Heaps

Explain with AI

Consider a binary min-heap containing 105105 distinct elements. Let kk be the index (in the underlying array) of the maximum element stored in the heap. The number of possible values of kk is

Choose one option
View worked solution

Answer: A

In a binary min-heap, the value of any node is smaller than or equal to the values of its children. Consequently, the maximum element in a min-heap cannot have any children (if it did, those children would have to be larger than the maximum element, which is a contradiction).

Therefore, the maximum element must reside at one of the leaf nodes of the heap.

For a complete binary tree represented as an array of size NN (using 1-based indexing):

  • The parent of the last element (at index NN) is at index ⌊N/2⌋\lfloor N/2 \rfloor.
  • Thus, all nodes from index 11 to ⌊N/2⌋\lfloor N/2 \rfloor are internal nodes (they have at least one child).
  • The leaf nodes are located at indices from ⌊N/2⌋+1\lfloor N/2 \rfloor + 1 to NN.

Given N=105N = 105:

⌊N/2⌋=⌊105/2⌋=52\lfloor N/2 \rfloor = \lfloor 105/2 \rfloor = 52

So, the leaf nodes are located at indices from 52+1=5352 + 1 = 53 to 105105.

The number of leaf nodes (and thus the number of possible positions for the maximum element) is:

105−53+1=53105 - 53 + 1 = 53

Hence, there are 5353 possible values for the index kk.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Binary Heaps questions