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. Youwill do that by applying a certain number of swaps to the

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 aj of 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 100 000; 0 ij n-1; 0 ,a-1S 10). All ai are distinct. n ao,ai, 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 ime, using System.currentTimeMillis). Please print the elapsed time in seconds Sample 2 Input: 12345 Output: Explanation: The input array is already a heap, because it is sorted in increasing order. 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 aj of 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 100 000; 0 ij n-1; 0 ,a-1S 10). All ai are distinct. n ao,ai, 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 ime, using System.currentTimeMillis). Please print the elapsed time in seconds Sample 2 Input: 12345 Output: Explanation: The input array is already a heap, because it is sorted in increasing order

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!