Answered step by step
Verified Expert Solution
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
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started