Find the second maximum in an array?

Answer Posted / fish

int a1(int[] a)
{
int max1 = -1;
int max2 = -1;

for (int i=0; i<a.length; i++)
{
if (a[i] > max1)
{
max2 = max1;
max1 = a[i];
}
else if (a[i] != max1 && a[i] > max2)
max2 = a[i];
}

return max2;
}

Is This Answer Correct ?    0 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is anonymous object in c++?

626


What is the use of 'using' declaration in c++?

618


how can i access a direct (absolute, not the offset) memory address? here is what i tried: wrote a program that ask's for an address from the user, creates a FAR pointer to that adress and shows it. then the user can increment/decrement the value in that address by pressing p(inc+) and m(dec-). NOW, i compiled that program and opened it twice (in 2 different windows) and gave twice the same address to it. now look what happen - if i change the value in one "window" of the program, it DOES NOT change in the other! even if they point to the same address in the memory! here is the code snippet: //------------------------------------------------------ #include //INCLUDE EVERY KNOWN HEADER FILE #include //FOR ANY CASE... #include #include #include main() { int far *ptr; //FAR POINTER!!! long address; char key=0; //A KEY FROM THE KEYBOARD int temp=0; clrscr(); cout<<"Enter Address:"; cin>>hex>>address; //GETS THE ADDRESS clrscr(); (long)ptr=address; temp=*ptr; //PUTS THE ADDRESS IN THE PTR cout<<"["<

1805


Do we have to use initialization list in spite of the assignment in constructors?

557


what are the iterator and generic algorithms.

1680






What is tellg () in c++?

713


Describe the advantage of an external iterator.

607


Am pass the 10000 records to target in target I will take commit interval 15000 when I was stop the work flow what will happened

1068


How many keywords are used in c++?

550


What is the output of the following program? Why?

614


What is private inheritance?

589


What is abstraction in c++ with example?

554


What is the difference between interpreters and compilers?

620


What are the classes in c++?

630


What kind of problems can be solved by a namespace?

581