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

Which sort does c++ use?

652


What is iomanip c++?

683


What does the ios::ate argument do?

772


Is c++ faster than c?

686


What is null pointer and void pointer and what is their use?

677






Explain how would you handle a situation where you cannot call the destructor of a local explicitly?

619


What is singleton class in c++?

689


How many namespaces are there in c++?

642


Write a Program for find and replace a character in a string.

660


Which programming language should I learn first?

657


What is a try block?

723


How is c++ different from java?

671


What is the basic concept of c++?

657


What is type of 'this' pointer?

688


Explain how to initialize a const data member.

701