find second largest element in array w/o using sorting
techniques? use onle one for loop.

Answer Posted / ranjeet

public static void main(String[] args) {
int array[]={13,12,34,56,73,21,232,234,235,240};
int max ,secndmax;
max = secndmax= array[0];
System.out.println("Initial value is "+ secndmax);
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 ?    67 Yes 32 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the difference between functions getch() and getche()?

625


Write a program to generate random numbers in c?

666


Why doesnt long int work?

615


Why clrscr is used in c?

589


What is typeof in c?

611






program to find out date after adding 31 days to a date in the month of febraury also consider the leap year

2580


Do pointers need to be initialized?

567


What are global variables and how do you declare them?

623


What are pointers? What are different types of pointers?

634


swap 2 numbers without using third variable?

666


Can we use visual studio for c?

556


What is the symbol indicated the c-preprocessor?

699


What is substring in c?

643


What are the types of operators in c?

615


to find the closest pair

1825