Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

2. Convert a given array of integers into a heap. You will do that by applying a certain number of swaps to the array. Swap

image text in transcribed

image text in transcribed

image text in transcribed

2. Convert a given array of integers into a heap. You will do that by applying a certain number of swaps to the array. Swap is an operation which exchanges elements a, and ajof the array a for some i and j. Note that you will need to use a min-heap instead of a max-heap in this problem. Input Format. The first line of the input contains single integer n. The next line contains n space- separated integers ai Constraints. 1 n 100 000; 0 sijsn 10S ao,a -1 109. All ai are distinct. Output Format. The first line of the output should contain single integer mthe total number of swaps. The next m lines should contain the swap operations used to convert the array a into a heap. Each swap is described by a pair of integers ij- the 1-based indices of the elements to be swapped. After applying all the swaps in the specified order the array must become a min-heap. You also required to measure the elapsed time, using System.currentTimeMillis). Please print the elapsed time in seconds Sample 1. Input: 54321 Output: 1 4 Explanation: After swapping elements 4 in position 1 and 1 in position 4 the array becomes 5 1 3 24. After swapping elements 5 in position 0 and 1 in position 1 the array becomes 15 324. After swapping elements 5 in position 1 and 2 in position 3 the array becomes 1 2354, which is already a heap, because ao 12 a1,ao 1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Temporal Databases Research And Practice Lncs 1399

Authors: Opher Etzion ,Sushil Jajodia ,Suryanarayana Sripada

1st Edition

3540645195, 978-3540645191

More Books

Students also viewed these Databases questions