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

Explain the problem with overriding functions

609


How c functions prevents rework and therefore saves the programers time as wel as length of the code ?

625


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

567


What is the type of this pointer in c++?

636


What relational operators if statements in c++?

646






What is the arrow operator in c++?

562


What is auto used for in c++?

571


What is #include math h in c++?

575


Inline parameters : What does the compiler do with the parameters of inline function, that can be evaluated in runtime ?

2016


What is extern c++?

525


Is c++ faster than c?

596


the first character in the variable name must be an a) special symbol b) number c) alphabet

607


Why do we use templates?

606


What are the sizes and ranges of the basic c++ data types?

589


Where can I run c++ program?

605