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

Why namespace is used in c++?

772


What you know about structures in C++?

788


find the two largest values among the 6 numbers using control structures : do-while,for,if else,nestedif- else ,while. one or two of them.

2193


What are keywords in c++?

814


What is auto type c++?

831






Does c++ have foreach?

725


What is the difference between a "copy constructor" and an "assignment operator" in C++?

813


Write a program to find the reverse Fibonacci series starting from N.

796


Can java be faster than c++?

857


Can you write a function similar to printf()?

852


Why is main function important?

805


int age=35; if(age>80) {Console.WriteLine("Boy you are old");} else {Console.WrieLine("That is a good age");}

1014


What is lambda in c++?

870


Why do we use setw in c++?

840


What is the protected keyword used for?

826