Find the second maximum in an array?

Answer Posted / jaldeep

int sec_max(int a[])
{
if(a[0]>a[1])
{
max=a[0]
sec_max=a[1]
}
else
{
max=a[1]
sec_max=a[0]
}
for(i=1;i<n-1;i++)
{
if (a[i]>sec_max && a[i]< max)
{
sec_max=a[i];
}
else if(a[i]>max)
{
sec_max=max;
max=a[i];
}
return sec_max;
}

Is This Answer Correct ?    5 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is unary operator? List out the different operators involved in the unary operator.

595


What can c++ be used for?

586


Where the memory to the static variables is allocated?

562


Explain the purpose of the keyword volatile.

640


What do the header files usually contains?

638






What is an incomplete type in c++?

772


Name the debugging methods that are used to solve problems?

586


Is map sorted c++?

525


Why do we need runtime polymorphism in c++?

563


Implement stack operations with pointers with appropriate exception checks.

569


What is an object in c++?

616


What is a local variable?

582


What are the main features of c++?

548


Why is c++ still popular?

589


What does it mean to declare a member function as static?

614