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 are the iterator and generic algorithms.

0 Answers  


What is the advantage of c++ over c?

0 Answers  


Write is a binary search tree? Write an algo and tell complexity?

0 Answers   Axtria,


What is stl containers in c++?

0 Answers  


What is the difference between shallow copy and deep copy?

1 Answers  


How can you quickly find the number of elements stored in a a) static array b) dynamic array ?

5 Answers   Lucent,


What is difference c and c++?

1 Answers  


Why is it called c++?

0 Answers  


what is the order of initialization for data?

10 Answers   Amazon, TCS, Wipro,


What is meant by const_cast?

0 Answers  


How do you differentiate between overloading the prefix and postfix increments?

0 Answers  


Reverse the Linked List. Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL

0 Answers  


Categories