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 what is oop?

619


Implement stack operations with pointers with appropriate exception checks.

567


How should runtime errors be handled in c++?

609


Why Pointers are not used in C++?

622


What are abstract data types in c++?

532






describe private access specifiers?

637


Write a recursive program to calculate factorial in c++.

607


Is java easier than c++?

588


What's the best free c++ profiler for windows?

618


Will this c++ program execute or not?

599


Reads in the size of a square from the screen; 2. Prints a hollow square of that size out of “-“, “|” and blanks on screen; 3. Prints the same hollow square onto a text file. Your program should work for squares of all side sizes between 1 and 20. --- │ │ │ │ │ │ ---

1643


Who discovered c++?

558


Difference between struct and class in terms of access modifier.

695


What is #include iostream?

733


What are separators in c++?

622