Find the second maximum in an array?

Answer Posted / jeena

/*Code to write second Minimum Number*/
int[] intarr = { 2, 5, 1, 8, 3, 6, 0, 4, 3, 2, 78, 1, 8 };

int intminval = 0, intsecondminval = 0;

for (int i = 0; i < intarr.Length; i++)
{
if (i == 0)
{
intminval = intsecondminval = intarr[i];
}
else
{
if (intarr[i] < intminval)
{
intsecondminval = intminval;
intminval = intarr[i];

}
else if (intminval == intsecondminval && intarr[i] > intminval)
{
// this conditon is to handle the case
//where the array contains only 2 values
// for e.g. {1,1,2,1,2,2,1}
intsecondminval = intarr[i];
}
}
}

Is This Answer Correct ?    5 Yes 6 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How do we balance an AVL Tree in C++?

836


What is rtti in c++?

823


How many static variables are created if you put one static member into a template class definition?

740


True or false, if you keep incrementing a variable, it will become negative a) True b) False c) It depends

2083


What is the limitation of cin while taking input for character array?

1687






How the delete operator differs from the delete[]operator?

833


What is a class definition?

795


How many namespaces are there in c++?

751


Which ide is best for c++?

741


Explain about templates of C++.

870


What is scope resolution operator in c++ with example?

757


Differentiate between the manipulator and setf( ) function?

807


What is function prototyping? What are its advantages?

788


Describe about storage allocation and scope of global, extern, static, local and register variables?

992


What are inline functions? What is the syntax for defining an inline function?

775