GATE Computer ScienceVery hardGATE CS (PYQ pattern)

For n = 7 distinct keys, what is the maximum number of swaps that the bottom-up (Floyd) build-heap for a max-heap can perform, over all 5040 input orders?

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

Correct answer: D. 4

Indices 1 and 2 have only leaf children, so each can swap at most once; the root can sink two levels, at most 2 swaps: 1 + 1 + 2 = 4, attained for example by 1 2 3 4 5 6 7 (checked over all 5040 orders).

More Programming and Data Structures (GATE) questions

Exams that ask this topic