Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please provide step by step solution in C++, If image is not clearly visible ple

ID: 3848191 • Letter: P

Question

Please provide step by step solution in C++, If image is not clearly visible please zoom in chegg is not allowing me to make the image larger

As the question says i need the algorithm to find and delete the second largest element of A

Let A[1..n] be a max-heap with n greaterthanorequalto 3. Assume that all the elements in A are distinct. Design an efficient algorithm for finding and deleting the second largest element of A After the deletion. A is a max-heap. What is the running time of your algorithm?

Explanation / Answer

initialize maximum_value and second_maximum_value to INT_MIN;

traverse arrayvalue from INDEX 0 to N-1. Hence the currnt value will be in arrayvalue[i]

if arrayvalue > maximum_value, then set second_maximum_value = max, max = arrayvalue[i]

else if arrayvalue[i] between maximun_value and second_maximum_value (ie., array[i] > second_maximum_value and array[i] < maximum_value)

store the second_maximum_value in the largest second element at the end of the loop and set maximum_index = i.

then delete the mximum_index, which stores the second largest element (ie., array[i].remove(maxindex)


#include <iostream>
#include <stdio.h>
#include <limits.h>
using namespace std;
int main()
{
int array[20], n, i, maxval, secmax, maxind = 0;
cout<<" Enter number of elements in a array: ";
cin>>n;
cout<<" Enter the elements in the array: ";
for(i=0; i<n; i++)
{
cin>>array[i];
}
maxval = secmax = INT_MIN;
for(i=0; i<n; i++)
{
if(array[i] > maxval)
{
secmax = maxval;
maxval = array[i];
}
else if(array[i] > secmax && array[i] < maxval)
{
secmax = array[i];
maxind = i;
}
}
cout<<" Second Largest Element = "<<secmax;
//cout<<" Delete Second Largest Element ="<<remove(maxind);
return 0;
}


OUTPUT


Enter number of elements in a array:
Enter the elements in the array:
Second Largest Element = 67

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote