GATE Computer ScienceVery hardGATE CS (PYQ pattern)

Keys 1..7 form a max-heap stored in a 0-indexed array (children of i are 2i+1, 2i+2). The key 5 is the third largest. In how many different array positions can it appear over all valid heaps?

  1. A. 3
  2. B. 4
  3. C. 5
  4. D. 6

Correct answer: D. 6

Brute force over all valid max-heaps shows key 5 can occupy positions [1, 2, 3, 4, 5, 6]; the root always holds 7, so every non-root slot is possible.

More Programming and Data Structures (GATE) questions

Exams that ask this topic