find second largest element in array w/o using sorting
techniques? use onle one for loop.
Answer Posted / rajni kant
public class Findarray {
public static void main(String[] args) {
int array[]={250,12,34,56,73,260,232,234,235,240};
int max ,secndmax;
max = array[0];
secndmax=0;// assign it 0 not by array[0]as initial value
System.out.println("Initial value is "+ max);
for (int i=1;i<array.length;i++){
if (array[i]>max ){
secndmax=max;
max=array[i];
}else if(array[i]>secndmax){
secndmax = array[i];
}
}
System.out.println("Max element is "+ max);
System.out.println("Second Max element is "+
secndmax);
}
}
Is This Answer Correct ? | 32 Yes | 11 No |
Post New Answer View All Answers
How do you sort filenames in a directory?
What are multibyte characters?
When do we get logical errors?
What are the valid places to have keyword “break”?
What are the complete rules for header file searching?
What is c token?
What is the size of array float a(10)?
Difference between linking and loading?
If null and 0 are equivalent as null pointer constants, which should I use?
Write a program that accept anumber in words
What does %c do in c?
Given an array of 1s and 0s arrange the 1s together and 0s together in a single scan of the array. Optimize the boundary conditions?
how to create duplicate link list using C???
shorting algorithmS
Where static variables are stored in memory in c?