Find the second maximum in an array?

Answers were Sorted based on User's Feedback



Find the second maximum in an array? ..

Answer / navs

if we take the array as
arr[]={1,4,7,2,10,0,6}

get the count

int count =6;
int max =arr[0];
int smax;

for (i=0;i<=count;i++)
{
if(max<arr[i])
{
smax=max;
max=max[i];
}
}

smax would give the second largest number

Is This Answer Correct ?    0 Yes 6 No

Find the second maximum in an array? ..

Answer / vili

// this deals with negative numbers as well
int getSecondMax( int* pArray, int nSize )
{
int nMax = pArray[0];
int n2ndMax = pArray[0];
for ( int i = 1; i < nSize; i++ )
{
if ( nMax < pArray[i] )
{
n2ndMax = nMax;
n2ndMax = pArray[i];
}
}
return n2ndMax;
}

Is This Answer Correct ?    6 Yes 22 No

Post New Answer

More C++ General Interview Questions

What data encapsulation is in c++?

0 Answers  


Define pure virtual function?

0 Answers  


How do I exit turbo c++?

0 Answers  


What is static in c++?

0 Answers  


What is input operator in c++?

0 Answers  






Is c++ vector dynamic?

0 Answers  


What type of question are asked in GE code writing test based on c++ data structures and pointers?

0 Answers  


Write a corrected statement in c++ so that the statement will work properly. if (x = y) x = 2*z;

2 Answers  


What is vector string in c++?

0 Answers  


What is the basic difference between C and C++?

0 Answers   NIIT,


What operators can you overload in c++?

0 Answers  


Snake Game: This is normal snake game which you can find in most of the mobiles. You can develop it in Java, C/C++, C# or what ever language you know.

0 Answers  


Categories