Question: 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
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 a, of some i andj. 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- the array a for separated integers a, Constraints. 1 n S 100 000;0S ijsn -1;0Saoa.10. All a are distinct. Output Format. The first line of the output should contain single integer m the 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 i the 1-based indices of the elements to be swapped. After applying all the swaps in the specified order the array must a min-heap. You also required to measure the elapsed time, using System.currentTimeMilis). Please print the elapsed time in seconds. Sample I Input: 54321 Output: 1 4 0 1 Explanation: After swapping elements 4 in position I and 1 in position 4 the array becomes 5 1 3 24. After swapping elements 5 in position 0 and 1 in position I the array becomes 1 5 324. After swapping elements 5 in position 1 and 2 in position 3 the array becomes 1 23 54, which is already a heap, because ao 12, 3-a2a1 25 a,a1 2
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
