Find the second maximum in an array?
Answers were Sorted based on User's Feedback
Answer / jagdish patel
it's simple just sort an array in descending and then
print 2nd element of an array!!!!
| Is This Answer Correct ? | 27 Yes | 8 No |
Answer / foreverkushal
int SecondMax(int *Array, int ACount)
{
int Fm = 0, Sm = 0;
for (int i = 0; i < ACount; i++)
{
if (Array[i] > Sm)
{
if (Array[i] < Fm) Sm = Array[i];
else
{
Sm = Fm;
Fm = Array[i];
}
}
}
return Sm;
}
| Is This Answer Correct ? | 31 Yes | 22 No |
Answer / jaldeep
int sec_max(int a[])
{
if(a[0]>a[1])
{
max=a[0]
sec_max=a[1]
}
else
{
max=a[1]
sec_max=a[0]
}
for(i=1;i<n-1;i++)
{
if (a[i]>sec_max && a[i]< max)
{
sec_max=a[i];
}
else if(a[i]>max)
{
sec_max=max;
max=a[i];
}
return sec_max;
}
| Is This Answer Correct ? | 5 Yes | 1 No |
Answer / gang
public void getSecondMax(double[] arr){
double fmax, smax;
fmax=arr[0];
smax=arr[1];
for (int i = 1; i < arr.length; i++) {
if (arr[i]>fmax){
smax = fmax;
fmax = arr[i];
}
else if (arr[i]>smax)
smax = arr[i];
}
System.out.println("The 1st
highest="+fmax+"\t"+"The 2nd highest="+smax);
}
Note that it would not work if the array's size is only 1.
| Is This Answer Correct ? | 4 Yes | 2 No |
Answer / adnan sheikh
int SecondMax(int Array[], int ALength)
{
int Fmax,Smax;
Fmax=Smax=Araay[0];
for (int i = 0; i < ALength; i++)
{
if (Array[i] > Smax){
if (Array[i] < Fmax){
Smax = Array[i];
}
else{
Smax = Fmax;
Fmax = Array[i];
}
}
}
return Smax;
}
| Is This Answer Correct ? | 9 Yes | 8 No |
Answer / 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 |
Answer / rw-rwx
This might help.
max1=a[0];max2=a[1];
for(i=1;i<n;i++)
{
if(max1<a[i])
{
max2=max1;
max1=a[i];
}
if(max2==max1) max2=a[i+1];
if(max2==a[n]) { printf("All numbers are the same no second max.\n"); return 0;}
if(max2<a[i] && max1!=a[i]) max2=a[i];
}
| Is This Answer Correct ? | 0 Yes | 1 No |
Answer / 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 |
Answer / vinod, bangalore, india
int Second_Max(int* numbers, int Lenght)
{
int Max, Sec_Max, index;
Max = Sec_Max = numbers[0];
for(index=0; (index<Lenght) &&(Max == Sec_Max);
index++)
{
if(Max > numbers[index])
Sec_Max = numbers[index];
else
Max = numbers[index];
}
if(index == Lenght)
{
printf("Array contain simillar data and the
data is = %d \n", Max);
return false;
}
for(index =0; index < Lenght; index++)
{
if(numbers[index] > Max)
{
Sec_Max = Max;
Max = numbers[index];
}
if((numbers[index] < Max) && (numbers
[index] > Sec_Max))
{
Sec_Max = numbers[index];
}
}
return Sec_Max;
}
| Is This Answer Correct ? | 2 Yes | 7 No |
What apps are written in c++?
What do nonglobal variables default to a) auto b) register c) static
How Virtual functions call up is maintained?
What is the header file for setw?
what is the difference between linear list linked representaion and linked representation? what is the purpose of representing the linear list in linked represention ? is it not avoiding rules of linear represention?
What is your strongest programming language (Java, ASP, C, C++, VB, HTML,C#, etc.)?
24 Answers Infosys, Microsoft, TCS,
What is an iterator?
How does code-bloating occur in c++?
Which is not an ANSII C++ function a) sin() b) tmpnam() c) kbhit()
Differentiate between the message and method?
What is the use of ‘using’ declaration?
Arrange Doubly linked list in the ascending order of its integral value and replace integer 5 with 7?