GATE Computer ScienceVery hardGATE CS (PYQ pattern)

The distinct keys 1..8 are arranged as a max-heap in a 0-indexed array of size 8 (children of i at 2i+1, 2i+2). Over all valid max-heaps, in how many different array positions can the key 3 (the third smallest) appear?

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

Correct answer: C. 6

Brute force over all 210 valid heaps shows key 3 can sit in the positions [2, 3, 4, 5, 6, 7]: only keys 1 and 2 are smaller, so key 3 can have at most two descendants; that rules out the root and index 1 (which have more), leaving indices 2 to 7 (6 positions).

More Programming and Data Structures (GATE) questions

Exams that ask this topic