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

Answer Posted / paresh thorat

package pkg;

public class Number {
public static void main(String [] args){
//int arr[]={6,4,5,2};
int arr[]={1,2,3,4,5};
int max,min=0,i;
max=arr[0];

try {
for(i=1;i<arr.length;i++){
System.out.println("Loop start");
if(arr[i]>max){
min=max;
max=arr[i];
System.out.println("Large->array "+max+"->"+arr[i]);
}
System.out.println("min-> array "+min+"->"+arr[i]);

if(max>arr[i] && arr[i]>min){
System.out.println("I am in second if loop");
min = arr[i];
System.out.println("Now min is "+min);
}

}
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Second Large"+min);
}
}

Is This Answer Correct ?    5 Yes 4 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is #define?

578


What is the difference between far and near in c?

604


I have a varargs function which accepts a float parameter?

583


What is union and structure in c?

621


Explain logical errors? Compare with syntax errors.

633






Why c is faster than c++?

636


write a program to display all prime numbers

1460


What are the differences between new and malloc in C?

612


What is a null pointer in c?

600


What does %p mean c?

634


How can you determine the maximum value that a numeric variable can hold?

643


What is the difference between far and near ?

689


Are there namespaces in c?

570


Tell me what is the purpose of 'register' keyword in c language?

620


Explain the use of fflush() function?

628