Which one of the following sequences when stored in an array at locations…
2023
Which one of the following sequences when stored in an array at locations A[1],...,A[10] forms a max-heap?
Answer: B. 23, 17, 14, 7, 13, 10, 1, 5, 6, 12 — Key property: In a 1-indexed array representation of a max-heap, for every index i the element at i must be greater than or equal to the elements at indices…
- A.
23, 17, 10, 6, 13, 14, 1, 5, 7, 12
- B.
23, 17, 14, 7, 13, 10, 1, 5, 6, 12
- C.
23, 17, 14, 6, 13, 10, 1, 5, 7, 15
- D.
23, 14, 17, 1, 10, 13, 16, 12, 7, 5
Attempted by 317 students.
Show answer & explanation
Correct answer: B
Key property: In a 1-indexed array representation of a max-heap, for every index i the element at i must be greater than or equal to the elements at indices 2i and 2i+1 (if those indices exist).
Sequence 23, 17, 14, 7, 13, 10, 1, 5, 6, 12 is a max-heap because every parent is >= its children: index 1: 23 >= 17,14; index 2: 17 >= 7,13; index 3: 14 >= 10,1; index 4: 7 >= 5,6; index 5: 13 >= 12.
Sequence 23, 17, 10, 6, 13, 14, 1, 5, 7, 12 fails because at index 3 the parent 10 is less than its child 14 (index 6).
Sequence 23, 17, 14, 6, 13, 10, 1, 5, 7, 15 fails because at index 5 the parent 13 is less than its child 15 (index 10).
Sequence 23, 14, 17, 1, 10, 13, 16, 12, 7, 5 fails because at index 4 the parent 1 is less than its child 12 (index 8).
How to check: For any candidate array, iterate i from 1 to floor(n/2) and verify array[i] >= array[2i] and array[i] >= array[2i+1] when those child indices are ≤ n.
A video solution is available for this question — log in and enroll to watch it.
Explore the full course: Iocl Engineers Officers Grade A Paper 2