Data Structures & AlgorithmsHardCodeforces Div 2 · ICPC regional
Array [27, 21, 1, 3, 16, 28, 17] is turned into a min-heap in place with bottom-up heapify: for i from ⌊n/2⌋−1 down to 0, sift the element at index i down (swap with its smaller child while a child is smaller). What is the resulting array?
- A. [1, 3, 17, 21, 16, 28, 27]
- B. [1, 3, 16, 17, 21, 27, 28]
- C. [1, 21, 17, 3, 16, 28, 27]
- D. [1, 3, 17, 27, 16, 28, 21]
Correct answer: A. [1, 3, 17, 21, 16, 28, 27]
Bottom-up heapify on [27, 21, 1, 3, 16, 28, 17] produces [1, 3, 17, 21, 16, 28, 27]. Inserting one element at a time gives a different heap, and a single sift-down from the root does not fix the internal nodes.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- On an 8 × 8 chessboard with rows and columns numbered 0 to 7, a knight starts at (5, 7) and must reach (0, 5) (as (row, column)). A knight…
- Undirected weighted graph on vertices A, B, C, D, E, F with edges A–B (6), A–E (6), A–F (7), B–C (3), B–D (6), B–F (9), C–E (8), D–F (5)…
- Words ['dd', 'da', 'bb', 'bd', 'db', 'cc', 'aba'] (each may be used at most once). A chain is a sequence of these words in which the first…
- Undirected weighted graph on vertices A, B, C, D, E with edges A–D (2), A–E (1), B–C (2), C–D (1), C–E (3), D–E (2). An edge is CRITICAL…
- Connected undirected weighted graph on vertices A, B, C, D, E with edges A–B (1), A–D (2), B–C (1), B–D (6), B–E (3), C–E (1). Among all…
- Directed weighted graph on vertices A, B, C, D, E with edges listed in this exact order: D→A (3); B→A (2); E→A (-3); A→B (3); D→E (4); A→D…