IAE Academy

GATE 2024 CS1 Q.43: solution and answer

GATE 2024 · CS1Q.43 · 2 marks · MCQ

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

  1. (A)

    53

  2. (B)

    52

  3. (C)

    27

  4. (D)

    1

See the question in the paper

Answer and solution

ANSWER

The number of possible values of k is 53 — option (A).

Official answer: ASource: Official answer key, GATE 2024 CS1 (IISc Bengaluru)

  1. Min-heap property: for any parent–child pair, key(parent) ≤ key(child). If a node has at least one child, that child's key is ≥ the node's key, and since all 105 elements are distinct, the child's key must be strictly greater. So a node with a child can never hold the maximum element.

  2. Therefore the maximum element must sit at a node with no children — a leaf of the heap.

  3. Using 1-indexed array representation for nodes, node has a child at , which exists only when , i.e. . So indices through are internal (non-leaf) nodes.

  4. Hence leaf indices run from to , giving leaf positions.

  5. For each of these 53 leaf positions, a valid min-heap can be constructed with the maximum element placed at that leaf: put the largest value there and arrange the remaining 104 values anywhere else consistent with the min-heap order (always possible, since a leaf imposes no downward constraint and its parent only needs to be the maximum value, which trivially holds). So every leaf index is achievable.

  6. Therefore the number of possible values of equals the number of leaf positions , which is option (A).

KEY IDEA

In a min-heap the maximum element can never occupy any internal (non-leaf) node, because every internal node is each of its children, so a node with a child cannot hold the overall maximum; hence the maximum must sit at some leaf, and every leaf position is achievable by some valid heap on the multiset.

WATCH OUT FOR

Assuming the maximum must be at the "last" leaf or at one of the two children of the root (confusing max-heap intuition), or miscounting leaves by using instead of for the boundary between internal nodes and leaves, or forgetting to check that every leaf position, not just some of them, is actually achievable.

From the GATE 2024 CS1 paper. Every question of this paper · Download the full paper · All 26 Trees & heaps questions, with notes

More Trees & heaps questions